def test_init_with_config_and_pull_secrets(self): ps = [{"name": "yomama"}] name = "sofat" cfg = K8sConfig(kubeconfig=_utils.kubeconfig_fallback, pull_secret=ps) pod = _utils.create_pod(config=cfg, name=name) self.assertIsNotNone(pod.config) self.assertEqual(ps, pod.config.pull_secret)
def __init__(self, config=None, obj_type=None, name=None): super(K8sObject, self).__init__() if config is not None and not isinstance(config, K8sConfig): raise SyntaxError( "K8sObject: config: [ {0} ] must be of type K8sConfig.".format( config.__class__.__name__)) if config is None: config = K8sConfig() self.config = config if obj_type not in VALID_K8s_OBJS: valid = ", ".join(VALID_K8s_OBJS) raise InvalidObjectException( "K8sObject: obj_type: [ {0} ] must be in: [ {1} ]".format( obj_type, valid)) self.obj_type = obj_type self.model = str_to_class(obj_type) self.name = name try: urls = BaseUrls(api=self.config.version, namespace=self.config.namespace) self.base_url = urls.get_base_url(object_type=obj_type) except Exception as err: raise Exception( "Could not set BaseUrl for type: [ {0} ]".format(obj_type))
def test_get_by_labels_nonexistent(self): name = "yopod-{0}".format(str(uuid.uuid4())) config = K8sConfig(kubeconfig=_utils.kubeconfig_fallback) if _utils.is_reachable(config): pods = K8sPod.get_by_labels(config=config, labels={"name": name}) self.assertIsInstance(pods, list) self.assertEqual(0, len(pods))
def test_init_with_name_and_config(self): name = "yoname" nspace = "yomama" config = K8sConfig(kubeconfig=_utils.kubeconfig_fallback, namespace=nspace) secret = _utils.create_service_account(config=config, name=name) self.assertIsNotNone(secret) self.assertIsInstance(secret, K8sServiceAccount) self.assertEqual(secret.name, name) self.assertEqual(secret.config.namespace, nspace) self.assertEqual('ServiceAccount', secret.obj_type)
def test_init_with_name_and_config(self): nspace = "default" config = K8sConfig(kubeconfig=_utils.kubeconfig_fallback, namespace=nspace) name = "yo-name" c = _utils.create_component_status(config=config, name=name) self.assertIsNotNone(c) self.assertIsInstance(c, K8sComponentStatus) self.assertEqual(c.name, name) self.assertEqual('ComponentStatus', c.obj_type) self.assertIsInstance(c.config, K8sConfig)
def cleanup_objects(): config = K8sConfig(kubeconfig=kubeconfig_fallback) if is_reachable(config): cleanup_rc() cleanup_deployments() cleanup_rs() cleanup_pods() cleanup_pv() cleanup_pvc() cleanup_secrets() cleanup_services() cleanup_namespaces() cleanup_nodes()
def test_create_with_container(self): config = K8sConfig(kubeconfig=_utils.kubeconfig_fallback) pods = [] count = 3 if _utils.is_reachable(config): name = "yocontainer" container = _utils.create_container(name=name) for i in range(0, count): name = "yopod-{0}".format(str(uuid.uuid4())) pod = _utils.create_pod(config, name) pod.add_container(container) result = pod.create() self.assertIsInstance(result, K8sPod) self.assertEqual(pod, result) pods.append(pod) self.assertEqual(count, len(pods))
def assert_server_version(api_host=None, major=None, minor=None, type="exact"): try: if not api_host: return False if is_reachable(api_host): cfg = K8sConfig(api_host=api_host) obj = K8sObject(config=cfg, obj_type="Pod", name="temp") v = obj.server_version() if type == "exact": if int(v["major"]) != major or int(v["minor"]) != minor: msg = "Desired: [ {}.{} ]. Observed: [ {}.{} ].".format( major, minor, v["major"], v["minor"]) raise VersionMismatchException(msg) return True return False except VersionMismatchException: return False
def assert_server_version(api_host=None, major=None, minor=None, type='exact'): try: if not api_host: return False if is_reachable(api_host): cfg = K8sConfig(api_host=api_host) obj = K8sObject(config=cfg, obj_type='Pod', name='temp') v = obj.server_version() if type == 'exact': if int(v['major']) != major or int(v['minor']) != minor: msg = 'Desired: [ {}.{} ]. Observed: [ {}.{} ].'.format( major, minor, v['major'], v['minor']) raise VersionMismatchException(msg) return True return False except VersionMismatchException: return False
def test_list_multiple(self): name = "yocontainer" container = _utils.create_container(name=name) config = K8sConfig(kubeconfig=_utils.kubeconfig_fallback) deployments = [] count = 3 objs = [] if _utils.is_reachable(config): for i in range(0, count): name = "yodep-{0}".format(str(uuid.uuid4())) dep = _utils.create_deployment(config, name) dep.add_container(container) result = dep.create() self.assertIsInstance(result, K8sDeployment) self.assertEqual(dep, result) deployments.append(dep) objs = dep.list() self.assertEqual(count, len(deployments)) self.assertEqual(count, len(objs))
def create_config(): try: config = K8sConfig(kubeconfig=kubeconfig_fallback) except Exception: config = K8sConfig() return config