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",
49 "load_balancer": Dict(
52 Dict({"hostname": None, "ip": "192.168.0.201"})
66 def list_service_for_all_namespaces(self
, **kwargs
):
67 return fake_list_services
70 class ProvisionerTest(TestCase
):
71 @mock.patch("n2vc.kubectl.config.load_kube_config")
72 @mock.patch("n2vc.kubectl.client.CoreV1Api")
73 def setUp(self
, mock_core
, mock_config
):
74 mock_core
.return_value
= mock
.MagicMock()
75 mock_config
.return_value
= mock
.MagicMock()
76 self
.kubectl
= Kubectl()
78 @mock.patch("n2vc.kubectl.client.CoreV1Api")
79 def test_get_service(self
, mock_corev1api
):
80 mock_corev1api
.return_value
= FakeCoreV1Api()
81 services
= self
.kubectl
.get_services(
82 field_selector
="metadata.namespace", label_selector
="juju-operator=squid"
84 keys
= ["name", "cluster_ip", "type", "ports", "external_ip"]
85 self
.assertTrue(k
in service
for service
in services
for k
in keys
)
87 @mock.patch("n2vc.kubectl.client.CoreV1Api.list_service_for_all_namespaces")
88 def test_get_service_exception(self
, list_services
):
89 list_services
.side_effect
= ApiException()
90 with self
.assertRaises(ApiException
):
91 self
.kubectl
.get_services()