blob: 9853d158bd39b453c9125faa037c80399b250d13 [file] [log] [blame]
Helena McGough95b92b32017-10-01 12:03:53 +01001# 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
Helena McGoughfe92f842017-11-17 14:57:08 +000024import json
25
26import logging
27
Helena McGough95b92b32017-10-01 12:03:53 +010028import unittest
29
Helena McGoughfe92f842017-11-17 14:57:08 +000030from keystoneclient.v3 import client
31
Helena McGough95b92b32017-10-01 12:03:53 +010032import mock
33
Benjamin Diaz181cce82018-03-28 21:12:11 -030034from osm_mon.core.auth import AuthManager
35from osm_mon.core.database import VimCredentials
Helena McGougheffeb7c2017-11-23 15:54:08 +000036from osm_mon.plugins.OpenStack.common import Common
37from osm_mon.plugins.OpenStack.settings import Config
Helena McGough95b92b32017-10-01 12:03:53 +010038
39import requests
40
Helena McGoughfe92f842017-11-17 14:57:08 +000041__author__ = "Helena McGough"
42
43log = logging.getLogger(__name__)
44
45
46class Message(object):
47 """Mock a message for an access credentials request."""
48
49 def __init__(self):
50 """Initialise the topic and value of access_cred message."""
51 self.topic = "access_credentials"
52 self.value = json.dumps({"mock_value": "mock_details",
53 "vim_type": "OPENSTACK",
54 "access_config":
Benjamin Diaz181cce82018-03-28 21:12:11 -030055 {"openstack_site": "my_site",
56 "user": "my_user",
57 "password": "my_password",
58 "vim_tenant_name": "my_tenant"}})
Helena McGoughfe92f842017-11-17 14:57:08 +000059
Helena McGough95b92b32017-10-01 12:03:53 +010060
61class TestCommon(unittest.TestCase):
62 """Test the common class for OpenStack plugins."""
63
64 def setUp(self):
65 """Test Setup."""
66 super(TestCommon, self).setUp()
67 self.common = Common()
Benjamin Diaz181cce82018-03-28 21:12:11 -030068 self.creds = VimCredentials()
69 self.creds.id = 'test_id'
70 self.creds.user = 'user'
71 self.creds.url = 'url'
72 self.creds.password = 'password'
73 self.creds.tenant_name = 'tenant_name'
Helena McGough95b92b32017-10-01 12:03:53 +010074
Benjamin Diaz181cce82018-03-28 21:12:11 -030075 @mock.patch.object(AuthManager, "get_credentials")
Helena McGoughfe92f842017-11-17 14:57:08 +000076 @mock.patch.object(Config, "instance")
77 @mock.patch.object(client, "Client")
Benjamin Diaz181cce82018-03-28 21:12:11 -030078 def test_get_auth_token(self, key_client, cfg, get_creds):
Helena McGoughfe92f842017-11-17 14:57:08 +000079 """Test generating a new authentication token."""
Benjamin Diaz181cce82018-03-28 21:12:11 -030080 get_creds.return_value = self.creds
81 Common.get_auth_token('test_id')
82 get_creds.assert_called_with('test_id')
83 key_client.assert_called_with(auth_url='url', password='password', tenant_name='tenant_name', username='user')
Helena McGoughfe92f842017-11-17 14:57:08 +000084
Helena McGough95b92b32017-10-01 12:03:53 +010085 @mock.patch.object(requests, 'post')
86 def test_post_req(self, post):
87 """Testing a post request."""
Benjamin Diaz181cce82018-03-28 21:12:11 -030088 Common.perform_request("url", "auth_token", req_type="post",
89 payload="payload")
Helena McGough95b92b32017-10-01 12:03:53 +010090
91 post.assert_called_with("url", data="payload", headers=mock.ANY,
92 timeout=mock.ANY)
93
94 @mock.patch.object(requests, 'get')
95 def test_get_req(self, get):
96 """Testing a get request."""
97 # Run the defualt get request without any parameters
Benjamin Diaz181cce82018-03-28 21:12:11 -030098 Common.perform_request("url", "auth_token", req_type="get")
Helena McGough95b92b32017-10-01 12:03:53 +010099
100 get.assert_called_with("url", params=None, headers=mock.ANY,
101 timeout=mock.ANY)
102
103 # Test with some parameters specified
104 get.reset_mock()
Benjamin Diaz181cce82018-03-28 21:12:11 -0300105 Common.perform_request("url", "auth_token", req_type="get",
106 params="some parameters")
Helena McGough95b92b32017-10-01 12:03:53 +0100107
108 get.assert_called_with("url", params="some parameters",
109 headers=mock.ANY, timeout=mock.ANY)
110
111 @mock.patch.object(requests, 'put')
112 def test_put_req(self, put):
113 """Testing a put request."""
Benjamin Diaz181cce82018-03-28 21:12:11 -0300114 Common.perform_request("url", "auth_token", req_type="put",
115 payload="payload")
Helena McGough95b92b32017-10-01 12:03:53 +0100116 put.assert_called_with("url", data="payload", headers=mock.ANY,
117 timeout=mock.ANY)
118
119 @mock.patch.object(requests, 'delete')
120 def test_delete_req(self, delete):
121 """Testing a delete request."""
Benjamin Diaz181cce82018-03-28 21:12:11 -0300122 Common.perform_request("url", "auth_token", req_type="delete")
Helena McGough95b92b32017-10-01 12:03:53 +0100123
124 delete.assert_called_with("url", headers=mock.ANY, timeout=mock.ANY)