sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init a year ago
Clone
1064ba
From 05561668e42d905cca7d72c2b80a939fbddb2c9d Mon Sep 17 00:00:00 2001
bfa931
From: Junjie Wang <jingni.wjj@alibaba-inc.com>
bfa931
Date: Fri, 21 Apr 2017 20:06:09 +0800
bfa931
Subject: [PATCH 4/5] AliYun: Enable platform identification and enable by
bfa931
 default.
bfa931
bfa931
AliYun cloud platform is now identifying themselves by setting the dmi
bfa931
product id to the well known value "Alibaba Cloud ECS". The changes here
bfa931
identify that properly in tools/ds-identify and in the DataSourceAliYun.
bfa931
bfa931
Since the 'get_data' for AliYun now identifies itself correctly, we can
bfa931
enable AliYun by default.
bfa931
bfa931
LP: #1638931
bfa931
(cherry picked from commit 4a60af54957634920e84a928aa22b4fc9a6dfd11)
bfa931
1064ba
Resolves: rhbz#1482547
bfa931
bfa931
Signed-off-by: Ryan McCabe <rmccabe@redhat.com>
bfa931
---
bfa931
 cloudinit/settings.py                          |  1 +
bfa931
 cloudinit/sources/DataSourceAliYun.py          | 14 ++++++-
bfa931
 cloudinit/sources/DataSourceEc2.py             |  7 ++++
bfa931
 tests/unittests/test_datasource/test_aliyun.py | 51 +++++++++++++++++++++++++-
bfa931
 tests/unittests/test_datasource/test_common.py |  1 +
bfa931
 5 files changed, 71 insertions(+), 3 deletions(-)
bfa931
bfa931
diff --git a/cloudinit/settings.py b/cloudinit/settings.py
bfa931
index 0d39aab7..d6046dc6 100644
bfa931
--- a/cloudinit/settings.py
bfa931
+++ b/cloudinit/settings.py
bfa931
@@ -27,6 +27,7 @@ CFG_BUILTIN = {
bfa931
         'MAAS',
bfa931
         'GCE',
bfa931
         'OpenStack',
bfa931
+        'AliYun',
bfa931
         'Ec2',
bfa931
         'CloudSigma',
bfa931
         'CloudStack',
bfa931
diff --git a/cloudinit/sources/DataSourceAliYun.py b/cloudinit/sources/DataSourceAliYun.py
bfa931
index 9debe947..380e27cb 100644
bfa931
--- a/cloudinit/sources/DataSourceAliYun.py
bfa931
+++ b/cloudinit/sources/DataSourceAliYun.py
bfa931
@@ -4,8 +4,10 @@ import os
bfa931
 
bfa931
 from cloudinit import sources
bfa931
 from cloudinit.sources import DataSourceEc2 as EC2
bfa931
+from cloudinit import util
bfa931
 
bfa931
 DEF_MD_VERSION = "2016-01-01"
bfa931
+ALIYUN_PRODUCT = "Alibaba Cloud ECS"
bfa931
 
bfa931
 
bfa931
 class DataSourceAliYun(EC2.DataSourceEc2):
bfa931
@@ -24,7 +26,17 @@ class DataSourceAliYun(EC2.DataSourceEc2):
bfa931
 
bfa931
     @property
bfa931
     def cloud_platform(self):
bfa931
-        return EC2.Platforms.ALIYUN
bfa931
+        if self._cloud_platform is None:
bfa931
+            if _is_aliyun():
bfa931
+                self._cloud_platform = EC2.Platforms.ALIYUN
bfa931
+            else:
bfa931
+                self._cloud_platform = EC2.Platforms.NO_EC2_METADATA
bfa931
+
bfa931
+        return self._cloud_platform
bfa931
+
bfa931
+
bfa931
+def _is_aliyun():
bfa931
+    return util.read_dmi_data('system-product-name') == ALIYUN_PRODUCT
bfa931
 
bfa931
 
bfa931
 def parse_public_keys(public_keys):
bfa931
diff --git a/cloudinit/sources/DataSourceEc2.py b/cloudinit/sources/DataSourceEc2.py
bfa931
index c7df8060..31825665 100644
bfa931
--- a/cloudinit/sources/DataSourceEc2.py
bfa931
+++ b/cloudinit/sources/DataSourceEc2.py
bfa931
@@ -32,7 +32,12 @@ class Platforms(object):
bfa931
     AWS = "AWS"
bfa931
     BRIGHTBOX = "Brightbox"
bfa931
     SEEDED = "Seeded"
bfa931
+    # UNKNOWN indicates no positive id.  If strict_id is 'warn' or 'false',
bfa931
+    # then an attempt at the Ec2 Metadata service will be made.
bfa931
     UNKNOWN = "Unknown"
bfa931
+    # NO_EC2_METADATA indicates this platform does not have a Ec2 metadata
bfa931
+    # service available. No attempt at the Ec2 Metadata service will be made.
bfa931
+    NO_EC2_METADATA = "No-EC2-Metadata"
bfa931
 
bfa931
 
bfa931
 class DataSourceEc2(sources.DataSource):
bfa931
@@ -65,6 +70,8 @@ class DataSourceEc2(sources.DataSource):
bfa931
                   strict_mode, self.cloud_platform)
bfa931
         if strict_mode == "true" and self.cloud_platform == Platforms.UNKNOWN:
bfa931
             return False
bfa931
+        elif self.cloud_platform == Platforms.NO_EC2_METADATA:
bfa931
+            return False
bfa931
 
bfa931
         try:
bfa931
             if not self.wait_for_metadata_service():
