summaryrefslogtreecommitdiffstats
path: root/tests/cmd/subcommands/test_test.py
blob: ee7d93a7b41cf628046e3504656f6aa4e56f0f99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# Joint copyright:
#  - Copyright 2015 Hewlett-Packard Development Company, L.P.
#
# 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.

# The goal of these tests is to check that given a particular set of flags to
# Jenkins Job Builder's command line tools it will result in a particular set
# of actions by the JJB library, usually through interaction with the
# python-jenkins library.

import difflib
import filecmp
import io
import os
import shutil
import tempfile
import yaml

import jenkins
from six.moves import StringIO
import testtools

from jenkins_jobs.cli import entry
from tests.base import mock
from tests.cmd.test_cmd import CmdTestsBase


@mock.patch("jenkins_jobs.builder.JenkinsManager.get_plugins_info", mock.MagicMock)
class TestTests(CmdTestsBase):
    def test_non_existing_job(self):
        """
        Run test mode and pass a non-existing job name
        (probably better to fail here)
        """
        args = [
            "--conf",
            self.default_config_file,
            "test",
            os.path.join(self.fixtures_path, "cmd-001.yaml"),
            "invalid",
        ]
        self.execute_jenkins_jobs_with_args(args)

    def test_valid_job(self):
        """
        Run test mode and pass a valid job name
        """
        args = [
            "--conf",
            self.default_config_file,
            "test",
            os.path.join(self.fixtures_path, "cmd-001.yaml"),
            "foo-job",
        ]
        console_out = io.BytesIO()
        with mock.patch("sys.stdout", console_out):
            self.execute_jenkins_jobs_with_args(args)

    def test_console_output(self):
        """
        Run test mode and verify that resulting XML gets sent to the console.
        """

        console_out = io.BytesIO()
        with mock.patch("sys.stdout", console_out):
            args = [
                "--conf",
                self.default_config_file,
                "test",
                os.path.join(self.fixtures_path, "cmd-001.yaml"),
            ]
            self.execute_jenkins_jobs_with_args(args)
        xml_content = io.open(
            os.path.join(self.fixtures_path, "cmd-001.xml"), "r", encoding="utf-8"
        ).read()
        self.assertEqual(console_out.getvalue().decode("utf-8"), xml_content)

    def test_output_dir(self):
        """
        Run test mode with output to directory and verify that output files are
        generated.
        """
        tmpdir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, tmpdir)
        args = ["test", os.path.join(self.fixtures_path, "cmd-001.yaml"), "-o", tmpdir]
        self.execute_jenkins_jobs_with_args(args)
        self.expectThat(
            os.path.join(tmpdir, "foo-job"), testtools.matchers.FileExists()
        )

    def test_output_dir_config_xml(self):
        """
        Run test mode with output to directory in "config.xml" mode and verify
        that output files are generated.
        """
        tmpdir = tempfile.mkdtemp()
        self.addCleanup(shutil.rmtree, tmpdir)
        args = [
            "test",
            os.path.join(self.fixtures_path, "cmd-001.yaml"),
            "-o",
            tmpdir,
            "--config-xml",
        ]
        self.execute_jenkins_jobs_with_args(args)
        self.expectThat(
            os.path.join(tmpdir, "foo-job", "config.xml"),
            testtools.matchers.FileExists(),
        )

    def test_stream_input_output_no_encoding_exceed_recursion(self):
        """
        Test that we don't have issues processing large number of jobs and
        outputting the result if the encoding is not set.
        """
        console_out = io.BytesIO()

        input_file = os.path.join(self.fixtures_path, "large-number-of-jobs-001.yaml")
        with io.open(input_file, "r") as f:
            with mock.patch("sys.stdout", console_out):
                console_out.encoding = None
                with mock.patch("sys.stdin", f):
                    args = ["test"]
                    self.execute_jenkins_jobs_with_args(args)

    def test_stream_input_output_utf8_encoding(self):
        """
        Run test mode simulating using pipes for input and output using
        utf-8 encoding
        """
        console_out = io.BytesIO()

        input_file = os.path.join(self.fixtures_path, "cmd-001.yaml")
        with io.open(input_file, "r") as f:
            with mock.patch("sys.stdout", console_out):
                with mock.patch("sys.stdin", f):
                    args = ["--conf", self.default_config_file, "test"]
                    self.execute_jenkins_jobs_with_args(args)

        xml_content = io.open(
            os.path.join(self.fixtures_path, "cmd-001.xml"), "r", encoding="utf-8"
        ).read()
        value = console_out.getvalue().decode("utf-8")
        self.assertEqual(value, xml_content)

    def test_stream_input_output_ascii_encoding(self):
        """
        Run test mode simulating using pipes for input and output using
        ascii encoding with unicode input
        """
        console_out = io.BytesIO()
        console_out.encoding = "ascii"

        input_file = os.path.join(self.fixtures_path, "cmd-001.yaml")
        with io.open(input_file, "r") as f:
            with mock.patch("sys.stdout", console_out):
                with mock.patch("sys.stdin", f):
                    args = ["--conf", self.default_config_file, "test"]
                    self.execute_jenkins_jobs_with_args(args)

        xml_content = io.open(
            os.path.join(self.fixtures_path, "cmd-001.xml"), "r", encoding="utf-8"
        ).read()
        value = console_out.getvalue().decode("ascii")
        self.assertEqual(value, xml_content)

    def test_stream_output_ascii_encoding_invalid_char(self):
        """
        Run test mode simulating using pipes for input and output using
        ascii encoding for output with include containing a character
        that cannot be converted.
        """
        console_out = io.BytesIO()
        console_out.encoding = "ascii"

        input_file = os.path.join(self.fixtures_path, "unicode001.yaml")
        with io.open(input_file, "r", encoding="utf-8") as f:
            with mock.patch("sys.stdout", console_out):
                with mock.patch("sys.stdin", f):
                    args = ["--conf", self.default_config_file, "test"]
                    jenkins_jobs = entry.JenkinsJobs(args)
                    e = self.assertRaises(UnicodeError, jenkins_jobs.execute)
        self.assertIn("'ascii' codec can't encode character", str(e))

    @mock.patch("jenkins_jobs.cli.subcommand.update.XmlJobGenerator.generateXML")
    @mock.patch("jenkins_jobs.cli.subcommand.update.ModuleRegistry")
    def test_plugins_info_stub_option(self, registry_mock, generateXML_mock):
        """
        Test handling of plugins_info stub option.
        """
        plugins_info_stub_yaml_file = os.path.join(
            self.fixtures_path, "plugins-info.yaml"
        )
        args = [
            "--conf",
            os.path.join(self.fixtures_path, "cmd-001.conf"),
            "test",
            "-p",
            plugins_info_stub_yaml_file,
            os.path.join(self.fixtures_path, "cmd-001.yaml"),
        ]

        self.execute_jenkins_jobs_with_args(args)

        with io.open(plugins_info_stub_yaml_file, "r", encoding="utf-8") as yaml_file:
            plugins_info_list = yaml.safe_load(yaml_file)

        registry_mock.assert_called_with(mock.ANY, plugins_info_list)

    @mock.patch("jenkins_jobs.cli.subcommand.update.XmlJobGenerator.generateXML")
    @mock.patch("jenkins_jobs.cli.subcommand.update.ModuleRegistry")
    def test_bogus_plugins_info_stub_option(self, registry_mock, generateXML_mock):
        """
        Verify that a JenkinsJobException is raised if the plugins_info stub
        file does not yield a list as its top-level object.
        """
        plugins_info_stub_yaml_file = os.path.join(
            self.fixtures_path, "bogus-plugins-info.yaml"
        )
        args = [
            "--conf",
            os.path.join(self.fixtures_path, "cmd-001.conf"),
            "test",
            "-p",
            plugins_info_stub_yaml_file,
            os.path.join(self.fixtures_path, "cmd-001.yaml"),
        ]

        stderr = StringIO()
        with mock.patch("sys.stderr", stderr):
            self.assertRaises(SystemExit, entry.JenkinsJobs, args)
        self.assertIn("must contain a Yaml list", stderr.getvalue())


