|
1
|
|
|
import os |
|
2
|
|
|
import string |
|
3
|
|
|
import random |
|
4
|
|
|
import shutil |
|
5
|
|
|
|
|
6
|
|
|
from st2tests.base import BaseSensorTestCase |
|
7
|
|
|
|
|
8
|
|
|
from git_commit_sensor import GitCommitSensor |
|
9
|
|
|
|
|
10
|
|
|
TEST_CONFIG = { |
|
11
|
|
|
'repositories': [ |
|
12
|
|
|
{ |
|
13
|
|
|
'url': 'https://github.com/StackStorm/st2', |
|
14
|
|
|
'branch': 'master', |
|
15
|
|
|
'local_clone_path': '/tmp/.test_st2_', |
|
16
|
|
|
}, |
|
17
|
|
|
{ |
|
18
|
|
|
'url': 'https://github.com/StackStorm/st2contrib.git', |
|
19
|
|
|
'branch': 'master', |
|
20
|
|
|
'local_clone_path': '/tmp/.test_st2contrib_', |
|
21
|
|
|
}, |
|
22
|
|
|
], |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class GitCommitSensorTestCase(BaseSensorTestCase): |
|
27
|
|
|
sensor_cls = GitCommitSensor |
|
28
|
|
|
|
|
29
|
|
|
# A helper method to generate cloned directory name |
|
30
|
|
|
def _get_unique_path(self, basepath): |
|
31
|
|
|
path = basepath + random.choice(string.ascii_letters) |
|
32
|
|
|
if os.path.exists(path): |
|
33
|
|
|
path = self._get_unique_path(path) |
|
34
|
|
|
return path |
|
35
|
|
|
|
|
36
|
|
|
def tearDown(self): |
|
37
|
|
|
for repo in TEST_CONFIG['repositories']: |
|
38
|
|
|
shutil.rmtree(repo['local_clone_path']) |
|
39
|
|
|
|
|
40
|
|
|
def test_setup(self): |
|
41
|
|
|
# prepare the configuration params |
|
42
|
|
|
for repo in TEST_CONFIG['repositories']: |
|
43
|
|
|
repo['local_clone_path'] = self._get_unique_path(repo['local_clone_path']) |
|
44
|
|
|
|
|
45
|
|
|
sensor = self.get_sensor_instance(config=TEST_CONFIG) |
|
46
|
|
|
|
|
47
|
|
|
for repo in TEST_CONFIG['repositories']: |
|
48
|
|
|
self.assertFalse(os.path.exists(repo['local_clone_path'])) |
|
49
|
|
|
|
|
50
|
|
|
# will clone repositories |
|
51
|
|
|
sensor.setup() |
|
52
|
|
|
|
|
53
|
|
|
for repo in TEST_CONFIG['repositories']: |
|
54
|
|
|
self.assertTrue(os.path.exists(os.path.join(repo['local_clone_path'], '.git'))) |
|
55
|
|
|
|
|
56
|
|
|
sensor.poll() |
|
57
|
|
|
|
|
58
|
|
|
self.assertEqual(len(self.get_dispatched_triggers()), 2) |
|
59
|
|
|
self.assertTriggerDispatched(trigger='git.head_sha_monitor') |
|
60
|
|
|
|
|
61
|
|
|
# clear informations of past dispatching |
|
62
|
|
|
self.sensor_service.dispatched_triggers = [] |
|
63
|
|
|
|
|
64
|
|
|
sensor.poll() |
|
65
|
|
|
|
|
66
|
|
|
self.assertEqual(len(self.get_dispatched_triggers()), 0) |
|
67
|
|
|
|