|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright (c) 2016 IBM. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +""" |
| 16 | +_cloud_foundry_tests_ |
| 17 | +
|
| 18 | +Unit tests for the CloudFoundryService class. |
| 19 | +""" |
| 20 | + |
| 21 | +import json |
| 22 | +import mock |
| 23 | +import unittest |
| 24 | + |
| 25 | +from cloudant._common_util import CloudFoundryService |
| 26 | +from cloudant.error import CloudantException |
| 27 | + |
| 28 | + |
| 29 | +class CloudFoundryServiceTests(unittest.TestCase): |
| 30 | + |
| 31 | + def __init__(self, *args, **kwargs): |
| 32 | + super(CloudFoundryServiceTests, self).__init__(*args, **kwargs) |
| 33 | + self._test_vcap_services_single = json.dumps({'cloudantNoSQLDB': [ |
| 34 | + { |
| 35 | + 'name': 'Cloudant NoSQL DB 1', # valid service |
| 36 | + 'credentials': { |
| 37 | + 'host': 'example.cloudant.com', |
| 38 | + 'password': 'pa$$w0rd01', |
| 39 | + 'port': 1234, |
| 40 | + 'username': 'example' |
| 41 | + } |
| 42 | + } |
| 43 | + ]}) |
| 44 | + self._test_vcap_services_multiple = json.dumps({'cloudantNoSQLDB': [ |
| 45 | + { |
| 46 | + 'name': 'Cloudant NoSQL DB 1', # valid service |
| 47 | + 'credentials': { |
| 48 | + 'host': 'example.cloudant.com', |
| 49 | + 'password': 'pa$$w0rd01', |
| 50 | + 'port': 1234, |
| 51 | + 'username': 'example' |
| 52 | + } |
| 53 | + }, |
| 54 | + { |
| 55 | + 'name': 'Cloudant NoSQL DB 2', # valid service, default port |
| 56 | + 'credentials': { |
| 57 | + 'host': 'example.cloudant.com', |
| 58 | + 'password': 'pa$$w0rd01', |
| 59 | + 'username': 'example' |
| 60 | + } |
| 61 | + }, |
| 62 | + { |
| 63 | + 'name': 'Cloudant NoSQL DB 3', # missing host |
| 64 | + 'credentials': { |
| 65 | + 'password': 'pa$$w0rd01', |
| 66 | + 'port': 1234, |
| 67 | + 'username': 'example' |
| 68 | + } |
| 69 | + }, |
| 70 | + { |
| 71 | + 'name': 'Cloudant NoSQL DB 4', # missing password |
| 72 | + 'credentials': { |
| 73 | + 'host': 'example.cloudant.com', |
| 74 | + 'port': 1234, |
| 75 | + 'username': 'example' |
| 76 | + } |
| 77 | + }, |
| 78 | + { |
| 79 | + 'name': 'Cloudant NoSQL DB 5', # missing username |
| 80 | + 'credentials': { |
| 81 | + 'host': 'example.cloudant.com', |
| 82 | + 'password': 'pa$$w0rd01', |
| 83 | + 'port': 1234, |
| 84 | + } |
| 85 | + }, |
| 86 | + { |
| 87 | + 'name': 'Cloudant NoSQL DB 6', # invalid credentials type |
| 88 | + 'credentials': [ |
| 89 | + 'example.cloudant.com', |
| 90 | + 'pa$$w0rd01', |
| 91 | + 'example' |
| 92 | + ] |
| 93 | + } |
| 94 | + ]}) |
| 95 | + |
| 96 | + @mock.patch('os.getenv') |
| 97 | + def test_get_vcap_service_default_success(self, m_getenv): |
| 98 | + m_getenv.return_value = self._test_vcap_services_single |
| 99 | + service = CloudFoundryService() |
| 100 | + self.assertEqual('Cloudant NoSQL DB 1', service.name) |
| 101 | + |
| 102 | + @mock.patch('os.getenv') |
| 103 | + def test_get_vcap_service_default_failure_multiple_services(self, m_getenv): |
| 104 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 105 | + with self.assertRaises(CloudantException) as cm: |
| 106 | + CloudFoundryService() |
| 107 | + self.assertEqual('Missing service in VCAP_SERVICES', str(cm.exception)) |
| 108 | + |
| 109 | + @mock.patch('os.getenv') |
| 110 | + def test_get_vcap_service_instance_host(self, m_getenv): |
| 111 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 112 | + service = CloudFoundryService('Cloudant NoSQL DB 1') |
| 113 | + self.assertEqual('example.cloudant.com', service.host) |
| 114 | + |
| 115 | + @mock.patch('os.getenv') |
| 116 | + def test_get_vcap_service_instance_password(self, m_getenv): |
| 117 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 118 | + service = CloudFoundryService('Cloudant NoSQL DB 1') |
| 119 | + self.assertEqual('pa$$w0rd01', service.password) |
| 120 | + |
| 121 | + @mock.patch('os.getenv') |
| 122 | + def test_get_vcap_service_instance_port(self, m_getenv): |
| 123 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 124 | + service = CloudFoundryService('Cloudant NoSQL DB 1') |
| 125 | + self.assertEqual('1234', service.port) |
| 126 | + |
| 127 | + @mock.patch('os.getenv') |
| 128 | + def test_get_vcap_service_instance_port_default(self, m_getenv): |
| 129 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 130 | + service = CloudFoundryService('Cloudant NoSQL DB 2') |
| 131 | + self.assertEqual('443', service.port) |
| 132 | + |
| 133 | + @mock.patch('os.getenv') |
| 134 | + def test_get_vcap_service_instance_url(self, m_getenv): |
| 135 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 136 | + service = CloudFoundryService('Cloudant NoSQL DB 1') |
| 137 | + self.assertEqual('https://example.cloudant.com:1234', service.url) |
| 138 | + |
| 139 | + @mock.patch('os.getenv') |
| 140 | + def test_get_vcap_service_instance_username(self, m_getenv): |
| 141 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 142 | + service = CloudFoundryService('Cloudant NoSQL DB 1') |
| 143 | + self.assertEqual('example', service.username) |
| 144 | + |
| 145 | + @mock.patch('os.getenv') |
| 146 | + def test_raise_error_for_missing_host(self, m_getenv): |
| 147 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 148 | + with self.assertRaises(CloudantException): |
| 149 | + CloudFoundryService('Cloudant NoSQL DB 3') |
| 150 | + |
| 151 | + @mock.patch('os.getenv') |
| 152 | + def test_raise_error_for_missing_password(self, m_getenv): |
| 153 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 154 | + with self.assertRaises(CloudantException) as cm: |
| 155 | + CloudFoundryService('Cloudant NoSQL DB 4') |
| 156 | + self.assertEqual( |
| 157 | + "Invalid service: 'password' missing", |
| 158 | + str(cm.exception) |
| 159 | + ) |
| 160 | + |
| 161 | + @mock.patch('os.getenv') |
| 162 | + def test_raise_error_for_missing_username(self, m_getenv): |
| 163 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 164 | + with self.assertRaises(CloudantException) as cm: |
| 165 | + CloudFoundryService('Cloudant NoSQL DB 5') |
| 166 | + self.assertEqual( |
| 167 | + "Invalid service: 'username' missing", |
| 168 | + str(cm.exception) |
| 169 | + ) |
| 170 | + |
| 171 | + @mock.patch('os.getenv') |
| 172 | + def test_raise_error_for_invalid_credentials_type(self, m_getenv): |
| 173 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 174 | + with self.assertRaises(CloudantException) as cm: |
| 175 | + CloudFoundryService('Cloudant NoSQL DB 6') |
| 176 | + self.assertEqual( |
| 177 | + 'Failed to decode VCAP_SERVICES service credentials', |
| 178 | + str(cm.exception) |
| 179 | + ) |
| 180 | + |
| 181 | + @mock.patch('os.getenv') |
| 182 | + def test_raise_error_for_missing_service(self, m_getenv): |
| 183 | + m_getenv.return_value = self._test_vcap_services_multiple |
| 184 | + with self.assertRaises(CloudantException) as cm: |
| 185 | + CloudFoundryService('Cloudant NoSQL DB 7') |
| 186 | + self.assertEqual('Missing service in VCAP_SERVICES', str(cm.exception)) |
0 commit comments