Пример #1
0
class DigitalOcean_v2_Tests(LibcloudTestCase):
    def setUp(self):
        DigitalOceanNodeDriver.connectionCls.conn_classes = \
            (None, DigitalOceanMockHttp)
        DigitalOceanMockHttp.type = None
        self.driver = DigitalOceanNodeDriver(*DIGITALOCEAN_v2_PARAMS)

    def test_authentication(self):
        DigitalOceanMockHttp.type = 'UNAUTHORIZED'
        self.assertRaises(InvalidCredsError, self.driver.list_nodes)

    def test_list_images_success(self):
        images = self.driver.list_images()
        self.assertTrue(len(images) >= 1)

        image = images[0]
        self.assertTrue(image.id is not None)
        self.assertTrue(image.name is not None)

    def test_list_sizes_success(self):
        sizes = self.driver.list_sizes()
        self.assertTrue(len(sizes) >= 1)

        size = sizes[0]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '512mb')
        self.assertEqual(size.ram, 512)

        size = sizes[1]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '1gb')
        self.assertEqual(size.ram, 1024)

    def test_list_locations_success(self):
        locations = self.driver.list_locations()
        self.assertTrue(len(locations) >= 1)

        location = locations[0]
        self.assertEqual(location.id, 'nyc1')
        self.assertEqual(location.name, 'New York 1')

    def test_list_nodes_success(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].name, 'example.com')
        self.assertEqual(nodes[0].public_ips, ['104.236.32.182'])
        self.assertEqual(nodes[0].extra['image']['id'], 6918990)
        self.assertEqual(nodes[0].extra['size_slug'], '512mb')

    def test_create_node_invalid_size(self):
        image = NodeImage(id='invalid', name=None, driver=self.driver)
        size = self.driver.list_sizes()[0]
        location = self.driver.list_locations()[0]

        DigitalOceanMockHttp.type = 'INVALID_IMAGE'
        expected_msg = r'You specified an invalid image for Droplet creation. \(code: 404\)'
        self.assertRaisesRegexp(Exception,
                                expected_msg,
                                self.driver.create_node,
                                name='test',
                                size=size,
                                image=image,
                                location=location)

    def test_reboot_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'REBOOT'
        result = self.driver.reboot_node(node)
        self.assertTrue(result)

    def test_create_image_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'SNAPSHOT'
        result = self.driver.create_image(node, 'My snapshot')
        self.assertTrue(result)

    def test_get_image_success(self):
        image = self.driver.get_image(12345)
        self.assertEqual(image.name, 'My snapshot')
        self.assertEqual(image.id, '12345')
        self.assertEqual(image.extra['distribution'], 'Ubuntu')

    def test_delete_image_success(self):
        image = self.driver.get_image(12345)
        DigitalOceanMockHttp.type = 'DESTROY'
        result = self.driver.delete_image(image)
        self.assertTrue(result)

    def test_ex_power_on_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'POWERON'
        result = self.driver.ex_power_on_node(node)
        self.assertTrue(result)

    def test_ex_shutdown_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'SHUTDOWN'
        result = self.driver.ex_shutdown_node(node)
        self.assertTrue(result)

    def test_destroy_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'DESTROY'
        result = self.driver.destroy_node(node)
        self.assertTrue(result)

    def test_ex_rename_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'RENAME'
        result = self.driver.ex_rename_node(node, 'fedora helios')
        self.assertTrue(result)

    def test_list_key_pairs(self):
        keys = self.driver.list_key_pairs()
        self.assertEqual(len(keys), 1)
        self.assertEqual(keys[0].extra['id'], 7717)
        self.assertEqual(keys[0].name, 'test1')
        self.assertEqual(keys[0].public_key,
                         "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDGk5 example")

    def test_create_key_pair(self):
        DigitalOceanMockHttp.type = 'CREATE'
        key = self.driver.create_key_pair(
            name="test1",
            public_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQsxRiUKn example")
        self.assertEqual(key.name, "test1")
        self.assertEqual(key.fingerprint,
                         "f5:d1:78:ed:28:72:5f:e1:ac:94:fd:1f:e0:a3:48:6d")

    def test_delete_key_pair(self):
        key = self.driver.list_key_pairs()[0]
        result = self.driver.delete_key_pair(key)
        self.assertTrue(result)

    def test__paginated_request_single_page(self):
        nodes = self.driver._paginated_request('/v2/droplets', 'droplets')
        self.assertEqual(nodes[0]['name'], 'example.com')
        self.assertEqual(nodes[0]['image']['id'], 6918990)
        self.assertEqual(nodes[0]['size_slug'], '512mb')

    def test__paginated_request_two_pages(self):
        DigitalOceanMockHttp.type = 'PAGE_ONE'
        nodes = self.driver._paginated_request('/v2/droplets', 'droplets')
        self.assertEqual(len(nodes), 2)
Пример #2
0
class DigitalOcean_v2_Tests(LibcloudTestCase):

    def setUp(self):
        DigitalOceanNodeDriver.connectionCls.conn_class = \
            DigitalOceanMockHttp
        DigitalOceanMockHttp.type = None
        self.driver = DigitalOceanNodeDriver(*DIGITALOCEAN_v2_PARAMS)

    def test_v1_Error(self):
        self.assertRaises(DigitalOcean_v1_Error, DigitalOceanNodeDriver,
                          *DIGITALOCEAN_v1_PARAMS, api_version='v1')

    def test_v2_uses_v1_key(self):
        self.assertRaises(InvalidCredsError, DigitalOceanNodeDriver,
                          *DIGITALOCEAN_v1_PARAMS, api_version='v2')

    def test_authentication(self):
        DigitalOceanMockHttp.type = 'UNAUTHORIZED'
        self.assertRaises(InvalidCredsError, self.driver.list_nodes)

    def test_list_images_success(self):
        images = self.driver.list_images()
        self.assertTrue(len(images) >= 1)

        image = images[0]
        self.assertTrue(image.id is not None)
        self.assertTrue(image.name is not None)

    def test_list_sizes_success(self):
        sizes = self.driver.list_sizes()
        self.assertTrue(len(sizes) >= 1)

        size = sizes[0]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '512mb')
        self.assertEqual(size.ram, 512)

        size = sizes[1]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '1gb')
        self.assertEqual(size.ram, 1024)

    def test_list_locations_success(self):
        locations = self.driver.list_locations()
        self.assertTrue(len(locations) >= 1)

        location = locations[0]
        self.assertEqual(location.id, 'nyc1')
        self.assertEqual(location.name, 'New York 1')

    def test_list_nodes_success(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].name, 'example.com')
        self.assertEqual(nodes[0].public_ips, ['104.236.32.182'])
        self.assertEqual(nodes[0].extra['image']['id'], 6918990)
        self.assertEqual(nodes[0].extra['size_slug'], '512mb')
        self.assertEqual(len(nodes[0].extra['tags']), 2)

    def test_list_nodes_fills_created_datetime(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(nodes[0].created_at, datetime(2014, 11, 14, 16, 29, 21, tzinfo=UTC))

    def test_create_node_invalid_size(self):
        image = NodeImage(id='invalid', name=None, driver=self.driver)
        size = self.driver.list_sizes()[0]
        location = self.driver.list_locations()[0]

        DigitalOceanMockHttp.type = 'INVALID_IMAGE'
        expected_msg = \
            r'You specified an invalid image for Droplet creation. \(code: (404|HTTPStatus.NOT_FOUND)\)'
        assertRaisesRegex(self, Exception, expected_msg,
                          self.driver.create_node,
                          name='test', size=size, image=image,
                          location=location)

    def test_reboot_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'REBOOT'
        result = self.driver.reboot_node(node)
        self.assertTrue(result)

    def test_create_image_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'SNAPSHOT'
        result = self.driver.create_image(node, 'My snapshot')
        self.assertTrue(result)

    def test_get_image_success(self):
        image = self.driver.get_image(12345)
        self.assertEqual(image.name, 'My snapshot')
        self.assertEqual(image.id, '12345')
        self.assertEqual(image.extra['distribution'], 'Ubuntu')

    def test_delete_image_success(self):
        image = self.driver.get_image(12345)
        DigitalOceanMockHttp.type = 'DESTROY'
        result = self.driver.delete_image(image)
        self.assertTrue(result)

    def test_ex_power_on_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'POWERON'
        result = self.driver.ex_power_on_node(node)
        self.assertTrue(result)

    def test_ex_shutdown_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'SHUTDOWN'
        result = self.driver.ex_shutdown_node(node)
        self.assertTrue(result)

    def test_ex_hard_reboot_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'POWERCYCLE'
        result = self.driver.ex_hard_reboot(node)
        self.assertTrue(result)

    def test_ex_rebuild_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'REBUILD'
        result = self.driver.ex_rebuild_node(node)
        self.assertTrue(result)

    def test_ex_resize_node_success(self):
        node = self.driver.list_nodes()[0]
        size = self.driver.list_sizes()[0]
        DigitalOceanMockHttp.type = 'RESIZE'
        result = self.driver.ex_resize_node(node, size)
        self.assertTrue(result)

    def test_destroy_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'DESTROY'
        result = self.driver.destroy_node(node)
        self.assertTrue(result)

    def test_ex_change_kernel_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'KERNELCHANGE'
        result = self.driver.ex_change_kernel(node, 7515)
        self.assertTrue(result)

    def test_ex_enable_ipv6_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'ENABLEIPV6'
        result = self.driver.ex_enable_ipv6(node)
        self.assertTrue(result)

    def test_ex_rename_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'RENAME'
        result = self.driver.ex_rename_node(node, 'fedora helios')
        self.assertTrue(result)

    def test_list_key_pairs(self):
        keys = self.driver.list_key_pairs()
        self.assertEqual(len(keys), 1)
        self.assertEqual(keys[0].extra['id'], 7717)
        self.assertEqual(keys[0].name, 'test1')
        self.assertEqual(keys[0].public_key,
                         "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDGk5 example")

    def test_create_key_pair(self):
        DigitalOceanMockHttp.type = 'CREATE'
        key = self.driver.create_key_pair(
            name="test1",
            public_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQsxRiUKn example"
        )
        self.assertEqual(key.name, "test1")
        self.assertEqual(key.fingerprint,
                         "f5:d1:78:ed:28:72:5f:e1:ac:94:fd:1f:e0:a3:48:6d")

    def test_delete_key_pair(self):
        key = self.driver.list_key_pairs()[0]
        result = self.driver.delete_key_pair(key)
        self.assertTrue(result)

    def test__paginated_request_single_page(self):
        nodes = self.driver._paginated_request('/v2/droplets', 'droplets')
        self.assertEqual(nodes[0]['name'], 'example.com')
        self.assertEqual(nodes[0]['image']['id'], 6918990)
        self.assertEqual(nodes[0]['size_slug'], '512mb')

    def test__paginated_request_two_pages(self):
        DigitalOceanMockHttp.type = 'PAGE_ONE'
        nodes = self.driver._paginated_request('/v2/droplets', 'droplets')
        self.assertEqual(len(nodes), 2)

    def test_list_volumes(self):
        volumes = self.driver.list_volumes()
        self.assertEqual(len(volumes), 1)
        volume = volumes[0]
        self.assertEqual(volume.id, "62766883-2c28-11e6-b8e6-000f53306ae1")
        self.assertEqual(volume.name, "example")
        self.assertEqual(volume.size, 4)
        self.assertEqual(volume.driver, self.driver)

    def test_list_volumes_empty(self):
        DigitalOceanMockHttp.type = 'EMPTY'
        volumes = self.driver.list_volumes()
        self.assertEqual(len(volumes), 0)

    def test_create_volume(self):
        nyc1 = [r for r in self.driver.list_locations() if r.id == 'nyc1'][0]
        DigitalOceanMockHttp.type = 'CREATE'
        volume = self.driver.create_volume(4, 'example', nyc1)
        self.assertEqual(volume.id, "62766883-2c28-11e6-b8e6-000f53306ae1")
        self.assertEqual(volume.name, "example")
        self.assertEqual(volume.size, 4)
        self.assertEqual(volume.driver, self.driver)

    def test_attach_volume(self):
        node = self.driver.list_nodes()[0]
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'ATTACH'
        resp = self.driver.attach_volume(node, volume)
        self.assertTrue(resp)

    def test_detach_volume(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'DETACH'
        resp = self.driver.detach_volume(volume)
        self.assertTrue(resp)

    def test_destroy_volume(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'DESTROY'
        resp = self.driver.destroy_volume(volume)
        self.assertTrue(resp)

    def test_list_volume_snapshots(self):
        volume = self.driver.list_volumes()[0]
        snapshots = self.driver.list_volume_snapshots(volume)
        self.assertEqual(len(snapshots), 3)
        snapshot1, snapshot2, snapshot3 = snapshots
        self.assertEqual(snapshot1.id, "c0def940-9324-11e6-9a56-000f533176b1")
        self.assertEqual(snapshot2.id, "c2036724-9343-11e6-aef4-000f53315a41")
        self.assertEqual(snapshot3.id, "d347e033-9343-11e6-9a56-000f533176b1")

    def test_create_volume_snapshot(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'CREATE'
        snapshot = self.driver.create_volume_snapshot(volume, 'test-snapshot')
        self.assertEqual(snapshot.id, "c0def940-9324-11e6-9a56-000f533176b1")
        self.assertEqual(snapshot.name, 'test-snapshot')
        self.assertEqual(volume.driver, self.driver)

    def test_delete_volume_snapshot(self):
        volume = self.driver.list_volumes()[0]
        snapshot = self.driver.list_volume_snapshots(volume)[0]
        DigitalOceanMockHttp.type = 'DELETE'
        result = self.driver.delete_volume_snapshot(snapshot)
        self.assertTrue(result)

    def test_ex_get_node_details(self):
        node = self.driver.ex_get_node_details('3164444')
        self.assertEqual(node.name, 'example.com')
        self.assertEqual(node.public_ips, ['36.123.0.123'])
        self.assertEqual(node.extra['image']['id'], 12089443)
        self.assertEqual(node.extra['size_slug'], '8gb')
        self.assertEqual(len(node.extra['tags']), 2)

    def test_ex_create_floating_ip(self):
        nyc1 = [r for r in self.driver.list_locations() if r.id == 'nyc1'][0]
        floating_ip = self.driver.ex_create_floating_ip(nyc1)

        # Note that this is the ID. There is no real ID for a floating IP at
        # DigitalOcean, but the IP is unique so we can use that instead.
        self.assertEqual(floating_ip.id, '167.138.123.111')
        self.assertEqual(floating_ip.ip_address, '167.138.123.111')
        self.assertEqual(floating_ip.extra['region']['slug'], 'nyc1')
        # The newly created floating IP reserved to a region is not
        # associated with any droplet. See the DigitalOcean API docs
        # how to create a floating IP that is associated with an instance
        # from the start. This API call creates an unattached IP.
        self.assertIsNone(floating_ip.node_id)

    def test_ex_delete_floating_ip(self):
        nyc1 = [r for r in self.driver.list_locations() if r.id == 'nyc1'][0]
        floating_ip = self.driver.ex_create_floating_ip(nyc1)
        ret = self.driver.ex_delete_floating_ip(floating_ip)

        # The API returns 204 NO CONTENT if all is well.
        self.assertTrue(ret)

    def test_floating_ip_can_be_deleted_by_calling_delete_on_floating_ip_object(self):
        nyc1 = [r for r in self.driver.list_locations() if r.id == 'nyc1'][0]
        floating_ip = self.driver.ex_create_floating_ip(nyc1)
        ret = floating_ip.delete()

        self.assertTrue(ret)

    def test_list_floating_ips(self):
        floating_ips = self.driver.ex_list_floating_ips()

        self.assertEqual(len(floating_ips), 2, 'Wrong floating IPs count')

        floating_ip = floating_ips[0]
        self.assertEqual(floating_ip.id, '133.166.122.204')
        self.assertEqual(floating_ip.ip_address, '133.166.122.204')
        self.assertEqual(floating_ip.extra['region']['slug'], 'ams3')
        self.assertEqual(84155775, floating_ip.node_id)

    def test_get_floating_ip(self):
        floating_ip = self.driver.ex_get_floating_ip('133.166.122.204')

        self.assertEqual(floating_ip.id, '133.166.122.204')
        self.assertEqual(floating_ip.ip_address, '133.166.122.204')
        self.assertEqual(floating_ip.extra['region']['slug'], 'ams3')
        self.assertEqual(84155775, floating_ip.node_id)

    def test_ex_attach_floating_ip_to_node(self):
        node = self.driver.list_nodes()[0]
        floating_ip = self.driver.ex_get_floating_ip('133.166.122.204')

        ret = self.driver.ex_attach_floating_ip_to_node(node, floating_ip)

        self.assertTrue(ret)

    def test_ex_detach_floating_ip_from_node(self):
        node = self.driver.list_nodes()[0]
        floating_ip = self.driver.ex_get_floating_ip('154.138.103.175')

        ret = self.driver.ex_detach_floating_ip_from_node(node, floating_ip)

        self.assertTrue(ret)
Пример #3
0
class DigitalOceanTests(LibcloudTestCase):

    def setUp(self):
        DigitalOceanNodeDriver.connectionCls.conn_classes = \
            (None, DigitalOceanMockHttp)
        DigitalOceanMockHttp.type = None
        self.driver = DigitalOceanNodeDriver(*DIGITAL_OCEAN_PARAMS)

    def test_authentication(self):
        DigitalOceanMockHttp.type = 'UNAUTHORIZED_CLIENT'
        self.assertRaises(InvalidCredsError, self.driver.list_nodes)

    def test_list_images_success(self):
        images = self.driver.list_images()
        self.assertTrue(len(images) >= 1)

        image = images[0]
        self.assertTrue(image.id is not None)
        self.assertTrue(image.name is not None)

    def test_list_sizes_success(self):
        sizes = self.driver.list_sizes()
        self.assertTrue(len(sizes) >= 1)

        size = sizes[0]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '512MB')
        self.assertEqual(size.ram, 512)

        size = sizes[4]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '8GB')
        self.assertEqual(size.ram, 8 * 1024)

    def test_list_locations_success(self):
        locations = self.driver.list_locations()
        self.assertTrue(len(locations) >= 1)

        location = locations[0]
        self.assertEqual(location.id, '1')
        self.assertEqual(location.name, 'New York 1')

    def test_list_nodes_success(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].name, 'test-2')
        self.assertEqual(nodes[0].public_ips, [])
        self.assertEqual(nodes[0].extra['image_id'], 1601)
        self.assertEqual(nodes[0].extra['size_id'], 66)

    def test_create_node_invalid_size(self):
        image = NodeImage(id='invalid', name=None, driver=self.driver)
        size = self.driver.list_sizes()[0]
        location = self.driver.list_locations()[0]

        DigitalOceanMockHttp.type = 'INVALID_IMAGE'
        expected_msg = r'You specified an invalid image for Droplet creation. \(code: 404\)'
        self.assertRaisesRegexp(Exception, expected_msg,
                                self.driver.create_node,
                                name='test', size=size, image=image,
                                location=location)

    def test_reboot_node_success(self):
        node = self.driver.list_nodes()[0]
        result = self.driver.reboot_node(node)
        self.assertTrue(result)

    def test_destroy_node_success(self):
        node = self.driver.list_nodes()[0]
        result = self.driver.destroy_node(node)
        self.assertTrue(result)

    def test_ex_rename_node_success(self):
        node = self.driver.list_nodes()[0]
        result = self.driver.ex_rename_node(node, 'fedora helios')
        self.assertTrue(result)

    def test_ex_list_ssh_keys(self):
        keys = self.driver.ex_list_ssh_keys()
        self.assertEqual(len(keys), 1)

        self.assertEqual(keys[0].id, 7717)
        self.assertEqual(keys[0].name, 'test1')
        self.assertEqual(keys[0].pub_key, None)

    def test_ex_destroy_ssh_key(self):
        key = self.driver.ex_list_ssh_keys()[0]
        result = self.driver.ex_destroy_ssh_key(key.id)
        self.assertTrue(result)
Пример #4
0
class DigitalOceanTests(LibcloudTestCase):
    def setUp(self):
        DigitalOceanNodeDriver.connectionCls.conn_classes = \
            (None, DigitalOceanMockHttp)
        DigitalOceanMockHttp.type = None
        self.driver = DigitalOceanNodeDriver(*DIGITAL_OCEAN_PARAMS)

    def test_authentication(self):
        DigitalOceanMockHttp.type = 'UNAUTHORIZED_CLIENT'
        self.assertRaises(InvalidCredsError, self.driver.list_nodes)

    def test_list_images_success(self):
        images = self.driver.list_images()
        self.assertTrue(len(images) >= 1)

        image = images[0]
        self.assertTrue(image.id is not None)
        self.assertTrue(image.name is not None)

    def test_list_sizes_success(self):
        sizes = self.driver.list_sizes()
        self.assertTrue(len(sizes) >= 1)

        size = sizes[0]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '512MB')
        self.assertEqual(size.ram, 512)

        size = sizes[4]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '8GB')
        self.assertEqual(size.ram, 8 * 1024)

    def test_list_locations_success(self):
        locations = self.driver.list_locations()
        self.assertTrue(len(locations) >= 1)

        location = locations[0]
        self.assertEqual(location.id, '1')
        self.assertEqual(location.name, 'New York 1')

    def test_list_nodes_success(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].name, 'test-2')
        self.assertEqual(nodes[0].public_ips, [])
        self.assertEqual(nodes[0].extra['image_id'], 1601)
        self.assertEqual(nodes[0].extra['size_id'], 66)

    def test_create_node_invalid_size(self):
        image = NodeImage(id='invalid', name=None, driver=self.driver)
        size = self.driver.list_sizes()[0]
        location = self.driver.list_locations()[0]

        DigitalOceanMockHttp.type = 'INVALID_IMAGE'
        expected_msg = r'You specified an invalid image for Droplet creation. \(code: 404\)'
        self.assertRaisesRegexp(Exception,
                                expected_msg,
                                self.driver.create_node,
                                name='test',
                                size=size,
                                image=image,
                                location=location)

    def test_reboot_node_success(self):
        node = self.driver.list_nodes()[0]
        result = self.driver.reboot_node(node)
        self.assertTrue(result)

    def test_destroy_node_success(self):
        node = self.driver.list_nodes()[0]
        result = self.driver.destroy_node(node)
        self.assertTrue(result)

    def test_ex_rename_node_success(self):
        node = self.driver.list_nodes()[0]
        result = self.driver.ex_rename_node(node, 'fedora helios')
        self.assertTrue(result)

    def test_ex_list_ssh_keys(self):
        keys = self.driver.ex_list_ssh_keys()
        self.assertEqual(len(keys), 1)

        self.assertEqual(keys[0].id, 7717)
        self.assertEqual(keys[0].name, 'test1')
        self.assertEqual(keys[0].pub_key, None)

    def test_ex_destroy_ssh_key(self):
        key = self.driver.ex_list_ssh_keys()[0]
        result = self.driver.ex_destroy_ssh_key(key.id)
        self.assertTrue(result)
