1
|
|
|
#!/usr/bin/env python |
2
|
|
|
|
3
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
4
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
5
|
|
|
# this work for additional information regarding copyright ownership. |
6
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
7
|
|
|
# (the "License"); you may not use this file except in compliance with |
8
|
|
|
# the License. You may obtain a copy of the License at |
9
|
|
|
# |
10
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
# |
12
|
|
|
# Unless required by applicable law or agreed to in writing, software |
13
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
# See the License for the specific language governing permissions and |
16
|
|
|
# limitations under the License. |
17
|
|
|
|
18
|
|
|
import mock |
19
|
|
|
import os |
20
|
|
|
import shutil |
21
|
|
|
import tempfile |
22
|
|
|
import hashlib |
23
|
|
|
|
24
|
|
|
from git.repo import Repo |
25
|
|
|
from st2common.services import packs as pack_service |
26
|
|
|
from st2tests.base import BaseActionTestCase |
27
|
|
|
|
28
|
|
|
from pack_mgmt.download import DownloadGitRepoAction |
29
|
|
|
|
30
|
|
|
PACK_INDEX = { |
31
|
|
|
"test": { |
32
|
|
|
"version": "0.4", |
33
|
|
|
"name": "test", |
34
|
|
|
"repo_url": "https://github.com/StackStorm-Exchange/stackstorm-test", |
35
|
|
|
"author": "st2-dev", |
36
|
|
|
"keywords": ["some", "search", "another", "terms"], |
37
|
|
|
"email": "[email protected]", |
38
|
|
|
"description": "st2 pack to test package management pipeline" |
39
|
|
|
}, |
40
|
|
|
"test2": { |
41
|
|
|
"version": "0.5", |
42
|
|
|
"name": "test2", |
43
|
|
|
"repo_url": "https://github.com/StackStorm-Exchange/stackstorm-test2", |
44
|
|
|
"author": "stanley", |
45
|
|
|
"keywords": ["some", "special", "terms"], |
46
|
|
|
"email": "[email protected]", |
47
|
|
|
"description": "another st2 pack to test package management pipeline" |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
@mock.patch.object(pack_service, 'fetch_pack_index', mock.MagicMock(return_value=PACK_INDEX)) |
53
|
|
|
@mock.patch.object(os.path, 'expanduser', mock.MagicMock(return_value=tempfile.mkdtemp())) |
54
|
|
|
class DownloadGitRepoActionTestCase(BaseActionTestCase): |
55
|
|
|
action_cls = DownloadGitRepoAction |
56
|
|
|
|
57
|
|
|
def setUp(self): |
58
|
|
|
super(DownloadGitRepoActionTestCase, self).setUp() |
59
|
|
|
|
60
|
|
|
clone_from = mock.patch.object(Repo, 'clone_from') |
61
|
|
|
|
62
|
|
|
self.addCleanup(clone_from.stop) |
63
|
|
|
self.clone_from = clone_from.start() |
64
|
|
|
|
65
|
|
|
self.repo_base = tempfile.mkdtemp() |
66
|
|
|
|
67
|
|
|
def side_effect(url, to_path, **kwargs): |
68
|
|
|
# Since we have no way to pass pack name here, we would have to derive it from repo url |
69
|
|
|
fixture_name = url.split('/')[-1] |
70
|
|
|
fixture_path = os.path.join(self._get_base_pack_path(), 'tests/fixtures', fixture_name) |
71
|
|
|
shutil.copytree(fixture_path, to_path) |
72
|
|
|
|
73
|
|
|
self.clone_from.side_effect = side_effect |
74
|
|
|
|
75
|
|
|
def tearDown(self): |
76
|
|
|
shutil.rmtree(self.repo_base) |
77
|
|
|
|
78
|
|
|
def test_run_pack_download(self): |
79
|
|
|
action = self.get_action_instance() |
80
|
|
|
result = action.run(packs=['test'], abs_repo_base=self.repo_base) |
81
|
|
|
temp_dir = hashlib.md5(PACK_INDEX['test']['repo_url']).hexdigest() |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
self.assertEqual(result, {'test': 'Success.'}) |
85
|
|
|
self.clone_from.assert_called_once_with(PACK_INDEX['test']['repo_url'], |
86
|
|
|
os.path.join(os.path.expanduser('~'), temp_dir), |
87
|
|
|
branch='master') |
88
|
|
|
self.assertTrue(os.path.isfile(os.path.join(self.repo_base, 'test/pack.yaml'))) |
89
|
|
|
|
90
|
|
|
def test_run_pack_download_existing_pack(self): |
91
|
|
|
action = self.get_action_instance() |
92
|
|
|
action.run(packs=['test'], abs_repo_base=self.repo_base) |
93
|
|
|
self.assertTrue(os.path.isfile(os.path.join(self.repo_base, 'test/pack.yaml'))) |
94
|
|
|
|
95
|
|
|
result = action.run(packs=['test'], abs_repo_base=self.repo_base) |
96
|
|
|
|
97
|
|
|
self.assertEqual(result, {'test': 'Success.'}) |
98
|
|
|
|
99
|
|
|
def test_run_pack_download_multiple_packs(self): |
100
|
|
|
action = self.get_action_instance() |
101
|
|
|
result = action.run(packs=['test', 'test2'], abs_repo_base=self.repo_base) |
102
|
|
|
temp_dirs = [ |
103
|
|
|
hashlib.md5(PACK_INDEX['test']['repo_url']).hexdigest(), |
104
|
|
|
hashlib.md5(PACK_INDEX['test2']['repo_url']).hexdigest() |
105
|
|
|
] |
106
|
|
|
|
107
|
|
|
self.assertEqual(result, {'test': 'Success.', 'test2': 'Success.'}) |
108
|
|
|
self.clone_from.assert_any_call(PACK_INDEX['test']['repo_url'], |
109
|
|
|
os.path.join(os.path.expanduser('~'), temp_dirs[0]), |
110
|
|
|
branch='master') |
111
|
|
|
self.clone_from.assert_any_call(PACK_INDEX['test2']['repo_url'], |
112
|
|
|
os.path.join(os.path.expanduser('~'), temp_dirs[1]), |
113
|
|
|
branch='master') |
114
|
|
|
self.assertEqual(self.clone_from.call_count, 2) |
115
|
|
|
self.assertTrue(os.path.isfile(os.path.join(self.repo_base, 'test/pack.yaml'))) |
116
|
|
|
self.assertTrue(os.path.isfile(os.path.join(self.repo_base, 'test2/pack.yaml'))) |
117
|
|
|
|
118
|
|
|
@mock.patch.object(Repo, 'clone_from') |
119
|
|
|
def test_run_pack_download_error(self, clone_from): |
120
|
|
|
clone_from.side_effect = Exception('Something went terribly wrong during the clone') |
121
|
|
|
|
122
|
|
|
action = self.get_action_instance() |
123
|
|
|
self.assertRaises(Exception, action.run, packs=['test'], abs_repo_base=self.repo_base) |
124
|
|
|
|