Skip to content

Commit

Permalink
Merge branch 'w/2.4/improvement/no-longer-use-hostname-in-tests' into…
Browse files Browse the repository at this point in the history
… tmp/octopus/q/2.4
  • Loading branch information
bert-e committed Jan 21, 2020
2 parents 144f877 + 9b2b0df commit 45e8ab3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 32 deletions.
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
35 changes: 15 additions & 20 deletions tests/install/steps/test_expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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(
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion tests/kube_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
8 changes: 4 additions & 4 deletions tests/post/steps/test_static_pods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/post/steps/test_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 6 additions & 5 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 45e8ab3

Please sign in to comment.