File: test_network_meter_rule.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 (128 lines) | stat: -rw-r--r-- 4,318 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
# Copyright (c) 2016, Intel Corporation.
# All Rights Reserved.
#
#    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 unittest
import uuid

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


class TestMeterRule(common.NetworkTests):
    """Functional tests for meter rule"""

    METER_ID = None
    METER_RULE_ID = None

    @classmethod
    def setUpClass(cls):
        super().setUpClass()

        if not cls.is_extension_enabled("metering"):
            raise unittest.SkipTest("No metering extension present")

        if cls.haz_network:
            cls.METER_NAME = uuid.uuid4().hex

            json_output = cls.openstack(
                'network meter create ' + cls.METER_NAME,
                parse_output=True,
            )
            cls.METER_ID = json_output.get('id')

    @classmethod
    def tearDownClass(cls):
        try:
            if cls.haz_network:
                raw_output = cls.openstack(
                    'network meter delete ' + cls.METER_ID
                )
                cls.assertOutput('', raw_output)
        finally:
            super().tearDownClass()

    def test_meter_rule_delete(self):
        """test create, delete"""
        json_output = self.openstack(
            'network meter rule create '
            + '--remote-ip-prefix 10.0.0.0/8 '
            + self.METER_ID,
            parse_output=True,
        )
        rule_id = json_output.get('id')
        re_ip = json_output.get('remote_ip_prefix')

        self.addCleanup(self.openstack, 'network meter rule delete ' + rule_id)
        self.assertIsNotNone(re_ip)
        self.assertIsNotNone(rule_id)
        self.assertEqual('10.0.0.0/8', re_ip)

    def test_meter_rule_list(self):
        """Test create, list, delete"""
        json_output = self.openstack(
            'network meter rule create '
            + '--remote-ip-prefix 10.0.0.0/8 '
            + self.METER_ID,
            parse_output=True,
        )
        rule_id_1 = json_output.get('id')
        self.addCleanup(
            self.openstack, 'network meter rule delete ' + rule_id_1
        )
        self.assertEqual('10.0.0.0/8', json_output.get('remote_ip_prefix'))

        json_output_1 = self.openstack(
            'network meter rule create '
            + '--remote-ip-prefix 11.0.0.0/8 '
            + self.METER_ID,
            parse_output=True,
        )
        rule_id_2 = json_output_1.get('id')
        self.addCleanup(
            self.openstack, 'network meter rule delete ' + rule_id_2
        )
        self.assertEqual('11.0.0.0/8', json_output_1.get('remote_ip_prefix'))

        json_output = self.openstack(
            'network meter rule list',
            parse_output=True,
        )
        rule_id_list = [item.get('ID') for item in json_output]
        ip_prefix_list = [item.get('Remote IP Prefix') for item in json_output]
        self.assertIn(rule_id_1, rule_id_list)
        self.assertIn(rule_id_2, rule_id_list)
        self.assertIn('10.0.0.0/8', ip_prefix_list)
        self.assertIn('11.0.0.0/8', ip_prefix_list)

    def test_meter_rule_show(self):
        """Test create, show, delete"""
        json_output = self.openstack(
            'network meter rule create '
            + '--remote-ip-prefix 10.0.0.0/8 '
            + '--egress '
            + self.METER_ID,
            parse_output=True,
        )
        rule_id = json_output.get('id')

        self.assertEqual('egress', json_output.get('direction'))

        json_output = self.openstack(
            'network meter rule show ' + rule_id,
            parse_output=True,
        )
        self.assertEqual('10.0.0.0/8', json_output.get('remote_ip_prefix'))
        self.assertIsNotNone(rule_id)

        self.addCleanup(self.openstack, 'network meter rule delete ' + rule_id)