diff options
Diffstat (limited to 'collections-debian-merged/ansible_collections/community/kubevirt/tests/integration')
6 files changed, 365 insertions, 0 deletions
diff --git a/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/aliases b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/aliases new file mode 100644 index 00000000..765b70da --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/aliases @@ -0,0 +1 @@ +shippable/posix/group2 diff --git a/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/constraints.txt b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/constraints.txt new file mode 100644 index 00000000..c44f44e9 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/constraints.txt @@ -0,0 +1 @@ +setuptools < 45 ; python_version <= '2.7' # setuptools 45 and later require python 3.5 or later diff --git a/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/inventory_diff.py b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/inventory_diff.py new file mode 100755 index 00000000..df5a7661 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/inventory_diff.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import json +import sys + + +def check_hosts(contrib, plugin): + contrib_hosts = sorted(contrib['_meta']['hostvars'].keys()) + plugin_hosts = sorted(plugin['_meta']['hostvars'].keys()) + assert contrib_hosts == plugin_hosts + return contrib_hosts, plugin_hosts + + +def check_groups(contrib, plugin): + contrib_groups = set(contrib.keys()) + plugin_groups = set(plugin.keys()) + missing_groups = contrib_groups.difference(plugin_groups) + if missing_groups: + print("groups: %s are missing from the plugin" % missing_groups) + assert not missing_groups + return contrib_groups, plugin_groups + + +def check_host_vars(key, value, plugin, host): + # tags are a dict in the plugin + if key.startswith('ec2_tag'): + print('assert tag', key, value) + assert 'tags' in plugin['_meta']['hostvars'][host], 'b file does not have tags in host' + btags = plugin['_meta']['hostvars'][host]['tags'] + tagkey = key.replace('ec2_tag_', '') + assert tagkey in btags, '%s tag not in b file host tags' % tagkey + assert value == btags[tagkey], '%s != %s' % (value, btags[tagkey]) + else: + print('assert var', key, value, key in plugin['_meta']['hostvars'][host], plugin['_meta']['hostvars'][host].get(key)) + assert key in plugin['_meta']['hostvars'][host], "%s not in b's %s hostvars" % (key, host) + assert value == plugin['_meta']['hostvars'][host][key], "%s != %s" % (value, plugin['_meta']['hostvars'][host][key]) + + +def main(): + # a should be the source of truth (the script output) + a = sys.argv[1] + # b should be the thing to check (the plugin output) + b = sys.argv[2] + + with open(a, 'r') as f: + adata = json.loads(f.read()) + with open(b, 'r') as f: + bdata = json.loads(f.read()) + + print(adata) + print(bdata) + + # all hosts should be present obviously + ahosts, bhosts = check_hosts(adata, bdata) + + # all groups should be present obviously + agroups, bgroups = check_groups(adata, bdata) + + # check host vars can be reconstructed + for ahost in ahosts: + contrib_host_vars = adata['_meta']['hostvars'][ahost] + for key, value in contrib_host_vars.items(): + check_host_vars(key, value, bdata, ahost) + + +if __name__ == "__main__": + main() diff --git a/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/runme.sh b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/runme.sh new file mode 100755 index 00000000..ea163ab1 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/runme.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash + +if [[ $(python --version 2>&1) =~ 2\.6 ]] + then + echo "Openshift client is not supported on Python 2.6" + exit 0 +fi + +set -eux + +# TODO: quay.io/ansible/default-test-container:2.7.0 doesn't have virtualenv included +apt -y update +apt -y install python3-virtualenv + +source virtualenv.sh +pip install openshift -c constraints.txt + +./server.py & + +cleanup() { + kill -9 "$(jobs -p)" +} + +trap cleanup INT TERM EXIT + +# Fake auth file +mkdir -p ~/.kube/ +cat <<EOF > ~/.kube/config +apiVersion: v1 +clusters: +- cluster: + insecure-skip-tls-verify: true + server: http://localhost:12345 + name: development +contexts: +- context: + cluster: development + user: developer + name: dev-frontend +current-context: dev-frontend +kind: Config +preferences: {} +users: +- name: developer + user: + token: ZDNg7LzSlp8a0u0fht_tRnPMTOjxqgJGCyi_iy0ecUw +EOF + +################################################# +# RUN THE PLUGIN +################################################# + +# run the plugin second +export ANSIBLE_INVENTORY_ENABLED=community.kubevirt.kubevirt +export ANSIBLE_INVENTORY=test.kubevirt.yml + +cat << EOF > "$OUTPUT_DIR/test.kubevirt.yml" +plugin: community.kubevirt.kubevirt +connections: + - namespaces: + - default +EOF + +ANSIBLE_JINJA2_NATIVE=1 ansible-inventory -vvvv -i "$OUTPUT_DIR/test.kubevirt.yml" --list --output="$OUTPUT_DIR/plugin.out" + +################################################# +# DIFF THE RESULTS +################################################# + +./inventory_diff.py "$(pwd)/test.out" "$OUTPUT_DIR/plugin.out" diff --git a/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/server.py b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/server.py new file mode 100755 index 00000000..376889e7 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/server.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import json +import os + +try: + from http.server import HTTPServer + from http.server import SimpleHTTPRequestHandler +except ImportError: + from BaseHTTPServer import HTTPServer + from SimpleHTTPServer import SimpleHTTPRequestHandler + +from threading import Thread + +try: + from urllib.parse import urlparse +except ImportError: + from urlparse import urlparse + + +class TestHandler(SimpleHTTPRequestHandler): + # Path handlers: + handlers = {} + + def log_message(self, format, *args): + """ + Empty method, so we don't mix output of HTTP server with tests + """ + pass + + def do_GET(self): + params = urlparse(self.path) + + if params.path in self.handlers: + self.handlers[params.path](self) + else: + SimpleHTTPRequestHandler.do_GET(self) + + def do_POST(self): + params = urlparse(self.path) + + if params.path in self.handlers: + self.handlers[params.path](self) + else: + SimpleHTTPRequestHandler.do_POST(self) + + +class TestServer(object): + # The host and port and path used by the embedded tests web server: + PORT = None + + # The embedded web server: + _httpd = None + # Thread for http server: + _thread = None + + def set_json_response(self, path, code, body): + def _handle_request(handler): + handler.send_response(code) + handler.send_header('Content-Type', 'application/json') + handler.end_headers() + + data = json.dumps(body, ensure_ascii=False).encode('utf-8') + handler.wfile.write(data) + + TestHandler.handlers[path] = _handle_request + + def start_server(self, host='localhost'): + self._httpd = HTTPServer((host, 12345), TestHandler) + self._thread = Thread(target=self._httpd.serve_forever) + self._thread.start() + + def stop_server(self): + self._httpd.shutdown() + self._thread.join() + + +if __name__ == '__main__': + print(os.getpid()) + server = TestServer() + server.start_server() + server.set_json_response(path="/version", code=200, body={}) + server.set_json_response(path="/api", code=200, body={ + "kind": "APIVersions", "versions": ["v1"], "serverAddressByClientCIDRs": [{"clientCIDR": "0.0.0.0/0", "serverAddress": "localhost:12345"}] + }) + server.set_json_response(path="/api/v1", code=200, body={'resources': {}}) + server.set_json_response(path="/apis", code=200, body={ + "kind": "APIGroupList", "apiVersion": "v1", + "groups": [{ + "name": "kubevirt.io", "versions": [{"groupVersion": "kubevirt.io/v1alpha3", "version": "v1alpha3"}], + "preferredVersion": {"groupVersion": "kubevirt.io/v1alpha3", "version": "v1alpha3"} + }] + }) + server.set_json_response( + path="/apis/kubevirt.io/v1alpha3", + code=200, + body={ + "kind": "APIResourceList", "apiVersion": "v1", "groupVersion": "kubevirt.io/v1alpha3", + "resources": [{ + "name": "virtualmachineinstances", "singularName": "virtualmachineinstance", + "namespaced": True, "kind": "VirtualMachineInstance", + "verbs": ["delete", "deletecollection", "get", "list", "patch", "create", "update", "watch"], + "shortNames":["vmi", "vmis"] + }] + } + ) + server.set_json_response( + path="/apis/kubevirt.io/v1alpha3/namespaces/default/virtualmachineinstances", + code=200, + body={'apiVersion': 'kubevirt.io/v1alpha3', + 'items': [{'apiVersion': 'kubevirt.io/v1alpha3', + 'kind': 'VirtualMachineInstance', + 'metadata': {'annotations': {'ansible': '{"data1": "yes", "data2": "no"}'}, + 'creationTimestamp': '2019-04-05T14:17:02Z', + 'generateName': 'myvm', + 'generation': 1, + 'labels': {'kubevirt.io/nodeName': 'localhost', + 'label': 'x', + 'vm.cnv.io/name': 'myvm'}, + 'name': 'myvm', + 'namespace': 'default', + 'ownerReferences': [{'apiVersion': 'kubevirt.io/v1alpha3', + 'blockOwnerDeletion': True, + 'controller': True, + 'kind': 'VirtualMachine', + 'name': 'myvm', + 'uid': 'f78ebe62-5666-11e9-a214-0800279ffc6b'}], + 'resourceVersion': '1614085', + 'selfLink': '/apis/kubevirt.io/v1alpha3/namespaces/default/virtualmachineinstances/myvm', + 'uid': '7ba1b196-57ad-11e9-9e2e-0800279ffc6b'}, + 'spec': {'domain': {'devices': {'disks': [{'disk': {'bus': 'virtio'}, + 'name': 'containerdisk'}, + {'disk': {'bus': 'virtio'}, 'name': 'ansiblecloudinitdisk'}], + 'interfaces': [{'bridge': {}, 'name': 'default'}]}, + 'firmware': {'uuid': 'cdf77e9e-871b-5acb-a707-80ef3d4b9849'}, + 'machine': {'type': ''}, + 'resources': {'requests': {'memory': '64M'}}}, + 'networks': [{'name': 'default', 'pod': {}}], + 'volumes': [{'containerDisk': {'image': 'kubevirt/cirros-container-disk-demo:v0.11.0'}, + 'name': 'containerdisk'}, + {'cloudInitNoCloud': {'userData': '#cloud-config\npassword: password\nchpasswd: { expire: False }'}, + 'name': 'ansiblecloudinitdisk'}]}, + 'status': {'conditions': [{'lastProbeTime': None, + 'lastTransitionTime': None, + 'status': 'True', + 'type': 'LiveMigratable'}, + {'lastProbeTime': None, + 'lastTransitionTime': '2019-04-05T14:17:27Z', + 'status': 'True', + 'type': 'Ready'}], + 'interfaces': [{'ipAddress': '172.17.0.19', + 'mac': '02:42:ac:11:00:13', + 'name': 'default'}], + 'migrationMethod': 'BlockMigration', + 'nodeName': 'localhost', + 'phase': 'Running'}}], + 'kind': 'VirtualMachineInstanceList', + 'metadata': {'continue': '', + 'resourceVersion': '1614862', + 'selfLink': '/apis/kubevirt.io/v1alpha3/namespaces/default/virtualmachineinstances'}} + ) diff --git a/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/test.out b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/test.out new file mode 100644 index 00000000..932aade0 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/kubevirt/tests/integration/targets/inventory_kubevirt/test.out @@ -0,0 +1,61 @@ +{ + "_meta": { + "hostvars": { + "default-myvm-7ba1b196-57ad-11e9-9e2e-0800279ffc6b": { + "annotations": { + "ansible": "{\"data1\": \"yes\", \"data2\": \"no\"}" + }, + "ansible_host": "172.17.0.19", + "data1": "yes", + "data2": "no", + "labels": { + "kubevirt.io/nodeName": "localhost", + "label": "x", + "vm.cnv.io/name": "myvm" + }, + "object_type": "vm", + "resource_version": "1614085", + "uid": "7ba1b196-57ad-11e9-9e2e-0800279ffc6b" + } + } + }, + "all": { + "children": [ + "label_kubevirt_io_nodeName_localhost", + "label_label_x", + "label_vm_cnv_io_name_myvm", + "localhost_12345", + "ungrouped" + ] + }, + "label_kubevirt_io_nodeName_localhost": { + "hosts": [ + "default-myvm-7ba1b196-57ad-11e9-9e2e-0800279ffc6b" + ] + }, + "label_label_x": { + "hosts": [ + "default-myvm-7ba1b196-57ad-11e9-9e2e-0800279ffc6b" + ] + }, + "label_vm_cnv_io_name_myvm": { + "hosts": [ + "default-myvm-7ba1b196-57ad-11e9-9e2e-0800279ffc6b" + ] + }, + "localhost_12345": { + "children": [ + "namespace_default" + ] + }, + "namespace_default": { + "children": [ + "namespace_default_vms" + ] + }, + "namespace_default_vms": { + "hosts": [ + "default-myvm-7ba1b196-57ad-11e9-9e2e-0800279ffc6b" + ] + } +} |