1
# -*- coding: utf-8 -*-
2
# Copyright: (C) 2013 Canonical
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.
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
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
20
from . import BaseUnitTestCase, BaseUnitTestCaseWithErrors
24
from cupstream2distro import settings
25
from cupstream2distro.stack import Stack
28
class StackTests(BaseUnitTestCase):
29
'''Module to test one stack'''
32
'''set default stack test dir'''
33
super(StackTests, self).setUp()
34
os.environ['CONFIG_STACKS_DIR'] = os.path.join(self.data_dir, 'stack_configs', 'default')
35
self.workdir = os.path.join(self.data_dir, 'workdir', 'default')
37
def test_get_current_stack_infos(self):
38
'''Get stack infos based on current workdir path'''
39
self.create_temp_workdir()
40
path_to_create = os.path.join('head', 'stack1')
41
os.makedirs(path_to_create)
42
os.chdir(path_to_create)
43
stack = Stack('head', 'stack1')
44
self.assertEquals(Stack.get_current_stack(), stack)
46
def test_get_root_stacks_dir_default(self):
47
'''Default root stacks dir is the expected one'''
48
os.environ.pop('CONFIG_STACKS_DIR')
49
self.assertEquals(Stack.get_root_stacks_dir(),
50
os.path.join(os.path.dirname(settings.ROOT_CU2D),
51
'cupstream2distro-config', 'stacks'))
53
def test_tweak_stacks_dir(self):
54
'''Can override the stack dir with CONFIG_STACKS_DIR'''
55
self.assertEquals(Stack.get_root_stacks_dir(),
56
os.environ['CONFIG_STACKS_DIR'])
58
def test_detect_stack_files_simple(self):
59
'''Detect stack files a simple directory structure filtering the none cfg ones'''
60
simple_path = os.path.join(self.data_dir, 'stack_configs', 'simple')
61
os.environ['CONFIG_STACKS_DIR'] = simple_path
62
self.maxDiff = None # Helps diagnostic by showing the full diff
63
self.assertEquals(sorted(list(Stack.get_stacks_file_path('foo'))),
64
[os.path.join(simple_path, 'foo', 'unity.cfg'),
65
os.path.join(simple_path, 'foo', 'webapp.cfg')])
67
def test_detect_stack_files_regular(self):
68
'''Return the expected stack files in a nested environment'''
70
stack_path = os.environ['CONFIG_STACKS_DIR']
71
self.assertEquals(list(Stack.get_stacks_file_path('front')),
72
[os.path.join(stack_path, 'front', 'stack1.cfg')])
73
self.assertEquals(list(Stack.get_stacks_file_path('back')),
74
[os.path.join(stack_path, 'back', 'stack4.cfg')])
75
self.assertEquals(sorted(list(Stack.get_stacks_file_path('head'))),
76
[os.path.join(stack_path, 'head', 'stack1.cfg'),
77
os.path.join(stack_path, 'head', 'stack2.cfg'),
78
os.path.join(stack_path, 'head', 'stack3.cfg')])
80
def test_get_stack_file_path(self):
81
'''Return the right file in a nested stack environment'''
82
stack_path = os.environ['CONFIG_STACKS_DIR']
83
self.assertEquals(Stack('back', 'stack4').stack_file_path,
84
os.path.join(stack_path, 'back', 'stack4.cfg'))
86
def test_get_stack_file_path_duplicated(self):
87
'''Return the right file corresponding to the right release for duplicated filename'''
88
stack_path = os.environ['CONFIG_STACKS_DIR']
89
self.assertEquals(Stack('front', 'stack1').stack_file_path,
90
os.path.join(stack_path, 'front', 'stack1.cfg'))
91
self.assertEquals(Stack('head', 'stack1').stack_file_path,
92
os.path.join(stack_path, 'head', 'stack1.cfg'))
94
def test_stack1_not_started(self):
95
'''Ensure a non started stack isn't seen as started'''
96
shutil.copytree(self.workdir, 'workdir')
97
current_workdir = os.path.join('workdir', 'head', 'stack2')
98
os.chdir(current_workdir)
99
self.assertFalse(Stack("head", "stack1").is_started())
101
def test_stack3_not_started(self):
102
'''Ensure a started stack is seen as started'''
103
shutil.copytree(self.workdir, 'workdir')
104
current_workdir = os.path.join('workdir', 'head', 'stack2')
105
os.chdir(current_workdir)
106
self.assertTrue(Stack("head", "stack3").is_started())
108
def test_get_status(self):
109
'''Return stack status for the current stack'''
110
shutil.copytree(self.workdir, 'workdir')
111
current_workdir = os.path.join('workdir', 'head', 'stack2')
112
os.chdir(current_workdir)
113
self.assertEquals(Stack("head", "stack1").get_status(), 0)
114
self.assertEquals(Stack("head", "stack3").get_status(), 1)
116
def test_ignore_status(self):
117
'''Ensure that we return good status everytime we hit a "ignored" stack'''
118
shutil.copytree(self.workdir, 'workdir')
119
current_workdir = os.path.join('workdir', 'head', 'stack2')
120
os.chdir(current_workdir)
121
self.assertEquals(Stack("head", "stack2").get_status(), 0)
122
self.assertEquals(Stack("back", "stack4").get_status(), 0)
124
def test_get_direct_depending_stacks(self):
125
'''Get stack dependencies if they exist'''
126
stack = Stack('front', 'stack1')
127
dep_stacks = [stack, Stack('back', 'stack4')]
128
self.assertEquals(stack.get_direct_depending_stacks(), dep_stacks)
130
def test_ignore_if_no_dependencies(self):
131
'''Return nothing if there is no dependencies'''
132
self.assertEquals(Stack('head', 'stack3').get_direct_depending_stacks(), [])
134
def test_ignore_if_dependencies_empty(self):
135
'''Return an empty array if there is no dependencies'''
136
self.assertEquals(Stack('head', 'stack2').get_direct_depending_stacks(), [])
138
def test_get_direct_depending_stacks_if_no_release(self):
139
'''Get stack dependencies if there is no release specified'''
140
stack = Stack('head', 'stack1')
141
dep_stacks = [Stack('head', 'stack2'), Stack('front', 'stack1')]
142
self.assertEquals(stack.get_direct_depending_stacks(), dep_stacks)
144
def test_get_direct_rdepends_stack(self):
145
'''Get a list of direct rdepends'''
146
stack = Stack('head', 'stack2')
147
stack_rdepends = Stack('head', 'stack1')
148
self.assertEquals(stack.get_direct_rdepends_stack(), [stack_rdepends])
151
class StackTestsErrors(BaseUnitTestCaseWithErrors):
153
def test_get_exception_for_unexisting_file(self):
154
'''Return an exception if the stack file doesn't exist'''
155
with self.assertRaises(Exception):
158
def test_get_exception_for_existing_file_but_wrong_release(self):
159
'''Return an exception if the file doesn't exist for the current release'''
160
with self.assertRaises(Exception):
161
Stack('front', 'stack4')
163
def test_get_exception_for_non_existing_release_path(self):
164
'''Return an empty list if we give a non existing path for configuration'''
165
os.environ['CONFIG_STACKS_DIR'] = "/nope"
166
with self.assertRaises(Exception):