a1e18d10086bdee5c807c1cacc2f31ca8b6bc7db
[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 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
30 from dcae_cli.cli import cli
31 from dcae_cli.catalog import MockCatalog
32
33 TEST_DIR = os.path.dirname(__file__)
34
35
36 def _get_spec(path):
37     with open(path) as file:
38         return json.load(file)
39
40
41 def test_comp_docker(obj=None):
42
43     obj = {'catalog': MockCatalog(purge_existing=True, db_name='dcae_cli.test.db', enforce_image=False),
44            'config': {'user': 'test-user'}}
45
46     df_kpi = os.path.join(TEST_DIR, 'mocked_components', 'collector', 'vnf-kpi.format.json')
47     comp_coll = os.path.join(TEST_DIR, 'mocked_components', 'collector', 'kpi-collector.comp.json')
48
49     df_cls = os.path.join(TEST_DIR, 'mocked_components', 'model', 'int-class.format.json')
50     comp_model = os.path.join(TEST_DIR, 'mocked_components', 'model', 'anomaly-model.comp.json')
51
52     df_empty = os.path.join(TEST_DIR, 'mocked_components', 'viz', 'empty.format.json')
53     df_url = os.path.join(TEST_DIR, 'mocked_components', 'viz', 'web-url.format.json')
54     comp_viz = os.path.join(TEST_DIR, 'mocked_components', 'viz', 'line-viz.comp.json')
55
56     runner = CliRunner()
57
58
59     # add the collector
60     cmd = "data_format add {:}".format(df_kpi).split()
61     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
62
63     cmd = "component add {:}".format(comp_coll).split()
64     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
65
66
67     # add the model
68     cmd = "data_format add {:}".format(df_cls).split()
69     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
70
71     cmd = "component add {:}".format(comp_model).split()
72     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
73
74
75     # add the viz
76     cmd = "data_format add {:}".format(df_empty).split()
77     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
78
79     cmd = "data_format add {:}".format(df_url).split()
80     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
81
82     cmd = "component add {:}".format(comp_viz).split()
83     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
84
85
86     # light test of component list
87     df_cls_spec = _get_spec(df_cls)
88     df_cls_name, df_cls_ver = df_cls_spec['self']['name'], df_cls_spec['self']['version']
89     comp_model_spec = _get_spec(comp_model)
90     comp_model_name = comp_model_spec['self']['name']
91
92     cmd = "component list -pub {:}".format(df_cls_name).split()
93     assert comp_model_name in runner.invoke(cli, cmd, obj=obj).output
94
95     cmd = "component list -pub {:}:{:}".format(df_cls_name, df_cls_ver).split()
96     assert comp_model_name in runner.invoke(cli, cmd, obj=obj).output
97
98
99     # light test of component info
100     cmd = "component show {:}".format(comp_model_name).split()
101     spec_str = runner.invoke(cli, cmd, obj=obj).output
102     assert comp_model_spec == json.loads(spec_str)
103
104
105 def test_comp_cdap(obj=None):
106     """
107     This is not a unit test. It is bigger than that. It Does a full "workflow" test:
108     1) adds a data format
109     2) adds a cdap component
110     3) runs a cdap component using our "Rework" broker
111     4) undeploys the cdap component using our "Rework" broker
112
113     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. 
114     """
115
116     obj = {'catalog': MockCatalog(purge_existing=True, db_name='dcae_cli.test.db'),
117            'config': {'user': 'test-user'}}
118     runner = CliRunner()
119     
120     #add the data format
121     df = os.path.join(TEST_DIR, 'mocked_components', 'cdap', 'format.json')
122     cmd = "data_format add {:}".format(df).split()
123     assert runner.invoke(cli, cmd, obj=obj).exit_code == 0
124     
125     #add the CDAP components
126     # TODO: Need to update the host
127     jar = 'http://make-me-valid/HelloWorld-3.4.3.jar'
128     
129     comp_cdap_start = os.path.join(TEST_DIR, 'mocked_components', 'cdap', 'spec_start.json')
130     cmd = "component add {0}".format(comp_cdap_start).split()
131     print(cmd)
132     result = runner.invoke(cli, cmd, obj=obj)
133     print(result.output)
134     assert result.exit_code == 0
135     
136     comp_cdap_end = os.path.join(TEST_DIR, 'mocked_components', 'cdap', 'spec_end.json')
137     cmd = "component add {0}".format(comp_cdap_end).split()
138     print(cmd)
139     result = runner.invoke(cli, cmd, obj=obj)
140     print(result.output)
141     assert result.exit_code == 0
142     
143     #run the terminating component first
144     cmd = "component run --force cdap.helloworld.mock.catalog.testing.endnode".split()
145     print(cmd)
146     result = runner.invoke(cli, cmd, obj=obj)
147     print(result.output)
148     assert result.exit_code == 0
149
150     #run the component again: this time the second component finds the first
151     cmd = "component run --force cdap.helloworld.mock.catalog.testing.startnode".split()
152     print(cmd)
153     result = runner.invoke(cli, cmd, obj=obj)
154     assert "config_key 'service_call_example' has no compatible downstream components." not in result.output #touchdown baby
155     assert result.exit_code == 0
156
157     #sleep
158     time.sleep(5)
159
160     #delete the components
161     cmd = "component undeploy cdap.helloworld.mock.catalog.testing.startnode".split()
162     print(cmd)
163     result = runner.invoke(cli, cmd, obj=obj)
164     assert result.exit_code == 0
165
166     cmd = "component undeploy cdap.helloworld.mock.catalog.testing.endnode".split()
167     print(cmd)
168     result = runner.invoke(cli, cmd, obj=obj)
169     assert result.exit_code == 0
170
171 if __name__ == '__main__':
172     '''Test area'''
173     #pytest.main([__file__, ])
174     test_comp_cdap()