1 # ============LICENSE_START=======================================================
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 # -*- coding: utf-8 -*-
23 Tests the profiles module
26 from functools import partial
31 from dcae_cli import util
32 from dcae_cli.util.exc import DcaeException
33 from dcae_cli.util import profiles
34 from dcae_cli.util.profiles import (get_active_name, get_profile, get_profiles, get_profiles_path,
35 create_profile, delete_profile, update_profile, ACTIVE,
36 activate_profile, CONSUL_HOST)
37 from dcae_cli.util import config
40 def test_profiles(monkeypatch, tmpdir):
41 '''Tests the creation and initialization of profiles on a clean install'''
43 config_dict = { "active_profile": "fake-solutioning", "db_url": "some-db" }
44 config_file = tmpdir.join("config.json")
45 config_file.write(json.dumps(config_dict))
48 profile_dict = { "fake-solutioning": { "cdap_broker": "cdap_broker",
49 "config_binding_service": "config_binding_service",
50 "consul_host": "realsolcnsl00.dcae.solutioning.com",
51 "docker_host": "realsoldokr00.dcae.solutioning.com:2376" }}
52 profile_file = tmpdir.join("profiles.json")
53 profile_file.write(json.dumps(profile_dict))
55 monkeypatch.setattr(click, "get_app_dir", lambda app: str(tmpdir.realpath()))
57 assert get_active_name() == config_dict["active_profile"]
58 assert get_profile() == profiles.Profile(**profile_dict["fake-solutioning"])
60 # Failures looking for unknown profile
62 with pytest.raises(DcaeException):
65 with pytest.raises(DcaeException):
68 with pytest.raises(DcaeException):
69 update_profile('foo', **{}) # doesn't exist
71 # Cannot delete active profile
73 assert delete_profile(get_active_name()) == False
75 # Do different get_profiles queries
77 assert get_profiles(user_only=True) == profile_dict
78 all_profiles = copy.deepcopy(profile_dict)
79 all_profiles[ACTIVE] = profile_dict["fake-solutioning"]
80 assert get_profiles(user_only=False) == all_profiles
82 # Create and activate new profile
85 activate_profile('foo')
86 assert get_active_name() == 'foo'
90 update_profile('foo', **{CONSUL_HOST:'bar'})
91 assert get_profiles()['foo'][CONSUL_HOST] == 'bar'
92 assert get_profile()._asdict()[CONSUL_HOST] == 'bar'
94 activate_profile("fake-solutioning")
95 assert delete_profile('foo') == True
98 def test_reinit_via_get_profiles(monkeypatch, tmpdir):
99 monkeypatch.setattr(click, "get_app_dir", lambda app: str(tmpdir.realpath()))
101 def fake_reinit_failure():
102 raise profiles.ProfilesInitError("Faked failure")
104 monkeypatch.setattr(profiles, "reinit_profiles", fake_reinit_failure)
106 with pytest.raises(DcaeException):
110 def test_reinit_profiles(monkeypatch, tmpdir):
111 monkeypatch.setattr(click, "get_app_dir", lambda app: str(tmpdir.realpath()))
113 # Setup config (need this because the "active_profile" is needed)
114 config_dict = { "active_profile": "fake-solutioning", "db_url": "some-db" }
115 config_file = tmpdir.join("config.json")
116 config_file.write(json.dumps(config_dict))
118 # Start with empty profiles
120 profile_dict = { "fake-solutioning": { "cdap_broker": "cdap_broker",
121 "config_binding_service": "config_binding_service",
122 "consul_host": "realsolcnsl00.dcae.solutioning.com",
123 "docker_host": "realsoldokr00.dcae.solutioning.com:2376" }}
125 def fetch_profile(target_profile, path):
126 return target_profile
128 monkeypatch.setattr(util, "fetch_file_from_nexus", partial(fetch_profile,
130 profiles.reinit_profiles()
131 assert profiles.get_profiles(include_active=False) == profile_dict
135 profile_dict = { "fake-5g": { "cdap_broker": "cdap_broker",
136 "config_binding_service": "config_binding_service",
137 "consul_host": "realsolcnsl00.dcae.solutioning.com",
138 "docker_host": "realsoldokr00.dcae.solutioning.com:2376" }}
140 monkeypatch.setattr(util, "fetch_file_from_nexus", partial(fetch_profile,
142 profiles.reinit_profiles()
143 all_profiles = profiles.get_profiles(include_active=False)
144 assert "fake-5g" in all_profiles
145 assert "fake-solutioning" in all_profiles
149 def fetch_failure(path):
150 raise RuntimeError("Mysterious error")
152 monkeypatch.setattr(util, "fetch_file_from_nexus", fetch_failure)
154 with pytest.raises(profiles.ProfilesInitError):
155 profiles.reinit_profiles()
158 if __name__ == '__main__':
160 pytest.main([__file__, ])