Completed
Pull Request — master (#2842)
by Edward
06:44
created

test_run_pack_download_existing_pack()   A

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
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
23
from git.repo import Repo
24
from st2common.services import packs as pack_service
25
from st2tests.base import BaseActionTestCase
26
27
from pack_mgmt.download import DownloadGitRepoAction
28
29
PACK_INDEX = {
30
    "test": {
31
        "version": "0.4",
32
        "name": "test",
33
        "repo_url": "https://github.com/StackStorm-Exchange/stackstorm-test",
34
        "author": "st2-dev",
35
        "keywords": ["some", "search", "another", "terms"],
36
        "email": "[email protected]",
37
        "description": "st2 pack to test package management pipeline"
38
    },
39
    "test2": {
40
        "version": "0.5",
41
        "name": "test2",
42
        "repo_url": "https://github.com/StackStorm-Exchange/stackstorm-test2",
43
        "author": "stanley",
44
        "keywords": ["some", "special", "terms"],
45
        "email": "[email protected]",
46
        "description": "another st2 pack to test package management pipeline"
47
    }
48
}
49
50
51
@mock.patch.object(pack_service, 'fetch_pack_index', mock.MagicMock(return_value=PACK_INDEX))
52
@mock.patch.object(os.path, 'expanduser', mock.MagicMock(return_value=tempfile.mkdtemp()))
53
class DownloadGitRepoActionTestCase(BaseActionTestCase):
54
    action_cls = DownloadGitRepoAction
55
56
    def setUp(self):
57
        super(DownloadGitRepoActionTestCase, self).setUp()
58
59
        clone_from = mock.patch.object(Repo, 'clone_from')
60
61
        self.addCleanup(clone_from.stop)
62
        self.clone_from = clone_from.start()
63
64
        self.repo_base = tempfile.mkdtemp()
65
66
        def side_effect(url, to_path, **kwargs):
67
            # Since we have no way to pass pack name here, we would have to derive it from repo url
68
            fixture_name = url.split('/')[-1]
69
            fixture_path = os.path.join(self._get_base_pack_path(), 'tests/fixtures', fixture_name)
70
            shutil.copytree(fixture_path, to_path)
71
72
        self.clone_from.side_effect = side_effect
73
74
    def tearDown(self):
75
        shutil.rmtree(self.repo_base)
76
77
    def test_run_pack_download(self):
78
        action = self.get_action_instance()
79
        result = action.run(packs=['test'], abs_repo_base=self.repo_base)
80
81
        self.assertEqual(result, {'test': 'Success.'})
82
        self.clone_from.assert_called_once_with(PACK_INDEX['test']['repo_url'],
83
                                           os.path.join(os.path.expanduser('~'), 'test'),
84
                                           branch='master')
85
        self.assertTrue(os.path.isfile(os.path.join(self.repo_base, 'test/pack.yaml')))
86
87
    def test_run_pack_download_existing_pack(self):
88
        action = self.get_action_instance()
89
        action.run(packs=['test'], abs_repo_base=self.repo_base)
90
        self.assertTrue(os.path.isfile(os.path.join(self.repo_base, 'test/pack.yaml')))
91
92
        result = action.run(packs=['test'], abs_repo_base=self.repo_base)
93
94
        self.assertEqual(result, {'test': 'Success.'})
95
96
    def test_run_pack_download_multiple_packs(self):
97
        action = self.get_action_instance()
98
        result = action.run(packs=['test', 'test2'], abs_repo_base=self.repo_base)
99
100
        self.assertEqual(result, {'test': 'Success.', 'test2': 'Success.'})
101
        self.clone_from.assert_any_call(PACK_INDEX['test']['repo_url'],
102
                                        os.path.join(os.path.expanduser('~'), 'test'),
103
                                        branch='master')
104
        self.clone_from.assert_any_call(PACK_INDEX['test2']['repo_url'],
105
                                        os.path.join(os.path.expanduser('~'), 'test2'),
106
                                        branch='master')
107
        self.assertEqual(self.clone_from.call_count, 2)
108
        self.assertTrue(os.path.isfile(os.path.join(self.repo_base, 'test/pack.yaml')))
109
        self.assertTrue(os.path.isfile(os.path.join(self.repo_base, 'test2/pack.yaml')))
110
111
    @mock.patch.object(Repo, 'clone_from')
112
    def test_run_pack_download_error(self, clone_from):
113
        clone_from.side_effect = Exception('Something went terribly wrong during the clone')
114
115
        action = self.get_action_instance()
116
        self.assertRaises(Exception, action.run, packs=['test'], abs_repo_base=self.repo_base)
117