summaryrefslogtreecommitdiffstats
path: root/tests/cmd/subcommands/test_update.py
blob: f755f4b87d0f755ce6266eeefd832731ae9932db (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
# 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 os
import six

from tests.base import mock
from tests.cmd.test_cmd import CmdTestsBase


@mock.patch("jenkins_jobs.builder.JenkinsManager.get_plugins_info", mock.MagicMock)
class UpdateTests(CmdTestsBase):
    @mock.patch("jenkins_jobs.builder.jenkins.Jenkins.job_exists")
    @mock.patch("jenkins_jobs.builder.jenkins.Jenkins.get_all_jobs")
    @mock.patch("jenkins_jobs.builder.jenkins.Jenkins.reconfig_job")
    def test_update_jobs(
        self, jenkins_reconfig_job, jenkins_get_jobs, jenkins_job_exists
    ):
        """
        Test update_job is called
        """
        path = os.path.join(self.fixtures_path, "cmd-002.yaml")
        args = ["--conf", self.default_config_file, "update", path]

        self.execute_jenkins_jobs_with_args(args)

        jenkins_reconfig_job.assert_has_calls(
            [
                mock.call(job_name, mock.ANY)
                for job_name in ["bar001", "bar002", "baz001", "bam001"]
            ],
            any_order=True,
        )

    @mock.patch("jenkins_jobs.builder.JenkinsManager.is_job", return_value=True)
    @mock.patch("jenkins_jobs.builder.JenkinsManager.get_jobs")
    @mock.patch("jenkins_jobs.builder.JenkinsManager.get_job_md5")
    @mock.patch("jenkins_jobs.builder.JenkinsManager.update_job")
    def test_update_jobs_decode_job_output(
        self, update_job_mock, get_job_md5_mock, get_jobs_mock, is_job_mock
    ):
        """
        Test that job xml output has been decoded before attempting to update
        """
        # don't care about the value returned here
        update_job_mock.return_value = ([], 0)

        path = os.path.join(self.fixtures_path, "cmd-002.yaml")
        args = ["--conf", self.default_config_file, "update", path]

        self.execute_jenkins_jobs_with_args(args)
        self.assertTrue(isinstance(update_job_mock.call_args[0][1], six.text_type))

    @mock.patch("jenkins_jobs.builder.jenkins.Jenkins.job_exists")
    @mock.patch("jenkins_jobs.builder.jenkins.Jenkins.get_all_jobs")
    @mock.patch("jenkins_jobs.builder.jenkins.Jenkins.reconfig_job")
    @mock.patch("jenkins_jobs.builder.jenkins.Jenkins.delete_job")
    def test_update_jobs_and_delete_old(
        self,
        jenkins_delete_job,
        jenkins_reconfig_job,
        jenkins_get_all_jobs,
        jenkins_job_exists,
    ):
        """
        Test update behaviour with --delete-old option

        * mock out a call to jenkins.Jenkins.get_jobs() to return a known list
          of job names.
        * mock out a call to jenkins.Jenkins.reconfig_job() and
          jenkins.Jenkins.delete_job() to detect calls being made to determine
          that JJB does correctly delete the jobs it should delete when passed
          a specific set of inputs.
        * mock out a call to jenkins.Jenkins.job_exists() to always return
          True.
        """
        yaml_jobs = ["bar001", "bar002", "baz001", "bam001"]
        extra_jobs = ["old_job001", "old_job002", "unmanaged"]

        path = os.path.join(self.fixtures_path, "cmd-002.yaml")
        args = ["--conf", self.default_config_file, "update", "--delete-old", path]

        jenkins_get_all_jobs.return_value = [
            {"fullname": name} for name in yaml_jobs + extra_jobs
        ]

        with mock.patch(
            "jenkins_jobs.builder.JenkinsManager.is_managed",
            side_effect=(lambda name: name != "unmanaged"),
        ):
            self.execute_jenkins_jobs_with_args(args)

        jenkins_reconfig_job.assert_has_calls(
            [mock.call(job_name, mock.ANY) for job_name in yaml_jobs], any_order=True
        )
        calls = [mock.call(name) for name in extra_jobs if name != "unmanaged"]
        jenkins_delete_job.assert_has_calls(calls)
        # to ensure only the calls we expected were made, have to check
        # there were no others, as no API call for assert_has_only_calls
        self.assertEqual(jenkins_delete_job.call_count, len(calls))

    def test_update_timeout_not_set(self):
        """Validate update timeout behavior when timeout not explicitly configured.
        """
        self.skipTest("TODO: Develop actual update timeout test approach.")

    def test_update_timeout_set(self):
        """Validate update timeout behavior when timeout is explicitly configured.
        """
        self.skipTest("TODO: Develop actual update timeout test approach.")