Пример #5
0
class DigitalOcean_v2_Tests(LibcloudTestCase):
    def setUp(self):
        DigitalOceanNodeDriver.connectionCls.conn_class = DigitalOceanMockHttp
        DigitalOceanMockHttp.type = None
        self.driver = DigitalOceanNodeDriver(*DIGITALOCEAN_v2_PARAMS)

    def test_v1_Error(self):
        self.assertRaises(
            DigitalOcean_v1_Error,
            DigitalOceanNodeDriver,
            *DIGITALOCEAN_v1_PARAMS,
            api_version="v1",
        )

    def test_v2_uses_v1_key(self):
        self.assertRaises(
            InvalidCredsError,
            DigitalOceanNodeDriver,
            *DIGITALOCEAN_v1_PARAMS,
            api_version="v2",
        )

    def test_authentication(self):
        DigitalOceanMockHttp.type = "UNAUTHORIZED"
        self.assertRaises(InvalidCredsError, self.driver.list_nodes)

    def test_list_images_success(self):
        images = self.driver.list_images()
        self.assertTrue(len(images) >= 1)

        image = images[0]
        self.assertTrue(image.id is not None)
        self.assertTrue(image.name is not None)

    def test_list_sizes_success(self):
        sizes = self.driver.list_sizes()
        self.assertTrue(len(sizes) >= 1)

        size = sizes[0]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, "512mb")
        self.assertEqual(size.ram, 512)

        size = sizes[1]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, "1gb")
        self.assertEqual(size.ram, 1024)

    def test_list_sizes_filter_by_location_success(self):
        location = self.driver.list_locations()[1]
        sizes = self.driver.list_sizes(location=location)
        self.assertTrue(len(sizes) >= 1)

        size = sizes[0]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, "512mb")
        self.assertTrue(location.id in size.extra["regions"])

        location = self.driver.list_locations()[1]
        location.id = "doesntexist"
        sizes = self.driver.list_sizes(location=location)
        self.assertEqual(len(sizes), 0)

    def test_list_locations_success(self):
        locations = self.driver.list_locations()
        self.assertTrue(len(locations) == 2)

        location = locations[0]
        self.assertEqual(location.id, "nyc1")
        self.assertEqual(location.name, "New York 1")

        locations = self.driver.list_locations(ex_available=True)
        self.assertTrue(len(locations) == 2)

        locations = self.driver.list_locations(ex_available=False)
        self.assertTrue(len(locations) == 3)

    def test_list_nodes_success(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].name, "ubuntu-s-1vcpu-1gb-sfo3-01")
        self.assertEqual(nodes[0].public_ips, ["128.199.13.158"])
        self.assertEqual(nodes[0].extra["image"]["id"], 69463186)
        self.assertEqual(nodes[0].extra["size_slug"], "s-1vcpu-1gb")
        self.assertEqual(len(nodes[0].extra["tags"]), 0)

    def test_list_nodes_fills_created_datetime(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(nodes[0].created_at,
                         datetime(2020, 10, 15, 13, 58, 22, tzinfo=UTC))

    def test_create_node_invalid_size(self):
        image = NodeImage(id="invalid", name=None, driver=self.driver)
        size = self.driver.list_sizes()[0]
        location = self.driver.list_locations()[0]

        DigitalOceanMockHttp.type = "INVALID_IMAGE"
        expected_msg = (
            r"You specified an invalid image for Droplet creation." +
            r" \(code: (404|HTTPStatus.NOT_FOUND)\)")
        assertRaisesRegex(
            self,
            Exception,
            expected_msg,
            self.driver.create_node,
            name="test",
            size=size,
            image=image,
            location=location,
        )

    def test_reboot_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = "REBOOT"
        result = self.driver.reboot_node(node)
        self.assertTrue(result)

    def test_create_image_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = "SNAPSHOT"
        result = self.driver.create_image(node, "My snapshot")
        self.assertTrue(result)

    def test_get_image_success(self):
        image = self.driver.get_image(12345)
        self.assertEqual(image.name, "My snapshot")
        self.assertEqual(image.id, "12345")
        self.assertEqual(image.extra["distribution"], "Ubuntu")

    def test_delete_image_success(self):
        image = self.driver.get_image(12345)
        DigitalOceanMockHttp.type = "DESTROY"
        result = self.driver.delete_image(image)
        self.assertTrue(result)

    def test_ex_power_on_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = "POWERON"
        result = self.driver.ex_power_on_node(node)
        self.assertTrue(result)

    def test_ex_shutdown_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = "SHUTDOWN"
        result = self.driver.ex_shutdown_node(node)
        self.assertTrue(result)

    def test_ex_hard_reboot_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = "POWERCYCLE"
        result = self.driver.ex_hard_reboot(node)
        self.assertTrue(result)

    def test_ex_rebuild_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = "REBUILD"
        result = self.driver.ex_rebuild_node(node)
        self.assertTrue(result)

    def test_ex_resize_node_success(self):
        node = self.driver.list_nodes()[0]
        size = self.driver.list_sizes()[0]
        DigitalOceanMockHttp.type = "RESIZE"
        result = self.driver.ex_resize_node(node, size)
        self.assertTrue(result)

    def test_destroy_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = "DESTROY"
        result = self.driver.destroy_node(node)
        self.assertTrue(result)

    def test_ex_change_kernel_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = "KERNELCHANGE"
        result = self.driver.ex_change_kernel(node, 7515)
        self.assertTrue(result)

    def test_ex_enable_ipv6_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = "ENABLEIPV6"
        result = self.driver.ex_enable_ipv6(node)
        self.assertTrue(result)

    def test_ex_rename_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = "RENAME"
        result = self.driver.ex_rename_node(node, "fedora helios")
        self.assertTrue(result)

    def test_list_key_pairs(self):
        keys = self.driver.list_key_pairs()
        self.assertEqual(len(keys), 1)
        self.assertEqual(keys[0].extra["id"], 7717)
        self.assertEqual(keys[0].name, "test1")
        self.assertEqual(keys[0].public_key,
                         "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDGk5 example")

    def test_create_key_pair(self):
        DigitalOceanMockHttp.type = "CREATE"
        key = self.driver.create_key_pair(
            name="test1",
            public_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQsxRiUKn example")
        self.assertEqual(key.name, "test1")
        self.assertEqual(key.fingerprint,
                         "f5:d1:78:ed:28:72:5f:e1:ac:94:fd:1f:e0:a3:48:6d")

    def test_delete_key_pair(self):
        key = self.driver.list_key_pairs()[0]
        result = self.driver.delete_key_pair(key)
        self.assertTrue(result)

    def test__paginated_request_single_page(self):
        nodes = self.driver._paginated_request("/v2/droplets", "droplets")
        self.assertEqual(nodes[0]["name"], "ubuntu-s-1vcpu-1gb-sfo3-01")
        self.assertEqual(nodes[0]["image"]["id"], 69463186)
        self.assertEqual(nodes[0]["size_slug"], "s-1vcpu-1gb")

    def test__paginated_request_two_pages(self):
        DigitalOceanMockHttp.type = "PAGE_ONE"
        nodes = self.driver._paginated_request("/v2/droplets", "droplets")
        self.assertEqual(len(nodes), 2)

    def test_list_volumes(self):
        volumes = self.driver.list_volumes()
        self.assertEqual(len(volumes), 1)
        volume = volumes[0]
        self.assertEqual(volume.id, "62766883-2c28-11e6-b8e6-000f53306ae1")
        self.assertEqual(volume.name, "example")
        self.assertEqual(volume.size, 4)
        self.assertEqual(volume.driver, self.driver)

    def test_list_volumes_empty(self):
        DigitalOceanMockHttp.type = "EMPTY"
        volumes = self.driver.list_volumes()
        self.assertEqual(len(volumes), 0)

    def test_create_volume(self):
        nyc1 = [r for r in self.driver.list_locations() if r.id == "nyc1"][0]
        DigitalOceanMockHttp.type = "CREATE"
        volume = self.driver.create_volume(4, "example", nyc1)
        self.assertEqual(volume.id, "62766883-2c28-11e6-b8e6-000f53306ae1")
        self.assertEqual(volume.name, "example")
        self.assertEqual(volume.size, 4)
        self.assertEqual(volume.driver, self.driver)

    def test_attach_volume(self):
        node = self.driver.list_nodes()[0]
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = "ATTACH"
        resp = self.driver.attach_volume(node, volume)
        self.assertTrue(resp)

    def test_detach_volume(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = "DETACH"
        resp = self.driver.detach_volume(volume)
        self.assertTrue(resp)

    def test_destroy_volume(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = "DESTROY"
        resp = self.driver.destroy_volume(volume)
        self.assertTrue(resp)

    def test_list_volume_snapshots(self):
        volume = self.driver.list_volumes()[0]
        snapshots = self.driver.list_volume_snapshots(volume)
        self.assertEqual(len(snapshots), 3)
        snapshot1, snapshot2, snapshot3 = snapshots
        self.assertEqual(snapshot1.id, "c0def940-9324-11e6-9a56-000f533176b1")
        self.assertEqual(snapshot2.id, "c2036724-9343-11e6-aef4-000f53315a41")
        self.assertEqual(snapshot3.id, "d347e033-9343-11e6-9a56-000f533176b1")

    def test_create_volume_snapshot(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = "CREATE"
        snapshot = self.driver.create_volume_snapshot(volume, "test-snapshot")
        self.assertEqual(snapshot.id, "c0def940-9324-11e6-9a56-000f533176b1")
        self.assertEqual(snapshot.name, "test-snapshot")
        self.assertEqual(volume.driver, self.driver)

    def test_delete_volume_snapshot(self):
        volume = self.driver.list_volumes()[0]
        snapshot = self.driver.list_volume_snapshots(volume)[0]
        DigitalOceanMockHttp.type = "DELETE"
        result = self.driver.delete_volume_snapshot(snapshot)
        self.assertTrue(result)

    def test_ex_get_node_details(self):
        node = self.driver.ex_get_node_details("3164444")
        self.assertEqual(node.name, "example.com")
        self.assertEqual(node.public_ips, ["36.123.0.123"])
        self.assertEqual(node.extra["image"]["id"], 12089443)
        self.assertEqual(node.extra["size_slug"], "8gb")
        self.assertEqual(len(node.extra["tags"]), 2)

    def test_ex_create_floating_ip(self):
        nyc1 = [r for r in self.driver.list_locations() if r.id == "nyc1"][0]
        floating_ip = self.driver.ex_create_floating_ip(nyc1)

        # Note that this is the ID. There is no real ID for a floating IP at
        # DigitalOcean, but the IP is unique so we can use that instead.
        self.assertEqual(floating_ip.id, "167.138.123.111")
        self.assertEqual(floating_ip.ip_address, "167.138.123.111")
        self.assertEqual(floating_ip.extra["region"]["slug"], "nyc1")
        # The newly created floating IP reserved to a region is not
        # associated with any droplet. See the DigitalOcean API docs
        # how to create a floating IP that is associated with an instance
        # from the start. This API call creates an unattached IP.
        self.assertIsNone(floating_ip.node_id)

    def test_ex_delete_floating_ip(self):
        nyc1 = [r for r in self.driver.list_locations() if r.id == "nyc1"][0]
        floating_ip = self.driver.ex_create_floating_ip(nyc1)
        ret = self.driver.ex_delete_floating_ip(floating_ip)

        # The API returns 204 NO CONTENT if all is well.
        self.assertTrue(ret)

    def test_floating_ip_can_be_deleted_by_calling_delete_on_floating_ip_object(
        self, ):  # noqa: E501
        nyc1 = [r for r in self.driver.list_locations() if r.id == "nyc1"][0]
        floating_ip = self.driver.ex_create_floating_ip(nyc1)
        ret = floating_ip.delete()

        self.assertTrue(ret)

    def test_list_floating_ips(self):
        floating_ips = self.driver.ex_list_floating_ips()

        self.assertEqual(len(floating_ips), 2, "Wrong floating IPs count")

        floating_ip = floating_ips[0]
        self.assertEqual(floating_ip.id, "133.166.122.204")
        self.assertEqual(floating_ip.ip_address, "133.166.122.204")
        self.assertEqual(floating_ip.extra["region"]["slug"], "ams3")
        self.assertEqual(84155775, floating_ip.node_id)

    def test_get_floating_ip(self):
        floating_ip = self.driver.ex_get_floating_ip("133.166.122.204")

        self.assertEqual(floating_ip.id, "133.166.122.204")
        self.assertEqual(floating_ip.ip_address, "133.166.122.204")
        self.assertEqual(floating_ip.extra["region"]["slug"], "ams3")
        self.assertEqual(84155775, floating_ip.node_id)

    def test_ex_attach_floating_ip_to_node(self):
        node = self.driver.list_nodes()[0]
        floating_ip = self.driver.ex_get_floating_ip("133.166.122.204")

        ret = self.driver.ex_attach_floating_ip_to_node(node, floating_ip)

        self.assertTrue(ret)

    def test_ex_detach_floating_ip_from_node(self):
        node = self.driver.list_nodes()[0]
        floating_ip = self.driver.ex_get_floating_ip("154.138.103.175")

        ret = self.driver.ex_detach_floating_ip_from_node(node, floating_ip)

        self.assertTrue(ret)
Пример #6
0
class DigitalOcean_v2_Tests(LibcloudTestCase):
    def setUp(self):
        DigitalOceanNodeDriver.connectionCls.conn_class = \
            DigitalOceanMockHttp
        DigitalOceanMockHttp.type = None
        self.driver = DigitalOceanNodeDriver(*DIGITALOCEAN_v2_PARAMS)

    def test_v1_Error(self):
        self.assertRaises(DigitalOcean_v1_Error,
                          DigitalOceanNodeDriver,
                          *DIGITALOCEAN_v1_PARAMS,
                          api_version='v1')

    def test_v2_uses_v1_key(self):
        self.assertRaises(InvalidCredsError,
                          DigitalOceanNodeDriver,
                          *DIGITALOCEAN_v1_PARAMS,
                          api_version='v2')

    def test_authentication(self):
        DigitalOceanMockHttp.type = 'UNAUTHORIZED'
        self.assertRaises(InvalidCredsError, self.driver.list_nodes)

    def test_list_images_success(self):
        images = self.driver.list_images()
        self.assertTrue(len(images) >= 1)

        image = images[0]
        self.assertTrue(image.id is not None)
        self.assertTrue(image.name is not None)

    def test_list_sizes_success(self):
        sizes = self.driver.list_sizes()
        self.assertTrue(len(sizes) >= 1)

        size = sizes[0]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '512mb')
        self.assertEqual(size.ram, 512)

        size = sizes[1]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '1gb')
        self.assertEqual(size.ram, 1024)

    def test_list_locations_success(self):
        locations = self.driver.list_locations()
        self.assertTrue(len(locations) >= 1)

        location = locations[0]
        self.assertEqual(location.id, 'nyc1')
        self.assertEqual(location.name, 'New York 1')

    def test_list_nodes_success(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].name, 'example.com')
        self.assertEqual(nodes[0].public_ips, ['104.236.32.182'])
        self.assertEqual(nodes[0].extra['image']['id'], 6918990)
        self.assertEqual(nodes[0].extra['size_slug'], '512mb')

    def test_list_nodes_fills_created_datetime(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(nodes[0].created_at,
                         datetime(2014, 11, 14, 16, 29, 21, tzinfo=UTC))

    def test_create_node_invalid_size(self):
        image = NodeImage(id='invalid', name=None, driver=self.driver)
        size = self.driver.list_sizes()[0]
        location = self.driver.list_locations()[0]

        DigitalOceanMockHttp.type = 'INVALID_IMAGE'
        expected_msg = \
            r'You specified an invalid image for Droplet creation. \(code: (404|HTTPStatus.NOT_FOUND)\)'
        self.assertRaisesRegexp(Exception,
                                expected_msg,
                                self.driver.create_node,
                                name='test',
                                size=size,
                                image=image,
                                location=location)

    def test_reboot_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'REBOOT'
        result = self.driver.reboot_node(node)
        self.assertTrue(result)

    def test_create_image_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'SNAPSHOT'
        result = self.driver.create_image(node, 'My snapshot')
        self.assertTrue(result)

    def test_get_image_success(self):
        image = self.driver.get_image(12345)
        self.assertEqual(image.name, 'My snapshot')
        self.assertEqual(image.id, '12345')
        self.assertEqual(image.extra['distribution'], 'Ubuntu')

    def test_delete_image_success(self):
        image = self.driver.get_image(12345)
        DigitalOceanMockHttp.type = 'DESTROY'
        result = self.driver.delete_image(image)
        self.assertTrue(result)

    def test_ex_power_on_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'POWERON'
        result = self.driver.ex_power_on_node(node)
        self.assertTrue(result)

    def test_ex_shutdown_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'SHUTDOWN'
        result = self.driver.ex_shutdown_node(node)
        self.assertTrue(result)

    def test_ex_hard_reboot_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'POWERCYCLE'
        result = self.driver.ex_hard_reboot(node)
        self.assertTrue(result)

    def test_destroy_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'DESTROY'
        result = self.driver.destroy_node(node)
        self.assertTrue(result)

    def test_ex_change_kernel_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'KERNELCHANGE'
        result = self.driver.ex_change_kernel(node, 7515)
        self.assertTrue(result)

    def test_ex_enable_ipv6_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'ENABLEIPV6'
        result = self.driver.ex_enable_ipv6(node)
        self.assertTrue(result)

    def test_ex_rename_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'RENAME'
        result = self.driver.ex_rename_node(node, 'fedora helios')
        self.assertTrue(result)

    def test_list_key_pairs(self):
        keys = self.driver.list_key_pairs()
        self.assertEqual(len(keys), 1)
        self.assertEqual(keys[0].extra['id'], 7717)
        self.assertEqual(keys[0].name, 'test1')
        self.assertEqual(keys[0].public_key,
                         "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDGk5 example")

    def test_create_key_pair(self):
        DigitalOceanMockHttp.type = 'CREATE'
        key = self.driver.create_key_pair(
            name="test1",
            public_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQsxRiUKn example")
        self.assertEqual(key.name, "test1")
        self.assertEqual(key.fingerprint,
                         "f5:d1:78:ed:28:72:5f:e1:ac:94:fd:1f:e0:a3:48:6d")

    def test_delete_key_pair(self):
        key = self.driver.list_key_pairs()[0]
        result = self.driver.delete_key_pair(key)
        self.assertTrue(result)

    def test__paginated_request_single_page(self):
        nodes = self.driver._paginated_request('/v2/droplets', 'droplets')
        self.assertEqual(nodes[0]['name'], 'example.com')
        self.assertEqual(nodes[0]['image']['id'], 6918990)
        self.assertEqual(nodes[0]['size_slug'], '512mb')

    def test__paginated_request_two_pages(self):
        DigitalOceanMockHttp.type = 'PAGE_ONE'
        nodes = self.driver._paginated_request('/v2/droplets', 'droplets')
        self.assertEqual(len(nodes), 2)

    def test_list_volumes(self):
        volumes = self.driver.list_volumes()
        self.assertEqual(len(volumes), 1)
        volume = volumes[0]
        self.assertEqual(volume.id, "62766883-2c28-11e6-b8e6-000f53306ae1")
        self.assertEqual(volume.name, "example")
        self.assertEqual(volume.size, 4)
        self.assertEqual(volume.driver, self.driver)

    def test_list_volumes_empty(self):
        DigitalOceanMockHttp.type = 'EMPTY'
        volumes = self.driver.list_volumes()
        self.assertEqual(len(volumes), 0)

    def test_create_volume(self):
        nyc1 = [r for r in self.driver.list_locations() if r.id == 'nyc1'][0]
        DigitalOceanMockHttp.type = 'CREATE'
        volume = self.driver.create_volume(4, 'example', nyc1)
        self.assertEqual(volume.id, "62766883-2c28-11e6-b8e6-000f53306ae1")
        self.assertEqual(volume.name, "example")
        self.assertEqual(volume.size, 4)
        self.assertEqual(volume.driver, self.driver)

    def test_attach_volume(self):
        node = self.driver.list_nodes()[0]
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'ATTACH'
        resp = self.driver.attach_volume(node, volume)
        self.assertTrue(resp)

    def test_detach_volume(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'DETACH'
        resp = self.driver.detach_volume(volume)
        self.assertTrue(resp)

    def test_destroy_volume(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'DESTROY'
        resp = self.driver.destroy_volume(volume)
        self.assertTrue(resp)

    def test_list_volume_snapshots(self):
        volume = self.driver.list_volumes()[0]
        snapshots = self.driver.list_volume_snapshots(volume)
        self.assertEqual(len(snapshots), 3)
        snapshot1, snapshot2, snapshot3 = snapshots
        self.assertEqual(snapshot1.id, "c0def940-9324-11e6-9a56-000f533176b1")
        self.assertEqual(snapshot2.id, "c2036724-9343-11e6-aef4-000f53315a41")
        self.assertEqual(snapshot3.id, "d347e033-9343-11e6-9a56-000f533176b1")

    def test_create_volume_snapshot(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'CREATE'
        snapshot = self.driver.create_volume_snapshot(volume, 'test-snapshot')
        self.assertEqual(snapshot.id, "c0def940-9324-11e6-9a56-000f533176b1")
        self.assertEqual(snapshot.name, 'test-snapshot')
        self.assertEqual(volume.driver, self.driver)

    def test_delete_volume_snapshot(self):
        volume = self.driver.list_volumes()[0]
        snapshot = self.driver.list_volume_snapshots(volume)[0]
        DigitalOceanMockHttp.type = 'DELETE'
        result = self.driver.delete_volume_snapshot(snapshot)
        self.assertTrue(result)
Пример #7
0
class DigitalOceanTests(unittest.TestCase):

    def setUp(self):
        DigitalOceanNodeDriver.connectionCls.conn_classes = \
            (None, DigitalOceanMockHttp)
        DigitalOceanMockHttp.type = None
        self.driver = DigitalOceanNodeDriver(*DIGITAL_OCEAN_PARAMS)

    def test_authentication(self):
        DigitalOceanMockHttp.type = 'UNAUTHORIZED_CLIENT'
        self.assertRaises(InvalidCredsError, self.driver.list_nodes)

    def test_list_images_success(self):
        images = self.driver.list_images()
        self.assertTrue(len(images) >= 1)

        image = images[0]
        self.assertTrue(image.id is not None)
        self.assertTrue(image.name is not None)

    def test_list_sizes_success(self):
        sizes = self.driver.list_sizes()
        self.assertTrue(len(sizes) >= 1)

        size = sizes[0]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '512MB')
        self.assertEqual(size.ram, 512)

        size = sizes[4]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '8GB')
        self.assertEqual(size.ram, 8 * 1024)

    def test_list_locations_success(self):
        locations = self.driver.list_locations()
        self.assertTrue(len(locations) >= 1)

        location = locations[0]
        self.assertEqual(location.id, '1')
        self.assertEqual(location.name, 'New York 1')

    def test_list_nodes_success(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].name, 'test-2')
        self.assertEqual(nodes[0].public_ips, [])

    def test_reboot_node_success(self):
        node = self.driver.list_nodes()[0]
        result = self.driver.reboot_node(node)
        self.assertTrue(result)

    def test_destroy_node_success(self):
        node = self.driver.list_nodes()[0]
        result = self.driver.destroy_node(node)
        self.assertTrue(result)

    def test_ex_list_ssh_keys(self):
        keys = self.driver.ex_list_ssh_keys()
        self.assertEqual(len(keys), 1)

        self.assertEqual(keys[0].id, 7717)
        self.assertEqual(keys[0].name, 'test1')
        self.assertEqual(keys[0].pub_key, None)

    def test_ex_destroy_ssh_key(self):
        key = self.driver.ex_list_ssh_keys()[0]
        result = self.driver.ex_destroy_ssh_key(key.id)
        self.assertTrue(result)
Пример #8
0
class DigitalOcean_v2_Tests(LibcloudTestCase):

    def setUp(self):
        DigitalOceanNodeDriver.connectionCls.conn_classes = \
            (None, DigitalOceanMockHttp)
        DigitalOceanMockHttp.type = None
        self.driver = DigitalOceanNodeDriver(*DIGITALOCEAN_v2_PARAMS)

    def test_v2_uses_v1_key(self):
        self.assertRaises(InvalidCredsError, DigitalOceanNodeDriver,
                          *DIGITALOCEAN_v1_PARAMS, api_version='v2')

    def test_authentication(self):
        DigitalOceanMockHttp.type = 'UNAUTHORIZED'
        self.assertRaises(InvalidCredsError, self.driver.list_nodes)

    def test_list_images_success(self):
        images = self.driver.list_images()
        self.assertTrue(len(images) >= 1)

        image = images[0]
        self.assertTrue(image.id is not None)
        self.assertTrue(image.name is not None)

    def test_list_sizes_success(self):
        sizes = self.driver.list_sizes()
        self.assertTrue(len(sizes) >= 1)

        size = sizes[0]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '512mb')
        self.assertEqual(size.ram, 512)

        size = sizes[1]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '1gb')
        self.assertEqual(size.ram, 1024)

    def test_list_locations_success(self):
        locations = self.driver.list_locations()
        self.assertTrue(len(locations) >= 1)

        location = locations[0]
        self.assertEqual(location.id, 'nyc1')
        self.assertEqual(location.name, 'New York 1')

    def test_list_nodes_success(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].name, 'example.com')
        self.assertEqual(nodes[0].public_ips, ['104.236.32.182'])
        self.assertEqual(nodes[0].extra['image']['id'], 6918990)
        self.assertEqual(nodes[0].extra['size_slug'], '512mb')

    def test_create_node_invalid_size(self):
        image = NodeImage(id='invalid', name=None, driver=self.driver)
        size = self.driver.list_sizes()[0]
        location = self.driver.list_locations()[0]

        DigitalOceanMockHttp.type = 'INVALID_IMAGE'
        expected_msg = \
            r'You specified an invalid image for Droplet creation. \(code: (404|HTTPStatus.NOT_FOUND)\)'
        self.assertRaisesRegexp(Exception, expected_msg,
                                self.driver.create_node,
                                name='test', size=size, image=image,
                                location=location)

    def test_reboot_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'REBOOT'
        result = self.driver.reboot_node(node)
        self.assertTrue(result)

    def test_create_image_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'SNAPSHOT'
        result = self.driver.create_image(node, 'My snapshot')
        self.assertTrue(result)

    def test_get_image_success(self):
        image = self.driver.get_image(12345)
        self.assertEqual(image.name, 'My snapshot')
        self.assertEqual(image.id, '12345')
        self.assertEqual(image.extra['distribution'], 'Ubuntu')

    def test_delete_image_success(self):
        image = self.driver.get_image(12345)
        DigitalOceanMockHttp.type = 'DESTROY'
        result = self.driver.delete_image(image)
        self.assertTrue(result)

    def test_ex_power_on_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'POWERON'
        result = self.driver.ex_power_on_node(node)
        self.assertTrue(result)

    def test_ex_shutdown_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'SHUTDOWN'
        result = self.driver.ex_shutdown_node(node)
        self.assertTrue(result)

    def test_destroy_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'DESTROY'
        result = self.driver.destroy_node(node)
        self.assertTrue(result)

    def test_ex_rename_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'RENAME'
        result = self.driver.ex_rename_node(node, 'fedora helios')
        self.assertTrue(result)

    def test_list_key_pairs(self):
        keys = self.driver.list_key_pairs()
        self.assertEqual(len(keys), 1)
        self.assertEqual(keys[0].extra['id'], 7717)
        self.assertEqual(keys[0].name, 'test1')
        self.assertEqual(keys[0].public_key,
                         "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDGk5 example")

    def test_create_key_pair(self):
        DigitalOceanMockHttp.type = 'CREATE'
        key = self.driver.create_key_pair(
            name="test1",
            public_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQsxRiUKn example"
        )
        self.assertEqual(key.name, "test1")
        self.assertEqual(key.fingerprint,
                         "f5:d1:78:ed:28:72:5f:e1:ac:94:fd:1f:e0:a3:48:6d")

    def test_delete_key_pair(self):
        key = self.driver.list_key_pairs()[0]
        result = self.driver.delete_key_pair(key)
        self.assertTrue(result)

    def test__paginated_request_single_page(self):
        nodes = self.driver._paginated_request('/v2/droplets', 'droplets')
        self.assertEqual(nodes[0]['name'], 'example.com')
        self.assertEqual(nodes[0]['image']['id'], 6918990)
        self.assertEqual(nodes[0]['size_slug'], '512mb')

    def test__paginated_request_two_pages(self):
        DigitalOceanMockHttp.type = 'PAGE_ONE'
        nodes = self.driver._paginated_request('/v2/droplets', 'droplets')
        self.assertEqual(len(nodes), 2)
Пример #9
0
class DigitalOceanTests(unittest.TestCase):
    def setUp(self):
        DigitalOceanNodeDriver.connectionCls.conn_classes = \
            (None, DigitalOceanMockHttp)
        DigitalOceanMockHttp.type = None
        self.driver = DigitalOceanNodeDriver(*DIGITAL_OCEAN_PARAMS)

    def test_authentication(self):
        DigitalOceanMockHttp.type = 'UNAUTHORIZED_CLIENT'
        self.assertRaises(InvalidCredsError, self.driver.list_nodes)

    def test_list_images_success(self):
        images = self.driver.list_images()
        self.assertTrue(len(images) >= 1)

        image = images[0]
        self.assertTrue(image.id is not None)
        self.assertTrue(image.name is not None)

    def test_list_sizes_success(self):
        sizes = self.driver.list_sizes()
        self.assertTrue(len(sizes) >= 1)

        size = sizes[0]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '512MB')
        self.assertEqual(size.ram, 512)

        size = sizes[4]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '8GB')
        self.assertEqual(size.ram, 8 * 1024)

    def test_list_locations_success(self):
        locations = self.driver.list_locations()
        self.assertTrue(len(locations) >= 1)

        location = locations[0]
        self.assertEqual(location.id, '1')
        self.assertEqual(location.name, 'New York 1')

    def test_list_nodes_success(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].name, 'test-2')
        self.assertEqual(nodes[0].public_ips, [])

    def test_reboot_node_success(self):
        node = self.driver.list_nodes()[0]
        result = self.driver.reboot_node(node)
        self.assertTrue(result)

    def test_destroy_node_success(self):
        node = self.driver.list_nodes()[0]
        result = self.driver.destroy_node(node)
        self.assertTrue(result)

    def test_ex_list_ssh_keys(self):
        keys = self.driver.ex_list_ssh_keys()
        self.assertEqual(len(keys), 1)

        self.assertEqual(keys[0].id, 7717)
        self.assertEqual(keys[0].name, 'test1')
        self.assertEqual(keys[0].pub_key, None)

    def test_ex_destroy_ssh_key(self):
        key = self.driver.ex_list_ssh_keys()[0]
        result = self.driver.ex_destroy_ssh_key(key.id)
        self.assertTrue(result)
