def test_parse_data(
     self,
     stream,
     expected_parsed_output,
 ):
     print(expected_parsed_output)
     if expected_parsed_output[1]:
         parsed_output = serialization.parse_json_yaml(stream)
         self.assertEqual(parsed_output, expected_parsed_output[0])
     else:
         with self.assertRaises(serialization.YamlParserConfigError):
             serialization.parse_json_yaml(stream)
Ejemplo n.º 2
0
    def get_network_details_v2(self):
        try:
            raw_network_data = self._get_cache_data("network-config",
                                                    decode=True)
            network_data = serialization.parse_json_yaml(raw_network_data)
            if not network_data:
                LOG.info("V2 network metadata is empty")
                return
            if not isinstance(network_data, dict):
                LOG.warning("V2 network metadata is not a dictionary")
                return
        except base.NotExistingMetadataException:
            LOG.info("V2 network metadata not found")
            return
        except serialization.YamlParserConfigError:
            LOG.exception("V2 network metadata could not be deserialized")
            return

        network_data_version = network_data.get("version")
        if network_data_version != 1:
            LOG.error("Network data version '%s' is not supported",
                      network_data_version)
            return

        network_config_parser = NoCloudNetworkConfigV1Parser()
        return network_config_parser.parse(network_data.get("config"))
    def load(self):
        super(VMwareGuestInfoService, self).load()

        if not CONF.vmwareguestinfo.vmware_rpctool_path:
            LOG.info("rpctool_path is empty. "
                     "Please provide a value for VMware rpctool path.")
            return False

        self._rpc_tool_path = os.path.abspath(
            os.path.expandvars(CONF.vmwareguestinfo.vmware_rpctool_path))

        if not os.path.exists(self._rpc_tool_path):
            LOG.info("%s does not exist. "
                     "Please provide a valid value for VMware rpctool path." %
                     self._rpc_tool_path)
            return False

        self._meta_data = serialization.parse_json_yaml(
            self._get_guest_data('metadata'))
        if not isinstance(self._meta_data, dict):
            LOG.warning("Instance metadata is not a dictionary.")
            self._meta_data = {}

        self._user_data = self._get_guest_data('userdata')

        if self._meta_data or self._user_data:
            return True
    def test_parse_empty_result(self, input, expected_result):

        with self.snatcher:
            result = self._parser.parse(serialization.parse_json_yaml(input))

        self.assertEqual(True, expected_result[0] in self.snatcher.output[0])
        self.assertEqual(result, expected_result[1])
Ejemplo n.º 5
0
    def _get_meta_data(self):
        if self._meta_data:
            return self._meta_data

        raw_meta_data = self._get_cache_data("meta-data", decode=True)
        try:
            self._meta_data = (serialization.parse_json_yaml(raw_meta_data))
        except base.YamlParserConfigError as ex:
            LOG.error("Metadata could not be parsed")
            LOG.exception(ex)

        return self._meta_data
Ejemplo n.º 6
0
    def test_parse_data(self, stream, expected_parsed_output, mock_yaml_load,
                        mock_json_loads):
        if not expected_parsed_output[1]:
            mock_json_loads.return_value = expected_parsed_output[0]
        else:
            mock_json_loads.side_effect = TypeError("Failed to parse json")
            mock_yaml_load.return_value = expected_parsed_output[0]

        parsed_output = serialization.parse_json_yaml(stream)

        mock_json_loads.assert_called_once_with(stream)
        if expected_parsed_output[1]:
            loader = getattr(yaml, 'CLoader', yaml.Loader)
            mock_yaml_load.assert_called_once_with(stream, Loader=loader)

        self.assertEqual(parsed_output, expected_parsed_output[0])
    def test_network_details_v2(self):
        expected_bond = nm.Bond(
            members=["gbe0", "gbe1"],
            type=nm.BOND_TYPE_ACTIVE_BACKUP,
            lb_algorithm=None,
            lacp_rate=None,
        )
        expected_link_bond = nm.Link(
            id='bond0',
            name='bond0',
            type=nm.LINK_TYPE_BOND,
            enabled=True,
            mac_address="52:54:00:12:34:00",
            mtu=1450,
            bond=expected_bond,
            vlan_link=None,
            vlan_id=None,
        )
        expected_link = nm.Link(
            id='interface0',
            name='interface0',
            type=nm.LINK_TYPE_PHYSICAL,
            enabled=True,
            mac_address="52:54:00:12:34:00",
            mtu=1450,
            bond=None,
            vlan_link=None,
            vlan_id=None,
        )
        expected_link_vlan = nm.Link(
            id='vlan0',
            name='vlan0',
            type=nm.LINK_TYPE_VLAN,
            enabled=True,
            mac_address="52:54:00:12:34:00",
            mtu=1450,
            bond=None,
            vlan_link='eth1',
            vlan_id=150,
        )
        expected_network = nm.Network(
            link='interface0',
            address_cidr='192.168.1.10/24',
            dns_nameservers=['192.168.1.11'],
            routes=[nm.Route(network_cidr='0.0.0.0/0', gateway="192.168.1.1")])

        expected_network_bond = nm.Network(
            link='bond0',
            address_cidr='192.168.1.10/24',
            dns_nameservers=['192.168.1.11'],
            routes=[],
        )

        expected_network_vlan = nm.Network(
            link='vlan0',
            address_cidr='192.168.1.10/24',
            dns_nameservers=['192.168.1.11'],
            routes=[],
        )
        expected_nameservers = nm.NameServerService(
            addresses=['192.168.23.2', '8.8.8.8'], search='acme.local')

        parser_data = """
           - type: physical
             name: interface0
             mac_address: "52:54:00:12:34:00"
             mtu: 1450
             subnets:
                - type: static
                  address: 192.168.1.10
                  netmask: 255.255.255.0
                  gateway: 192.168.1.1
                  dns_nameservers:
                    - 192.168.1.11
           - type: bond
             name: bond0
             bond_interfaces:
               - gbe0
               - gbe1
             mac_address: "52:54:00:12:34:00"
             params:
               bond-mode: active-backup
               bond-lacp-rate: false
             mtu: 1450
             subnets:
                - type: static
                  address: 192.168.1.10
                  netmask: 255.255.255.0
                  dns_nameservers:
                    - 192.168.1.11
           - type: vlan
             name: vlan0
             vlan_link: eth1
             vlan_id: 150
             mac_address: "52:54:00:12:34:00"
             mtu: 1450
             subnets:
                - type: static
                  address: 192.168.1.10
                  netmask: 255.255.255.0
                  dns_nameservers:
                    - 192.168.1.11
           - type: nameserver
             address:
               - 192.168.23.2
               - 8.8.8.8
             search: acme.local
        """

        result = self._parser.parse(serialization.parse_json_yaml(parser_data))

        self.assertEqual(result.links[0], expected_link)
        self.assertEqual(result.networks[0], expected_network)

        self.assertEqual(result.links[1], expected_link_bond)
        self.assertEqual(result.networks[1], expected_network_bond)

        self.assertEqual(result.links[2], expected_link_vlan)
        self.assertEqual(result.networks[2], expected_network_vlan)

        self.assertEqual(result.services[0], expected_nameservers)