Rename create_from_map to create_from_dict, add tests

This commit is contained in:
Oz Tiram 2019-03-26 23:30:25 +01:00
parent aa28bc7067
commit cee4e49eda
2 changed files with 37 additions and 3 deletions

View File

@ -14,6 +14,7 @@
import unittest
import yaml
from kubernetes import utils, client
from kubernetes.e2e_test import base
@ -42,6 +43,39 @@ class TestUtils(unittest.TestCase):
name="nginx-app", namespace="default",
body={})
def test_create_apps_deployment_from_yaml_string(self):
k8s_client = client.api_client.ApiClient(configuration=self.config)
with open(self.path_prefix + "apps-deployment.yaml") as f:
yaml_str = f.read()
utils.create_from_yaml(
k8s_client, yaml_str)
app_api = client.AppsV1beta1Api(k8s_client)
dep = app_api.read_namespaced_deployment(name="nginx-app",
namespace="default")
self.assertIsNotNone(dep)
app_api.delete_namespaced_deployment(
name="nginx-app", namespace="default",
body={})
def test_create_apps_deployment_from_yaml_obj(self):
k8s_client = client.api_client.ApiClient(configuration=self.config)
with open(self.path_prefix + "apps-deployment.yaml") as f:
yml_obj = yaml.safe_load(f)
utils.create_from_dict(
k8s_client, yml_obj)
app_api = client.AppsV1beta1Api(k8s_client)
dep = app_api.read_namespaced_deployment(name="nginx-app",
namespace="default")
self.assertIsNotNone(dep)
app_api.delete_namespaced_deployment(
name="nginx-app", namespace="default",
body={})
def test_create_extensions_deployment_from_yaml(self):
"""
Should be able to create an extensions/v1beta1 deployment.

View File

@ -30,7 +30,7 @@ def create_from_yaml(
Perform an action from a yaml file. Pass True for verbose to
print confirmation information.
Input:
yaml_file: string. Contains yaml string or a path to yaml file.
yaml_file: string. Contains yaml string or a path to yaml file.
k8s_client: an ApiClient object, initialized with the client args.
verbose: If True, print confirmation from the create action.
Default is False.
@ -61,11 +61,11 @@ def create_from_yaml(
yml_document_all = yaml.safe_load_all(yaml_file)
# Load all documents from a single YAML file
for yml_document in yml_document_all:
create_from_map(k8s_client, yml_document, verbose,
create_from_dict(k8s_client, yml_document, verbose,
**kwargs)
def create_from_map(k8s_client, yml_document, verbose=False, **kwargs):
def create_from_dict(k8s_client, yml_document, verbose=False, **kwargs):
# If it is a list type, will need to iterate its items
api_exceptions = []