Added Helm Plugin unit test code 07/57107/1
authorHong Guan <hg4105@att.com>
Sun, 22 Jul 2018 11:23:20 +0000 (07:23 -0400)
committerHong Guan <hg4105@att.com>
Sun, 22 Jul 2018 11:24:14 +0000 (07:24 -0400)
Change-Id: If35021a7161a2f0f0dba8f045356e52a3bce6ccf
Issue-ID: CCSDK-392
Signed-off-by: Hong Guan <hg4105@att.com>
helm/plugin/tests/blueprint/blueprint.yaml
helm/plugin/tests/blueprint/plugin/test_plugin.yaml
helm/plugin/tests/test_plugin.py

index 2588e8d..9636991 100644 (file)
@@ -16,27 +16,59 @@ inputs:
     test_input:
         description: an input for the test
         default: default_test_input
+    tiller-server-ip:
+        default: 1.1.1.1
+    tiller-server-port:
+        default: 8888
+    namespace:
+        default: onap
+    chart-repo-url:
+        default: local
+    chart-version :
+        default: 2.0.0
+    jsonConfig:
+        default: ''
+    config-url:
+        default: ''
+    config-format:
+        default: 'json'
+    tls-enable:
+        type: boolean
+        default: false
+    config-dir:
+        type: string
+        default: './'
+    stable-repo-url:
+        type: string
+        default: 'http://0.0.0.0/stable'
+
 
 node_templates:
     # defining a single node template that will serve as our test node
-    test_node_template:
+    test_node:
         # using base cloudify type
-        type: cloudify.nodes.Root
-        interfaces:
-            cloudify.interfaces.lifecycle:
-                start:
-                    # here we map the single plugin task to the start operation
-                    # of the cloudify.interfaces.lifecycle interface
-                    implementation: plugin_name.plugin.tasks.my_task
-                    inputs:
-                        # my_task accepts a single property named
-                        # some property. Here we inject this property
-                        # from the input provided by the test
-                        # (or 'default_test_input' if no input was provided)
-                        some_property: { get_input: test_input }
+        type: onap.nodes.component
+        properties:
+            tiller-server-ip: { get_input: tiller-server-ip }
+            tiller-server-port: { get_input: tiller-server-port }
+            component-name: test_node
+            chart-repo-url: { get_input: chart-repo-url }
+            chart-version: { get_input: chart-version }
+            namespace: { get_input: namespace }
+            config: { get_input: jsonConfig}
+            config-url: { get_input: config-url}
+            config-format: { get_input: config-format}
+            tls-enable: { get_input: tls-enable}
+            ca: { get_secret: ca_value}
+            cert: { get_secret: cert_value}
+            key: { get_secret: key_value}
+            config-dir: { get_input: config-dir}
+            stable-repo-url: { get_input: stable-repo-url}
 
 outputs:
     # example output the could be used to simplify assertions by test
     test_output:
         description: an output for the test
-        value: { get_attribute: [test_node_template, some_property] }
+        value: 
+            helm-value: { get_attribute: [test_node, current-helm-value] }
+            helm-history: { get_attribute: [test_node, helm-history] }
index 9701318..e3b257e 100644 (file)
+# ============LICENSE_START==========================================
+# ===================================================================
+# Copyright (c) 2017 AT&T
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#============LICENSE_END============================================
+
 plugins:
-    # Name could be anything, this name is what appears on the beginning of operation
-    # mappings.
-    plugin_name:
-        # Could be 'central_deployment_agent' or 'host_agent'.
-        # If 'central_deployment_agent', this plugin will be executed on the
-        # deployment dedicated agent, other wise it will be executed on the host agent.
-        # We set it the 'central_deployment_agent' here because 'host_agent' plugins should
-        # be contained in a host and this is not required for testing purposes
-        executor: central_deployment_agent
+  helm-plugin:
+    executor: central_deployment_agent
+    package_name: helm
+    install: false
+
+node_types:
+
+  onap.nodes.component:
+    derived_from: cloudify.nodes.Root
+    properties:
+      tiller-server-ip:
+        description: IP of tiller server
+        type: string
+      tiller-server-port:
+        default: local
+        description: Port of tiller server
+        type: string
+      chart-repo-url:
+        default: local
+        description: helm chart repo url
+        type: string
+      component-name:
+        description: onap component string
+        type: string
+      chart-version:
+        description: helm chart version
+        type: string
+      config-dir:
+        description: config file dir
+        default: '/opt/manager/resources/'
+        type: string
+      namespace:
+        description: k8s namespace
+        default: onap
+      config:
+        description: String format config file
+        type: string
+        default: ''
+      config-url:
+        description: String format config file url
+        type: string
+        default: ''
+      config-format:
+        description: String format config file format
+        type: string
+        default: 'json'
+      runtime-config:
+        default: ''
+        description: String format json object. To save the runtime config generate from other nodes.
+      tls-enable:
+        description: enable helm TSL
+        type: boolean
+        default: false
+      ca:
+        description: value of ca.pem
+        type: string
+        default: ''
+      cert:
+        description:  value of cert.pem
+        type: string
+        default: ''
+      key:
+        description:  value of key.pem
+        type: string
+        default: ''
+      stable-repo-url:
+        description:   URL for stable repository
+        type: string
+        default: 'https://kubernetes-charts.storage.googleapis.com'
+
+    interfaces:
+      cloudify.interfaces.lifecycle:
+        configure: helm-plugin.plugin.tasks.config
+        start: helm-plugin.plugin.tasks.start
+        stop: helm-plugin.plugin.tasks.stop
+        upgrade: helm-plugin.plugin.tasks.upgrade
+        rollback: helm-plugin.plugin.tasks.rollback
 
