67769eb1b40171423409481b85cea755b0244789
[dcaegen2/platform/cli.git] / dcae-cli / dcae_cli / commands / tests / test_component_cmd.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 # -*- coding: utf-8 -*-
22 '''
23 Tests component CLI commands
24 '''
25 import os
26 import json
27 from click.testing import CliRunner
28 import time
29 import pytest
30
31 from dcae_cli.cli import cli
32 from dcae_cli.catalog import MockCatalog
33
34 TEST_DIR = os.path.dirname(__file__)
35
36
37 def _get_spec(path):
38     with open(path) as file:
39         return json.load(file)
40
41
42 def test_comp_docker(mock_cli_config, mock_db_url, obj=None):
43
44     obj = {'catalog': MockCatalog(purge_existing=True, db_name='dcae_cli.test.db', 
45         enforce_image=False, db_url=mock_db_url),
46            'config': {'user': 'test-user'}}
47
48     df_kpi = os.path.join(TEST_DIR, 'mocked_components', 'collector', 'vnf-kpi.format.json')
49     comp_coll = os.path.join(TEST_DIR, 'mocked_components', 'collector', 'kpi-collector.comp.json')
50
51     df_cls = os.path.join(TEST_DIR, 'mocked_components', 'model', 'int-class.format.json')
52     comp_model = os.path.join(TEST_DIR, 'mocked_components', 'model', 'anomaly-model.comp.json')
53
54     df_empty = os.path.join(TEST_DIR, 'mocked_components', 'viz', 'empty.format.json')
55     df_url = os.path.join(TEST_DIR, 'mocked_components', 'viz', 'web-url.format.json')
56     comp_viz = os.path.join(TEST_DIR, 'mocked_components', 'viz', 'line-viz.comp.json')
57
58     runner = CliRunner()
59
60
61     # add the collector
62     cmd = "data_format add {:}".format(df_kpi).split()
63     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
64
65     cmd = "component add {:}".format(comp_coll).split()
66     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
67
68
69     # add the model
70     cmd = "data_format add {:}".format(df_cls).split()
71     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
72
73     cmd = "component add {:}".format(comp_model).split()
74     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
75
76
77     # add the viz
78     cmd = "data_format add {:}".format(df_empty).split()
79     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
80
81     cmd = "data_format add {:}".format(df_url).split()
82     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
83
84     cmd = "component add {:}".format(comp_viz).split()
85     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
86
87
88     # light test of component list
89     df_cls_spec = _get_spec(df_cls)
90     df_cls_name, df_cls_ver = df_cls_spec['self']['name'], df_cls_spec['self']['version']
91     comp_model_spec = _get_spec(comp_model)
92     comp_model_name = comp_model_spec['self']['name']
93
94     cmd = "component list -pub {:}".format(df_cls_name).split()
95     #assert comp_model_name in runner.invoke(cli, cmd, obj=obj).output
96
97     cmd = "component list -pub {:}:{:}".format(df_cls_name, df_cls_ver).split()
98     #assert comp_model_name in runner.invoke(cli, cmd, obj=obj).output
99
100
101     # light test of component info
102     cmd = "component show {:}".format(comp_model_name).split()
103     spec_str = runner.invoke(cli, cmd, obj=obj).output
104     assert comp_model_spec == json.loads(spec_str)
105
106
107 @pytest.mark.skip(reason="This is not a pure unit test. Need a way to setup dependencies and trigger in the appropriate stages of testing.")
108 def test_comp_cdap(obj=None):
109     """
110     This is not a unit test. It is bigger than that. It Does a full "workflow" test:
111     1) adds a data format
112     2) adds a cdap component
113     3) runs a cdap component using our "Rework" broker
114     4) undeploys the cdap component using our "Rework" broker
115
116     NOTE: TODO: Mocking out the broker would be an improvement over this, probably. This is impure. Mocking the broker owuld be a huge undertaking, though. 
117     """
118
119     obj = {'catalog': MockCatalog(purge_existing=True, db_name='dcae_cli.test.db'),
120            'config': {'user': 'test-user'}}
121     runner = CliRunner()
122     
123     #add the data format
124     df = os.path.join(TEST_DIR, 'mocked_components', 'cdap', 'format.json')
125     cmd = "data_format add {:}".format(df).split()
126     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
127     
128     #add the CDAP components
129     # TODO: Need to update the host
130     jar = 'http://make-me-valid/HelloWorld-3.4.3.jar'
131     
132     comp_cdap_start = os.path.join(TEST_DIR, 'mocked_components', 'cdap', 'spec_start.json')
133     cmd = "component add {0}".format(comp_cdap_start).split()
134     print(cmd)
135     result = runner.invoke(cli, cmd, obj=obj)
136     print(result.output)
137     assert result.exit_code == 0
138     
139     comp_cdap_end = os.path.join(TEST_DIR, 'mocked_components', 'cdap', 'spec_end.json')
140     cmd = "component add {0}".format(comp_cdap_end).split()
141     print(cmd)
142     result = runner.invoke(cli, cmd, obj=obj)
143     print(result.output)
144     assert result.exit_code == 0
145     
146     #run the terminating component first
147     cmd = "component run --force cdap.helloworld.mock.catalog.testing.endnode".split()
148     print(cmd)
149     result = runner.invoke(cli, cmd, obj=obj)
150     print(result.output)
151     assert result.exit_code == 0
152
153     #run the component again: this time the second component finds the first
154     cmd = "component run --force cdap.helloworld.mock.catalog.testing.startnode".split()
155     print(cmd)
156     result = runner.invoke(cli, cmd, obj=obj)
157     assert "config_key 'service_call_example' has no compatible downstream components." not in result.output #touchdown baby
158     assert result.exit_code == 0
159
160     #sleep
161     time.sleep(5)
162
163     #delete the components
164     cmd = "component undeploy cdap.helloworld.mock.catalog.testing.startnode".split()
165     print(cmd)
166     result = runner.invoke(cli, cmd, obj=obj)
167     assert result.exit_code == 0
168
169     cmd = "component undeploy cdap.helloworld.mock.catalog.testing.endnode".split()
170     print(cmd)
171     result = runner.invoke(cli, cmd, obj=obj)
172     assert result.exit_code == 0
173
174 if __name__ == '__main__':
175     '''Test area'''
176     #pytest.main([__file__, ])
177     test_comp_cdap()