diff --git a/tests/conftest.py b/tests/conftest.py index a5903ee699..606b512be9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -42,6 +42,19 @@ def hostname(host): return host.check_output('hostname') +@pytest.fixture(scope="module") +def nodename(host): + """Return the kubernetes node name of the `host` + + Node name need to be equal to the salt minion id so just retrieve the + salt minion id + """ + with host.sudo(): + return host.check_output( + 'salt-call --local --out txt grains.get id | cut -c 8-' + ) + + @pytest.fixture(scope="module") def kubeconfig_data(request, host): """Fixture to generate a kubeconfig file for remote usage.""" diff --git a/tests/install/steps/test_expansion.py b/tests/install/steps/test_expansion.py index 7afd284913..b555a37433 100644 --- a/tests/install/steps/test_expansion.py +++ b/tests/install/steps/test_expansion.py @@ -18,22 +18,20 @@ def test_cluster_expansion(host): # When {{{ -@when(parsers.parse('we declare a new "{node_type}" node on host "{hostname}"')) +@when(parsers.parse('we declare a new "{node_type}" node on host "{node_name}"')) def declare_node( - ssh_config, version, k8s_client, node_type, hostname, bootstrap_config + ssh_config, version, k8s_client, node_type, node_name, bootstrap_config ): """Declare the given node in Kubernetes.""" - node_ip = get_node_ip(hostname, ssh_config, bootstrap_config) - node_name = utils.resolve_hostname(hostname, ssh_config) + node_ip = get_node_ip(node_name, ssh_config, bootstrap_config) node_manifest = get_node_manifest( node_type, version, node_ip, node_name ) k8s_client.create_node(body=node_from_manifest(node_manifest)) -@when(parsers.parse('we deploy the node "{name}"')) -def deploy_node(host, ssh_config, version, name): - node_name = utils.resolve_hostname(name, ssh_config) +@when(parsers.parse('we deploy the node "{node_name}"')) +def deploy_node(host, ssh_config, version, node_name): accept_ssh_key = [ 'salt-ssh', '-i', node_name, 'test.ping', '--roster=kubernetes' ] @@ -50,21 +48,18 @@ def deploy_node(host, ssh_config, version, name): # }}} # Then {{{ -@then(parsers.parse('node "{hostname}" is registered in Kubernetes')) -def check_node_is_registered(ssh_config, k8s_client, hostname): +@then(parsers.parse('node "{node_name}" is registered in Kubernetes')) +def check_node_is_registered(k8s_client, node_name): """Check if the given node is registered in Kubernetes.""" - node_name = utils.resolve_hostname(hostname, ssh_config) try: k8s_client.read_node(node_name) except k8s.client.rest.ApiException as exn: pytest.fail(str(exn)) -@then(parsers.parse('node "{hostname}" status is "{expected_status}"')) -def check_node_status(ssh_config, k8s_client, hostname, expected_status): +@then(parsers.parse('node "{node_name}" status is "{expected_status}"')) +def check_node_status(k8s_client, node_name, expected_status): """Check if the given node has the expected status.""" - node_name = utils.resolve_hostname(hostname, ssh_config) - def _check_node_status(): try: status = k8s_client.read_node_status(node_name).status @@ -90,7 +85,6 @@ def _check_node_status(): @then(parsers.parse('node "{node_name}" is a member of etcd cluster')) def check_etcd_role(ssh_config, k8s_client, node_name): """Check if the given node is a member of the etcd cluster.""" - node_name = utils.resolve_hostname(node_name, ssh_config) etcd_member_list = etcdctl(k8s_client, ['member', 'list'], ssh_config) assert node_name in etcd_member_list, \ 'node {} is not part of the etcd cluster'.format(node_name) @@ -126,12 +120,12 @@ def kubectl_exec( ) return output -def get_node_ip(hostname, ssh_config, bootstrap_config): - """Return the IP of the node `hostname`. +def get_node_ip(node_name, ssh_config, bootstrap_config): + """Return the IP of the node `node_name`. We have to jump through hoops because `testinfra` does not provide a simple way to get this information… """ - infra_node = testinfra.get_host(hostname, ssh_config=ssh_config) + infra_node = testinfra.get_host(node_name, ssh_config=ssh_config) control_plane_cidr = bootstrap_config['networks']['controlPlane'] return utils.get_ip_from_cidr(infra_node, control_plane_cidr) @@ -154,7 +148,7 @@ def run_salt_command(host, command, ssh_config): """Run a command inside the salt-master container.""" pod = 'salt-master-{}'.format( - utils.resolve_hostname('bootstrap', ssh_config) + utils.get_node_name('bootstrap', ssh_config) ) output = kubectl_exec( @@ -174,8 +168,9 @@ def run_salt_command(host, command, ssh_config): def etcdctl(k8s_client, command, ssh_config): """Run an etcdctl command inside the etcd container.""" name = 'etcd-{}'.format( - utils.resolve_hostname('bootstrap', ssh_config) + utils.get_node_name('bootstrap', ssh_config) ) + etcd_command = [ 'etcdctl', '--endpoints', 'https://localhost:2379', diff --git a/tests/kube_utils.py b/tests/kube_utils.py index 5b64979741..7a3aef3406 100644 --- a/tests/kube_utils.py +++ b/tests/kube_utils.py @@ -20,7 +20,7 @@ def get_pods( field_selector.append('status.phase={}'.format(state)) if node: - nodename = utils.resolve_hostname(node, ssh_config) + nodename = utils.get_node_name(node, ssh_config) field_selector.append('spec.nodeName={}'.format(nodename)) kwargs = {} diff --git a/tests/post/steps/test_static_pods.py b/tests/post/steps/test_static_pods.py index 09452d0df9..4d571561d2 100644 --- a/tests/post/steps/test_static_pods.py +++ b/tests/post/steps/test_static_pods.py @@ -48,7 +48,7 @@ def test_static_pods_restart(host, transient_files): @given("I have set up a static pod", target_fixture="static_pod_id") def set_up_static_pod( - host, hostname, k8s_client, utils_image, transient_files + host, nodename, k8s_client, utils_image, transient_files ): manifest_path = str(MANIFESTS_PATH / "{}.yaml".format(DEFAULT_POD_NAME)) @@ -102,7 +102,7 @@ def set_up_static_pod( # See: https://github.com/kubernetes/kubernetes/issues/65825 transient_files.insert(0, manifest_path) - fullname = "{}-{}".format(DEFAULT_POD_NAME, hostname) + fullname = "{}-{}".format(DEFAULT_POD_NAME, nodename) utils.retry( kube_utils.check_pod_status(k8s_client, fullname), @@ -137,8 +137,8 @@ def manage_static_pod(host): @then("the static pod was changed") -def check_static_pod_changed(host, hostname, k8s_client, static_pod_id): - fullname = "{}-{}".format(DEFAULT_POD_NAME, hostname) +def check_static_pod_changed(host, nodename, k8s_client, static_pod_id): + fullname = "{}-{}".format(DEFAULT_POD_NAME, nodename) wait_for_pod = kube_utils.check_pod_status( k8s_client, diff --git a/tests/post/steps/test_volume.py b/tests/post/steps/test_volume.py index 0040cb0852..290bdc5a3c 100644 --- a/tests/post/steps/test_volume.py +++ b/tests/post/steps/test_volume.py @@ -545,8 +545,8 @@ def list(self): )['items'] def _create(self, body): - # Fixup the hostname. - body['spec']['nodeName'] = utils.resolve_hostname( + # Fixup the node name. + body['spec']['nodeName'] = utils.get_node_name( body['spec']['nodeName'], self._ssh_config ) self._client.create_cluster_custom_object( diff --git a/tests/utils.py b/tests/utils.py index 0787e5b373..f2d0e0eb89 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -52,11 +52,12 @@ def get_ip_from_cidr(host, cidr): return None -def resolve_hostname(nodename, ssh_config): - """Resolve a node name (from SSH config) to a real hostname.""" +def get_node_name(nodename, ssh_config=None): + """Get a node name (from SSH config).""" if ssh_config is not None: node = testinfra.get_host(nodename, ssh_config=ssh_config) - nodename = node.check_output('hostname') - else: - assert nodename == 'bootstrap' + with node.sudo(): + return node.check_output( + 'salt-call --local --out txt grains.get id | cut -c 8-' + ) return nodename