Fix tox issue
[multicloud/azure.git] / azure / multicloud_azure / tests / test_pub_utils.py
1 # Copyright (c) 2018 Amdocs
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
13 import unittest
14 import os
15 import mock
16 import json
17
18 from multicloud_azure.pub.utils.restcall import AAIClient
19 from multicloud_azure.pub.msapi import extsys
20 from multicloud_azure.pub.utils.timeutil import now_time
21 from multicloud_azure.pub.utils.fileutil import make_dirs, delete_dirs
22 from multicloud_azure.pub.utils.fileutil import download_file_from_http
23 from multicloud_azure.pub.vim.vimsdk.azure_credentials import ClientObj
24 from multicloud_azure.pub.exceptions import VimDriverAzureException
25
26 TENANT_ID = '123'
27 CLIENT = '456'
28 KEY = '789'
29
30 params = {
31             'username': TENANT_ID,
32             'password': KEY,
33             'tenant_id': CLIENT
34         }
35
36
37 class TestPub(unittest.TestCase):
38
39     def test_client_obj(self):
40         self.assertRaises(VimDriverAzureException,
41                           ClientObj().get_client_obj, params)
42
43     def test_time(self):
44         fmt = "%Y-%m-%d %H:%M:%S"
45         self.assertIsNotNone(now_time(fmt))
46
47     def test_make_dirs(self):
48         path = "/tmp/azure/azure/bin"
49         self.assertEqual(os.makedirs(path, 0o777), make_dirs(path))
50
51     def test_delete_dirs(self):
52         path = "/tmp/azure/azure/bin"
53         self.assertIsNone(delete_dirs(path))
54
55     def test_download_file(self):
56         url = "https://raw.githubusercontent.com/onapdemo/" \
57               "onap-scripts/master/entrypoint/azure-rancher-server.sh"
58         local_dir = "usr/local/bin"
59         file_name = "azure"
60         self.assertNotEquals(False, "usr/local/bin/azure",
61                              download_file_from_http(url, local_dir,
62                                                      file_name))
63
64     def test_split_vim_to_owner_region(self):
65         vim_id = 'ATT_eastus2'
66         self.assertEquals(('ATT', 'eastus2'),
67                           extsys.split_vim_to_owner_region(vim_id))
68
69     @mock.patch.object(AAIClient, 'get_vim')
70     def test_get_vim_id(self, mock_vim_info):
71         vim_id = 'ATT_eastus2'
72         json_file = os.path.join(os.path.dirname(
73             __file__), 'aai_response.json')
74         f = open(json_file).read()
75         ret = json.loads(f)
76         mock_vim_info.return_value = ret
77         self.assertEqual(ret, extsys.get_vim_by_id(vim_id))