def createNode(provider, driverUno, driverDos, driverTres, driverCuatro, name,
               size, image, location, ex_network):
    pass
    nodesProvider = ''
    if provider == "Digital Ocean":
        pass
        accessKey = driverUno
        sshKey = driverDos
        driverTres = driverTres
        driverCuatro = driverCuatro
        driver = DigitalOceanNodeDriver(accessKey)

        sshKeyID = list()

        ssh = int(sshKey)
        keyPairs = driver.list_key_pairs()
        sizesNode = driver.list_sizes()
        imagesNode = driver.list_images()
        locationsNode = driver.list_locations()

        for keyPair in keyPairs:
            if keyPair.extra['id'] == ssh:
                pass
                sshKeyID.append(keyPair.extra['id'])

        for sizeNode in sizesNode:
            if sizeNode.name == size:
                pass
                sizeName = sizeNode

        for imageNode in imagesNode:
            if imageNode.id == image:
                pass
                imageId = imageNode

        for locationNode in locationsNode:
            if locationNode.id == location:
                pass
                locationId = locationNode

        idNodes = driver.create_node(name, sizeName, imageId, locationId, None,
                                     sshKeyID, None)
        idNodes.extra = json.dumps(idNodes.extra)
        attr = {
            'id': idNodes.id,
            'name': idNodes.name,
            'state': idNodes.state,
            'public_ips': idNodes.public_ips,
            'private_ips': idNodes.private_ips,
            'provider': 'DigitalOcean',
            'extra': idNodes.extra
        }

        nodesProvider = json.dumps(attr)
    if provider == "EC2":
        pass
        accessId = driverUno
        secretKey = driverDos
        region = driverTres
        driverCuatro = driverCuatro
        reg = region[0:len(region) - 1]
        #	driver = BaseEC2NodeDriver(accessId,secretKey,'eu-west-1')

        cls = get_driver(Provider.EC2)
        driver = cls(accessId, secretKey, region=reg)

        sizesNode = driver.list_sizes()
        imagesNode = driver.list_images()

        sizeName = ''
        imageId = ''

        for sizeNode in sizesNode:
            if sizeNode.id == size:
                pass
                sizeName = sizeNode

        for imageNode in imagesNode:
            if imageNode.name == image:
                pass
                imageId = imageNode

        nodeCreate = driver.create_node(name=name,
                                        image=imageId,
                                        size=sizeName)

        types = 'running'

        node = checkStatus.checkStatus(driver, nodeCreate.id, types)

        if node != 0:
            pass
            elastic = elasticIP.setElasticIP(driver, node.id)
            if elastic != 0:
                pass
                v4 = []
                ips = {
                    'ipaddress': elastic.ip,
                    'gateway': 'NULL',
                    'mask': 'NULL',
                    'private_ip': node.private_ips[0]
                }

                v4.append(ips)

                network = {'v4': v4}

                extra = {
                    'launch_time': node.extra['launch_time'],
                    'instance_type': node.extra['instance_type'],
                    'network': network
                }

                attr = {
                    'id': node.id,
                    'region': reg,
                    'name': node.name,
                    'state': node.state,
                    'public_ip': elastic.ip,
                    'provider': 'Amazon',
                    'extra': extra
                }

                print json.dumps(attr)
            else:
                v4 = []
                ips = {
                    'ipaddress': node.public_ips[0],
                    'gateway': 'NULL',
                    'mask': 'NULL',
                    'private_ip': node.private_ips[0]
                }

                v4.append(ips)

                network = {'v4': v4}

                extra = {
                    'launch_time': node.extra['launch_time'],
                    'instance_type': node.extra['instance_type'],
                    'network': network
                }

                attr = {
                    'id': node.id,
                    'region': region,
                    'name': node.name,
                    'state': node.state,
                    'public_ip': node.public_ips[0],
                    'provider': 'Amazon',
                    'extra': extra,
                    'elastic': '0'
                }

            nodesProvider = json.dumps(attr)

    if provider == "Azure":
        pass
        tenantId = driverUno
        subscriptionId = driverDos
        applicationId = driverTres
        keyPaswd = driverCuatro
        driver = AzureNodeDriver(tenantId, subscriptionId, applicationId,
                                 keyPaswd)

        locations = driver.list_locations()
        networks = driver.ex_list_networks()

        for network in networks:
            pass
            if network.name == ex_network:
                pass
                ex_network = network

        for loc in locations:
            pass
            if loc.id == location:
                pass
                locationID = loc

        sizesNode = driver.list_sizes(location=locationID)
        imagesNode = driver.list_images(location=locationID,
                                        ex_publisher=None,
                                        ex_offer=None,
                                        ex_sku=None,
                                        ex_version=None)
        for sizeNode in sizesNode:
            if sizeNode.name == size:
                pass
                sizeName = sizeNode

        for imageNode in imagesNode:
            if imageNode.id == image:
                pass
                imageId = imageNode

        nodeCreate = driver.create_node(name,
                                        sizeName,
                                        imageId,
                                        None,
                                        None,
                                        None,
                                        location=locationID,
                                        ex_network=ex_network,
                                        ex_subnet=None,
                                        ex_nic=None)

        types = 'running'

        node = checkStatus.checkStatus(driver, nodeCreate.id, types)

        if node != 0:
            pass
            v4 = []
            ips = {
                'ipaddress': node.public_ips,
                'gateway': 'NULL',
                'mask': 'NULL',
                'private_ip': node.private_ips
            }

            v4.append(ips)

            network = {'v4': v4}

            extra = {
                'instance_type':
                node.extra['properties']['hardwareProfile']['vmSize'],
                'network':
                network
            }

            attr = {
                'id': node.extra['properties']['vmId'],
                'region': node.extra['location'],
                'name': node.name,
                'state': node.state,
                'public_ip': node.public_ips,
                'provider': 'Azure Virtual machines',
                'extra': extra
            }

            nodesProvider = json.dumps(attr)

    if provider == "Linode":
        pass
        apiKey = driverUno
        driverDos = driverDos
        driverTres = driverTres
        driverCuatro = driverCuatro
        nodeId = nodeId
        driver = LinodeNodeDriver(apiKey)

        node = driver.create_node(name, image, size, auth, location)
        nodesProvider = json.dumps(node)

    return nodesProvider