-        # Setting install to false in testing environment. In the non-test plugin definition
-        # this property could be omitted usually (its default is true), in which case
-        # the source property should be set
-        install: false
 
-        # source: URL to archive containing the plugin or name of directory containing
-        #         the plugin if it is included in the the blueprint directory under the
-        #         "plugins" directory. Not required in testing environments as the plugin
-        #         need not be installed on any agent
+workflows:
+  upgrade:
+    mapping: helm-plugin.plugin.workflows.upgrade
+    parameters:
+      node_instance_id:
+        description: The id of the node-instance that you want to modify.
+      config_json:
+        description: The changes to the new config json
+        default: ''
+      config_url:
+        description: The config input url
+        default: ''
+      config_format:
+        description: The config url input format
+        default: 'json'
+      chartVersion:
+        description: chart version
+      chartRepo:
+        description: chart repo url
+  rollback:
+    mapping: helm-plugin.plugin.workflows.rollback
+    parameters:
+      node_instance_id:
+        description: The id of the node-instance that you want to modify.
+        default: 'node_instance_id'
+      revision:
+        description: Check the node runtime property history, find the revision number you want to rollback to
+        default: 1
index be0882f..1b90aa0 100644 (file)
@@ -1,23 +1,31 @@
-########
-# Copyright (c) 2014 GigaSpaces Technologies Ltd. All rights reserved
+# ============LICENSE_START==========================================
+# ===================================================================
+# Copyright (c) 2018 AT&T
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
-#        http://www.apache.org/licenses/LICENSE-2.0
+#         http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
-#    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-#    * See the License for the specific language governing permissions and
-#    * limitations under the License.
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END============================================
 
 
 from os import path
 import unittest
+import mock
+import plugin.tasks
 
 from cloudify.test_utils import workflow_test
+from cloudify.mocks import MockNodeInstanceContext
+from cloudify.mocks import MockCloudifyContext
+from cloudify.state import current_ctx
+from cloudify import ctx
 
 
 class TestPlugin(unittest.TestCase):
@@ -25,23 +33,153 @@ class TestPlugin(unittest.TestCase):
     @workflow_test(path.join('blueprint', 'blueprint.yaml'),
                    resources_to_copy=[(path.join('blueprint', 'plugin',
                                                  'test_plugin.yaml'),
-                                       'plugin')],
-                   inputs={'test_input': 'new_test_input'})
-    def test_my_task(self, cfy_local):
+                                       'plugin')])
+    @mock.patch('plugin.tasks.execute_command')
+    def test_stop(self, cfy_local, mock_execute_command):
         # execute install workflow
         """
 
         :param cfy_local:
         """
-        cfy_local.execute('install', task_retries=0)
+        with mock.patch('plugin.tasks.shutil.rmtree'):
+            cfy_local.execute('uninstall', task_retries=0)
 
         # extract single node instance
         instance = cfy_local.storage.get_node_instances()[0]
 
-        # assert runtime properties is properly set in node instance
-        self.assertEqual(instance.runtime_properties['some_property'],
-                         'new_test_input')
+        mock_execute_command.assert_called_with('helm delete --purge onap-test_node --host 1.1.1.1:8888 ')
 
