Get tox working
[dcaegen2/platform/cli.git] / dcae-cli / dcae_cli / util / tests / test_config.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 the config functionality
24 """
25 import os, json
26 from functools import partial
27 from mock import patch
28
29 import pytest
30 import click
31
32 import dcae_cli
33 from dcae_cli.util import config, write_pref
34 from dcae_cli.util.config import get_app_dir, get_config, get_config_path
35
36
37 def test_no_config(monkeypatch, tmpdir):
38     '''Tests the creation and initialization of a config on a clean install'''
39     monkeypatch.setattr(click, "get_app_dir", lambda app: str(tmpdir.realpath()))
40
41     mock_config = {'user': 'mock-user'}
42
43     config_file = tmpdir.join("config.json")
44     config_file.write(json.dumps(mock_config))
45
46     assert get_config() == mock_config
47
48
49 def test_init_config_user(monkeypatch):
50     good_case = "abc123"
51     values = [ good_case, "d-e-f", "g*h*i", "j k l" ]
52
53     def fake_input(values, message, type="red"):
54         return values.pop()
55
56     monkeypatch.setattr(click, 'prompt', partial(fake_input, values))
57     assert config._init_config_user() == good_case
58
59
60 def test_init_config(monkeypatch):
61     monkeypatch.setattr(config, '_init_config_user', lambda: "bigmama")
62     monkeypatch.setattr(config, '_init_config_server_url',
63             lambda: "http://some-nexus-in-the-sky.com")
64     monkeypatch.setattr(dcae_cli.util, 'fetch_file_from_web',
65             lambda server_url, path: { "db_url": "conn" })
66     monkeypatch.setattr("dcae_cli._version.__version__", "2.X.X")
67
68     expected = {'cli_version': '2.X.X', 'user': 'bigmama', 'db_url': 'conn',
69             'server_url': 'http://some-nexus-in-the-sky.com',
70             'active_profile': 'default' }
71     assert expected == config._init_config()
72
73     # Test using of db fallback
74
75     monkeypatch.setattr(dcae_cli.util, 'fetch_file_from_web',
76             lambda server_url, path: { "db_url": "" })
77
78     db_url = "postgresql://king:of@mountain:5432/dcae_onboarding_db"
79
80     def fake_init_config_db_url():
81         return db_url
82
83     monkeypatch.setattr(config, "_init_config_db_url",
84             fake_init_config_db_url)
85
86     assert db_url == config._init_config()["db_url"]
87
88     monkeypatch.setattr(dcae_cli.util, 'fetch_file_from_web',
89             lambda server_url, path: {})
90
91     assert db_url == config._init_config()["db_url"]
92
93     # Simulate error trying to fetch
94
95     def fetch_simulate_error(server_url, path):
96         raise RuntimeError("Simulated error")
97
98     monkeypatch.setattr(dcae_cli.util, 'fetch_file_from_web',
99             fetch_simulate_error)
100     # Case when user opts out of manually setting up
101     monkeypatch.setattr(click, "confirm", lambda msg: False)
102
103     with pytest.raises(config.ConfigurationInitError):
104         config._init_config()
105
106
107 def test_should_force_reinit():
108     bad_config = {}
109     assert config.should_force_reinit(bad_config) == True
110
111     old_config = { "cli_version": "1.0.0" }
112     assert config.should_force_reinit(old_config) == True
113
114     uptodate_config = { "cli_version": "2.0.0" }
115     assert config.should_force_reinit(uptodate_config) == False
116
117
118 def test_reinit_config(monkeypatch, tmpdir):
119     monkeypatch.setattr(click, "get_app_dir", lambda app: str(tmpdir.realpath()))
120
121     new_config = { "user": "ninny", "db_url": "some-db" }
122
123     def init():
124         return new_config
125
126     assert config._reinit_config(init) == new_config
127
128     old_config = { "user": "super", "db_url": "other-db", "hidden": "yo" }
129     write_pref(old_config, get_config_path())
130
131     new_config["hidden"] = "yo"
132     assert config._reinit_config(init) == new_config
133
134
135 if __name__ == '__main__':
136     '''Test area'''
137     pytest.main([__file__, ])