Esempio n. 1
0
def create_list_of_instence_network_interface_obj_from_data():
    fake_instance_scanner_list = []
    for data in fake_instance_scanner_data.INSTANCE_DATA:
        fake_instance_scanner_list.append(
            instance.Instance(
                'fake-instance', **data).create_network_interfaces())
    return fake_instance_scanner_list
    def test_instance(self):
        """Test instance.Key."""
        url_1 = ('https://www.googleapis.com/compute/v1/'
                 'projects/foo/zones/us-central1-a/instances/bar')
        obj_1 = instance.Instance('bar',
                                  parent=project.Project('foo'),
                                  locations=['us-central1-a'],
                                  name='bar')
        key_1 = key.Key(instance.KEY_OBJECT_KIND, {
            'project_id': 'foo',
            'zone': 'us-central1-a',
            'name': 'bar'
        })
        self.assertEqual(key_1, obj_1.key)
        self.assertEqual(key_1, instance.Key.from_url(url_1))

        url_invalid_1 = ('https://www.googleapis.com/compute/v1/'
                         'zones/bar/instances/baz')
        url_invalid_2 = ('https://www.googleapis.com/compute/v1/'
                         'projects/foo/instances/bar')
        url_invalid_3 = ('https://www.googleapis.com/compute/v1/'
                         'projects/foo/zones/bar')
        self.assertRaises(ValueError, instance.Key.from_url, url_invalid_1)
        self.assertRaises(ValueError, instance.Key.from_url, url_invalid_2)
        self.assertRaises(ValueError, instance.Key.from_url, url_invalid_3)
 def test_legacy_networks(self):
     """ Test legacy networks without a subnet works."""
     network_interfaces = (instance.Instance(
         'name-0', **fake_instance.FAKE_INSTANCE_RESPONSE_LEGACY)
                           .create_network_interfaces())
     self.assertEqual(len(network_interfaces), 1)
     network_interface = network_interfaces[0]
     self.assertEqual('compute#networkInterface', network_interface.kind)
     self.assertEqual('nic0', network_interface.name)
     self.assertEqual('https://www.googleapis.com/compute/v1/projects/'
                      'project-1/global/networks/network-1',
                      network_interface.network)
     self.assertEqual('000.000.000.000', network_interface.network_ip)
     self.assertEqual([{u'kind': u'compute#accessConfig',
                        u'type': u'ONE_TO_ONE_NAT', u'name': u'External NAT',
                        u'natIP': u'000.000.000.001'}],
                      network_interface.access_configs)
    def test_network_interface_creation(self):
        """Test that network_interface creation is correct."""
        network_interfaces = (instance.Instance(
           'name-0', **fake_instance.FAKE_INSTANCE_RESPONSE_1)
                              .create_network_interfaces())

        self.assertEqual(len(network_interfaces), 1)
        network_interface = network_interfaces[0]
        self.assertEqual('compute#networkInterface', network_interface.kind)
        self.assertEqual('nic0', network_interface.name)
        self.assertEqual('https://www.googleapis.com/compute/v1/projects/'
                         'project-1/global/networks/network-1',
                         network_interface.network)
        self.assertEqual('000.000.000.000', network_interface.network_ip)
        self.assertEqual('https://www.googleapis.com/compute/v1/projects'
                         '/project-1/regions/datacenter'
                         '/subnetworks/subnetwork-1',
                         network_interface.subnetwork)
        self.assertEqual([{u'kind': u'compute#accessConfig',
                           u'type': u'ONE_TO_ONE_NAT', u'name': u'External NAT',
                           u'natIP': u'000.000.000.001'}],
                         network_interface.access_configs)
 def test_recognize_two_network_interfaces(self):
     """Test that it recognizes two network_interfaces."""
     network_interfaces = (instance.Instance(
        'name-1', **fake_instance.FAKE_INSTANCE_RESPONSE_2)
                           .create_network_interfaces())
     self.assertEqual(len(network_interfaces), 2)
        firewall_rule_source_ranges=json.dumps(['preempted']),
        firewall_rule_denied=json.dumps([{
            'IPProtocol': 'tcp',
        }]),
    ),
}
PROJECTS = {
    'foo': project_type.Project(project_id='foo'),
}
INSTANCES = {
    'i1':
    instance_type.Instance('i1',
                           parent=PROJECTS['foo'],
                           name='i1',
                           tags={'items': ['tag_i1']},
                           locations=['wl-redqueen1-a'],
                           data=("""{
    "name": "i2",
    "selfLink": "https://www.googleapis.com/compute/v1/projects/foo/zones/wl-redqueen1-a/instances/i1",
    "tags": {"items": ["tag_i1"]}
}""")),
    'i2':
    instance_type.Instance('i2',
                           parent=PROJECTS['foo'],
                           name='i2',
                           tags=[],
                           locations=['wl-redqueen1-a'],
                           data=("""{
    "name": "i2",
    "selfLink": "https://www.googleapis.com/compute/v1/projects/foo/zones/wl-redqueen1-a/instances/i2",
    "tags": {}
}""")),