-        # assert deployment outputs are ok
-        self.assertDictEqual(cfy_local.outputs(),
-                             {'test_output': 'new_test_input'})
+    @workflow_test(path.join('blueprint', 'blueprint.yaml'),
+                   resources_to_copy=[(path.join('blueprint', 'plugin',
+                                                 'test_plugin.yaml'),
+                                       'plugin')])
+    @mock.patch('plugin.tasks.execute_command')
+    def test_start(self, cfy_local, mock_execute_command):
+        # execute install workflow
+        """
+
+        :param cfy_local:
+        """
+        with mock.patch('plugin.tasks.config'):
+            with mock.patch('plugin.tasks.get_current_helm_value'):
+                with mock.patch('plugin.tasks.get_helm_history'):
+                    cfy_local.execute('install', task_retries=0)
+
+        # extract single node instance
+        instance = cfy_local.storage.get_node_instances()[0]
+
+        mock_execute_command.assert_called_with('helm install local/test_node-2.0.0.tgz --name onap-test_node --namespace onap --host 1.1.1.1:8888 ')
+
+    @workflow_test(path.join('blueprint', 'blueprint.yaml'),
+                   resources_to_copy=[(path.join('blueprint', 'plugin',
+                                                 'test_plugin.yaml'),
+                                       'plugin')])
+    @mock.patch('plugin.tasks.execute_command')
+    def test_config(self, cfy_local, mock_execute_command):
+        # execute install workflow
+        """
+
+        :param cfy_local:
+        """
+        with mock.patch('plugin.tasks.start'):
+            cfy_local.execute('install', task_retries=0)
+
+        # extract single node instance
+        instance = cfy_local.storage.get_node_instances()[0]
+
+        mock_execute_command.assert_called_with('helm init --client-only --stable-repo-url http://0.0.0.0/stable')
+
+    @workflow_test(path.join('blueprint', 'blueprint.yaml'),
+                   resources_to_copy=[(path.join('blueprint', 'plugin',
+                                                 'test_plugin.yaml'),
+                                       'plugin')])
+    def test_rollback(self, cfy_local):
+        # execute install workflow
+        """
+
+        :param cfy_local:
+        """
+        node_instance_id = 'node_instance_id'
+        revision = 1
+        try:
+            cfy_local.execute('rollback', task_retries=0,
+                                     parameters={'node_instance_id': node_instance_id, 'revision': revision})
+            self.fail('Expected exception due to operation not exist')
+        except Exception as e:
+            self.assertTrue('operation not available')
+
+    @workflow_test(path.join('blueprint', 'blueprint.yaml'),
+                   resources_to_copy=[(path.join('blueprint', 'plugin',
+                                                 'test_plugin.yaml'),
+                                       'plugin')])
+    def test_upgrade(self, cfy_local):
+        # execute install workflow
+        """
+
+        :param cfy_local:
+        """
+        node_instance_id = 'node_instance_id'
+        config_json = ''
+        config_url = ''
+        config_format = 'json'
+        chartVersion = '2.0.0'
+        chartRepo = 'repo'
+        try:
+            cfy_local.execute('upgrade', task_retries=0,
+                                     parameters={'node_instance_id': node_instance_id, 'config_json': config_json,
+                                                        'config_url': config_url, 'config_format': config_format,
+                                                        'chartVersion': chartVersion, 'chartRepo': chartRepo})
+            self.fail('Expected exception due to operation not exist')
+        except Exception as e:
+            self.assertTrue('operation not available')
+
+    @mock.patch('plugin.tasks.execute_command')
+    def test_op_rollback(self, mock_execute_command):
+        # test operation rollback
+        """
+
+        :rollback operation test:
+        """
+        props = {
+            'component-name': 'test_node',
+            'namespace': 'onap',
+            'tiller-server-port': '8888',
+            'tiller-server-ip': '1.1.1.1',
+            'tls-enable': 'false'
+        }
+        args = {'revision': '1'}
+        mock_ctx = MockCloudifyContext(node_id='test_node_id', node_name='test_node_name',
+                                         properties=props)
+        try:
+            current_ctx.set(mock_ctx)
+            with mock.patch('plugin.tasks.get_current_helm_value'):
+                with mock.patch('plugin.tasks.get_helm_history'):
+                    plugin.tasks.rollback(**args)
+        finally:
+            current_ctx.clear()
+
+    @mock.patch('plugin.tasks.execute_command')
+    def test_op_upgrade(self, mock_execute_command):
+        # test operation upgrade
+        """
+
+        :upgrade operation test:
+        """
+        props = {
+            'component-name': 'test_node',
+            'namespace': 'onap',
+            'tiller-server-port': '8888',
+            'tiller-server-ip': '1.1.1.1',
+            'tls-enable': 'false',
+            'config-dir': '/tmp'
+        }
+        args = {'revision': '1', 'config': '', 'chart_repo': 'repo', 'chart_version': '2'}
+        mock_ctx = MockCloudifyContext(node_id='test_node_id', node_name='test_node_name',
+                                         properties=props)
+        try:
+            current_ctx.set(mock_ctx)
+            with mock.patch('plugin.tasks.get_current_helm_value'):
+                with mock.patch('plugin.tasks.get_helm_history'):
+                    plugin.tasks.upgrade(**args)
+        finally:
+            current_ctx.clear()