Adds use of CustomCollector in Prometheus exporter
[osm/MON.git] / osm_mon / test / plugins / OpenStack / unit / test_common.py
1 # Copyright 2017 Intel Research and Development Ireland Limited
2 # *************************************************************
3
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
6
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10
11 # http://www.apache.org/licenses/LICENSE-2.0
12
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: helena.mcgough@intel.com or adrian.hoban@intel.com
21 ##
22 """Tests for all common OpenStack methods."""
23
24 import json
25 import logging
26 import unittest
27
28 import mock
29 import requests
30 from keystoneclient.v3 import client
31
32 from osm_mon.core.auth import AuthManager
33 from osm_mon.core.database import VimCredentials
34 from osm_mon.plugins.OpenStack.common import Common
35
36 __author__ = "Helena McGough"
37
38 log = logging.getLogger(__name__)
39
40
41 class Message(object):
42 """Mock a message for an access credentials request."""
43
44 def __init__(self):
45 """Initialise the topic and value of access_cred message."""
46 self.topic = "access_credentials"
47 self.value = json.dumps({"mock_value": "mock_details",
48 "vim_type": "OPENSTACK",
49 "access_config":
50 {"openstack_site": "my_site",
51 "user": "my_user",
52 "password": "my_password",
53 "vim_tenant_name": "my_tenant"}})
54
55
56 class TestCommon(unittest.TestCase):
57 """Test the common class for OpenStack plugins."""
58
59 def setUp(self):
60 """Test Setup."""
61 super(TestCommon, self).setUp()
62 self.common = Common()
63 self.creds = VimCredentials()
64 self.creds.id = 'test_id'
65 self.creds.user = 'user'
66 self.creds.url = 'url'
67 self.creds.password = 'password'
68 self.creds.tenant_name = 'tenant_name'
69
70 @mock.patch.object(AuthManager, "get_credentials")
71 @mock.patch.object(client.Client, "get_raw_token_from_identity_service")
72 def test_get_auth_token(self, get_token, get_creds):
73 """Test generating a new authentication token."""
74 get_creds.return_value = self.creds
75 Common.get_auth_token('test_id')
76 get_creds.assert_called_with('test_id')
77 get_token.assert_called_with(auth_url='url', password='password', project_name='tenant_name', username='user',
78 project_domain_id='default', user_domain_id='default')
79
80 @mock.patch.object(requests, 'post')
81 def test_post_req(self, post):
82 """Testing a post request."""
83 Common.perform_request("url", "auth_token", req_type="post",
84 payload="payload")
85
86 post.assert_called_with("url", data="payload", headers=mock.ANY,
87 timeout=mock.ANY, verify=True)
88
89 @mock.patch.object(requests, 'get')
90 def test_get_req(self, get):
91 """Testing a get request."""
92 # Run the defualt get request without any parameters
93 Common.perform_request("url", "auth_token", req_type="get")
94
95 get.assert_called_with("url", params=None, headers=mock.ANY,
96 timeout=mock.ANY, verify=True)
97
98 # Test with some parameters specified
99 get.reset_mock()
100 Common.perform_request("url", "auth_token", req_type="get",
101 params="some parameters")
102
103 get.assert_called_with("url", params="some parameters",
104 headers=mock.ANY, timeout=mock.ANY, verify=True)
105
106 @mock.patch.object(requests, 'put')
107 def test_put_req(self, put):
108 """Testing a put request."""
109 Common.perform_request("url", "auth_token", req_type="put",
110 payload="payload")
111 put.assert_called_with("url", data="payload", headers=mock.ANY,
112 timeout=mock.ANY, verify=True)
113
114 @mock.patch.object(requests, 'delete')
115 def test_delete_req(self, delete):
116 """Testing a delete request."""
117 Common.perform_request("url", "auth_token", req_type="delete")
118
119 delete.assert_called_with("url", headers=mock.ANY, timeout=mock.ANY, verify=True)