File: test_address_group.py

package info (click to toggle)
python-openstackclient 7.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,012 kB
  • sloc: python: 135,291; makefile: 140; sh: 22
file content (181 lines) | stat: -rw-r--r-- 5,971 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import uuid

from openstackclient.tests.functional.network.v2 import common


class AddressGroupTests(common.NetworkTests):
    """Functional tests for address group"""

    def setUp(self):
        super().setUp()

        if not self.is_extension_enabled('address-group'):
            self.skipTest("No address-group extension present")

    def test_address_group_create_and_delete(self):
        """Test create, delete multiple"""
        name1 = uuid.uuid4().hex
        cmd_output = self.openstack(
            'address group create ' + name1,
            parse_output=True,
        )
        self.assertEqual(
            name1,
            cmd_output['name'],
        )

        name2 = uuid.uuid4().hex
        cmd_output = self.openstack(
            'address group create ' + name2,
            parse_output=True,
        )
        self.assertEqual(
            name2,
            cmd_output['name'],
        )

        raw_output = self.openstack(
            'address group delete ' + name1 + ' ' + name2,
        )
        self.assertOutput('', raw_output)

    def test_address_group_list(self):
        """Test create, list filters, delete"""
        # Get project IDs
        cmd_output = self.openstack('token issue ', parse_output=True)
        auth_project_id = cmd_output['project_id']

        cmd_output = self.openstack('project list ', parse_output=True)
        admin_project_id = None
        demo_project_id = None
        for p in cmd_output:
            if p['Name'] == 'admin':
                admin_project_id = p['ID']
            if p['Name'] == 'demo':
                demo_project_id = p['ID']

        # Verify assumptions:
        # * admin and demo projects are present
        # * demo and admin are distinct projects
        # * tests run as admin
        self.assertIsNotNone(admin_project_id)
        self.assertIsNotNone(demo_project_id)
        self.assertNotEqual(admin_project_id, demo_project_id)
        self.assertEqual(admin_project_id, auth_project_id)

        name1 = uuid.uuid4().hex
        cmd_output = self.openstack(
            'address group create ' + name1,
            parse_output=True,
        )
        self.addCleanup(self.openstack, 'address group delete ' + name1)
        self.assertEqual(
            admin_project_id,
            cmd_output["project_id"],
        )

        name2 = uuid.uuid4().hex
        cmd_output = self.openstack(
            'address group create '
            + '--project '
            + demo_project_id
            + ' '
            + name2,
            parse_output=True,
        )
        self.addCleanup(self.openstack, 'address group delete ' + name2)
        self.assertEqual(
            demo_project_id,
            cmd_output["project_id"],
        )

        # Test list
        cmd_output = self.openstack(
            'address group list ',
            parse_output=True,
        )
        names = [x["Name"] for x in cmd_output]
        self.assertIn(name1, names)
        self.assertIn(name2, names)

        # Test list --project
        cmd_output = self.openstack(
            'address group list ' + '--project ' + demo_project_id,
            parse_output=True,
        )
        names = [x["Name"] for x in cmd_output]
        self.assertNotIn(name1, names)
        self.assertIn(name2, names)

        # Test list --name
        cmd_output = self.openstack(
            'address group list ' + '--name ' + name1,
            parse_output=True,
        )
        names = [x["Name"] for x in cmd_output]
        self.assertIn(name1, names)
        self.assertNotIn(name2, names)

    def test_address_group_set_unset_and_show(self):
        """Tests create options, set, unset, and show"""
        name = uuid.uuid4().hex
        newname = name + "_"
        cmd_output = self.openstack(
            'address group create '
            + '--description aaaa '
            + '--address 10.0.0.1 --address 2001::/16 '
            + name,
            parse_output=True,
        )
        self.addCleanup(self.openstack, 'address group delete ' + newname)
        self.assertEqual(name, cmd_output['name'])
        self.assertEqual('aaaa', cmd_output['description'])
        self.assertEqual(2, len(cmd_output['addresses']))

        # Test set name, description and address
        raw_output = self.openstack(
            'address group set '
            + '--name '
            + newname
            + ' '
            + '--description bbbb '
            + '--address 10.0.0.2 --address 192.0.0.0/8 '
            + name,
        )
        self.assertOutput('', raw_output)

        # Show the updated address group
        cmd_output = self.openstack(
            'address group show ' + newname,
            parse_output=True,
        )
        self.assertEqual(newname, cmd_output['name'])
        self.assertEqual('bbbb', cmd_output['description'])
        self.assertEqual(4, len(cmd_output['addresses']))

        # Test unset address
        raw_output = self.openstack(
            'address group unset '
            + '--address 10.0.0.1 --address 2001::/16 '
            + '--address 10.0.0.2 --address 192.0.0.0/8 '
            + newname,
        )
        self.assertEqual('', raw_output)

        cmd_output = self.openstack(
            'address group show ' + newname,
            parse_output=True,
        )
        self.assertEqual(0, len(cmd_output['addresses']))