Пример #11
0
def getLocations(provider, driverUno, driverDos, driverTres, driverCuatro):
    pass
    nodesProvider = ''
    if provider == "Digital Ocean":
        pass
        accessKey = driverUno
        driverDos = driverDos
        driverTres = driverTres
        driverCuatro = driverCuatro
        driver = DigitalOceanNodeDriver(accessKey)

        listLocations = driver.list_locations()

        nodelocations = []
        for listLocation in listLocations:
            data = []
            flag = False
            matches = re.search(r'[a-zA-Z]{0,}', listLocation.name)

            match = re.search(r'[a-zA-Z]{0,}[ ][a-zA-Z]{0,}',
                              listLocation.name)

            if match:
                region = match.group()
            else:
                region = matches.group()

            for nodelocation in nodelocations:
                if nodelocation['city'] == region:
                    datacenter = {
                        'id': listLocation.id,
                        'name': listLocation.name
                    }
                    nodelocation['datacenter'].append(datacenter)
                    flag = True
            if flag != True:
                datacenter = {'id': listLocation.id, 'name': listLocation.name}
                data.append(datacenter)
                node = {'country': 'null', 'city': region, 'datacenter': data}

                nodelocations.append(node)

        nodesProvider = json.dumps(nodelocations)

    if provider == "EC2":
        pass
        accessId = driverUno
        secretKey = driverDos
        region = driverTres
        driverCuatro = driverCuatro
        driver = EC2NodeDriver(accessId, secretKey)

        listRegions = driver.list_regions()

        #	print listRegions

        nodelocations = []

        for listRegion in listRegions:
            region = listRegion

            if region == 'ap-south-1':
                pass
                region = 'ap-southeast-1'
            if region == 'us-east-2':
                pass
                region = 'us-east-1'
            if region == 'cn-north-1':
                pass
                region = 'us-east-1'
            if region == 'ca-central-1':
                pass
                region = 'us-east-1'
            if region == 'ap-northeast-2':
                pass
                region = 'ap-northeast-1'
            if region == 'us-gov-west-1':
                pass
                region = 'us-east-1'
            if region == 'eu-central-1':
                pass
                region = 'eu-west-1'
            if region == 'eu-west-2':
                pass
                region = 'eu-west-1'

            driverLocation = EC2NodeDriver(accessId, secretKey, None, None,
                                           None, region, None)

            listLocations = driverLocation.list_locations()

            #		print region

            #		print listLocations
            datacenterArray = []
            for listLocation in listLocations:
                flag = False
                for nodelocation in nodelocations:
                    if 'country' in nodelocation:
                        if nodelocation['country'] == listLocation.country:
                            datacenter = {
                                'id': listLocation.name,
                                'name': listLocation.name
                            }
                            datacenterArray.append(datacenter)
                            flag = True
                if flag != True:
                    datacenter = {
                        'id': listLocation.name,
                        'name': listLocation.name
                    }
                    datacenterArray.append(datacenter)
                    regionName = getRegionName.regionsAmazon(listRegion)
                    if regionName != 'null':
                        regionID = {
                            'country': listLocation.country,
                            'city': regionName,
                            'datacenter': datacenterArray
                        }
                        nodelocations.append(regionID)

        nodesProvider = json.dumps(nodelocations)

    if provider == "Azure":
        pass
        tenantId = driverUno
        subscriptionId = driverDos
        applicationId = driverTres
        keyPaswd = driverCuatro
        driver = AzureNodeDriver(tenantId, subscriptionId, applicationId,
                                 keyPaswd)

        listLocations = driver.list_locations()

        nodelocations = []
        for listLocation in listLocations:
            data = []
            flag = False
            country = listLocation.country

            if country is not None:
                pass
                matches = country.partition(',')

                if matches[1] == ',':

                    dos = matches[2]
                    country = dos[1:len(dos)]
                    city = matches[0]

                    for nodelocation in nodelocations:
                        if nodelocation['city'] == city:
                            datacenter = {
                                'id': listLocation.id,
                                'name': listLocation.name
                            }
                            nodelocation['datacenter'].append(datacenter)
                            flag = True
                    if flag != True:
                        datacenter = {
                            'id': listLocation.id,
                            'name': listLocation.name
                        }
                        data.append(datacenter)
                        node = {
                            'country': country,
                            'city': city,
                            'datacenter': data
                        }
                        nodelocations.append(node)
                else:

                    city = 'null'

                    datacenter = {
                        'id': listLocation.id,
                        'name': listLocation.name
                    }
                    data.append(datacenter)
                    node = {
                        'country': country,
                        'city': city,
                        'datacenter': data
                    }
                    nodelocations.append(node)
            else:

                city = getRegionName.regionsAzure(listLocation.name)

                country = 'null'

                datacenter = {'id': listLocation.id, 'name': listLocation.name}
                data.append(datacenter)

                node = {'country': country, 'city': city, 'datacenter': data}

                nodelocations.append(node)

        nodesProvider = json.dumps(nodelocations)
    if provider == "Linode":
        pass
        apiKey = driverUno
        driverDos = driverDos
        driverTres = driverTres
        driverCuatro = driverCuatro
        driver = DigitalOceanNodeDriver(apiKey)

        listLocations = driver.list_locations()

        nodesProvider = listLocations
    return nodesProvider
