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