| 1 |
# Copyright (C) 2013 Yahoo! Inc. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# 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, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import argparse |
| 17 |
|
| 18 |
from oslo_utils import netutils |
| 19 |
|
| 20 |
import testtools |
| 21 |
|
| 22 |
from neutronclient.common import exceptions |
| 23 |
from neutronclient.common import utils |
| 24 |
|
| 25 |
|
| 26 |
class TestUtils(testtools.TestCase): |
| 27 |
def test_string_to_bool_true(self): |
| 28 |
self.assertTrue(utils.str2bool('true')) |
| 29 |
|
| 30 |
def test_string_to_bool_false(self): |
| 31 |
self.assertFalse(utils.str2bool('false')) |
| 32 |
|
| 33 |
def test_string_to_bool_None(self): |
| 34 |
self.assertIsNone(utils.str2bool(None)) |
| 35 |
|
| 36 |
def test_string_to_dictionary(self): |
| 37 |
input_str = 'key1=value1,key2=value2' |
| 38 |
expected = {'key1': 'value1', 'key2': 'value2'} |
| 39 |
self.assertEqual(expected, utils.str2dict(input_str)) |
| 40 |
|
| 41 |
def test_none_string_to_dictionary(self): |
| 42 |
input_str = '' |
| 43 |
expected = {} |
| 44 |
self.assertEqual(expected, utils.str2dict(input_str)) |
| 45 |
input_str = None |
| 46 |
expected = {} |
| 47 |
self.assertEqual(expected, utils.str2dict(input_str)) |
| 48 |
|
| 49 |
def test_invalid_string_to_dictionary(self): |
| 50 |
input_str = 'invalid' |
| 51 |
self.assertRaises(argparse.ArgumentTypeError, |
| 52 |
utils.str2dict, input_str) |
| 53 |
|
| 54 |
def test_string_with_comma_value_to_dictionary(self): |
| 55 |
input_str = ('opt_name=classless-static-route,' |
| 56 |
'opt_value=169.254.169.254/32,10.0.0.1') |
| 57 |
expected = {'opt_name': 'classless-static-route', |
| 58 |
'opt_value': '169.254.169.254/32,10.0.0.1'} |
| 59 |
self.assertEqual(expected, utils.str2dict(input_str)) |
| 60 |
|
| 61 |
def test_str2dict_optional_keys(self): |
| 62 |
self.assertDictEqual({'key1': 'value1'}, |
| 63 |
utils.str2dict('key1=value1', |
| 64 |
optional_keys=['key1', 'key2'])) |
| 65 |
self.assertDictEqual({'key1': 'value1', 'key2': 'value2'}, |
| 66 |
utils.str2dict('key1=value1,key2=value2', |
| 67 |
optional_keys=['key1', 'key2'])) |
| 68 |
e = self.assertRaises(argparse.ArgumentTypeError, |
| 69 |
utils.str2dict, |
| 70 |
'key1=value1,key2=value2,key3=value3', |
| 71 |
optional_keys=['key1', 'key2']) |
| 72 |
self.assertEqual("Invalid key(s) 'key3' specified. " |
| 73 |
"Valid key(s): 'key1, key2'.", |
| 74 |
str(e)) |
| 75 |
|
| 76 |
def test_str2dict_required_keys(self): |
| 77 |
self.assertDictEqual( |
| 78 |
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}, |
| 79 |
utils.str2dict('key1=value1,key2=value2,key3=value3', |
| 80 |
required_keys=['key1', 'key2'], |
| 81 |
optional_keys=['key3'])) |
| 82 |
self.assertDictEqual( |
| 83 |
{'key1': 'value1', 'key2': 'value2'}, |
| 84 |
utils.str2dict('key1=value1,key2=value2', |
| 85 |
required_keys=['key1', 'key2'])) |
| 86 |
e = self.assertRaises(argparse.ArgumentTypeError, |
| 87 |
utils.str2dict, 'key1=value1', |
| 88 |
required_keys=['key1', 'key2']) |
| 89 |
self.assertEqual("Required key(s) 'key2' not specified.", str(e)) |
| 90 |
|
| 91 |
def test_get_dict_item_properties(self): |
| 92 |
item = {'name': 'test_name', 'id': 'test_id'} |
| 93 |
fields = ('name', 'id') |
| 94 |
actual = utils.get_item_properties(item=item, fields=fields) |
| 95 |
self.assertEqual(('test_name', 'test_id'), actual) |
| 96 |
|
| 97 |
def test_get_object_item_properties_mixed_case_fields(self): |
| 98 |
class Fake(object): |
| 99 |
def __init__(self): |
| 100 |
self.id = 'test_id' |
| 101 |
self.name = 'test_name' |
| 102 |
self.test_user = 'test' |
| 103 |
|
| 104 |
fields = ('name', 'id', 'test user') |
| 105 |
mixed_fields = ('test user', 'ID') |
| 106 |
item = Fake() |
| 107 |
actual = utils.get_item_properties(item, fields, mixed_fields) |
| 108 |
self.assertEqual(('test_name', 'test_id', 'test'), actual) |
| 109 |
|
| 110 |
def test_get_object_item_desired_fields_differ_from_item(self): |
| 111 |
class Fake(object): |
| 112 |
def __init__(self): |
| 113 |
self.id = 'test_id_1' |
| 114 |
self.name = 'test_name' |
| 115 |
self.test_user = 'test' |
| 116 |
|
| 117 |
fields = ('name', 'id', 'test user') |
| 118 |
item = Fake() |
| 119 |
actual = utils.get_item_properties(item, fields) |
| 120 |
self.assertNotEqual(('test_name', 'test_id', 'test'), actual) |
| 121 |
|
| 122 |
def test_get_object_item_desired_fields_is_empty(self): |
| 123 |
class Fake(object): |
| 124 |
def __init__(self): |
| 125 |
self.id = 'test_id_1' |
| 126 |
self.name = 'test_name' |
| 127 |
self.test_user = 'test' |
| 128 |
|
| 129 |
fields = [] |
| 130 |
item = Fake() |
| 131 |
actual = utils.get_item_properties(item, fields) |
| 132 |
self.assertEqual((), actual) |
| 133 |
|
| 134 |
def test_get_object_item_with_formatters(self): |
| 135 |
class Fake(object): |
| 136 |
def __init__(self): |
| 137 |
self.id = 'test_id' |
| 138 |
self.name = 'test_name' |
| 139 |
self.test_user = 'test' |
| 140 |
|
| 141 |
class FakeCallable(object): |
| 142 |
def __call__(self, *args, **kwargs): |
| 143 |
return 'pass' |
| 144 |
|
| 145 |
fields = ('name', 'id', 'test user', 'is_public') |
| 146 |
formatters = {'is_public': FakeCallable()} |
| 147 |
item = Fake() |
| 148 |
act = utils.get_item_properties(item, fields, formatters=formatters) |
| 149 |
self.assertEqual(('test_name', 'test_id', 'test', 'pass'), act) |
| 150 |
|
| 151 |
def test_is_cidr(self): |
| 152 |
self.assertTrue(netutils.is_valid_cidr('10.10.10.0/24')) |
| 153 |
self.assertFalse(netutils.is_valid_cidr('10.10.10..0/24')) |
| 154 |
self.assertFalse(netutils.is_valid_cidr('wrong_cidr_format')) |
| 155 |
|
| 156 |
|
| 157 |
class ImportClassTestCase(testtools.TestCase): |
| 158 |
def test_get_client_class_invalid_version(self): |
| 159 |
self.assertRaises( |
| 160 |
exceptions.UnsupportedVersion, |
| 161 |
utils.get_client_class, 'image', '2', {'image': '2'}) |