class TestJenkinsGetPluginInfoError(CmdTestsBase):
    """ This test class is used for testing the 'test' subcommand when we want
    to validate its behavior without mocking
    jenkins_jobs.builder.JenkinsManager.get_plugins_info
    """

    @mock.patch("jenkins.Jenkins.get_plugins")
    def test_console_output_jenkins_connection_failure_warning(self, get_plugins_mock):
        """
        Run test mode and verify that failed Jenkins connection attempt
        exception does not bubble out of cmd.main. Ideally, we would also test
        that an appropriate message is logged to stderr but it's somewhat
        difficult to figure out how to actually enable stderr in this test
        suite.
        """

        get_plugins_mock.side_effect = jenkins.JenkinsException("Connection refused")
        with mock.patch("sys.stdout"):
            try:
                args = [
                    "--conf",
                    self.default_config_file,
                    "test",
                    os.path.join(self.fixtures_path, "cmd-001.yaml"),
                ]
                self.execute_jenkins_jobs_with_args(args)
            except jenkins.JenkinsException:
                self.fail("jenkins.JenkinsException propagated to main")
            except Exception:
                pass  # only care about jenkins.JenkinsException for now

    @mock.patch("jenkins.Jenkins.get_plugins")
    def test_skip_plugin_retrieval_if_no_config_provided(self, get_plugins_mock):
        """
        Verify that retrieval of information from Jenkins instance about its
        plugins will be skipped when run if no config file provided.
        """
        with mock.patch("sys.stdout", new_callable=io.BytesIO):
            args = [
                "--conf",
                self.default_config_file,
                "test",
                os.path.join(self.fixtures_path, "cmd-001.yaml"),
            ]
            entry.JenkinsJobs(args)
        self.assertFalse(get_plugins_mock.called)

    @mock.patch("jenkins.Jenkins.get_plugins_info")
    def test_skip_plugin_retrieval_if_disabled(self, get_plugins_mock):
        """
        Verify that retrieval of information from Jenkins instance about its
        plugins will be skipped when run if a config file provided and disables
        querying through a config option.
        """
        with mock.patch("sys.stdout", new_callable=io.BytesIO):
            args = [
                "--conf",
                os.path.join(self.fixtures_path, "disable-query-plugins.conf"),
                "test",
                os.path.join(self.fixtures_path, "cmd-001.yaml"),
            ]
            entry.JenkinsJobs(args)
        self.assertFalse(get_plugins_mock.called)


