| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 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 | |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 24 | import json |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 25 | import logging |
| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 26 | import unittest |
| 27 | |
| 28 | import mock |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame^] | 29 | import requests |
| 30 | from keystoneclient.v3 import client |
| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 31 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 32 | from osm_mon.core.auth import AuthManager |
| 33 | from osm_mon.core.database import VimCredentials |
| Benjamin Diaz | 7551247 | 2018-04-27 14:32:31 -0300 | [diff] [blame^] | 34 | from osm_mon.core.settings import Config |
| Helena McGough | effeb7c | 2017-11-23 15:54:08 +0000 | [diff] [blame] | 35 | from osm_mon.plugins.OpenStack.common import Common |
| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 36 | |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 37 | __author__ = "Helena McGough" |
| 38 | |
| 39 | log = logging.getLogger(__name__) |
| 40 | |
| 41 | |
| 42 | class Message(object): |
| 43 | """Mock a message for an access credentials request.""" |
| 44 | |
| 45 | def __init__(self): |
| 46 | """Initialise the topic and value of access_cred message.""" |
| 47 | self.topic = "access_credentials" |
| 48 | self.value = json.dumps({"mock_value": "mock_details", |
| 49 | "vim_type": "OPENSTACK", |
| 50 | "access_config": |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 51 | {"openstack_site": "my_site", |
| 52 | "user": "my_user", |
| 53 | "password": "my_password", |
| 54 | "vim_tenant_name": "my_tenant"}}) |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 55 | |
| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 56 | |
| 57 | class TestCommon(unittest.TestCase): |
| 58 | """Test the common class for OpenStack plugins.""" |
| 59 | |
| 60 | def setUp(self): |
| 61 | """Test Setup.""" |
| 62 | super(TestCommon, self).setUp() |
| 63 | self.common = Common() |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 64 | self.creds = VimCredentials() |
| 65 | self.creds.id = 'test_id' |
| 66 | self.creds.user = 'user' |
| 67 | self.creds.url = 'url' |
| 68 | self.creds.password = 'password' |
| 69 | self.creds.tenant_name = 'tenant_name' |
| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 70 | |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 71 | @mock.patch.object(AuthManager, "get_credentials") |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 72 | @mock.patch.object(Config, "instance") |
| 73 | @mock.patch.object(client, "Client") |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 74 | def test_get_auth_token(self, key_client, cfg, get_creds): |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 75 | """Test generating a new authentication token.""" |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 76 | get_creds.return_value = self.creds |
| 77 | Common.get_auth_token('test_id') |
| 78 | get_creds.assert_called_with('test_id') |
| 79 | key_client.assert_called_with(auth_url='url', password='password', tenant_name='tenant_name', username='user') |
| Helena McGough | fe92f84 | 2017-11-17 14:57:08 +0000 | [diff] [blame] | 80 | |
| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 81 | @mock.patch.object(requests, 'post') |
| 82 | def test_post_req(self, post): |
| 83 | """Testing a post request.""" |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 84 | Common.perform_request("url", "auth_token", req_type="post", |
| 85 | payload="payload") |
| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 86 | |
| 87 | post.assert_called_with("url", data="payload", headers=mock.ANY, |
| 88 | timeout=mock.ANY) |
| 89 | |
| 90 | @mock.patch.object(requests, 'get') |
| 91 | def test_get_req(self, get): |
| 92 | """Testing a get request.""" |
| 93 | # Run the defualt get request without any parameters |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 94 | Common.perform_request("url", "auth_token", req_type="get") |
| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 95 | |
| 96 | get.assert_called_with("url", params=None, headers=mock.ANY, |
| 97 | timeout=mock.ANY) |
| 98 | |
| 99 | # Test with some parameters specified |
| 100 | get.reset_mock() |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 101 | Common.perform_request("url", "auth_token", req_type="get", |
| 102 | params="some parameters") |
| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 103 | |
| 104 | get.assert_called_with("url", params="some parameters", |
| 105 | headers=mock.ANY, timeout=mock.ANY) |
| 106 | |
| 107 | @mock.patch.object(requests, 'put') |
| 108 | def test_put_req(self, put): |
| 109 | """Testing a put request.""" |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 110 | Common.perform_request("url", "auth_token", req_type="put", |
| 111 | payload="payload") |
| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 112 | put.assert_called_with("url", data="payload", headers=mock.ANY, |
| 113 | timeout=mock.ANY) |
| 114 | |
| 115 | @mock.patch.object(requests, 'delete') |
| 116 | def test_delete_req(self, delete): |
| 117 | """Testing a delete request.""" |
| Benjamin Diaz | 181cce8 | 2018-03-28 21:12:11 -0300 | [diff] [blame] | 118 | Common.perform_request("url", "auth_token", req_type="delete") |
| Helena McGough | 95b92b3 | 2017-10-01 12:03:53 +0100 | [diff] [blame] | 119 | |
| 120 | delete.assert_called_with("url", headers=mock.ANY, timeout=mock.ANY) |