|
| 1 | +# Copyright 2026 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +from kubernetes.client import ApiClient |
| 18 | +from kubernetes.client.models import V1DeploymentSpec |
| 19 | + |
| 20 | + |
| 21 | +class DeploymentSpecTest(unittest.TestCase): |
| 22 | + |
| 23 | + def test_round_trip_preserves_nested_fields(self): |
| 24 | + spec_dict = { |
| 25 | + 'selector': {'matchLabels': {'name': 'my-operator'}}, |
| 26 | + 'template': {'spec': { |
| 27 | + 'affinity': {'nodeAffinity': { |
| 28 | + 'requiredDuringSchedulingIgnoredDuringExecution': { |
| 29 | + 'nodeSelectorTerms': [{'matchExpressions': [{ |
| 30 | + 'key': 'kubernetes.io/arch', |
| 31 | + 'operator': 'In', |
| 32 | + 'values': ['s390x', 'amd64'], |
| 33 | + }]}], |
| 34 | + }, |
| 35 | + }}, |
| 36 | + 'containers': [{ |
| 37 | + 'name': 'op', |
| 38 | + 'image': 'img:1', |
| 39 | + 'imagePullPolicy': 'Always', |
| 40 | + 'env': [{ |
| 41 | + 'name': 'WATCH_NAMESPACE', |
| 42 | + 'valueFrom': { |
| 43 | + 'fieldRef': { |
| 44 | + 'fieldPath': "metadata.annotations['olm.targetNamespaces']", |
| 45 | + }, |
| 46 | + }, |
| 47 | + }], |
| 48 | + 'livenessProbe': { |
| 49 | + 'httpGet': {'path': '/healthz', 'port': 8081}, |
| 50 | + 'initialDelaySeconds': 15, |
| 51 | + 'failureThreshold': 4, |
| 52 | + }, |
| 53 | + }], |
| 54 | + }}, |
| 55 | + } |
| 56 | + expected = { |
| 57 | + **spec_dict, |
| 58 | + 'template': { |
| 59 | + **spec_dict['template'], |
| 60 | + 'spec': { |
| 61 | + **spec_dict['template']['spec'], |
| 62 | + 'containers': [{ |
| 63 | + **spec_dict['template']['spec']['containers'][0], |
| 64 | + 'image': 'img:2', |
| 65 | + }], |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + model = V1DeploymentSpec.from_dict(spec_dict) |
| 71 | + model.template.spec.containers[0].image = 'img:2' |
| 72 | + model = V1DeploymentSpec.from_dict(model.to_dict()) |
| 73 | + |
| 74 | + self.assertEqual( |
| 75 | + ApiClient().sanitize_for_serialization(model), |
| 76 | + expected, |
| 77 | + ) |
0 commit comments