class MatchesDirMissingFilesMismatch(object):
    def __init__(self, left_directory, right_directory):
        self.left_directory = left_directory
        self.right_directory = right_directory

    def describe(self):
        return "{0} and {1} contain different files".format(
            self.left_directory, self.right_directory
        )

    def get_details(self):
        return {}


class MatchesDirFileContentsMismatch(object):
    def __init__(self, left_file, right_file):
        self.left_file = left_file
        self.right_file = right_file

    def describe(self):
        left_contents = open(self.left_file).readlines()
        right_contents = open(self.right_file).readlines()

        return "{0} is not equal to {1}:\n{2}".format(
            difflib.unified_diff(
                left_contents,
                right_contents,
                fromfile=self.left_file,
                tofile=self.right_file,
            ),
            self.left_file,
            self.right_file,
        )

    def get_details(self):
        return {}


class MatchesDir(object):
    def __init__(self, directory):
        self.__directory = directory
        self.__files = self.__get_files(directory)

    def __get_files(self, directory):
        for root, _, files in os.walk(directory):
            return files

    def __str__(self,):
        return "MatchesDir({0})".format(self.__dirname)

    def match(self, other_directory):
        other_files = self.__get_files(other_directory)

        self.__files.sort()
        other_files.sort()

        if self.__files != other_files:
            return MatchesDirMissingFilesMismatch(self.__directory, other_directory)

        for i, file in enumerate(self.__files):
            my_file = os.path.join(self.__directory, file)
            other_file = os.path.join(other_directory, other_files[i])
            if not filecmp.cmp(my_file, other_file):
                return MatchesDirFileContentsMismatch(my_file, other_file)

        return None


@mock.patch("jenkins_jobs.builder.JenkinsManager.get_plugins_info", mock.MagicMock)
class TestTestsMultiPath(CmdTestsBase):
    def setUp(self):
        super(TestTestsMultiPath, self).setUp()

        path_list = [
            os.path.join(self.fixtures_path, "multi-path/yamldirs/", p)
            for p in ["dir1", "dir2"]
        ]
        self.multipath = os.pathsep.join(path_list)
        self.output_dir = tempfile.mkdtemp()

    def check_dirs_match(self, expected_dir):
        try:
            self.assertThat(self.output_dir, MatchesDir(expected_dir))
        except testtools.matchers.MismatchError:
            raise
        else:
            shutil.rmtree(self.output_dir)

    def test_multi_path(self):
        """
        Run test mode and pass multiple paths.
        """
        args = [
            "--conf",
            self.default_config_file,
            "test",
            "-o",
            self.output_dir,
            self.multipath,
        ]

        self.execute_jenkins_jobs_with_args(args)
        self.check_dirs_match(
            os.path.join(self.fixtures_path, "multi-path/output_simple")
        )

    def test_recursive_multi_path_command_line(self):
        """
        Run test mode and pass multiple paths with recursive path option.
        """
        args = [
            "--conf",
            self.default_config_file,
            "test",
            "-o",
            self.output_dir,
            "-r",
            self.multipath,
        ]

        self.execute_jenkins_jobs_with_args(args)
        self.check_dirs_match(
            os.path.join(self.fixtures_path, "multi-path/output_recursive")
        )

    def test_recursive_multi_path_config_file(self):
        # test recursive set in configuration file
        args = [
            "--conf",
            os.path.join(self.fixtures_path, "multi-path/builder-recursive.ini"),
            "test",
            "-o",
            self.output_dir,
            self.multipath,
        ]
        self.execute_jenkins_jobs_with_args(args)
        self.check_dirs_match(
            os.path.join(self.fixtures_path, "multi-path/output_recursive")
        )

    def test_recursive_multi_path_with_excludes(self):
        """
        Run test mode and pass multiple paths with recursive path option.
        """
        exclude_path = os.path.join(self.fixtures_path, "multi-path/yamldirs/dir2/dir1")
        args = [
            "--conf",
            self.default_config_file,
            "test",
            "-x",
            exclude_path,
            "-o",
            self.output_dir,
            "-r",
            self.multipath,
        ]

        self.execute_jenkins_jobs_with_args(args)
        self.check_dirs_match(
            os.path.join(
                self.fixtures_path, "multi-path/output_recursive_with_excludes"
            )
        )