|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 |
| 3 | + |
| 4 | +# Copyright 2017-2019 The FIAAS Authors |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | + |
| 19 | +import mock |
| 20 | +import pytest |
| 21 | + |
| 22 | +from k8s.client import NotFound |
| 23 | +from k8s.models.common import ObjectMeta |
| 24 | +from k8s.models.policy_v1_pod_disruption_budget import PodDisruptionBudget, PodDisruptionBudgetSpec, LabelSelector |
| 25 | + |
| 26 | +NAME = "my-pod-disruption-budget" |
| 27 | +NAMESPACE = "my-namespace" |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.usefixtures("k8s_config") |
| 31 | +class TestPodDisruptionBudget(object): |
| 32 | + def test_created_if_not_exists(self, post, api_get): |
| 33 | + api_get.side_effect = NotFound() |
| 34 | + pdb = _create_default_pdb() |
| 35 | + call_params = pdb.as_dict() |
| 36 | + post.return_value.json.return_value = call_params |
| 37 | + |
| 38 | + assert pdb._new |
| 39 | + pdb.save() |
| 40 | + assert not pdb._new |
| 41 | + |
| 42 | + pytest.helpers.assert_any_call(post, _uri(NAMESPACE), call_params) |
| 43 | + |
| 44 | + def test_updated_if_exists(self, get, put): |
| 45 | + mock_response = _create_mock_response() |
| 46 | + get.return_value = mock_response |
| 47 | + pdb = _create_default_pdb() |
| 48 | + |
| 49 | + from_api = PodDisruptionBudget.get_or_create( |
| 50 | + metadata=pdb.metadata, |
| 51 | + spec=pdb.spec, |
| 52 | + ) |
| 53 | + assert not from_api._new |
| 54 | + assert from_api.spec == pdb.spec |
| 55 | + |
| 56 | + def test_deleted(self, delete): |
| 57 | + PodDisruptionBudget.delete(NAME, namespace=NAMESPACE) |
| 58 | + pytest.helpers.assert_any_call(delete, _uri(NAMESPACE, NAME)) |
| 59 | + |
| 60 | + |
| 61 | +def _create_mock_response(): |
| 62 | + mock_response = mock.Mock() |
| 63 | + mock_response.json.return_value = { |
| 64 | + "apiVersion": "policy/v1", |
| 65 | + "kind": "PodDisruptionBudget", |
| 66 | + "metadata": { |
| 67 | + "creationTimestamp": "2017-09-08T13:37:00Z", |
| 68 | + "generation": 1, |
| 69 | + "labels": { |
| 70 | + "test": "true" |
| 71 | + }, |
| 72 | + "name": NAME, |
| 73 | + "namespace": NAMESPACE, |
| 74 | + "resourceVersion": "42", |
| 75 | + "selfLink": _uri(NAMESPACE, NAME), |
| 76 | + "uid": "d8f1ba26-b182-11e6-a364-fa163ea2a9c4" |
| 77 | + }, |
| 78 | + "spec": { |
| 79 | + "minAvailable": 1, |
| 80 | + "selector": { |
| 81 | + "matchLabels": { |
| 82 | + "application": "my-app", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | + return mock_response |
| 88 | + |
| 89 | + |
| 90 | +def _create_default_pdb(): |
| 91 | + object_meta = ObjectMeta(name=NAME, namespace=NAMESPACE, labels={"test": "true"}) |
| 92 | + selector = LabelSelector(matchLabels={"app": "my-app"}) |
| 93 | + spec = PodDisruptionBudgetSpec(maxUnavailable=1, selector=selector) |
| 94 | + return PodDisruptionBudget(metadata=object_meta, spec=spec) |
| 95 | + |
| 96 | + |
| 97 | +def _uri(namespace, name=""): |
| 98 | + uri = "/apis/policy/v1/namespaces/{namespace}/poddisruptionbudgets/{name}" |
| 99 | + return uri.format(name=name, namespace=namespace) |
0 commit comments