| 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 |
import copy |
| 14 |
|
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
from magnum.objects import fields as m_fields |
| 18 |
from magnum.tests.unit.db import base |
| 19 |
from magnum.tests.unit.objects import utils as obj_utils |
| 20 |
from magnum_capi_helm.common import capi_monitor |
| 21 |
|
| 22 |
|
| 23 |
class TestCAPIMonitor(base.DbTestCase): |
| 24 |
def setUp(self): |
| 25 |
super(TestCAPIMonitor, self).setUp() |
| 26 |
self.cluster_obj = obj_utils.create_test_cluster( |
| 27 |
self.context, |
| 28 |
name="cluster_example_$A", |
| 29 |
master_flavor_id="flavor_small", |
| 30 |
flavor_id="flavor_medium", |
| 31 |
stack_id="cluster-example-a-111111111111", |
| 32 |
) |
| 33 |
# add in missing node group flavor |
| 34 |
for ng in self.cluster_obj.nodegroups: |
| 35 |
if ng.role != "master": |
| 36 |
ng.flavor_id = "flavor_medium" |
| 37 |
ng.save() |
| 38 |
|
| 39 |
# Create capi monitor |
| 40 |
self.monitor = capi_monitor.CAPIMonitor(self.context, self.cluster_obj) |
| 41 |
|
| 42 |
# Patch k8s client and set Ready state for all CAPI objects. |
| 43 |
self.patcher = mock.patch.object( |
| 44 |
capi_monitor.CAPIMonitor, "_k8s_client" |
| 45 |
) |
| 46 |
self.mock_k8s = self.patcher.start() |
| 47 |
ready_state = { |
| 48 |
"status": {"conditions": [{"type": "Ready", "status": "True"}]} |
| 49 |
} |
| 50 |
self.mock_k8s.get_capi_cluster.return_value = copy.deepcopy( |
| 51 |
ready_state |
| 52 |
) |
| 53 |
self.mock_k8s.get_capi_openstackcluster.return_value = { |
| 54 |
"status": { |
| 55 |
"ready": True, |
| 56 |
} |
| 57 |
} |
| 58 |
self.mock_k8s.get_kubeadm_control_plane.return_value = copy.deepcopy( |
| 59 |
ready_state |
| 60 |
) |
| 61 |
self.mock_k8s.get_machine_deployment.return_value = copy.deepcopy( |
| 62 |
ready_state |
| 63 |
) |
| 64 |
|
| 65 |
def tearDown(self): |
| 66 |
super(TestCAPIMonitor, self).tearDown() |
| 67 |
self.patcher.stop() |
| 68 |
|
| 69 |
def test_healthy_cluster(self): |
| 70 |
self.monitor.poll_health_status() |
| 71 |
|
| 72 |
self.assertEqual( |
| 73 |
self.monitor.data["health_status"], |
| 74 |
m_fields.ClusterHealthStatus.HEALTHY, |
| 75 |
) |
| 76 |
self.assertEqual( |
| 77 |
self.monitor.data["health_status_reason"], |
| 78 |
{ |
| 79 |
"cluster": "Ready", |
| 80 |
"controlplane": "Ready", |
| 81 |
"infrastructure": "Ready", |
| 82 |
"nodegroup": "Ready", |
| 83 |
}, |
| 84 |
) |
| 85 |
|
| 86 |
def test_cluster_absent(self): |
| 87 |
self.mock_k8s.get_capi_cluster.return_value = None |
| 88 |
self.monitor.poll_health_status() |
| 89 |
|
| 90 |
self.assertEqual( |
| 91 |
self.monitor.data["health_status"], |
| 92 |
m_fields.ClusterHealthStatus.UNHEALTHY, |
| 93 |
) |
| 94 |
self.assertEqual( |
| 95 |
self.monitor.data["health_status_reason"], |
| 96 |
{ |
| 97 |
"cluster": "Cluster resource not found.", |
| 98 |
"controlplane": "Ready", |
| 99 |
"infrastructure": "Ready", |
| 100 |
"nodegroup": "Ready", |
| 101 |
}, |
| 102 |
) |
| 103 |
|
| 104 |
def test_cluster_unhealthy(self): |
| 105 |
cluster_state = { |
| 106 |
"status": { |
| 107 |
"conditions": [ |
| 108 |
{"type": "Ready", "status": "False"}, |
| 109 |
{"type": "ControlPlaneReady", "status": "False"}, |
| 110 |
] |
| 111 |
} |
| 112 |
} |
| 113 |
self.mock_k8s.get_capi_cluster.return_value = cluster_state |
| 114 |
self.monitor.poll_health_status() |
| 115 |
|
| 116 |
self.assertEqual( |
| 117 |
self.monitor.data["health_status"], |
| 118 |
m_fields.ClusterHealthStatus.UNHEALTHY, |
| 119 |
) |
| 120 |
self.assertEqual( |
| 121 |
self.monitor.data["health_status_reason"], |
| 122 |
{ |
| 123 |
"cluster": "Waiting on ['Ready', 'ControlPlaneReady']", |
| 124 |
"controlplane": "Ready", |
| 125 |
"infrastructure": "Ready", |
| 126 |
"nodegroup": "Ready", |
| 127 |
}, |
| 128 |
) |
| 129 |
|
| 130 |
def test_infra_unhealthy(self): |
| 131 |
infra_state = { |
| 132 |
"status": { |
| 133 |
"ready": False, |
| 134 |
} |
| 135 |
} |
| 136 |
self.mock_k8s.get_capi_openstackcluster.return_value = infra_state |
| 137 |
self.monitor.poll_health_status() |
| 138 |
|
| 139 |
self.assertEqual( |
| 140 |
self.monitor.data["health_status"], |
| 141 |
m_fields.ClusterHealthStatus.UNHEALTHY, |
| 142 |
) |
| 143 |
self.assertEqual( |
| 144 |
self.monitor.data["health_status_reason"], |
| 145 |
{ |
| 146 |
"cluster": "Ready", |
| 147 |
"controlplane": "Ready", |
| 148 |
"infrastructure": "Infrastructure not ready.", |
| 149 |
"nodegroup": "Ready", |
| 150 |
}, |
| 151 |
) |
| 152 |
|
| 153 |
def test_infra_has_failure_message(self): |
| 154 |
infra_state = { |
| 155 |
"status": { |
| 156 |
"ready": True, |
| 157 |
"failureMessage": "abc", |
| 158 |
"failureReason": "123", |
| 159 |
}, |
| 160 |
} |
| 161 |
self.mock_k8s.get_capi_openstackcluster.return_value = infra_state |
| 162 |
self.monitor.poll_health_status() |
| 163 |
|
| 164 |
self.assertEqual( |
| 165 |
self.monitor.data["health_status"], |
| 166 |
m_fields.ClusterHealthStatus.UNHEALTHY, |
| 167 |
) |
| 168 |
self.assertEqual( |
| 169 |
self.monitor.data["health_status_reason"], |
| 170 |
{ |
| 171 |
"cluster": "Ready", |
| 172 |
"controlplane": "Ready", |
| 173 |
"infrastructure": "123: abc", |
| 174 |
"nodegroup": "Ready", |
| 175 |
}, |
| 176 |
) |
| 177 |
|
| 178 |
def test_controlplane_has_conditions(self): |
| 179 |
cp_state = { |
| 180 |
"status": { |
| 181 |
"conditions": [ |
| 182 |
{"type": "Ready", "status": "False"}, |
| 183 |
{"type": "Available", "status": "False"}, |
| 184 |
{"type": "CertificatesAvailable", "status": "False"}, |
| 185 |
{ |
| 186 |
"type": "ControlPlaneComponentsHealthy", |
| 187 |
"status": "False", |
| 188 |
}, |
| 189 |
{"type": "EtcdClusterHealthy", "status": "False"}, |
| 190 |
{"type": "MachinesCreated", "status": "False"}, |
| 191 |
{"type": "MachinesReady", "status": "False"}, |
| 192 |
{"type": "Resized", "status": "False"}, |
| 193 |
] |
| 194 |
} |
| 195 |
} |
| 196 |
self.mock_k8s.get_kubeadm_control_plane.return_value = cp_state |
| 197 |
self.monitor.poll_health_status() |
| 198 |
|
| 199 |
self.assertEqual( |
| 200 |
self.monitor.data["health_status"], |
| 201 |
m_fields.ClusterHealthStatus.UNHEALTHY, |
| 202 |
) |
| 203 |
cp_status = ( |
| 204 |
"Waiting on ['Ready', 'Available', " |
| 205 |
"'CertificatesAvailable', " |
| 206 |
"'ControlPlaneComponentsHealthy', 'EtcdClusterHealthy', " |
| 207 |
"'MachinesCreated', 'MachinesReady', 'Resized']" |
| 208 |
) |
| 209 |
self.assertEqual( |
| 210 |
self.monitor.data["health_status_reason"], |
| 211 |
{ |
| 212 |
"cluster": "Ready", |
| 213 |
"controlplane": cp_status, |
| 214 |
"infrastructure": "Ready", |
| 215 |
"nodegroup": "Ready", |
| 216 |
}, |
| 217 |
) |
| 218 |
|
| 219 |
def test_nodegroup_has_conditions(self): |
| 220 |
ng_state = { |
| 221 |
"status": { |
| 222 |
"conditions": [ |
| 223 |
{"type": "Ready", "status": "True"}, |
| 224 |
{"type": "Available", "status": "False"}, |
| 225 |
] |
| 226 |
} |
| 227 |
} |
| 228 |
self.mock_k8s.get_machine_deployment.return_value = ng_state |
| 229 |
|
| 230 |
self.monitor.poll_health_status() |
| 231 |
|
| 232 |
self.assertEqual( |
| 233 |
self.monitor.data["health_status"], |
| 234 |
m_fields.ClusterHealthStatus.UNHEALTHY, |
| 235 |
) |
| 236 |
self.assertEqual( |
| 237 |
self.monitor.data["health_status_reason"], |
| 238 |
{ |
| 239 |
"cluster": "Ready", |
| 240 |
"controlplane": "Ready", |
| 241 |
"infrastructure": "Ready", |
| 242 |
"nodegroup": "test-worker waiting on ['Available']", |
| 243 |
}, |
| 244 |
) |
| 245 |
|
| 246 |
def test_machinedeployment_absent(self): |
| 247 |
self.mock_k8s.get_machine_deployment.return_value = None |
| 248 |
self.monitor.poll_health_status() |
| 249 |
|
| 250 |
self.assertEqual( |
| 251 |
self.monitor.data["health_status"], |
| 252 |
m_fields.ClusterHealthStatus.UNHEALTHY, |
| 253 |
) |
| 254 |
self.assertEqual( |
| 255 |
self.monitor.data["health_status_reason"], |
| 256 |
{ |
| 257 |
"cluster": "Ready", |
| 258 |
"controlplane": "Ready", |
| 259 |
"infrastructure": "Ready", |
| 260 |
"nodegroup": "test-worker resource not found.", |
| 261 |
}, |
| 262 |
) |
| 263 |
|
| 264 |
def test_all_missing(self): |
| 265 |
self.mock_k8s.get_capi_cluster.return_value = None |
| 266 |
self.mock_k8s.get_capi_openstackcluster.return_value = None |
| 267 |
self.mock_k8s.get_kubeadm_control_plane.return_value = None |
| 268 |
self.mock_k8s.get_machine_deployment.return_value = None |
| 269 |
self.monitor.poll_health_status() |
| 270 |
|
| 271 |
self.assertEqual( |
| 272 |
self.monitor.data["health_status"], |
| 273 |
m_fields.ClusterHealthStatus.UNHEALTHY, |
| 274 |
) |
| 275 |
self.assertEqual( |
| 276 |
self.monitor.data["health_status_reason"], |
| 277 |
{ |
| 278 |
"cluster": "Cluster resource not found.", |
| 279 |
"controlplane": "Control plane resource not found.", |
| 280 |
"infrastructure": "Infrastructure resource not found.", |
| 281 |
"nodegroup": "test-worker resource not found.", |
| 282 |
}, |
| 283 |
) |