~psivaa/uci-engine/lander-upstart-logs-to-srv

« back to all changes in this revision

Viewing changes to cupstream2distro/tests/unit/test_tools.py

  • Committer: Francis Ginther
  • Date: 2014-06-10 20:42:46 UTC
  • mto: This revision was merged to the branch mainline in revision 571.
  • Revision ID: francis.ginther@canonical.com-20140610204246-b1bsrik7nlcolqy7
Import lp:cupstream2distro rev 605.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright: (C) 2013 Canonical
 
3
#
 
4
# Authors:
 
5
#  Didier Roche
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify it under
 
8
# the terms of the GNU General Public License as published by the Free Software
 
9
# Foundation; version 3.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but WITHOUT
 
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
14
# details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along with
 
17
# this program; if not, write to the Free Software Foundation, Inc.,
 
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
19
 
 
20
from . import BaseUnitTestCase
 
21
 
 
22
from cupstream2distro import tools
 
23
 
 
24
import os
 
25
import shutil
 
26
 
 
27
 
 
28
class MockMerge:
 
29
    def __init__(self, source_branch, prereq_branch=None):
 
30
        self.source_branch = MockBranch(source_branch)
 
31
        if prereq_branch:
 
32
            self.prerequisite_branch = MockBranch(prereq_branch)
 
33
        else:
 
34
            self.prerequisite_branch = None
 
35
 
 
36
class MockBranch:
 
37
    def __init__(self, bzr_identity):
 
38
        self.bzr_identity = bzr_identity
 
39
    
 
40
class ToolsTests(BaseUnitTestCase):
 
41
 
 
42
    def setUp(self):
 
43
        '''add some default files'''
 
44
        super(ToolsTests, self).setUp()
 
45
        self.artefacts_dir = os.path.join(self.data_dir, 'artefacts')
 
46
 
 
47
    def test_get_previous_distro_version_from_config(self):
 
48
        '''We load and return the previous distro version from config'''
 
49
        shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
 
50
        self.assertEquals(tools.get_previous_distro_version_from_config('foo'), '6.12.0-0ubuntu1')
 
51
 
 
52
    def test_return_exception_if_config_doesnt_exist(self):
 
53
        '''We return an exception if the config files does not exist'''
 
54
        with self.assertRaises(Exception):
 
55
            tools.get_previous_distro_version_from_config('foo')
 
56
 
 
57
    def test_save_correct_project_file(self):
 
58
        '''We save the correct profiles file'''
 
59
        tools.save_project_config('foo', 'lp:foo', '42', '6.12.0-0ubuntu1', '6.12.0daily13.02.27-0ubuntu1')
 
60
        self.assertFilesAreIdenticals('foo.project', os.path.join(self.project_file_dir, 'foo.project'))
 
61
 
 
62
    def test_packaging_diff_filename(self):
 
63
        '''Return the right packaging diff name'''
 
64
        self.assertEquals(tools.get_packaging_diff_filename("foo", "1.0daily~-0ubuntu1"), "packaging_changes_foo_1.0daily~-0ubuntu1.diff")
 
65
 
 
66
    def test_generate_xml_artefacts_no_issue(self):
 
67
        '''Generate the xml jenkins artefacts when no issue occured'''
 
68
        tools.generate_xml_artefacts("Test Name", [], 'file.xml')
 
69
        self.assertFilesAreIdenticals('file.xml', os.path.join(self.artefacts_dir, 'nofailure.xml'))
 
70
 
 
71
    def test_generate_xml_artefacts_one_failure(self):
 
72
        '''Generate the xml jenkins artefacts when there is one failure'''
 
73
        tools.generate_xml_artefacts("Test Name", ["one issue"], 'file.xml')
 
74
        self.assertFilesAreIdenticals('file.xml', os.path.join(self.artefacts_dir, 'onefailure.xml'))
 
75
 
 
76
    def test_generate_xml_artefacts_two_failures(self):
 
77
        '''Generate the xml jenkins artefacts when there is more than one failure'''
 
78
        tools.generate_xml_artefacts("Test Name", ["one issue", "a second issue"], 'file.xml')
 
79
        self.assertFilesAreIdenticals('file.xml', os.path.join(self.artefacts_dir, 'twofailures.xml'))
 
80
 
 
81
    def test_mark_project_as_published(self):
 
82
        '''Rename current project filename once published to the unique project file to not republish it'''
 
83
        shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
 
84
        tools.mark_project_as_published('foo', '4.2dailysomething-0ubuntu1')
 
85
        self.assertTrue(os.path.isfile('foo.project_4.2dailysomething-0ubuntu1'))
 
86
 
 
87
    def test_mark_project_and_diff_as_published(self):
 
88
        '''Rename current project filename and diff file once published to the unique project file to not republish them'''
 
89
        shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
 
90
        shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), 'packaging_changes_foo_4.2dailysomething-0ubuntu1.diff')
 
91
        tools.mark_project_as_published('foo', '4.2dailysomething-0ubuntu1')
 
92
        self.assertTrue(os.path.isfile('packaging_changes_foo_4.2dailysomething-0ubuntu1.diff.published'))
 
93
 
 
94
    def test_no_project_published(self):
 
95
        '''Get no project published if nothing is published'''
 
96
        self.assertEquals(tools.get_published_to_distro_projects(), {})
 
97
 
 
98
    def test_one_project_published(self):
 
99
        '''Get one project published with one version'''
 
100
        shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
 
101
        tools.mark_project_as_published('foo', '42-0ubuntu1')
 
