1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 from unittest
import TestCase
, mock
25 from osm_mon
.collector
.utils
.openstack
import OpenstackUtils
26 from osm_mon
.core
.exceptions
import CertificateNotCreated
29 @mock.patch("osm_mon.collector.utils.openstack.session")
30 class OpenstackUtilsTest(TestCase
):
34 def test_session_without_insecure(self
, mock_session
):
39 "vim_password": "password",
40 "vim_tenant_name": "tenant_name",
42 OpenstackUtils
.get_session(creds
)
44 mock_session
.Session
.assert_called_once_with(
45 auth
=mock
.ANY
, verify
=True, timeout
=10
48 def test_session_with_insecure(self
, mock_session
):
50 "config": {"insecure": True},
53 "vim_password": "password",
54 "vim_tenant_name": "tenant_name",
56 OpenstackUtils
.get_session(creds
)
58 mock_session
.Session
.assert_called_once_with(
59 auth
=mock
.ANY
, verify
=False, timeout
=10
62 def test_session_with_insecure_false(self
, mock_session
):
64 "config": {"insecure": False},
67 "vim_password": "password",
68 "vim_tenant_name": "tenant_name",
70 OpenstackUtils
.get_session(creds
)
71 mock_session
.Session
.assert_called_once_with(
72 auth
=mock
.ANY
, verify
=True, timeout
=10
75 @mock.patch("osm_mon.collector.utils.openstack.OpenstackUtils._create_file_cert")
76 def test_session_with_ca_cert_content(self
, mock_create_file_cert
, mock_session
):
79 "config": {"ca_cert_content": "test"},
82 "vim_password": "password",
83 "vim_tenant_name": "tenant_name",
85 mock_create_file_cert
.return_value
= {"ca_cert": "testfile"}
86 OpenstackUtils
.get_session(creds
)
87 mock_session
.Session
.assert_called_once_with(
88 auth
=mock
.ANY
, verify
="testfile", timeout
=10
91 @mock.patch("osm_mon.collector.utils.openstack.makedirs", return_value
="")
92 @mock.patch("osm_mon.collector.utils.openstack.path")
93 def test_create_file_cert(self
, mock_path
, mock_makedirs
, mock_session
):
94 vim_config
= {"ca_cert_content": "test"}
96 mock_path
.isdir
.return_value
= False
98 with mock
.patch("builtins.open", mock
.mock_open()) as mocked_file
:
99 OpenstackUtils
._create
_file
_cert
(vim_config
, target_id
)
100 mock_makedirs
.assert_called_once_with("/app/osm_mon/certs/1234")
101 mocked_file
.assert_called_once_with(
102 f
"/app/osm_mon/certs/{target_id}/ca_cert", "w"
104 assert vim_config
["ca_cert"] == f
"/app/osm_mon/certs/{target_id}/ca_cert"
106 @mock.patch("osm_mon.collector.utils.openstack.makedirs")
107 @mock.patch("osm_mon.collector.utils.openstack.path")
108 def test_create_file_cert_exists(self
, mock_path
, mock_makedirs
, mock_session
):
109 vim_config
= {"ca_cert_content": "test"}
111 mock_path
.isdir
.return_value
= True
113 with mock
.patch("builtins.open", mock
.mock_open()) as mocked_file
:
114 OpenstackUtils
._create
_file
_cert
(vim_config
, target_id
)
115 mock_makedirs
.assert_not_called()
116 mocked_file
.assert_called_once_with(
117 f
"/app/osm_mon/certs/{target_id}/ca_cert", "w"
119 assert vim_config
["ca_cert"] == f
"/app/osm_mon/certs/{target_id}/ca_cert"
121 @mock.patch("osm_mon.collector.utils.openstack.makedirs", side_effect
=Exception)
122 @mock.patch("osm_mon.collector.utils.openstack.path")
123 def test_create_file_cert_makedirs_except(
124 self
, mock_path
, mock_makedirs
, mock_session
126 vim_config
= {"ca_cert_content": "test"}
128 mock_path
.isdir
.return_value
= False
130 with mock
.patch("builtins.open", mock
.mock_open()) as mocked_file
:
131 with self
.assertRaises(CertificateNotCreated
):
132 OpenstackUtils
._create
_file
_cert
(vim_config
, target_id
)
133 mock_makedirs
.assert_called_once_with("/app/osm_mon/certs/1234")
134 mocked_file
.assert_not_called()
135 assert vim_config
["ca_cert_content"] == "test"
137 @mock.patch("osm_mon.collector.utils.openstack.makedirs", return_value
="")
138 @mock.patch("osm_mon.collector.utils.openstack.path")
139 def test_create_file_cert_open_excepts(
140 self
, mock_path
, mock_makedirs
, mock_session
142 vim_config
= {"ca_cert_content": "test"}
144 mock_path
.isdir
.return_value
= False
146 with mock
.patch("builtins.open", mock
.mock_open()) as mocked_file
:
147 mocked_file
.side_effect
= Exception
148 with self
.assertRaises(CertificateNotCreated
):
149 OpenstackUtils
._create
_file
_cert
(vim_config
, target_id
)
150 mock_makedirs
.assert_called_once_with("/app/osm_mon/certs/1234")
151 mocked_file
.assert_called_once_with(
152 f
"/app/osm_mon/certs/{target_id}/ca_cert", "w"
154 assert vim_config
["ca_cert_content"] == "test"