move plugins from from ccsdk to dcaegen2
[dcaegen2/platform/plugins.git] / k8s / tests / conftest.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2018-2020 AT&T Intellectual Property. All rights reserved.
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 # ============LICENSE_END=========================================================
18 #
19
20 import pytest
21
22 @pytest.fixture()
23 def mockconfig(monkeypatch):
24     from configure import configure
25     """ Override the regular configure() routine that reads a file and calls Consul"""
26     def altconfig():
27       config = configure._set_defaults()
28       config["consul_host"] = config["consul_dns_name"]
29       return config
30     monkeypatch.setattr(configure, 'configure', altconfig)
31
32 @pytest.fixture()
33 def mockk8sapi(monkeypatch):
34     import k8sclient.k8sclient
35     from kubernetes import client
36
37     # We need to patch the kubernetes 'client' module
38     # Awkward because of the way it requires a function call
39     # to get an API object
40     core = client.CoreV1Api()
41     ext = client.ExtensionsV1beta1Api()
42
43     def pseudo_deploy(namespace, dep):
44         return dep
45
46     def pseudo_service(namespace, svc):
47         return svc
48
49     # patched_core returns a CoreV1Api object with the
50     # create_namespaced_service method stubbed out so that there
51     # is no attempt to call the k8s API server
52     def patched_core():
53         monkeypatch.setattr(core, "create_namespaced_service", pseudo_service)
54         return core
55
56     # patched_ext returns an ExtensionsV1beta1Api object with the
57     # create_namespaced_deployment method stubbed out so that there
58     # is no attempt to call the k8s API server
59     def patched_ext():
60         monkeypatch.setattr(ext,"create_namespaced_deployment", pseudo_deploy)
61         return ext
62
63     def pseudo_configure(loc):
64         pass
65
66     monkeypatch.setattr(k8sclient.k8sclient,"_configure_api", pseudo_configure)
67     monkeypatch.setattr(client, "CoreV1Api", patched_core)
68     monkeypatch.setattr(client,"ExtensionsV1beta1Api", patched_ext)