summaryrefslogtreecommitdiff
path: root/tempest/tests/lib/cmd/test_check_uuid.py
blob: edfb2c801d5e42a16a9df5b2f07e0039e408d080 (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
# 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 ast
import importlib
import os
import shutil
import sys
import tempfile
from unittest import mock

from tempest.lib.cmd import check_uuid
from tempest.tests import base


class TestCLInterface(base.TestCase):
    CODE = "import unittest\n" \
           "class TestClass(unittest.TestCase):\n" \
           "    def test_tests(self):\n" \
           "        pass"

    def setUp(self):
        super(TestCLInterface, self).setUp()
        self.directory = tempfile.mkdtemp(prefix='check-uuid', dir=".")
        self.addCleanup(shutil.rmtree, self.directory, ignore_errors=True)

        init_file = open(self.directory + "/__init__.py", "w")
        init_file.close()

        self.tests_file = self.directory + "/tests.py"
        with open(self.tests_file, "w") as fake_file:
            fake_file.write(TestCLInterface.CODE)
            fake_file.close()

    def test_fix_argument_no(self):
        sys.argv = [sys.argv[0]] + ["--package",
                                    os.path.relpath(self.directory)]

        self.assertRaises(SystemExit, check_uuid.run)
        with open(self.tests_file, "r") as f:
            self.assertTrue(TestCLInterface.CODE == f.read())

    def test_fix_argument_yes(self):

        sys.argv = [sys.argv[0]] + ["--fix", "--package",
                                    os.path.relpath(self.directory)]
        check_uuid.run()
        with open(self.tests_file, "r") as f:
            self.assertTrue(TestCLInterface.CODE != f.read())


class TestSourcePatcher(base.TestCase):
    def test_add_patch(self):
        patcher = check_uuid.SourcePatcher()
        fake_file = tempfile.NamedTemporaryFile("w+t", delete=False)
        file_contents = 'first_line\nsecond_line'
        fake_file.write(file_contents)
        fake_file.close()
        patcher.add_patch(fake_file.name, 'patch', 2)

        source_file = patcher.source_files[fake_file.name]
        self.assertEqual(1, len(patcher.patches))
        (patch_id, patch), = patcher.patches.items()
        self.assertEqual(patcher._quote('patch\n'), patch)
        self.assertEqual('first_line\n{%s:s}second_line' % patch_id,
                         patcher._unquote(source_file))

    def test_apply_patches(self):
        fake_file = tempfile.NamedTemporaryFile("w+t")
        patcher = check_uuid.SourcePatcher()
        patcher.patches = {'fake-uuid': patcher._quote('patch\n')}
        patcher.source_files = {
            fake_file.name: patcher._quote('first_line\n') +
            '{fake-uuid:s}second_line'}
        with mock.patch('sys.stdout'):
            patcher.apply_patches()

        lines = fake_file.read().split('\n')
        fake_file.close()
        self.assertEqual(['first_line', 'patch', 'second_line'], lines)
        self.assertFalse(patcher.patches)
        self.assertFalse(patcher.source_files)


class TestTestChecker(base.TestCase):
    IMPORT_LINE = "from tempest.lib import decorators\n"

    def _test_add_uuid_to_test(self, source_file):
        class Fake_test_node():
            lineno = 1
            col_offset = 4
        patcher = check_uuid.SourcePatcher()
        checker = check_uuid.TestChecker(importlib.import_module('tempest'))
        fake_file = tempfile.NamedTemporaryFile("w+t", delete=False)
        fake_file.write(source_file)
        fake_file.close()
        checker._add_uuid_to_test(patcher, Fake_test_node(), fake_file.name)

        self.assertEqual(1, len(patcher.patches))
        self.assertEqual(1, len(patcher.source_files))
        (patch_id, patch), = patcher.patches.items()
        changed_source_file, = patcher.source_files.values()
        self.assertEqual('{%s:s}%s' % (patch_id, patcher._quote(source_file)),
                         changed_source_file)
        expected_patch_start = patcher._quote(
            '    ' + check_uuid.DECORATOR_TEMPLATE.split('(')[0])
        self.assertTrue(patch.startswith(expected_patch_start))

    def test_add_uuid_to_test_def(self):
        source_file = ("    def test_test():\n"
                       "        pass")
        self._test_add_uuid_to_test(source_file)

    def test_add_uuid_to_test_decorator(self):
        source_file = ("    @decorators.idempotent_id\n"
                       "    def test_test():\n"
                       "        pass")
        self._test_add_uuid_to_test(source_file)

    @staticmethod
    def get_mocked_ast_object(lineno, col_offset, module, name, object_type):
        ast_object = mock.Mock(spec=object_type)
        name_obj = mock.Mock()
        ast_object.lineno = lineno
        ast_object.col_offset = col_offset
        name_obj.name = name
        ast_object.module = module
        ast_object.names = [name_obj]

        return ast_object

    def test_add_import_for_test_uuid_no_tempest(self):
        patcher = check_uuid.SourcePatcher()
        checker = check_uuid.TestChecker(importlib.import_module('tempest'))
        fake_file = tempfile.NamedTemporaryFile("w+t", delete=False)
        source_code = "from unittest import mock\n"
        fake_file.write(source_code)
        fake_file.close()

        class Fake_src_parsed():
            body = [TestTestChecker.get_mocked_ast_object(
                1, 4, 'unittest', 'mock', ast.ImportFrom)]

        checker._add_import_for_test_uuid(patcher, Fake_src_parsed,
                                          fake_file.name)
        patcher.apply_patches()

        with open(fake_file.name, "r") as f:
            expected_result = source_code + '\n' + TestTestChecker.IMPORT_LINE
            self.assertTrue(expected_result == f.read())

    def test_add_import_for_test_uuid_tempest(self):
        patcher = check_uuid.SourcePatcher()
        checker = check_uuid.TestChecker(importlib.import_module('tempest'))
        fake_file = tempfile.NamedTemporaryFile("w+t", delete=False)
        source_code = "from tempest import a_fake_module\n"
        fake_file.write(source_code)
        fake_file.close()

        class Fake_src_parsed:
            body = [TestTestChecker.get_mocked_ast_object(
                1, 4, 'tempest', 'a_fake_module', ast.ImportFrom)]

        checker._add_import_for_test_uuid(patcher, Fake_src_parsed,
                                          fake_file.name)
        patcher.apply_patches()

        with open(fake_file.name, "r") as f:
            expected_result = source_code + TestTestChecker.IMPORT_LINE
            self.assertTrue(expected_result == f.read())

    def test_add_import_no_import(self):
        patcher = check_uuid.SourcePatcher()
        patcher.add_patch = mock.Mock()
        checker = check_uuid.TestChecker(importlib.import_module('tempest'))
        fake_file = tempfile.NamedTemporaryFile("w+t", delete=False)
        fake_file.close()

        class Fake_src_parsed:
            body = []

        checker._add_import_for_test_uuid(patcher, Fake_src_parsed,
                                          fake_file.name)

        self.assertTrue(not patcher.add_patch.called)