diff --git a/tests/fixtures/config/remote-zuul-stream/git/common-config/zuul.yaml b/tests/fixtures/config/remote-zuul-stream/git/common-config/zuul.yaml new file mode 100644 index 0000000000..a07342e2ec --- /dev/null +++ b/tests/fixtures/config/remote-zuul-stream/git/common-config/zuul.yaml @@ -0,0 +1,17 @@ +- pipeline: + name: check + manager: independent + post-review: true + trigger: + gerrit: + - event: patchset-created + success: + gerrit: + Verified: 1 + failure: + gerrit: + Verified: -1 + +- job: + name: base + parent: null diff --git a/tests/fixtures/config/remote-zuul-stream/git/org_project/README b/tests/fixtures/config/remote-zuul-stream/git/org_project/README new file mode 100644 index 0000000000..9daeafb986 --- /dev/null +++ b/tests/fixtures/config/remote-zuul-stream/git/org_project/README @@ -0,0 +1 @@ +test diff --git a/tests/fixtures/config/remote-zuul-stream/git/org_project/playbooks/command.yaml b/tests/fixtures/config/remote-zuul-stream/git/org_project/playbooks/command.yaml new file mode 100644 index 0000000000..83a4d334f2 --- /dev/null +++ b/tests/fixtures/config/remote-zuul-stream/git/org_project/playbooks/command.yaml @@ -0,0 +1,16 @@ +- hosts: all + tasks: + - name: Start zuul_console daemon + zuul_console: + - name: Create first file + copy: + content: "command test one\n" + dest: "{{ ansible_user_dir }}/command_test_file1" + - name: Create second file + copy: + content: "command test two\n" + dest: "{{ ansible_user_dir }}/command_test_file2" + - name: Show contents of first file + command: "cat {{ ansible_user_dir }}/command_test_file1" + - name: Show contents of second file + command: "cat {{ ansible_user_dir }}/command_test_file2" diff --git a/tests/fixtures/config/remote-zuul-stream/git/org_project/playbooks/library/broken_module.py b/tests/fixtures/config/remote-zuul-stream/git/org_project/playbooks/library/broken_module.py new file mode 100755 index 0000000000..065509d0d1 --- /dev/null +++ b/tests/fixtures/config/remote-zuul-stream/git/org_project/playbooks/library/broken_module.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python + + +def main(): + print("This module is broken") + + +try: + from ansible.module_utils.basic import * # noqa +except ImportError: + pass + + +if __name__ == '__main__': + main() diff --git a/tests/fixtures/config/remote-zuul-stream/git/org_project/playbooks/module_failure.yaml b/tests/fixtures/config/remote-zuul-stream/git/org_project/playbooks/module_failure.yaml new file mode 100644 index 0000000000..cd61e2ae89 --- /dev/null +++ b/tests/fixtures/config/remote-zuul-stream/git/org_project/playbooks/module_failure.yaml @@ -0,0 +1,6 @@ +- hosts: all + tasks: + - name: Start zuul_console daemon + zuul_console: + - name: Module failure + broken_module: diff --git a/tests/fixtures/config/remote-zuul-stream/git/org_project/zuul.yaml b/tests/fixtures/config/remote-zuul-stream/git/org_project/zuul.yaml new file mode 100644 index 0000000000..2a5dca97f2 --- /dev/null +++ b/tests/fixtures/config/remote-zuul-stream/git/org_project/zuul.yaml @@ -0,0 +1,4 @@ +- project: + check: + jobs: + - noop diff --git a/tests/fixtures/config/remote-zuul-stream/main.yaml b/tests/fixtures/config/remote-zuul-stream/main.yaml new file mode 100644 index 0000000000..208e274b13 --- /dev/null +++ b/tests/fixtures/config/remote-zuul-stream/main.yaml @@ -0,0 +1,8 @@ +- tenant: + name: tenant-one + source: + gerrit: + config-projects: + - common-config + untrusted-projects: + - org/project diff --git a/tests/remote/test_remote_zuul_stream.py b/tests/remote/test_remote_zuul_stream.py new file mode 100644 index 0000000000..7c26b23b5b --- /dev/null +++ b/tests/remote/test_remote_zuul_stream.py @@ -0,0 +1,117 @@ +# Copyright 2018 Red Hat, 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 os +import re +import textwrap + +from tests.base import AnsibleZuulTestCase + + +class TestZuulStream(AnsibleZuulTestCase): + tenant_config_file = 'config/remote-zuul-stream/main.yaml' + + def setUp(self): + super(TestZuulStream, self).setUp() + self.fake_nodepool.remote_ansible = True + + ansible_remote = os.environ.get('ZUUL_REMOTE_IPV4') + self.assertIsNotNone(ansible_remote) + + def _run_job(self, job_name): + # Keep the jobdir around so we can inspect contents if an + # assert fails. It will be cleaned up anyway as it is contained + # in a tmp dir which gets cleaned up after the test. + self.executor_server.keep_jobdir = True + + # Output extra ansible info so we might see errors. + self.executor_server.verbose = True + conf = textwrap.dedent( + """ + - job: + name: {job_name} + run: playbooks/{job_name}.yaml + roles: + - zuul: org/common-config + nodeset: + nodes: + - name: controller + label: whatever + + - project: + check: + jobs: + - {job_name} + """.format(job_name=job_name)) + + file_dict = {'zuul.yaml': conf} + A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A', + files=file_dict) + self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) + self.waitUntilSettled() + + job = self.getJobFromHistory(job_name) + return job + + def _get_job_output(self, build): + path = os.path.join(self.test_root, build.uuid, + 'work', 'logs', 'job-output.txt') + with open(path) as f: + return f.read() + + def assertLogLine(self, line, log): + pattern = ('^\d\d\d\d-\d\d-\d\d \d\d:\d\d\:\d\d\.\d\d\d\d\d\d \| %s$' % + line) + log_re = re.compile(pattern, re.MULTILINE) + m = log_re.search(log) + if m is None: + raise Exception("'%s' not found in log" % (line,)) + + def test_command(self): + job = self._run_job('command') + with self.jobLog(job): + build = self.history[-1] + self.assertEqual(build.result, 'SUCCESS') + + text = self._get_job_output(build) + self.assertLogLine( + 'RUN START: \[untrusted : review.example.com/org/project/' + 'playbooks/command.yaml@master\]', text) + self.assertLogLine('PLAY \[all\]', text) + self.assertLogLine('TASK \[Show contents of first file\]', text) + self.assertLogLine('controller \| command test one', text) + self.assertLogLine( + 'controller \| ok: Runtime: \d:\d\d:\d\d\.\d\d\d\d\d\d', text) + self.assertLogLine('TASK \[Show contents of second file\]', text) + self.assertLogLine('controller \| command test one', text) + self.assertLogLine( + 'controller \| ok: Runtime: \d:\d\d:\d\d\.\d\d\d\d\d\d', text) + self.assertLogLine('PLAY RECAP', text) + self.assertLogLine( + 'controller \| ok: 5 changed: \d unreachable: 0 failed: 0', + text) + self.assertLogLine( + 'RUN END RESULT_NORMAL: \[untrusted : review.example.com/' + 'org/project/playbooks/command.yaml@master]', text) + + def test_module_failure(self): + job = self._run_job('module_failure') + with self.jobLog(job): + build = self.history[-1] + self.assertEqual(build.result, 'FAILURE') + + text = self._get_job_output(build) + self.assertLogLine('TASK \[Module failure\]', text) + self.assertLogLine( + 'controller \| MODULE FAILURE: This module is broken', text)