Debian Codegraph @

ironic_tempest_plugin/tests/api/admin/test_inspection_rules.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from tempest.lib.common.utils import data_utils
14 from tempest.lib import decorators
15
16 from ironic_tempest_plugin.tests.api.admin import api_microversion_fixture
17 from ironic_tempest_plugin.tests.api import base
18
19
20 class TestInspectionRules(base.BaseBaremetalTest):
21 """API tests for Inspection Rules endpoints"""
22
23 # Inspection rules API was introduced in microversion 1.96
24 # We will be skipping this test for the older version.
25 min_microversion = '1.96'
26
27 def setUp(self):
28 super(TestInspectionRules, self).setUp()
29
30 _, self.node = self.create_node(None)
31
32 self.useFixture(
33 api_microversion_fixture.APIMicroversionFixture('1.96'))
34
35 def _create_inspection_rule_payload(self, **kwargs):
36 """Create a Inspection rule payload."""
37 payload = {
38 "description": "Inspection rule to log node UUID",
39 "conditions": [],
40 "actions": [
41 {
42 "op": "log",
43 "args": {
44 "msg": "Node with UUID {node.uuid} is being inspected"
45 }
46 }
47 ],
48 "phase": "main",
49 "priority": 0,
50 "sensitive": False
51 }
52
53 payload.update(kwargs)
54
55 return payload
56
57 @decorators.idempotent_id('7fb771cd-b011-409e-a255-3c71cf7251e8')
58 def test_create_rule_sensitive_true(self):
59 """Test creating rule with sensitive=True."""
60 rule_uuid = data_utils.rand_uuid()
61 payload = self._create_inspection_rule_payload(sensitive=True)
62
63 self.create_inspection_rule(rule_uuid, payload)
64
65 _, fetched_rule = self.client.show_inspection_rule(rule_uuid)
66
67 self.assertTrue(fetched_rule.get('sensitive'))
68 self.assertIsNone(fetched_rule.get('conditions'))
69 self.assertIsNone(fetched_rule.get('actions'))
70
71 @decorators.idempotent_id('e60b4513-7c3d-4b2c-b485-17443bf6485f')
72 def test_create_rule_complex_logging_conditions_actions(self):
73 """Test creating rule with loop conditions and actions"""
74 complex_log_conditions = [
75 {
76 "op": "eq",
77 "args": [
78 "{inventory.system.product_name}",
79 "{item}"
80 ],
81 "loop": [
82 "product_name_1",
83 "product_name_2",
84 "product_name_3"
85 ],
86 "multiple": "any"
87 }
88 ]
89
90 complex_log_actions = [
91 {
92 "op": "set-attribute",
93 "args": [
94 "{item[path]}",
95 "{item[value]}"
96 ],
97 "loop": [
98 {
99 "path": "/driver_info/ipmi_username",
100 "value": "admin"
101 },
102 {
103 "path": "/driver_info/ipmi_password",
104 "value": "password"
105 },
106 {
107 "path": "/driver_info/ipmi_address",
108 "value": "{inventory[bmc_address]}"
109 }
110 ]
111 }
112 ]
113
114 payload = self._create_inspection_rule_payload(
115 conditions=complex_log_conditions,
116 actions=complex_log_actions,
117 )
118
119 _, created_rule = self.create_inspection_rule(None, payload)
120
121 self.assertEqual(complex_log_conditions,
122 created_rule.get('conditions'))
123 self.assertEqual(complex_log_actions,
124 created_rule.get('actions'))
125
126 @decorators.idempotent_id('a786a4ec-1e43-4fb9-8fc3-c53aa4e1f52f')
127 def test_patch_conditions_actions_priority(self):
128 """Test Updating rule'si priority, condition and actions"""
129 payload = self._create_inspection_rule_payload()
130
131 patch = [
132 {
133 "op": "replace",
134 "path": "/priority",
135 "value": 200
136 },
137 {
138 "op": "replace",
139 "path": "/conditions",
140 "value": [
141 {
142 "op": "eq",
143 "args": ["{{ inventory.cpu.count }}", 8]
144 }
145 ]
146 },
147 {
148 "op": "replace",
149 "path": "/actions",
150 "value": [
151 {
152 "op": "set-attribute",
153 "args": ["{{ /properties/cpu_model }}", "cpu_xyz"]
154 },
155 {
156 "op": "log",
157 "args": ["CPU model updated via rule."]
158 }
159 ]
160 }
161 ]
162
163 _, created_rule = self.create_inspection_rule(None, payload)
164 _, fetched_rule = self.client.update_inspection_rule(
165 created_rule.get('uuid'), patch)
166
167 self.assertEqual(fetched_rule.get('priority'),
168 patch[0]['value'])
169 self.assertEqual(fetched_rule.get('conditions'),
170 patch[1]['value'])
171 self.assertEqual(fetched_rule.get('actions'),
172 patch[2]['value'])