|
| 1 | +import pytest |
| 2 | + |
| 3 | +from commodore import k8sobject |
| 4 | + |
| 5 | +_test_objs = [ |
| 6 | + { |
| 7 | + "apiVersion": "v1", |
| 8 | + "kind": "ServiceAccount", |
| 9 | + "metadata": { |
| 10 | + "name": "test", |
| 11 | + "namespace": "test", |
| 12 | + }, |
| 13 | + }, |
| 14 | + { |
| 15 | + "apiVersion": "v1", |
| 16 | + "kind": "ServiceAccount", |
| 17 | + "metadata": { |
| 18 | + "name": "test-sa-2", |
| 19 | + "namespace": "test", |
| 20 | + }, |
| 21 | + }, |
| 22 | + { |
| 23 | + "apiVersion": "v1", |
| 24 | + "kind": "Pod", |
| 25 | + "metadata": { |
| 26 | + "name": "test", |
| 27 | + "namespace": "test", |
| 28 | + "labels": { |
| 29 | + "name": "test", |
| 30 | + }, |
| 31 | + }, |
| 32 | + "spec": { |
| 33 | + "image": "image", |
| 34 | + "command": "pause", |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + "apiVersion": "rbac.authorization.k8s.io/v1", |
| 39 | + "kind": "Role", |
| 40 | + "metadata": { |
| 41 | + "name": "test-role", |
| 42 | + "namespace": "test", |
| 43 | + }, |
| 44 | + }, |
| 45 | + { |
| 46 | + "apiVersion": "rbac.authorization.k8s.io/v1", |
| 47 | + "kind": "Role", |
| 48 | + "metadata": { |
| 49 | + "name": "test-role", |
| 50 | + "namespace": "test-2", |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + "apiVersion": "rbac.authorization.k8s.io/v1", |
| 55 | + "kind": "ClusterRole", |
| 56 | + "metadata": { |
| 57 | + "name": "test-cr", |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + "apiVersion": "rbac.authorization.k8s.io/v1", |
| 62 | + "kind": "ClusterRole", |
| 63 | + "metadata": { |
| 64 | + "name": "test-cr-2", |
| 65 | + }, |
| 66 | + }, |
| 67 | + { |
| 68 | + "test": "testing", |
| 69 | + }, |
| 70 | +] |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize( |
| 74 | + "k8sdict,expected", |
| 75 | + zip( |
| 76 | + [None] + _test_objs, |
| 77 | + [ |
| 78 | + { |
| 79 | + "kind": "", |
| 80 | + "name": "", |
| 81 | + "namespace": "", |
| 82 | + }, |
| 83 | + { |
| 84 | + "kind": "ServiceAccount", |
| 85 | + "name": "test", |
| 86 | + "namespace": "test", |
| 87 | + }, |
| 88 | + { |
| 89 | + "kind": "ServiceAccount", |
| 90 | + "name": "test-sa-2", |
| 91 | + "namespace": "test", |
| 92 | + }, |
| 93 | + { |
| 94 | + "kind": "Pod", |
| 95 | + "name": "test", |
| 96 | + "namespace": "test", |
| 97 | + }, |
| 98 | + { |
| 99 | + "kind": "Role", |
| 100 | + "name": "test-role", |
| 101 | + "namespace": "test", |
| 102 | + "spec": { |
| 103 | + "test": "testing", |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + "kind": "Role", |
| 108 | + "name": "test-role", |
| 109 | + "namespace": "test-2", |
| 110 | + "spec": { |
| 111 | + "test": "testing2", |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + "kind": "ClusterRole", |
| 116 | + "namespace": "", |
| 117 | + "name": "test-cr", |
| 118 | + }, |
| 119 | + { |
| 120 | + "kind": "ClusterRole", |
| 121 | + "namespace": "", |
| 122 | + "name": "test-cr-2", |
| 123 | + }, |
| 124 | + { |
| 125 | + "name": "", |
| 126 | + "namespace": "", |
| 127 | + "kind": "", |
| 128 | + }, |
| 129 | + ], |
| 130 | + ), |
| 131 | +) |
| 132 | +def test_k8sobject_constructor(k8sdict, expected): |
| 133 | + o = k8sobject.K8sObject(k8sdict) |
| 134 | + assert expected["kind"] == o._kind |
| 135 | + assert expected["name"] == o._name |
| 136 | + assert expected["namespace"] == o._namespace |
| 137 | + |
| 138 | + |
| 139 | +_cluster_scoped_obj = k8sobject.K8sObject( |
| 140 | + { |
| 141 | + "apiVersion": "v1", |
| 142 | + "kind": "Namespace", |
| 143 | + "metadata": { |
| 144 | + "name": "test", |
| 145 | + "labels": { |
| 146 | + "name": "test", |
| 147 | + }, |
| 148 | + }, |
| 149 | + } |
| 150 | +) |
| 151 | +_ns_scoped_obj = k8sobject.K8sObject( |
| 152 | + { |
| 153 | + "apiVersion": "v1", |
| 154 | + "kind": "ServiceAccount", |
| 155 | + "metadata": { |
| 156 | + "name": "test", |
| 157 | + "labels": { |
| 158 | + "name": "test", |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | +) |
| 163 | + |
| 164 | + |
| 165 | +@pytest.mark.parametrize( |
| 166 | + "k8sdict,to_cluster_scoped,to_ns_scoped", |
| 167 | + zip( |
| 168 | + _test_objs, |
| 169 | + [False, False, False, False, False, True, True, True], |
| 170 | + [False, False, True, True, True, True, True, True], |
| 171 | + ), |
| 172 | +) |
| 173 | +def test_k8sobject_less_than(k8sdict, to_cluster_scoped, to_ns_scoped): |
| 174 | + o = k8sobject.K8sObject(k8sdict) |
| 175 | + assert (o < _cluster_scoped_obj) == to_cluster_scoped |
| 176 | + assert (o < _ns_scoped_obj) == to_ns_scoped |
| 177 | + assert (o > _cluster_scoped_obj) == (not to_cluster_scoped) |
| 178 | + assert (o > _ns_scoped_obj) == (not to_ns_scoped) |
| 179 | + |
| 180 | + |
| 181 | +@pytest.mark.parametrize("k8sdict_a", _test_objs) |
| 182 | +@pytest.mark.parametrize("k8sdict_b", _test_objs) |
| 183 | +def test_k8sobject_equal(k8sdict_a, k8sdict_b): |
| 184 | + a = k8sobject.K8sObject(k8sdict_a) |
| 185 | + b = k8sobject.K8sObject(k8sdict_b) |
| 186 | + expect = False |
| 187 | + if ( |
| 188 | + k8sdict_a.get("kind", "") == k8sdict_b.get("kind", "") |
| 189 | + and k8sdict_a.get("metadata", {}).get("namespace", "") |
| 190 | + == k8sdict_b.get("metadata", {}).get("namespace", "") |
| 191 | + and k8sdict_a.get("metadata", {}).get("name", "") |
| 192 | + == k8sdict_b.get("metadata", {}).get("name", "") |
| 193 | + ): |
| 194 | + expect = True |
| 195 | + assert (a == b) == expect |
0 commit comments