You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by to...@apache.org on 2020/11/05 21:52:48 UTC

[libcloud] 03/08: add VultrNodeDriverHelper class, update fixtures; improve tests (#2)

This is an automated email from the ASF dual-hosted git repository.

tomaz pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/libcloud.git

commit c901a97b17e1c8775de76d408fa6a7f56fc8441d
Author: sergerdn <64...@users.noreply.github.com>
AuthorDate: Thu Oct 15 12:57:55 2020 +0300

    add VultrNodeDriverHelper class, update fixtures; improve tests (#2)
---
 libcloud/compute/drivers/vultr.py                  |  46 +++--
 .../test/compute/fixtures/vultr/list_images.json   | 185 ++++++++++++++++++++-
 .../compute/fixtures/vultr/list_locations.json     | 108 ++++++++++--
 .../test/compute/fixtures/vultr/list_sizes.json    |  10 +-
 libcloud/test/compute/test_vultr.py                |  26 ++-
 5 files changed, 337 insertions(+), 38 deletions(-)

diff --git a/libcloud/compute/drivers/vultr.py b/libcloud/compute/drivers/vultr.py
index 76029b0..4628d86 100644
--- a/libcloud/compute/drivers/vultr.py
+++ b/libcloud/compute/drivers/vultr.py
@@ -150,6 +150,19 @@ class VultrConnection(ConnectionKey):
             return True
 
 
+class VultrNodeDriverHelper(object):
+    """
+        VultrNode helper class.
+    """
+
+    def handle_extra(self, extra_keys, data):
+        extra = {}
+        for key in extra_keys:
+            if key in data:
+                extra[key] = data[key]
+        return extra
+
+
 class VultrNodeDriver(NodeDriver):
     """
     VultrNode node driver.
@@ -184,6 +197,10 @@ class VultrNodeDriver(NodeDriver):
     EX_CREATE_ATTRIBUTES.extend(EX_CREATE_YES_NO_ATTRIBUTES)
     EX_CREATE_ATTRIBUTES.extend(EX_CREATE_ID_ATTRIBUTES.keys())
 
+    def __init__(self, *args, **kwargs):
+        super(VultrNodeDriver, self).__init__(*args, **kwargs)
+        self._helper = VultrNodeDriverHelper()
+
     def list_nodes(self):
         return self._list_resources('/v1/server/list', self._to_node)
 
@@ -379,10 +396,7 @@ class VultrNodeDriver(NodeDriver):
             "OSID",  # Operating system to use. See v1/os/list.
             "APPID", "FIREWALLGROUPID"
         ]
-        extra = {}
-        for key in extra_keys:
-            if key in data:
-                extra[key] = data[key]
+        extra = self._helper.handle_extra(extra_keys, data)
 
         node = Node(id=data['SUBID'], name=data['label'], state=state,
                     public_ips=public_ips, private_ips=private_ips,
@@ -391,28 +405,36 @@ class VultrNodeDriver(NodeDriver):
         return node
 
     def _to_location(self, data):
+        extra_keys = ['continent', 'state', 'ddos_protection',
+                      'block_storage', 'regioncode']
+        extra = self._helper.handle_extra(extra_keys, data)
+
         return NodeLocation(id=data['DCID'], name=data['name'],
-                            country=data['country'], driver=self)
+                            country=data['country'], extra=extra, driver=self)
 
     def _to_size(self, data):
-        extra = {
-            'vcpu_count': int(data['vcpu_count']),
-            'plan_type': data['plan_type'],
-            'available_locations': data['available_locations']
-        }
+        extra_keys = [
+            'vcpu_count', 'plan_type', 'available_locations',
+        ]
+        extra = self._helper.handle_extra(extra_keys, data)
+
+        # backward compatibility
+        if extra.get('vcpu_count').isdigit():
+            extra['vcpu_count'] = int(extra['vcpu_count'])
+
         ram = int(data['ram'])
         disk = int(data['disk'])
         # NodeSize accepted int instead float
         bandwidth = int(float(data['bandwidth']))
         price = float(data['price_per_month'])
-
         return NodeSize(id=data['VPSPLANID'], name=data['name'],
                         ram=ram, disk=disk,
                         bandwidth=bandwidth, price=price,
                         extra=extra, driver=self)
 
     def _to_image(self, data):
-        extra = {'arch': data['arch'], 'family': data['family']}
+        extra_keys = ['arch', 'family']
+        extra = self._helper.handle_extra(extra_keys, data)
         return NodeImage(id=data['OSID'], name=data['name'], extra=extra,
                          driver=self)
 
diff --git a/libcloud/test/compute/fixtures/vultr/list_images.json b/libcloud/test/compute/fixtures/vultr/list_images.json
index 139f2af..b28d857 100644
--- a/libcloud/test/compute/fixtures/vultr/list_images.json
+++ b/libcloud/test/compute/fixtures/vultr/list_images.json
@@ -1 +1,184 @@
-{"127":{"OSID":127,"name":"CentOS 6 x64","arch":"x64","family":"centos","windows":false},"147":{"OSID":147,"name":"CentOS 6 i386","arch":"i386","family":"centos","windows":false},"162":{"OSID":162,"name":"CentOS 5 x64","arch":"x64","family":"centos","windows":false},"163":{"OSID":163,"name":"CentOS 5 i386","arch":"i386","family":"centos","windows":false},"167":{"OSID":167,"name":"CentOS 7 x64","arch":"x64","family":"centos","windows":false},"160":{"OSID":160,"name":"Ubuntu 14.04 x64","ar [...]
\ No newline at end of file
+{
+  "127": {
+    "OSID": 127,
+    "name": "CentOS 6 x64",
+    "arch": "x64",
+    "family": "centos",
+    "windows": false
+  },
+  "147": {
+    "OSID": 147,
+    "name": "CentOS 6 i386",
+    "arch": "i386",
+    "family": "centos",
+    "windows": false
+  },
+  "167": {
+    "OSID": 167,
+    "name": "CentOS 7 x64",
+    "arch": "x64",
+    "family": "centos",
+    "windows": false
+  },
+  "381": {
+    "OSID": 381,
+    "name": "CentOS 7 SELinux x64",
+    "arch": "x64",
+    "family": "centos",
+    "windows": false
+  },
+  "362": {
+    "OSID": 362,
+    "name": "CentOS 8 x64",
+    "arch": "x64",
+    "family": "centos",
+    "windows": false
+  },
+  "401": {
+    "OSID": 401,
+    "name": "CentOS 8 Stream x64",
+    "arch": "x64",
+    "family": "centos",
+    "windows": false
+  },
+  "215": {
+    "OSID": 215,
+    "name": "Ubuntu 16.04 x64",
+    "arch": "x64",
+    "family": "ubuntu",
+    "windows": false
+  },
+  "216": {
+    "OSID": 216,
+    "name": "Ubuntu 16.04 i386",
+    "arch": "i386",
+    "family": "ubuntu",
+    "windows": false
+  },
+  "270": {
+    "OSID": 270,
+    "name": "Ubuntu 18.04 x64",
+    "arch": "x64",
+    "family": "ubuntu",
+    "windows": false
+  },
+  "387": {
+    "OSID": 387,
+    "name": "Ubuntu 20.04 x64",
+    "arch": "x64",
+    "family": "ubuntu",
+    "windows": false
+  },
+  "194": {
+    "OSID": 194,
+    "name": "Debian 8 i386 (jessie)",
+    "arch": "i386",
+    "family": "debian",
+    "windows": false
+  },
+  "244": {
+    "OSID": 244,
+    "name": "Debian 9 x64 (stretch)",
+    "arch": "x64",
+    "family": "debian",
+    "windows": false
+  },
+  "352": {
+    "OSID": 352,
+    "name": "Debian 10 x64 (buster)",
+    "arch": "x64",
+    "family": "debian",
+    "windows": false
+  },
+  "230": {
+    "OSID": 230,
+    "name": "FreeBSD 11 x64",
+    "arch": "x64",
+    "family": "freebsd",
+    "windows": false
+  },
+  "327": {
+    "OSID": 327,
+    "name": "FreeBSD 12 x64",
+    "arch": "x64",
+    "family": "freebsd",
+    "windows": false
+  },
+  "366": {
+    "OSID": 366,
+    "name": "OpenBSD 6.6 x64",
+    "arch": "x64",
+    "family": "openbsd",
+    "windows": false
+  },
+  "394": {
+    "OSID": 394,
+    "name": "OpenBSD 6.7 x64",
+    "arch": "x64",
+    "family": "openbsd",
+    "windows": false
+  },
+  "391": {
+    "OSID": 391,
+    "name": "Fedora CoreOS",
+    "arch": "x64",
+    "family": "fedora-coreos",
+    "windows": false
+  },
+  "367": {
+    "OSID": 367,
+    "name": "Fedora 31 x64",
+    "arch": "x64",
+    "family": "fedora",
+    "windows": false
+  },
+  "389": {
+    "OSID": 389,
+    "name": "Fedora 32 x64",
+    "arch": "x64",
+    "family": "fedora",
+    "windows": false
+  },
+  "124": {
+    "OSID": 124,
+    "name": "Windows 2012 R2 x64",
+    "arch": "x64",
+    "family": "windows",
+    "windows": true
+  },
+  "240": {
+    "OSID": 240,
+    "name": "Windows 2016 x64",
+    "arch": "x64",
+    "family": "windows",
+    "windows": true
+  },
+  "159": {
+    "OSID": 159,
+    "name": "Custom",
+    "arch": "x64",
+    "family": "iso",
+    "windows": false
+  },
+  "164": {
+    "OSID": 164,
+    "name": "Snapshot",
+    "arch": "x64",
+    "family": "snapshot",
+    "windows": false
+  },
+  "180": {
+    "OSID": 180,
+    "name": "Backup",
+    "arch": "x64",
+    "family": "backup",
+    "windows": false
+  },
+  "186": {
+    "OSID": 186,
+    "name": "Application",
+    "arch": "x64",
+    "family": "application",
+    "windows": false
+  }
+}
\ No newline at end of file
diff --git a/libcloud/test/compute/fixtures/vultr/list_locations.json b/libcloud/test/compute/fixtures/vultr/list_locations.json
index 5230fbc..559daeb 100644
--- a/libcloud/test/compute/fixtures/vultr/list_locations.json
+++ b/libcloud/test/compute/fixtures/vultr/list_locations.json
@@ -4,97 +4,169 @@
     "name": "Atlanta",
     "country": "US",
     "continent": "North America",
-    "state": "GA"
+    "state": "GA",
+    "ddos_protection": false,
+    "block_storage": false,
+    "regioncode": "ATL"
   },
   "2": {
     "DCID": "2",
     "name": "Chicago",
     "country": "US",
     "continent": "North America",
-    "state": "IL"
+    "state": "IL",
+    "ddos_protection": true,
+    "block_storage": false,
+    "regioncode": "ORD"
   },
   "3": {
     "DCID": "3",
     "name": "Dallas",
     "country": "US",
     "continent": "North America",
-    "state": "TX"
+    "state": "TX",
+    "ddos_protection": true,
+    "block_storage": false,
+    "regioncode": "DFW"
   },
   "5": {
     "DCID": "5",
     "name": "Los Angeles",
     "country": "US",
     "continent": "North America",
-    "state": "CA"
+    "state": "CA",
+    "ddos_protection": true,
+    "block_storage": false,
+    "regioncode": "LAX"
   },
   "39": {
     "DCID": "39",
     "name": "Miami",
     "country": "US",
-    "continent": "",
-    "state": "FL"
+    "continent": "North America",
+    "state": "FL",
+    "ddos_protection": true,
+    "block_storage": false,
+    "regioncode": "MIA"
   },
   "1": {
     "DCID": "1",
     "name": "New Jersey",
     "country": "US",
     "continent": "North America",
-    "state": "NJ"
+    "state": "NJ",
+    "ddos_protection": true,
+    "block_storage": true,
+    "regioncode": "EWR"
   },
   "4": {
     "DCID": "4",
     "name": "Seattle",
     "country": "US",
     "continent": "North America",
-    "state": "WA"
+    "state": "WA",
+    "ddos_protection": true,
+    "block_storage": false,
+    "regioncode": "SEA"
   },
   "12": {
     "DCID": "12",
     "name": "Silicon Valley",
     "country": "US",
     "continent": "North America",
-    "state": "CA"
+    "state": "CA",
+    "ddos_protection": true,
+    "block_storage": false,
+    "regioncode": "SJC"
+  },
+  "40": {
+    "DCID": "40",
+    "name": "Singapore",
+    "country": "SG",
+    "continent": "Asia",
+    "state": "",
+    "ddos_protection": false,
+    "block_storage": false,
+    "regioncode": "SGP"
   },
   "7": {
     "DCID": "7",
     "name": "Amsterdam",
     "country": "NL",
     "continent": "Europe",
-    "state": ""
+    "state": "",
+    "ddos_protection": true,
+    "block_storage": false,
+    "regioncode": "AMS"
+  },
+  "34": {
+    "DCID": "34",
+    "name": "Seoul",
+    "country": "KR",
+    "continent": "Asia",
+    "state": "",
+    "ddos_protection": false,
+    "block_storage": false,
+    "regioncode": "ICN"
   },
   "25": {
     "DCID": "25",
     "name": "Tokyo",
     "country": "JP",
     "continent": "Asia",
-    "state": ""
+    "state": "",
+    "ddos_protection": false,
+    "block_storage": false,
+    "regioncode": "NRT"
   },
   "8": {
     "DCID": "8",
     "name": "London",
     "country": "GB",
     "continent": "Europe",
-    "state": ""
+    "state": "",
+    "ddos_protection": true,
+    "block_storage": false,
+    "regioncode": "LHR"
   },
   "24": {
     "DCID": "24",
-    "name": "France",
+    "name": "Paris",
     "country": "FR",
     "continent": "Europe",
-    "state": ""
+    "state": "",
+    "ddos_protection": true,
+    "block_storage": false,
+    "regioncode": "CDG"
   },
   "9": {
     "DCID": "9",
     "name": "Frankfurt",
     "country": "DE",
     "continent": "Europe",
-    "state": ""
+    "state": "",
+    "ddos_protection": true,
+    "block_storage": false,
+    "regioncode": "FRA"
+  },
+  "22": {
+    "DCID": "22",
+    "name": "Toronto",
+    "country": "CA",
+    "continent": "North America",
+    "state": "",
+    "ddos_protection": false,
+    "block_storage": false,
+    "regioncode": "YTO"
   },
   "19": {
     "DCID": "19",
-    "name": "Australia",
+    "name": "Sydney",
     "country": "AU",
     "continent": "Australia",
-    "state": ""
+    "state": "",
+    "ddos_protection": false,
+    "block_storage": false,
+    "regioncode": "SYD"
   }
-}
+}
\ No newline at end of file
diff --git a/libcloud/test/compute/fixtures/vultr/list_sizes.json b/libcloud/test/compute/fixtures/vultr/list_sizes.json
index 125358b..b61adb3 100644
--- a/libcloud/test/compute/fixtures/vultr/list_sizes.json
+++ b/libcloud/test/compute/fixtures/vultr/list_sizes.json
@@ -18,6 +18,7 @@
       5,
       6,
       7,
+      8,
       12,
       19,
       22,
@@ -81,7 +82,6 @@
       22,
       24,
       25,
-      34,
       39,
       40
     ]
@@ -163,7 +163,6 @@
       19,
       22,
       24,
-      25,
       39
     ]
   },
@@ -201,8 +200,7 @@
     "windows": false,
     "available_locations": [
       12,
-      19,
-      24
+      19
     ]
   },
   "115": {
@@ -218,8 +216,8 @@
     "windows": false,
     "available_locations": [
       1,
-      2,
-      12
+      12,
+      25
     ]
   },
   "116": {
diff --git a/libcloud/test/compute/test_vultr.py b/libcloud/test/compute/test_vultr.py
index 37546e7..8bb07ec 100644
--- a/libcloud/test/compute/test_vultr.py
+++ b/libcloud/test/compute/test_vultr.py
@@ -52,6 +52,10 @@ class VultrTests(LibcloudTestCase):
         self.assertTrue(image.id is not None)
         self.assertTrue(image.name is not None)
 
+    def test_list_sizes_dont_require_api_key(self):
+        self.driver.list_sizes()
+        self.assertFalse(self.driver.connection.require_api_key())
+
     def test_list_sizes_success(self):
         """count of current plans"""
         sizes = self.driver.list_sizes()
@@ -67,6 +71,10 @@ class VultrTests(LibcloudTestCase):
         self.assertEqual(size.name, '16384 MB RAM,384 GB SSD,5.00 TB BW')
         self.assertEqual(size.ram, 16384)
 
+    def test_list_locations_dont_require_api_key(self):
+        self.driver.list_locations()
+        self.assertFalse(self.driver.connection.require_api_key())
+
     def test_list_locations_success(self):
         locations = self.driver.list_locations()
         self.assertTrue(len(locations) >= 1)
@@ -74,6 +82,21 @@ class VultrTests(LibcloudTestCase):
         location = locations[0]
         self.assertEqual(location.id, '1')
         self.assertEqual(location.name, 'New Jersey')
+        self.assertEqual(location.extra['continent'], 'North America')
+
+    def test_list_locations_extra_success(self):
+        locations = self.driver.list_locations()
+        self.assertTrue(len(locations) >= 1)
+        extra_keys = [
+            'continent',
+            'state',
+            'ddos_protection',
+            'block_storage',
+            'regioncode']
+        for location in locations:
+            self.assertTrue(len(location.extra.keys()) >= 5)
+            self.assertTrue(all(item in location.extra.keys()
+                                for item in extra_keys))
 
     def test_list_nodes_require_api_key(self):
         self.driver.list_nodes()
@@ -95,7 +118,8 @@ class VultrTests(LibcloudTestCase):
         nodes = self.driver.list_nodes()
         for node in nodes:
             self.assertTrue(len(node.extra.keys()) > 5)
-            self.assertTrue(all(item in node.extra.keys() for item in extra_keys))
+            self.assertTrue(all(item in node.extra.keys()
+                                for item in extra_keys))
 
     def test_reboot_node_success(self):
         node = self.driver.list_nodes()[0]