bfa931
diff --git a/tests/unittests/test_datasource/test_aliyun.py b/tests/unittests/test_datasource/test_aliyun.py
bfa931
index c16d1a6e..990bff2c 100644
bfa931
--- a/tests/unittests/test_datasource/test_aliyun.py
bfa931
+++ b/tests/unittests/test_datasource/test_aliyun.py
bfa931
@@ -2,6 +2,7 @@
bfa931
 
bfa931
 import functools
bfa931
 import httpretty
bfa931
+import mock
bfa931
 import os
bfa931
 
bfa931
 from .. import helpers as test_helpers
bfa931
@@ -111,15 +112,29 @@ class TestAliYunDatasource(test_helpers.HttprettyTestCase):
bfa931
         self.assertEqual(self.default_metadata['hostname'],
bfa931
                          self.ds.get_hostname())
bfa931
 
bfa931
+    @mock.patch("cloudinit.sources.DataSourceAliYun._is_aliyun")
bfa931
     @httpretty.activate
bfa931
-    def test_with_mock_server(self):
bfa931
+    def test_with_mock_server(self, m_is_aliyun):
bfa931
+        m_is_aliyun.return_value = True
bfa931
         self.regist_default_server()
bfa931
-        self.ds.get_data()
bfa931
+        ret = self.ds.get_data()
bfa931
+        self.assertEqual(True, ret)
bfa931
+        self.assertEqual(1, m_is_aliyun.call_count)
bfa931
         self._test_get_data()
bfa931
         self._test_get_sshkey()
bfa931
         self._test_get_iid()
bfa931
         self._test_host_name()
bfa931
 
bfa931
+    @mock.patch("cloudinit.sources.DataSourceAliYun._is_aliyun")
bfa931
+    @httpretty.activate
bfa931
+    def test_returns_false_when_not_on_aliyun(self, m_is_aliyun):
bfa931
+        """If is_aliyun returns false, then get_data should return False."""
bfa931
+        m_is_aliyun.return_value = False
bfa931
+        self.regist_default_server()
bfa931
+        ret = self.ds.get_data()
bfa931
+        self.assertEqual(1, m_is_aliyun.call_count)
bfa931
+        self.assertEqual(False, ret)
bfa931
+
bfa931
     def test_parse_public_keys(self):
bfa931
         public_keys = {}
bfa931
         self.assertEqual(ay.parse_public_keys(public_keys), [])
bfa931
@@ -149,4 +164,36 @@ class TestAliYunDatasource(test_helpers.HttprettyTestCase):
bfa931
         self.assertEqual(ay.parse_public_keys(public_keys),
bfa931
                          public_keys['key-pair-0']['openssh-key'])
bfa931
 
bfa931
+
bfa931
+class TestIsAliYun(test_helpers.CiTestCase):
bfa931
+    ALIYUN_PRODUCT = 'Alibaba Cloud ECS'
bfa931
+    read_dmi_data_expected = [mock.call('system-product-name')]
bfa931
+
bfa931
+    @mock.patch("cloudinit.sources.DataSourceAliYun.util.read_dmi_data")
bfa931
+    def test_true_on_aliyun_product(self, m_read_dmi_data):
bfa931
+        """Should return true if the dmi product data has expected value."""
bfa931
+        m_read_dmi_data.return_value = self.ALIYUN_PRODUCT
bfa931
+        ret = ay._is_aliyun()
bfa931
+        self.assertEqual(self.read_dmi_data_expected,
bfa931
+                         m_read_dmi_data.call_args_list)
bfa931
+        self.assertEqual(True, ret)
bfa931
+
bfa931
+    @mock.patch("cloudinit.sources.DataSourceAliYun.util.read_dmi_data")
bfa931
+    def test_false_on_empty_string(self, m_read_dmi_data):
bfa931
+        """Should return false on empty value returned."""
bfa931
+        m_read_dmi_data.return_value = ""
bfa931
+        ret = ay._is_aliyun()
bfa931
+        self.assertEqual(self.read_dmi_data_expected,
bfa931
+                         m_read_dmi_data.call_args_list)
bfa931
+        self.assertEqual(False, ret)
bfa931
+
bfa931
+    @mock.patch("cloudinit.sources.DataSourceAliYun.util.read_dmi_data")
bfa931
+    def test_false_on_unknown_string(self, m_read_dmi_data):
bfa931
+        """Should return false on an unrelated string."""
bfa931
+        m_read_dmi_data.return_value = "cubs win"
bfa931
+        ret = ay._is_aliyun()
bfa931
+        self.assertEqual(self.read_dmi_data_expected,
bfa931
+                         m_read_dmi_data.call_args_list)
bfa931
+        self.assertEqual(False, ret)
bfa931
+
bfa931
 # vi: ts=4 expandtab
bfa931
diff --git a/tests/unittests/test_datasource/test_common.py b/tests/unittests/test_datasource/test_common.py
bfa931
index c08717f3..7649b9ae 100644
bfa931
--- a/tests/unittests/test_datasource/test_common.py
bfa931
+++ b/tests/unittests/test_datasource/test_common.py
bfa931
@@ -36,6 +36,7 @@ DEFAULT_LOCAL = [
bfa931
 ]
bfa931
 
bfa931
 DEFAULT_NETWORK = [
bfa931
+    AliYun.DataSourceAliYun,
bfa931
     AltCloud.DataSourceAltCloud,
bfa931
     Azure.DataSourceAzureNet,
bfa931
     Bigstep.DataSourceBigstep,
bfa931
-- 
bfa931
2.13.5
bfa931