def test_node_affinity_with_required(self): model = _constants.pod_with_node_affinity() pod = Pod(model) config = _utils.create_config() k8s = K8sPod(config=config, name="yo") k8s.model = pod if _utils.is_reachable(config): # ensure we have enough nodes nodes = K8sNode(config=config, name="yo").list() self.assertGreaterEqual(len(nodes), 3) # tag the nodes for i in range(len(nodes)): node = nodes[i] if i == 0: node.labels.update( {'kubernetes.io/e2e-az-name': 'e2e-az1'}) if i == 1: node.labels.update( {'kubernetes.io/e2e-az-name': 'e2e-az2'}) if i == 2: node.labels.update( {'kubernetes.io/e2e-az-name': 'e2e-az3'}) node.update() nodes = K8sNode(config=config, name="yo").list() for node in nodes: self.assertIn('kubernetes.io/e2e-az-name', node.labels) # create the pod k8s.create() # untag the nodes for node in nodes: node.labels.pop('kubernetes.io/e2e-az-name', None) node.update() pass
def create_node(config=None, name=None): if config is None: config = create_config() obj = K8sNode( config=config, name=name ) return obj
def test_pod_affinity_no_good_nodes(self): model = _constants.pod_with_pod_affinity() pod = Pod(model) config = _utils.create_config() k8s = K8sPod(config=config, name="yo") k8s.model = pod if _utils.is_reachable(config): nodes = K8sNode(config=config, name="yo").list() # untag all nodes for node in nodes: node.labels.pop('kubernetes.io/e2e-az-name', None) node.update() with self.assertRaises(TimedOutException): # timeout because of required k8s.create()
def test_pod_affinities(self): config = _utils.create_config() container = _utils.create_container(name="nginx", image="nginx:latest") pod_s1 = _utils.create_pod(config=config, name="s1") pod_s1.labels.update({'security': 'S1'}) pod_s1.node_selector = {'kubernetes.io/e2e-az-name': 'e2e-az1'} pod_s1.add_container(container) pod_s2 = _utils.create_pod(config=config, name="s2") pod_s2.labels.update({'security': 'S2'}) pod_s2.node_selector = {'kubernetes.io/e2e-az-name': 'e2e-az2'} pod_s2.add_container(container) model = _constants.pod_with_pod_affinity() pod = Pod(model) config = _utils.create_config() k8s = K8sPod(config=config, name="yo") k8s.model = pod if _utils.is_reachable(config): # ensure we have enough nodes nodes = K8sNode(config=config, name="yo").list() if len(nodes) < 3: self.skipTest(reason='Not enough nodes to perform test.') else: self.assertGreaterEqual(len(nodes), 3) # tag the nodes for i in range(len(nodes)): node = nodes[i] if i == 0: node.labels.update( {'kubernetes.io/e2e-az-name': 'e2e-az1'}) if i == 1: node.labels.update( {'kubernetes.io/e2e-az-name': 'e2e-az2'}) if i == 2: node.labels.update( {'kubernetes.io/e2e-az-name': 'e2e-az3'}) node.update() # create the pods for affinity lookup pod_s1.create() pod_s2.create() # create the pod with affinities k8s.create() pass
def cleanup_nodes(): ref = create_node(name="throwaway") if is_reachable(ref.config.api_host): node_pattern = re.compile(r'yo-') _list = ref.list() _filtered = filter(lambda x: node_pattern.match(x['metadata']['name']) is not None, _list) while len(_filtered) > 1: for p in _filtered: try: assert isinstance(p, dict) node_name = p['metadata']['name'] n = K8sNode(config=ref.config, name=node_name).get() n.delete() except NotFoundException: continue _list = ref.list() _filtered = filter(lambda x: node_pattern.match(x['metadata']['name']) is not None, _list)
def test_tolerations_timeout(self): config = _utils.create_config() container = _utils.create_container(name="nginx", image="nginx:latest") pod = _utils.create_pod(config=config, name="yo") pod.add_container(container) key = "key" value = "value" effect = "NoSchedule" if _utils.is_reachable(config): nodes = K8sNode(config=config, name="yo").list() for node in nodes: node.taint(key=key, value=value, effect=effect) with self.assertRaises(TimedOutException): pod.create() for node in nodes: node.untaint(key=key, value=value)
def test_tolerations_noschedule(self): config = _utils.create_config() container = _utils.create_container(name="nginx", image="nginx:latest") pod = _utils.create_pod(config=config, name="yo") pod.add_container(container) key = "key" value = "value" effect = "NoSchedule" if _utils.is_reachable(config): nodes = K8sNode(config=config, name="yo").list() for node in nodes: node.taint(key=key, value=value, effect=effect) pod.add_toleration(key=key, value=value, effect=effect) pod.create() self.assertEqual(3, len(pod.tolerations)) for node in nodes: node.untaint(key=key, value=value)