Add Optimization policy for hpa_score
[optf/osdf.git] / test / adapters / test_sms.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (c) 2018 Intel Corporation Intellectual Property
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 # -------------------------------------------------------------------------
18 #
19 import unittest
20 from uuid import uuid4
21
22 import requests_mock
23
24 import osdf.adapters.aaf.sms as SMS
25 from osdf.config.base import osdf_config
26
27
28 class TestSMS(unittest.TestCase):
29
30     def setUp(self):
31         self.config = osdf_config.deployment
32         self.base_domain_url = '{}/v1/sms/domain'
33         self.domain_url = '{}/v1/sms/domain/{}'
34         self.secret_url = self.domain_url + '/secret'
35
36     @requests_mock.mock()
37     def test_sms(self, mock_sms):
38         ''' NOTE: preload_secret generate the uuid for the domain
39                   Create Domain API is called during the deployment using a
40                   preload script. So the application oly knows the domain_uuid.
41                   All sub-sequent SMS API calls needs the uuid.
42                   For test purposes we need to do preload ourselves'''
43         sms_url = self.config["aaf_sms_url"]
44
45         # JSON Data responses
46         secretnames = {'secretnames': ['s1', 's2', 's3', 's4']}
47         secretvalues = {'values': {'Password': '', 'UserName': ''}}
48         expecect_secret_dict = dict()
49         for secret in secretnames['secretnames']:
50             expecect_secret_dict[secret] = secretvalues['values']
51
52         # Part 1 : Preload Secrets ONLY FOR TEST
53         # Mock requests for preload_secret
54         cd_url = self.base_domain_url.format(sms_url)
55         domain_uuid1 = str(uuid4())
56         s_url = self.secret_url.format(sms_url, domain_uuid1)
57         mock_sms.post(cd_url, status_code=200, json={'uuid': domain_uuid1})
58         mock_sms.post(s_url, status_code=200)
59         # Initialize Secrets from SMS
60         SMS.preload_secrets()
61
62         # Part 2: Retrieve Secret Test
63         # Mock requests for retrieve_secrets
64         # IMPORTANT: Read the config again as the preload_secrets has
65         # updated the config with uuid
66         domain_uuid2 = self.config["secret_domain"]
67         self.assertEqual(domain_uuid1, domain_uuid2)
68
69         d_url = self.domain_url.format(sms_url, domain_uuid2)
70         s_url = self.secret_url.format(sms_url, domain_uuid2)
71
72         # Retrieve Secrets from SMS and load to secret cache
73         # Use the secret_cache instead of config files
74         mock_sms.get(s_url, status_code=200, json=secretnames)
75         for secret in secretnames['secretnames']:
76             mock_sms.get('{}/{}'.format(s_url, secret),
77                          status_code=200, json=secretvalues)
78         secret_cache = SMS.retrieve_secrets()
79         self.assertDictEqual(expecect_secret_dict, secret_cache,
80                              'Failed to retrieve secrets')
81
82         # Part 3: Clean up Delete secrets and domain
83         # Mock requests for delete_secrets
84         mock_sms.delete(d_url, status_code=200)
85         self.assertTrue(SMS.delete_secrets())
86
87
88 if __name__ == "__main__":
89     unittest.main()