|
10 | 10 | # License for the specific language governing permissions and limitations |
11 | 11 | # under the License. |
12 | 12 |
|
| 13 | +import json |
13 | 14 | import uuid |
14 | 15 |
|
15 | 16 | from openstackclient.tests.functional import base |
|
18 | 19 | class AggregateTests(base.TestCase): |
19 | 20 | """Functional tests for aggregate.""" |
20 | 21 |
|
21 | | - NAME = uuid.uuid4().hex |
22 | | - HEADERS = ['Name'] |
23 | | - FIELDS = ['name'] |
| 22 | + def test_aggregate_create_and_delete(self): |
| 23 | + """Test create, delete multiple""" |
| 24 | + name1 = uuid.uuid4().hex |
| 25 | + cmd_output = json.loads(self.openstack( |
| 26 | + 'aggregate create -f json ' + |
| 27 | + '--zone nova ' + |
| 28 | + name1)) |
| 29 | + self.assertEqual( |
| 30 | + name1, |
| 31 | + cmd_output['name'] |
| 32 | + ) |
| 33 | + self.assertEqual( |
| 34 | + 'nova', |
| 35 | + cmd_output['availability_zone'] |
| 36 | + ) |
24 | 37 |
|
25 | | - @classmethod |
26 | | - def setUpClass(cls): |
27 | | - opts = cls.get_opts(cls.FIELDS) |
28 | | - # Use the default 'nova' availability zone for the aggregate. |
29 | | - raw_output = cls.openstack( |
30 | | - 'aggregate create --zone nova ' + cls.NAME + opts |
| 38 | + name2 = uuid.uuid4().hex |
| 39 | + cmd_output = json.loads(self.openstack( |
| 40 | + 'aggregate create -f json ' + |
| 41 | + '--zone nova ' + |
| 42 | + name2)) |
| 43 | + self.assertEqual( |
| 44 | + name2, |
| 45 | + cmd_output['name'] |
| 46 | + ) |
| 47 | + self.assertEqual( |
| 48 | + 'nova', |
| 49 | + cmd_output['availability_zone'] |
31 | 50 | ) |
32 | | - expected = cls.NAME + '\n' |
33 | | - cls.assertOutput(expected, raw_output) |
34 | 51 |
|
35 | | - @classmethod |
36 | | - def tearDownClass(cls): |
37 | | - raw_output = cls.openstack('aggregate delete ' + cls.NAME) |
38 | | - cls.assertOutput('', raw_output) |
| 52 | + del_output = self.openstack( |
| 53 | + 'aggregate delete ' + name1 + ' ' + name2) |
| 54 | + self.assertOutput('', del_output) |
39 | 55 |
|
40 | 56 | def test_aggregate_list(self): |
41 | | - opts = self.get_opts(self.HEADERS) |
42 | | - raw_output = self.openstack('aggregate list' + opts) |
43 | | - self.assertIn(self.NAME, raw_output) |
44 | | - |
45 | | - def test_aggregate_show(self): |
46 | | - opts = self.get_opts(self.FIELDS) |
47 | | - raw_output = self.openstack('aggregate show ' + self.NAME + opts) |
48 | | - self.assertEqual(self.NAME + "\n", raw_output) |
| 57 | + """Test aggregate list""" |
| 58 | + name1 = uuid.uuid4().hex |
| 59 | + self.openstack( |
| 60 | + 'aggregate create ' + |
| 61 | + '--zone nova ' + |
| 62 | + '--property a=b ' + |
| 63 | + name1) |
| 64 | + self.addCleanup(self.openstack, 'aggregate delete ' + name1) |
49 | 65 |
|
50 | | - def test_aggregate_properties(self): |
51 | | - opts = self.get_opts(['name', 'properties']) |
| 66 | + name2 = uuid.uuid4().hex |
| 67 | + self.openstack( |
| 68 | + 'aggregate create ' + |
| 69 | + '--zone internal ' + |
| 70 | + '--property c=d ' + |
| 71 | + name2) |
| 72 | + self.addCleanup(self.openstack, 'aggregate delete ' + name2) |
| 73 | + |
| 74 | + cmd_output = json.loads(self.openstack( |
| 75 | + 'aggregate list -f json')) |
| 76 | + names = [x['Name'] for x in cmd_output] |
| 77 | + self.assertIn(name1, names) |
| 78 | + self.assertIn(name2, names) |
| 79 | + zones = [x['Availability Zone'] for x in cmd_output] |
| 80 | + self.assertIn('nova', zones) |
| 81 | + self.assertIn('internal', zones) |
| 82 | + |
| 83 | + # Test aggregate list --long |
| 84 | + cmd_output = json.loads(self.openstack( |
| 85 | + 'aggregate list --long -f json')) |
| 86 | + names = [x['Name'] for x in cmd_output] |
| 87 | + self.assertIn(name1, names) |
| 88 | + self.assertIn(name2, names) |
| 89 | + zones = [x['Availability Zone'] for x in cmd_output] |
| 90 | + self.assertIn('nova', zones) |
| 91 | + self.assertIn('internal', zones) |
| 92 | + properties = [x['Properties'] for x in cmd_output] |
| 93 | + self.assertIn({'a': 'b'}, properties) |
| 94 | + self.assertIn({'c': 'd'}, properties) |
| 95 | + |
| 96 | + def test_aggregate_set_and_unset(self): |
| 97 | + """Test aggregate set, show and unset""" |
| 98 | + name1 = uuid.uuid4().hex |
| 99 | + name2 = uuid.uuid4().hex |
| 100 | + self.openstack( |
| 101 | + 'aggregate create ' + |
| 102 | + '--zone nova ' + |
| 103 | + '--property a=b ' + |
| 104 | + name1) |
| 105 | + self.addCleanup(self.openstack, 'aggregate delete ' + name2) |
52 | 106 |
|
53 | 107 | raw_output = self.openstack( |
54 | | - 'aggregate set --property a=b --property c=d ' + self.NAME |
| 108 | + 'aggregate set --name ' + |
| 109 | + name2 + |
| 110 | + ' --zone internal ' + |
| 111 | + '--no-property ' + |
| 112 | + '--property c=d ' + |
| 113 | + name1 |
55 | 114 | ) |
56 | | - self.assertEqual('', raw_output) |
57 | | - |
58 | | - raw_output = self.openstack('aggregate show ' + self.NAME + opts) |
59 | | - self.assertIn(self.NAME + "\na='b', c='d'\n", raw_output) |
| 115 | + self.assertOutput('', raw_output) |
60 | 116 |
|
61 | | - raw_output = self.openstack( |
62 | | - 'aggregate unset --property a ' + self.NAME |
| 117 | + cmd_output = json.loads(self.openstack( |
| 118 | + 'aggregate show -f json ' + name2)) |
| 119 | + self.assertEqual( |
| 120 | + name2, |
| 121 | + cmd_output['name'] |
63 | 122 | ) |
64 | | - self.assertEqual('', raw_output) |
65 | | - |
66 | | - raw_output = self.openstack('aggregate show ' + self.NAME + opts) |
67 | | - self.assertIn(self.NAME + "\nc='d'\n", raw_output) |
68 | | - |
69 | | - raw_output = self.openstack( |
70 | | - 'aggregate set --property a=b --property c=d ' + self.NAME |
| 123 | + self.assertEqual( |
| 124 | + 'internal', |
| 125 | + cmd_output['availability_zone'] |
71 | 126 | ) |
72 | | - self.assertEqual('', raw_output) |
73 | | - |
74 | | - raw_output = self.openstack( |
75 | | - 'aggregate set --no-property ' + self.NAME |
| 127 | + self.assertIn( |
| 128 | + "c='d'", |
| 129 | + cmd_output['properties'] |
| 130 | + ) |
| 131 | + self.assertNotIn( |
| 132 | + "a='b'", |
| 133 | + cmd_output['properties'] |
76 | 134 | ) |
77 | | - self.assertEqual('', raw_output) |
78 | | - |
79 | | - raw_output = self.openstack('aggregate show ' + self.NAME + opts) |
80 | | - self.assertNotIn("a='b', c='d'", raw_output) |
81 | | - |
82 | | - def test_aggregate_set(self): |
83 | | - opts = self.get_opts(["name", "availability_zone"]) |
84 | 135 |
|
| 136 | + # Test unset |
85 | 137 | raw_output = self.openstack( |
86 | | - 'aggregate set --zone Zone_1 ' + self.NAME) |
87 | | - self.assertEqual("", raw_output) |
| 138 | + 'aggregate unset --property c ' + |
| 139 | + name2 |
| 140 | + ) |
| 141 | + self.assertOutput('', raw_output) |
88 | 142 |
|
89 | | - raw_output = self.openstack('aggregate show ' + self.NAME + opts) |
90 | | - self.assertEqual("Zone_1\n" + self.NAME + "\n", raw_output) |
| 143 | + cmd_output = json.loads(self.openstack( |
| 144 | + 'aggregate show -f json ' + name2)) |
| 145 | + self.assertNotIn( |
| 146 | + "c='d'", |
| 147 | + cmd_output['properties'] |
| 148 | + ) |
91 | 149 |
|
92 | 150 | def test_aggregate_add_and_remove_host(self): |
93 | | - opts = self.get_opts(["hosts", "name"]) |
94 | | - |
95 | | - raw_output = self.openstack('host list -f value -c "Host Name"') |
96 | | - host_name = raw_output.split()[0] |
97 | | - |
| 151 | + """Test aggregate add and remove host""" |
| 152 | + name = uuid.uuid4().hex |
98 | 153 | self.openstack( |
99 | | - 'aggregate add host ' + self.NAME + ' ' + host_name) |
100 | | - raw_output = self.openstack('aggregate show ' + self.NAME + opts) |
101 | | - self.assertEqual("[u'" + host_name + "']" + "\n" + self.NAME + "\n", |
102 | | - raw_output) |
| 154 | + 'aggregate create ' + name) |
| 155 | + self.addCleanup(self.openstack, 'aggregate delete ' + name) |
| 156 | + |
| 157 | + # Get a host |
| 158 | + cmd_output = json.loads(self.openstack( |
| 159 | + 'host list -f json')) |
| 160 | + host_name = cmd_output[0]['Host Name'] |
| 161 | + |
| 162 | + # Test add host |
| 163 | + cmd_output = json.loads(self.openstack( |
| 164 | + 'aggregate add host -f json ' + |
| 165 | + name + ' ' + |
| 166 | + host_name |
| 167 | + )) |
| 168 | + self.assertIn( |
| 169 | + host_name, |
| 170 | + cmd_output['hosts'] |
| 171 | + ) |
103 | 172 |
|
104 | | - self.openstack( |
105 | | - 'aggregate remove host ' + self.NAME + ' ' + host_name) |
106 | | - raw_output = self.openstack('aggregate show ' + self.NAME + opts) |
107 | | - self.assertEqual("[]\n" + self.NAME + "\n", raw_output) |
| 173 | + # Test remove host |
| 174 | + cmd_output = json.loads(self.openstack( |
| 175 | + 'aggregate remove host -f json ' + |
| 176 | + name + ' ' + |
| 177 | + host_name |
| 178 | + )) |
| 179 | + self.assertNotIn( |
| 180 | + host_name, |
| 181 | + cmd_output['hosts'] |
| 182 | + ) |
0 commit comments