File: test_resources_job_template.py

package info (click to toggle)
ansible-tower-cli 3.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,484 kB
  • sloc: python: 10,467; sh: 183; makefile: 76
file content (193 lines) | stat: -rw-r--r-- 9,072 bytes parent folder | download
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
# Copyright 2015, Ansible, Inc.
# Alan Rominger <arominger@ansible.com>
#
# 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 tower_cli
from tower_cli.api import client

from tests.compat import unittest, mock
from tower_cli.conf import settings
from tower_cli.cli.resource import ResSubcommand

import click
import json


class TemplateTests(unittest.TestCase):
    """A set of tests for commands operating on the job template
    """
    def setUp(self):
        self.res = tower_cli.get_resource('job_template')

    def test_create(self):
        """Establish that a job template can be created
        """
        with client.test_mode as t:
            endpoint = '/job_templates/'
            t.register_json(endpoint, {'count': 0, 'results': [],
                            'next': None, 'previous': None},
                            method='GET')
            t.register_json(endpoint, {'changed': True, 'id': 42},
                            method='POST')
            self.res.create(name='bar', job_type='run', inventory=1,
                            project=1, playbook='foobar.yml', credential=1)
            self.assertEqual(t.requests[0].method, 'GET')
            self.assertEqual(t.requests[1].method, 'POST')
            self.assertEqual(len(t.requests), 2)

        # Check that default job_type will get added when needed
        with client.test_mode as t:
            endpoint = '/job_templates/'
            t.register_json(endpoint, {'count': 0, 'results': [],
                            'next': None, 'previous': None},
                            method='GET')
            t.register_json(endpoint, {'changed': True, 'id': 42},
                            method='POST')
            self.res.create(name='bar', inventory=1, project=1,
                            playbook='foobar.yml', credential=1)
            req_body = json.loads(t.requests[1].body)
            self.assertIn('job_type', req_body)
            self.assertEqual(req_body['job_type'], 'run')

    def test_job_template_create_with_echo(self):
        """Establish that a job template can be created
        """
        with client.test_mode as t:
            endpoint = '/job_templates/'
            t.register_json(endpoint, {'count': 0, 'results': [],
                            'next': None, 'previous': None},
                            method='GET')
            t.register_json(endpoint,
                            {'changed': True, 'id': 42,
                                'name': 'bar', 'inventory': 1, 'project': 1,
                                'playbook': 'foobar.yml', 'credential': 1},
                            method='POST')
            self.res.create(name='bar', job_type='run', inventory=1,
                            project=1, playbook='foobar.yml', credential=1)

            f = ResSubcommand(self.res)._echo_method(self.res.create)
            with mock.patch.object(click, 'secho'):
                with settings.runtime_values(format='human'):
                    f(name='bar', job_type='run', inventory=1,
                      project=1, playbook='foobar.yml', credential=1)

    def test_create_w_extra_vars(self):
        """Establish that a job template can be created
        and extra varas passed to it
        """
        with client.test_mode as t:
            endpoint = '/job_templates/'
            t.register_json(endpoint, {'count': 0, 'results': [],
                            'next': None, 'previous': None},
                            method='GET')
            t.register_json(endpoint, {'changed': True, 'id': 42},
                            method='POST')
            self.res.create(name='bar', job_type='run', inventory=1,
                            project=1, playbook='foobar.yml', credential=1,
                            extra_vars=['foo: bar'])
            self.assertEqual(t.requests[0].method, 'GET')
            self.assertEqual(t.requests[1].method, 'POST')
            self.assertEqual(len(t.requests), 2)

    def test_modify(self):
        """Establish that a job template can be modified
        """
        with client.test_mode as t:
            endpoint = '/job_templates/'
            t.register_json(endpoint, {'count': 1, 'results': [{'id': 1,
                            'name': 'bar'}], 'next': None, 'previous': None},
                            method='GET')
            t.register_json('/job_templates/1/', {'name': 'bar', 'id': 1,
                            'job_type': 'run'},
                            method='PATCH')
            self.res.modify(name='bar', playbook='foobared.yml')
            self.assertEqual(t.requests[0].method, 'GET')
            self.assertEqual(t.requests[1].method, 'PATCH')
            self.assertEqual(len(t.requests), 2)

    def test_modify_extra_vars(self):
        """Establish that a job template can be modified
        """
        with client.test_mode as t:
            endpoint = '/job_templates/'
            t.register_json(endpoint, {'count': 1, 'results': [{'id': 1,
                            'name': 'bar'}], 'next': None, 'previous': None},
                            method='GET')
            t.register_json('/job_templates/1/', {'name': 'bar', 'id': 1,
                            'job_type': 'run'},
                            method='PATCH')
            self.res.modify(name='bar', extra_vars=["a: 5"])
            self.assertEqual(t.requests[0].method, 'GET')
            self.assertEqual(t.requests[1].method, 'PATCH')
            self.assertEqual(len(t.requests), 2)

    def test_associate_credential(self):
        """Establish that the associate method makes the HTTP requests
        that we expect.
        """
        with client.test_mode as t:
            t.register_json('/job_templates/42/extra_credentials/?id=84',
                            {'count': 0, 'results': []})
            t.register_json('/job_templates/42/extra_credentials/', {}, method='POST')
            self.res.associate_credential(42, 84)
            self.assertEqual(t.requests[1].body,
                             json.dumps({'associate': True, 'id': 84}))

    def test_disassociate_credential(self):
        """Establish that the disassociate method makes the HTTP requests
        that we expect.
        """
        with client.test_mode as t:
            t.register_json('/job_templates/42/extra_credentials/?id=84',
                            {'count': 1, 'results': [{'id': 84}],
                             'next': None, 'previous': None})
            t.register_json('/job_templates/42/extra_credentials/', {}, method='POST')
            self.res.disassociate_credential(42, 84)
            self.assertEqual(t.requests[1].body,
                             json.dumps({'disassociate': True, 'id': 84}))

    def test_associate_notification_template(self):
        """Establish that a job template should be able to associate itself
        with an existing notification template.
        """
        with client.test_mode as t:
            t.register_json('/job_templates/5/notification_templates_any/'
                            '?id=3', {'count': 0, 'results': []})
            t.register_json('/job_templates/5/notification_templates_any/',
                            {}, method='POST')
            self.res.associate_notification_template(5, 3, 'any')
            self.assertEqual(t.requests[1].body,
                             json.dumps({'associate': True, 'id': 3}))

    def test_disassociate_notification_template(self):
        """Establish that a job template should be able to disassociate itself
        from an associated notification template.
        """
        with client.test_mode as t:
            t.register_json('/job_templates/5/notification_templates_any/'
                            '?id=3', {'count': 1, 'results': [{'id': 3}]})
            t.register_json('/job_templates/5/notification_templates_any/',
                            {}, method='POST')
            self.res.disassociate_notification_template(5, 3, 'any')
            self.assertEqual(t.requests[1].body,
                             json.dumps({'disassociate': True, 'id': 3}))

    def test_callback(self):
        """Establish that a job template should be able to conduct provisioning callback
        """
        with client.test_mode as t:
            t.register_json('/job_templates/5/callback/', {'host_config_key': 'foobar'})
            t.register_json('/job_templates/5/callback/', {}, method='POST')
            self.res.callback(5)
            self.assertEqual(t.requests[1].body, json.dumps({"host_config_key": "foobar"}))