1
|
|
|
from st2tests.base import BaseActionTestCase |
2
|
|
|
|
3
|
|
|
from lib.mssql_action import MSSQLAction |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class MSSQLExampleAction(MSSQLAction): |
7
|
|
|
def run(self): |
8
|
|
|
pass |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
# noinspection PyProtectedMemberInspection |
12
|
|
|
class MSSQLActionTestCase(BaseActionTestCase): |
13
|
|
|
action_cls = MSSQLExampleAction |
14
|
|
|
|
15
|
|
|
def test_connect_none(self): |
16
|
|
|
action = self.get_action_instance() |
17
|
|
|
action.config = {} |
18
|
|
|
self.assertRaises(Exception, action._connect_params) |
19
|
|
|
|
20
|
|
|
def test_connect_config(self): |
21
|
|
|
action = self.get_action_instance() |
22
|
|
|
action.config = { |
23
|
|
|
'default': 'db2', |
24
|
|
|
'db1': { |
25
|
|
|
'server': 'ahost1', |
26
|
|
|
'user': 'auser', |
27
|
|
|
'password': 'apass' |
28
|
|
|
}, |
29
|
|
|
'db2': { |
30
|
|
|
'server': 'bhost1', |
31
|
|
|
'user': 'buser', |
32
|
|
|
'password': 'bpass' |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
expected = {'database': 'db2', 'server': 'bhost1', 'user': 'buser', 'password': 'bpass'} |
36
|
|
|
self.assertEqual(expected, action._connect_params()) |
37
|
|
|
|
38
|
|
|
def test_connect_config_database(self): |
39
|
|
|
action = self.get_action_instance() |
40
|
|
|
action.config = { |
41
|
|
|
'default': 'db2', |
42
|
|
|
'db1': { |
43
|
|
|
'server': 'ahost1', |
44
|
|
|
'user': 'auser', |
45
|
|
|
'password': 'apass' |
46
|
|
|
}, |
47
|
|
|
'db2': { |
48
|
|
|
'database': 'wtdb', |
49
|
|
|
'server': 'bhost1', |
50
|
|
|
'user': 'buser', |
51
|
|
|
'password': 'bpass' |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
expected = {'database': 'wtdb', 'server': 'bhost1', 'user': 'buser', 'password': 'bpass'} |
55
|
|
|
self.assertEqual(expected, action._connect_params()) |
56
|
|
|
|
57
|
|
|
def test_connect_manual(self): |
58
|
|
|
action = self.get_action_instance() |
59
|
|
|
action.config = {} |
60
|
|
|
expected = {'database': 'db3', 'server': 'mhost1', 'user': 'muser', 'password': 'mpass'} |
61
|
|
|
self.assertEqual(expected, action._connect_params( |
62
|
|
|
database='db3', |
63
|
|
|
server='mhost1', |
64
|
|
|
user='muser', |
65
|
|
|
password='mpass' |
66
|
|
|
)) |
67
|
|
|
|
68
|
|
|
def test_connect_manual_database(self): |
69
|
|
|
action = self.get_action_instance() |
70
|
|
|
action.config = { |
71
|
|
|
'db1': { |
72
|
|
|
'server': 'ahost1', |
73
|
|
|
'user': 'auser', |
74
|
|
|
'password': 'apass' |
75
|
|
|
}, |
76
|
|
|
'db2': { |
77
|
|
|
'server': 'bhost1', |
78
|
|
|
'user': 'buser', |
79
|
|
|
'password': 'bpass' |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
expected = {'database': 'db1', 'server': 'ahost1', 'user': 'auser', 'password': 'apass'} |
83
|
|
|
self.assertEqual(expected, action._connect_params(database='db1')) |
84
|
|
|
|
85
|
|
|
def test_connect_override(self): |
86
|
|
|
action = self.get_action_instance() |
87
|
|
|
action.config = { |
88
|
|
|
'default': 'db2', |
89
|
|
|
'db1': { |
90
|
|
|
'server': 'ahost1', |
91
|
|
|
'user': 'auser', |
92
|
|
|
'password': 'apass' |
93
|
|
|
}, |
94
|
|
|
'db2': { |
95
|
|
|
'server': 'bhost1', |
96
|
|
|
'user': 'buser', |
97
|
|
|
'password': 'bpass' |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
expected = {'database': 'db1', 'server': 'mhost1', 'user': 'muser', 'password': 'apass'} |
101
|
|
|
self.assertEqual(expected, action._connect_params( |
102
|
|
|
database='db1', |
103
|
|
|
server='mhost1', |
104
|
|
|
user='muser', |
105
|
|
|
)) |
106
|
|
|
|
107
|
|
|
def test_connect_unlisted_database(self): |
108
|
|
|
action = self.get_action_instance() |
109
|
|
|
action.config = { |
110
|
|
|
'default': 'unknown-db', |
111
|
|
|
'db1': { |
112
|
|
|
'server': 'ahost1' |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
self.assertRaises(Exception, action._connect_params) |
116
|
|
|
|
117
|
|
|
def test_connect_missing_database(self): |
118
|
|
|
action = self.get_action_instance() |
119
|
|
|
action.config = { |
120
|
|
|
'db1': { |
121
|
|
|
'server': 'ahost1', |
122
|
|
|
'user': 'auser', |
123
|
|
|
'password': 'apass' |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
self.assertRaises(Exception, action._connect_params) |
127
|
|
|
|
128
|
|
|
def test_connect_missing_server(self): |
129
|
|
|
action = self.get_action_instance() |
130
|
|
|
action.config = { |
131
|
|
|
'default': 'db1', |
132
|
|
|
'db1': { |
133
|
|
|
'user': 'auser', |
134
|
|
|
'password': 'apass' |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
self.assertRaises(Exception, action._connect_params) |
138
|
|
|
|
139
|
|
|
def test_connect_missing_user(self): |
140
|
|
|
action = self.get_action_instance() |
141
|
|
|
action.config = { |
142
|
|
|
'default': 'db1', |
143
|
|
|
'db1': { |
144
|
|
|
'server': 'ahost1', |
145
|
|
|
'password': 'apass' |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
self.assertRaises(Exception, action._connect_params) |
149
|
|
|
|
150
|
|
|
def test_connect_missing_password(self): |
151
|
|
|
action = self.get_action_instance() |
152
|
|
|
action.config = { |
153
|
|
|
'default': 'db1', |
154
|
|
|
'db1': { |
155
|
|
|
'server': 'ahost1', |
156
|
|
|
'user': 'auser' |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
self.assertRaises(Exception, action._connect_params) |
160
|
|
|
|