2
2
import json
3
3
import sys
4
4
import os
5
- from unittest .mock import patch , MagicMock
6
5
sys .path .append (os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))))
7
6
8
- from ansible_link .ansible_link import app , load_config , validate_playbook
7
+ from ansible_link .ansible_link import app , load_config
9
8
10
9
class TestAnsibleLink (unittest .TestCase ):
11
10
def setUp (self ):
12
11
self .app = app .test_client ()
13
12
self .app .testing = True
14
- self .config = {
15
- 'playbook_dir' : '/tmp/playbooks' ,
16
- 'inventory_file' : '/tmp/inventory.ini' ,
17
- 'playbook_whitelist' : [r'test_.*\.yml' , r'production_.*\.yaml' ]
18
- }
19
13
20
14
def test_health_check (self ):
21
15
response = self .app .get ('/health' )
22
16
self .assertEqual (response .status_code , 200 )
23
17
data = json .loads (response .data )
24
18
self .assertEqual (data ['status' ], 'healthy' )
25
19
26
- @patch ('ansible_link.ansible_link.load_config' )
27
- def test_load_config (self , mock_load_config ):
28
- mock_load_config .return_value = self .config
20
+ def test_load_config (self ):
29
21
test_config = load_config ()
30
22
self .assertIsInstance (test_config , dict )
31
23
self .assertIn ('playbook_dir' , test_config )
32
24
self .assertIn ('inventory_file' , test_config )
33
- self .assertIn ('playbook_whitelist' , test_config )
34
25
35
- @patch ('ansible_link.ansible_link.validate_playbook' )
36
- @patch ('ansible_link.ansible_link.Path' )
37
- def test_playbook_endpoint (self , mock_path , mock_validate_playbook ):
38
- mock_path .return_value .is_file .return_value = True
39
- mock_validate_playbook .return_value = '/tmp/playbooks/test_playbook.yml'
40
-
26
+ def test_playbook_endpoint (self ):
41
27
payload = {
42
28
'playbook' : 'test_playbook.yml' ,
43
29
'inventory' : 'test_inventory.ini' ,
44
30
'vars' : {'test_var' : 'test_value' }
45
31
}
46
- response = self .app .post ('/api/v1/ ansible/playbook' , json = payload )
32
+ response = self .app .post ('/ansible/playbook' , json = payload )
47
33
self .assertEqual (response .status_code , 202 )
48
34
data = json .loads (response .data )
49
35
self .assertIn ('job_id' , data )
50
36
self .assertEqual (data ['status' ], 'running' )
51
37
52
38
def test_jobs_endpoint (self ):
53
- response = self .app .get ('/api/v1/ ansible/jobs' )
39
+ response = self .app .get ('/ansible/jobs' )
54
40
self .assertEqual (response .status_code , 200 )
55
41
data = json .loads (response .data )
56
42
self .assertIsInstance (data , dict )
57
43
58
- @patch ('ansible_link.ansible_link.validate_playbook' )
59
- @patch ('ansible_link.ansible_link.Path' )
60
- def test_job_endpoint (self , mock_path , mock_validate_playbook ):
61
- mock_path .return_value .is_file .return_value = True
62
- mock_validate_playbook .return_value = '/tmp/playbooks/test_playbook.yml'
63
-
44
+ def test_job_endpoint (self ):
64
45
payload = {'playbook' : 'test_playbook.yml' }
65
- response = self .app .post ('/api/v1/ ansible/playbook' , json = payload )
46
+ response = self .app .post ('/ansible/playbook' , json = payload )
66
47
job_id = json .loads (response .data )['job_id' ]
67
48
68
- response = self .app .get (f'/api/v1/ ansible/job/{ job_id } ' )
49
+ response = self .app .get (f'/ansible/job/{ job_id } ' )
69
50
self .assertEqual (response .status_code , 200 )
70
51
data = json .loads (response .data )
71
52
self .assertIn ('status' , data )
72
53
73
- @patch ('ansible_link.ansible_link.validate_playbook' )
74
- @patch ('ansible_link.ansible_link.Path' )
75
- def test_job_output_endpoint (self , mock_path , mock_validate_playbook ):
76
- mock_path .return_value .is_file .return_value = True
77
- mock_validate_playbook .return_value = '/tmp/playbooks/test_playbook.yml'
78
-
54
+ def test_job_output_endpoint (self ):
79
55
payload = {'playbook' : 'test_playbook.yml' }
80
- response = self .app .post ('/api/v1/ ansible/playbook' , json = payload )
56
+ response = self .app .post ('/ansible/playbook' , json = payload )
81
57
job_id = json .loads (response .data )['job_id' ]
82
58
83
- response = self .app .get (f'/api/v1/ansible/job/{ job_id } /output' )
84
- self .assertEqual (response .status_code , 202 ) # Assuming job is still running
85
-
86
- @patch ('ansible_link.ansible_link.Path' )
87
- def test_playbook_whitelist (self , mock_path ):
88
- mock_path .return_value .is_file .return_value = True
89
- mock_path .return_value .suffix = '.yml'
90
-
91
- # valid playbooks
92
- self .assertEqual (validate_playbook ('test_playbook.yml' ), '/tmp/playbooks/test_playbook.yml' )
93
- self .assertEqual (validate_playbook ('production_playbook.yaml' ), '/tmp/playbooks/production_playbook.yaml' )
94
-
95
- # invalid playbooks
96
- with self .assertRaises (ValueError ):
97
- validate_playbook ('invalid_playbook.yml' )
98
- with self .assertRaises (ValueError ):
99
- validate_playbook ('test_playbook.yaml' ) # yaml should error
100
-
101
- def test_invalid_playbook_request (self ):
102
- payload = {
103
- 'playbook' : 'invalid_playbook.yml' ,
104
- 'inventory' : 'non_existent_inventory.ini' ,
105
- 'vars' : 'invalid_vars' # should be dict
106
- }
107
- response = self .app .post ('/api/v1/ansible/playbook' , json = payload )
108
- self .assertEqual (response .status_code , 400 )
109
- data = json .loads (response .data )
110
- self .assertIn ('errors' , data )
111
- self .assertTrue (any ('whitelist' in error for error in data ['errors' ]))
112
- self .assertTrue (any ('vars' in error for error in data ['errors' ]))
113
-
114
- @patch ('ansible_link.ansible_link.validate_playbook' )
115
- @patch ('ansible_link.ansible_link.Path' )
116
- def test_playbook_with_tags (self , mock_path , mock_validate_playbook ):
117
- mock_path .return_value .is_file .return_value = True
118
- mock_validate_playbook .return_value = '/tmp/playbooks/test_playbook.yml'
119
-
120
- payload = {
121
- 'playbook' : 'test_playbook.yml' ,
122
- 'tags' : 'tag1,tag2' ,
123
- 'skip_tags' : 'tag3,tag4'
124
- }
125
- response = self .app .post ('/api/v1/ansible/playbook' , json = payload )
126
- self .assertEqual (response .status_code , 202 )
127
- data = json .loads (response .data )
128
- self .assertIn ('job_id' , data )
129
-
130
- def test_version_endpoint (self ):
131
- response = self .app .get ('/version' )
132
- self .assertEqual (response .status_code , 200 )
133
- data = json .loads (response .data )
134
- self .assertIn ('version' , data )
59
+ response = self .app .get (f'/ansible/job/{ job_id } /output' )
135
60
136
61
if __name__ == '__main__' :
137
62
unittest .main ()
0 commit comments