diff --git a/fuel_ccp_tests/helpers/post_install_k8s_checks.py b/fuel_ccp_tests/helpers/post_install_k8s_checks.py index 351984c..aa9e0a4 100644 --- a/fuel_ccp_tests/helpers/post_install_k8s_checks.py +++ b/fuel_ccp_tests/helpers/post_install_k8s_checks.py @@ -12,8 +12,15 @@ # License for the specific language governing permissions and limitations # under the License. +import re + from devops.helpers import helpers +from fuel_ccp_tests import logger + + +LOG = logger.logger + def check_calico_network(remote, k8sclient): dns_pod = [ @@ -31,3 +38,26 @@ def check_calico_network(remote, k8sclient): calico_options = remote.execute( 'calicoctl pool show --ipv4')['stdout'][3].split('|')[2].strip() assert calico_options == options + + +def required_images_exists(node_name, underlay, required_images): + """Check if there are all base containers on node + + :param node_name: string + :param underlay: fuel_ccp_tests.managers.UnderlaySSHManager + :param required_images: list + """ + cmd = "docker ps --no-trunc --format '{{.Image}}'" + result = underlay.sudo_check_call(cmd, node_name=node_name) + images = set([x.strip() for x in result['stdout']]) + LOG.debug('Containers on node "{0}" use images: ' + '{1}'.format(node_name, images)) + # Image name could contain unpredictable Docker registry name + # (host:port), e.g. example.net:5000/hyperkube-amd64:v1.4.1 + # Use regex to check that image (base name) is used by some container + assert all( + any(re.match('^([\w.-]+(:\d+)?/)?' # Host:port (optional) + '{0}:\S+$' # image name + ":" + image tag + .format(required_image), image) + for image in images) + for required_image in required_images) diff --git a/fuel_ccp_tests/tests/system/base_test.py b/fuel_ccp_tests/tests/system/base_test.py index 1e93dee..75fb82d 100644 --- a/fuel_ccp_tests/tests/system/base_test.py +++ b/fuel_ccp_tests/tests/system/base_test.py @@ -13,6 +13,8 @@ # under the License. from fuel_ccp_tests import logger +from fuel_ccp_tests.helpers import post_install_k8s_checks + LOG = logger.logger LOG.addHandler(logger.console) @@ -30,19 +32,6 @@ class SystemBaseTest(object): for node_name in underlay.node_names(): underlay.sudo_check_call(cmd, node_name=node_name) - def required_images_exists(self, node_name, underlay, required_images): - """Check if there are all base containers on node - - :param node_name: string - :param underlay: fuel_ccp_tests.managers.UnderlaySSHManager - :param required_images: list - """ - cmd = "docker ps --no-trunc --format '{{.Image}}'" - result = underlay.sudo_check_call(cmd, node_name=node_name) - images = [x.split(":")[0] for x in result['stdout']] - assert set(required_images) < set(images),\ - "Running containers check failed on node '{}'".format(node_name) - def check_list_required_images(self, underlay, required_images): """Check running containers on each node @@ -51,7 +40,9 @@ class SystemBaseTest(object): """ LOG.info("Check that required containers exist") for node_name in underlay.node_names(): - self.required_images_exists(node_name, underlay, required_images) + post_install_k8s_checks.required_images_exists(node_name, + underlay, + required_images) def check_number_kube_nodes(self, underlay, k8sclient): """Check number of slaves""" diff --git a/fuel_ccp_tests/tests/unit/test_system_funcs.py b/fuel_ccp_tests/tests/unit/test_system_funcs.py new file mode 100644 index 0000000..ba83fe8 --- /dev/null +++ b/fuel_ccp_tests/tests/unit/test_system_funcs.py @@ -0,0 +1,56 @@ +# Copyright 2016 Mirantis, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import pytest + +from fuel_ccp_tests.helpers import post_install_k8s_checks as funcs + +test_images1 = [ + "artifactory.example.net:5000/hyperkube-amd64:v1.4.1-test_100", + "andyshinn/dnsmasq:2.72", + "artifactory.example.net:5001/calico/node:v0.20.0-mcp-7b31adc", + "artifactory.example.net:5001/calico/ctl:v0.20.0-mcp-7b31adc", + "artifactory.example.net:5000/hyperkube-amd64:v1.4.1-test_100", +] + +test_images2 = [ + "andyshinn/dnsmasq:2.72", + "gcr.io/google_containers/pause-amd64:3.0", + "quay.io/coreos/etcd:v3.0.1", +] + +required_images = [ + "andyshinn/dnsmasq", + "calico/node", + "hyperkube-amd64", +] + + +class MockUnderlay(object): + def __init__(self, images): + self.images = images + + def sudo_check_call(self, *args, **kwargs): + return {'stdout': self.images} + + +@pytest.mark.unit_tests +def test_required_images_exists(): + funcs.required_images_exists(node_name='master', + underlay=MockUnderlay(test_images1), + required_images=required_images) + with pytest.raises(AssertionError): + funcs.required_images_exists(node_name='master', + underlay=MockUnderlay(test_images2), + required_images=required_images)