102
        self.assertEquals(tools.get_published_to_distro_projects(), {'foo': ['42-0ubuntu1']})
 
103
 
 
104
    def test_one_project_published_multiple_times(self):
 
105
        '''Get one project published with multiple published versions'''
 
106
        shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
 
107
        tools.mark_project_as_published('foo', '42-0ubuntu1')
 
108
        shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
 
109
        tools.mark_project_as_published('foo', '43-0ubuntu1')
 
110
        self.assertDictEqual(tools.get_published_to_distro_projects(), {'foo': ['43-0ubuntu1', '42-0ubuntu1']})
 
111
 
 
112
    def test_multiple_projects_published(self):
 
113
        '''Get one project published with multiple published versions'''
 
114
        shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), '.')
 
115
        tools.mark_project_as_published('foo', '42-0ubuntu1')
 
116
        shutil.copy2(os.path.join(self.project_file_dir, 'foo.project'), 'bar.project')
 
117
        tools.mark_project_as_published('bar', '43-0ubuntu1')
 
118
        self.assertDictEqual(tools.get_published_to_distro_projects(), {'foo': ['42-0ubuntu1'], 'bar': ['43-0ubuntu1']})
 
119
 
 
120
    def test_parse_and_clean_entry_space(self):
 
121
        '''Get an entry only separated by spaces'''
 
122
        self.assertEqual(tools.parse_and_clean_entry("foo1 foo2 foo3"),
 
123
                         ["foo1", "foo2", "foo3"])
 
124
 
 
125
    def test_parse_and_clean_entry_comma(self):
 
126
        '''Get an entry only separated by commas'''
 
127
        self.assertEqual(tools.parse_and_clean_entry("foo1, foo2, foo3"),
 
128
                         ["foo1", "foo2", "foo3"])
 
129
 
 
130
    def test_parse_and_clean_entry_comma_and_spaces(self):
 
131
        '''Get an entry only separated with a sep (commas) and a lot of additional spaces'''
 
132
        self.assertEqual(tools.parse_and_clean_entry("    foo1,    foo2  , foo3"),
 
133
                         ["foo1", "foo2", "foo3"])
 
134
 
 
135
 
 
136
    def test_parse_and_clean_entry_slash(self):
 
137
        '''Get an entry only separated by slash'''
 
138
        self.assertEqual(tools.parse_and_clean_entry("foo1/foo2/foo3", slash_as_sep=True),
 
139
                         ["foo1", "foo2", "foo3"])
 
140
 
 
141
    def test_parse_and_clean_entry_return(self):
 
142
        '''Get an entry only separated by commas and space'''
 
143
        self.assertEqual(tools.parse_and_clean_entry("foo1\nfoo2\nfoo3"),
 
144
                         ["foo1", "foo2", "foo3"])
 
145
 
 
146
    def test_parse_and_clean_entry_all_sep_but_slash(self):
 
147
        '''Get an entry only separated by all possible sep'''
 
148
        self.assertEqual(tools.parse_and_clean_entry("foo1, foo2\nfoo3\n foo4"),
 
149
                         ["foo1", "foo2", "foo3", "foo4"])
 
150
 
 
151
    def test_parse_and_clean_entry_all_sep(self):
 
152
        '''Get an entry only separated by all possible sep'''
 
153
        self.assertEqual(tools.parse_and_clean_entry("foo1, foo2\nfoo3\n foo4 / foo5  ", slash_as_sep=True),
 
154
                         ["foo1", "foo2", "foo3", "foo4", "foo5"])
 
155
 
 
156
    def test_parse_and_clean_entry_no_slash_sep(self):
 
157
        '''Get an entry only separated by commas. Entries contains slash and shouldn't be separated'''
 
158
        self.assertEqual(tools.parse_and_clean_entry("fo/o1, foo2, foo3"),
 
159
                         ["fo/o1", "foo2", "foo3"])
 
160
 
 
161
    def test_reorder_branches_regarding_prereqs(self):
 
162
        '''Check reorder projects according to prerequisite branches'''
 
163
        original_component_list = [ MockMerge("0"), MockMerge("2", "1"), MockMerge("1"), MockMerge("4"), MockMerge("3", "2") ]
 
164
        resulting_component_list = tools.reorder_branches_regarding_prereqs(original_component_list)
 
165
        self.assertEqual(len(resulting_component_list), 5)
 
166
        self.assertEqual(resulting_component_list[0].source_branch.bzr_identity, "0")
 
167
        self.assertEqual(resulting_component_list[1].source_branch.bzr_identity, "1")
 
168
        self.assertEqual(resulting_component_list[2].source_branch.bzr_identity, "2")
 
169
        self.assertEqual(resulting_component_list[3].source_branch.bzr_identity, "3")
 
170
        self.assertEqual(resulting_component_list[4].source_branch.bzr_identity, "4")
 
171
 
 
172
    def test_reorder_branches_regarding_prereqs_no_change(self):
 
173
        '''Check that no reorder happens when there are no prerequisite branches used'''
 
174
        original_component_list = [ MockMerge("1"), MockMerge("2"), MockMerge("3") ]
 
175
        resulting_component_list = tools.reorder_branches_regarding_prereqs(original_component_list)
 
176
        self.assertEqual(len(resulting_component_list), 3)
 
177
        self.assertEqual(resulting_component_list[0].source_branch.bzr_identity, "1")
 
178
        self.assertEqual(resulting_component_list[1].source_branch.bzr_identity, "2")
 
179
        self.assertEqual(resulting_component_list[2].source_branch.bzr_identity, "3")
 
 
b'\\ No newline at end of file'