class DigitalOcean_v2_Tests(LibcloudTestCase):

    def setUp(self):
        DigitalOceanNodeDriver.connectionCls.conn_class = \
            DigitalOceanMockHttp
        DigitalOceanMockHttp.type = None
        self.driver = DigitalOceanNodeDriver(*DIGITALOCEAN_v2_PARAMS)

    def test_v1_Error(self):
        self.assertRaises(DigitalOcean_v1_Error, DigitalOceanNodeDriver,
                          *DIGITALOCEAN_v1_PARAMS, api_version='v1')

    def test_v2_uses_v1_key(self):
        self.assertRaises(InvalidCredsError, DigitalOceanNodeDriver,
                          *DIGITALOCEAN_v1_PARAMS, api_version='v2')

    def test_authentication(self):
        DigitalOceanMockHttp.type = 'UNAUTHORIZED'
        self.assertRaises(InvalidCredsError, self.driver.list_nodes)

    def test_list_images_success(self):
        images = self.driver.list_images()
        self.assertTrue(len(images) >= 1)

        image = images[0]
        self.assertTrue(image.id is not None)
        self.assertTrue(image.name is not None)

    def test_list_sizes_success(self):
        sizes = self.driver.list_sizes()
        self.assertTrue(len(sizes) >= 1)

        size = sizes[0]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '512mb')
        self.assertEqual(size.ram, 512)

        size = sizes[1]
        self.assertTrue(size.id is not None)
        self.assertEqual(size.name, '1gb')
        self.assertEqual(size.ram, 1024)

    def test_list_locations_success(self):
        locations = self.driver.list_locations()
        self.assertTrue(len(locations) >= 1)

        location = locations[0]
        self.assertEqual(location.id, 'nyc1')
        self.assertEqual(location.name, 'New York 1')

    def test_list_nodes_success(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(len(nodes), 1)
        self.assertEqual(nodes[0].name, 'example.com')
        self.assertEqual(nodes[0].public_ips, ['104.236.32.182'])
        self.assertEqual(nodes[0].extra['image']['id'], 6918990)
        self.assertEqual(nodes[0].extra['size_slug'], '512mb')

    def test_list_nodes_fills_created_datetime(self):
        nodes = self.driver.list_nodes()
        self.assertEqual(nodes[0].created_at, datetime(2014, 11, 14, 16, 29, 21, tzinfo=UTC))

    def test_create_node_invalid_size(self):
        image = NodeImage(id='invalid', name=None, driver=self.driver)
        size = self.driver.list_sizes()[0]
        location = self.driver.list_locations()[0]

        DigitalOceanMockHttp.type = 'INVALID_IMAGE'
        expected_msg = \
            r'You specified an invalid image for Droplet creation. \(code: (404|HTTPStatus.NOT_FOUND)\)'
        self.assertRaisesRegexp(Exception, expected_msg,
                                self.driver.create_node,
                                name='test', size=size, image=image,
                                location=location)

    def test_reboot_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'REBOOT'
        result = self.driver.reboot_node(node)
        self.assertTrue(result)

    def test_create_image_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'SNAPSHOT'
        result = self.driver.create_image(node, 'My snapshot')
        self.assertTrue(result)

    def test_get_image_success(self):
        image = self.driver.get_image(12345)
        self.assertEqual(image.name, 'My snapshot')
        self.assertEqual(image.id, '12345')
        self.assertEqual(image.extra['distribution'], 'Ubuntu')

    def test_delete_image_success(self):
        image = self.driver.get_image(12345)
        DigitalOceanMockHttp.type = 'DESTROY'
        result = self.driver.delete_image(image)
        self.assertTrue(result)

    def test_ex_power_on_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'POWERON'
        result = self.driver.ex_power_on_node(node)
        self.assertTrue(result)

    def test_ex_shutdown_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'SHUTDOWN'
        result = self.driver.ex_shutdown_node(node)
        self.assertTrue(result)

    def test_ex_hard_reboot_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'POWERCYCLE'
        result = self.driver.ex_hard_reboot(node)
        self.assertTrue(result)

    def test_destroy_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'DESTROY'
        result = self.driver.destroy_node(node)
        self.assertTrue(result)

    def test_ex_change_kernel_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'KERNELCHANGE'
        result = self.driver.ex_change_kernel(node, 7515)
        self.assertTrue(result)

    def test_ex_rename_node_success(self):
        node = self.driver.list_nodes()[0]
        DigitalOceanMockHttp.type = 'RENAME'
        result = self.driver.ex_rename_node(node, 'fedora helios')
        self.assertTrue(result)

    def test_list_key_pairs(self):
        keys = self.driver.list_key_pairs()
        self.assertEqual(len(keys), 1)
        self.assertEqual(keys[0].extra['id'], 7717)
        self.assertEqual(keys[0].name, 'test1')
        self.assertEqual(keys[0].public_key,
                         "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDGk5 example")

    def test_create_key_pair(self):
        DigitalOceanMockHttp.type = 'CREATE'
        key = self.driver.create_key_pair(
            name="test1",
            public_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQsxRiUKn example"
        )
        self.assertEqual(key.name, "test1")
        self.assertEqual(key.fingerprint,
                         "f5:d1:78:ed:28:72:5f:e1:ac:94:fd:1f:e0:a3:48:6d")

    def test_delete_key_pair(self):
        key = self.driver.list_key_pairs()[0]
        result = self.driver.delete_key_pair(key)
        self.assertTrue(result)

    def test__paginated_request_single_page(self):
        nodes = self.driver._paginated_request('/v2/droplets', 'droplets')
        self.assertEqual(nodes[0]['name'], 'example.com')
        self.assertEqual(nodes[0]['image']['id'], 6918990)
        self.assertEqual(nodes[0]['size_slug'], '512mb')

    def test__paginated_request_two_pages(self):
        DigitalOceanMockHttp.type = 'PAGE_ONE'
        nodes = self.driver._paginated_request('/v2/droplets', 'droplets')
        self.assertEqual(len(nodes), 2)

    def test_list_volumes(self):
        volumes = self.driver.list_volumes()
        self.assertEqual(len(volumes), 1)
        volume = volumes[0]
        self.assertEqual(volume.id, "62766883-2c28-11e6-b8e6-000f53306ae1")
        self.assertEqual(volume.name, "example")
        self.assertEqual(volume.size, 4)
        self.assertEqual(volume.driver, self.driver)

    def test_list_volumes_empty(self):
        DigitalOceanMockHttp.type = 'EMPTY'
        volumes = self.driver.list_volumes()
        self.assertEqual(len(volumes), 0)

    def test_create_volume(self):
        nyc1 = [r for r in self.driver.list_locations() if r.id == 'nyc1'][0]
        DigitalOceanMockHttp.type = 'CREATE'
        volume = self.driver.create_volume(4, 'example', nyc1)
        self.assertEqual(volume.id, "62766883-2c28-11e6-b8e6-000f53306ae1")
        self.assertEqual(volume.name, "example")
        self.assertEqual(volume.size, 4)
        self.assertEqual(volume.driver, self.driver)

    def test_attach_volume(self):
        node = self.driver.list_nodes()[0]
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'ATTACH'
        resp = self.driver.attach_volume(node, volume)
        self.assertTrue(resp)

    def test_detach_volume(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'DETACH'
        resp = self.driver.detach_volume(volume)
        self.assertTrue(resp)

    def test_destroy_volume(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'DESTROY'
        resp = self.driver.destroy_volume(volume)
        self.assertTrue(resp)

    def test_list_volume_snapshots(self):
        volume = self.driver.list_volumes()[0]
        snapshots = self.driver.list_volume_snapshots(volume)
        self.assertEqual(len(snapshots), 3)
        snapshot1, snapshot2, snapshot3 = snapshots
        self.assertEqual(snapshot1.id, "c0def940-9324-11e6-9a56-000f533176b1")
        self.assertEqual(snapshot2.id, "c2036724-9343-11e6-aef4-000f53315a41")
        self.assertEqual(snapshot3.id, "d347e033-9343-11e6-9a56-000f533176b1")

    def test_create_volume_snapshot(self):
        volume = self.driver.list_volumes()[0]
        DigitalOceanMockHttp.type = 'CREATE'
        snapshot = self.driver.create_volume_snapshot(volume, 'test-snapshot')
        self.assertEqual(snapshot.id, "c0def940-9324-11e6-9a56-000f533176b1")
        self.assertEqual(snapshot.name, 'test-snapshot')
        self.assertEqual(volume.driver, self.driver)

    def test_delete_volume_snapshot(self):
        volume = self.driver.list_volumes()[0]
        snapshot = self.driver.list_volume_snapshots(volume)[0]
        DigitalOceanMockHttp.type = 'DELETE'
        result = self.driver.delete_volume_snapshot(snapshot)
        self.assertTrue(result)
from libcloud.compute.providers import get_driver
from libcloud.compute.drivers.digitalocean import DigitalOceanNodeDriver
from libcloud.compute.drivers.azure_arm import AzureNodeDriver
from libcloud.compute.drivers.ec2 import BaseEC2NodeDriver, EC2NodeDriver
from libcloud.compute.drivers.linode import LinodeNodeDriver

provider = sys.argv[1]
if provider == "Digital Ocean":
    pass
    accessKey = sys.argv[2]
    sshKey = sys.argv[3]
    driverTres = sys.argv[4]
    driverCuatro = sys.argv[5]
    driver = DigitalOceanNodeDriver(accessKey)

    listLocations = driver.list_locations()

    nodelocations = []
    for listLocation in listLocations:
        data = []
        flag = False
        matches = re.search(r'[a-zA-Z]{0,}', listLocation.name)

        match = re.search(r'[a-zA-Z]{0,}[ ][a-zA-Z]{0,}', listLocation.name)

        if match:
            region = match.group()
        else:
            region = matches.group()

        for nodelocation in nodelocations: