|
1 | 1 | import datetime
|
2 | 2 | import json
|
| 3 | +import re |
3 | 4 | from unittest.mock import MagicMock
|
4 | 5 |
|
5 | 6 | from kube_downscaler.scaler import DOWNTIME_REPLICAS_ANNOTATION
|
@@ -110,7 +111,83 @@ def get(url, version, **kwargs):
|
110 | 111 | default_uptime="never",
|
111 | 112 | default_downtime="always",
|
112 | 113 | include_resources=include_resources,
|
113 |
| - exclude_namespaces=["system-ns"], |
| 114 | + exclude_namespaces=[re.compile("system-ns")], |
| 115 | + exclude_deployments=[], |
| 116 | + dry_run=False, |
| 117 | + grace_period=300, |
| 118 | + downtime_replicas=0, |
| 119 | + ) |
| 120 | + |
| 121 | + assert api.patch.call_count == 1 |
| 122 | + |
| 123 | + # make sure that deploy-2 was updated (namespace of sysdep-1 was excluded) |
| 124 | + patch_data = { |
| 125 | + "metadata": { |
| 126 | + "name": "deploy-2", |
| 127 | + "namespace": "default", |
| 128 | + "creationTimestamp": "2019-03-01T16:38:00Z", |
| 129 | + "annotations": {ORIGINAL_REPLICAS_ANNOTATION: "2"}, |
| 130 | + }, |
| 131 | + "spec": {"replicas": 0}, |
| 132 | + } |
| 133 | + assert api.patch.call_args[1]["url"] == "/deployments/deploy-2" |
| 134 | + assert json.loads(api.patch.call_args[1]["data"]) == patch_data |
| 135 | + |
| 136 | + |
| 137 | +def test_scaler_namespace_excluded_regex(monkeypatch): |
| 138 | + api = MagicMock() |
| 139 | + monkeypatch.setattr( |
| 140 | + "kube_downscaler.scaler.helper.get_kube_api", MagicMock(return_value=api) |
| 141 | + ) |
| 142 | + |
| 143 | + def get(url, version, **kwargs): |
| 144 | + if url == "pods": |
| 145 | + data = {"items": []} |
| 146 | + elif url == "deployments": |
| 147 | + data = { |
| 148 | + "items": [ |
| 149 | + { |
| 150 | + "metadata": { |
| 151 | + "name": "sysdep-1", |
| 152 | + "namespace": "system-ns", |
| 153 | + "creationTimestamp": "2019-03-01T16:38:00Z", |
| 154 | + }, |
| 155 | + "spec": {"replicas": 1}, |
| 156 | + }, |
| 157 | + { |
| 158 | + "metadata": { |
| 159 | + "name": "deploy-2", |
| 160 | + "namespace": "default", |
| 161 | + "creationTimestamp": "2019-03-01T16:38:00Z", |
| 162 | + }, |
| 163 | + "spec": {"replicas": 2}, |
| 164 | + }, |
| 165 | + ] |
| 166 | + } |
| 167 | + elif url == "namespaces/default": |
| 168 | + data = {"metadata": {}} |
| 169 | + else: |
| 170 | + raise Exception(f"unexpected call: {url}, {version}, {kwargs}") |
| 171 | + |
| 172 | + response = MagicMock() |
| 173 | + response.json.return_value = data |
| 174 | + return response |
| 175 | + |
| 176 | + api.get = get |
| 177 | + |
| 178 | + include_resources = frozenset(["deployments"]) |
| 179 | + scale( |
| 180 | + namespace=None, |
| 181 | + upscale_period="never", |
| 182 | + downscale_period="never", |
| 183 | + default_uptime="never", |
| 184 | + default_downtime="always", |
| 185 | + include_resources=include_resources, |
| 186 | + exclude_namespaces=[ |
| 187 | + re.compile("foo.*"), |
| 188 | + re.compile("syst?em-.*"), |
| 189 | + re.compile("def"), |
| 190 | + ], |
114 | 191 | exclude_deployments=[],
|
115 | 192 | dry_run=False,
|
116 | 193 | grace_period=300,
|
|
0 commit comments