1 # Copyright 2020 Canonical Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from unittest
import TestCase
, mock
16 from n2vc
.kubectl
import Kubectl
17 from n2vc
.utils
import Dict
18 from kubernetes
.client
.rest
import ApiException
20 fake_list_services
= Dict(
29 "labels": {"juju-app": "squid"},
34 "cluster_ip": "10.152.183.79",
35 "type": "LoadBalancer",
51 "load_balancer": Dict(
54 Dict({"hostname": None, "ip": "192.168.0.201"})
68 def list_service_for_all_namespaces(self
, **kwargs
):
69 return fake_list_services
72 class ProvisionerTest(TestCase
):
73 @mock.patch("n2vc.kubectl.config.load_kube_config")
74 @mock.patch("n2vc.kubectl.client.CoreV1Api")
75 def setUp(self
, mock_core
, mock_config
):
76 mock_core
.return_value
= mock
.MagicMock()
77 mock_config
.return_value
= mock
.MagicMock()
78 self
.kubectl
= Kubectl()
80 @mock.patch("n2vc.kubectl.client.CoreV1Api")
81 def test_get_service(self
, mock_corev1api
):
82 mock_corev1api
.return_value
= FakeCoreV1Api()
83 services
= self
.kubectl
.get_services(
84 field_selector
="metadata.namespace", label_selector
="juju-operator=squid"
86 keys
= ["name", "cluster_ip", "type", "ports", "external_ip"]
87 self
.assertTrue(k
in service
for service
in services
for k
in keys
)
89 @mock.patch("n2vc.kubectl.client.CoreV1Api.list_service_for_all_namespaces")
90 def test_get_service_exception(self
, list_services
):
91 list_services
.side_effect
= ApiException()
92 with self
.assertRaises(ApiException
):
93 self
.kubectl
.get_services()