FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-libjuju/tests/unit/test_connection.py at master · Maxcode123/python-libjuju · GitHub
Maxcode123
python-libjuju
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-libjuju
/
tests
/
unit
/
test_connection.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
203 lines (185 loc) · 6.33 KB
Breadcrumbs
python-libjuju
/
tests
/
unit
/
test_connection.py
Copy path
File metadata and controls
203 lines (185 loc) · 6.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import
asyncio
import
json
from
collections
import
deque
import
mock
from
juju
.
errors
import
JujuRedirectException
from
juju
.
client
.
connection
import
Connection
from
websockets
.
exceptions
import
ConnectionClosed
import
pytest
from
..
import
base
class
WebsocketMock
:
def
__init__
(
self
,
responses
):
super
().
__init__
()
self
.
responses
=
deque
(
responses
)
self
.
open
=
True
self
.
closed
=
False
async
def
send
(
self
,
message
):
pass
async
def
recv
(
self
):
if
not
self
.
responses
:
await
asyncio
.
sleep
(
1
)
# delay to give test time to finish
raise
ConnectionClosed
(
0
,
'ran out of responses'
)
return
json
.
dumps
(
self
.
responses
.
popleft
())
async
def
close
(
self
):
self
.
open
=
False
self
.
closed
=
True
@
pytest
.
mark
.
asyncio
async
def
test_out_of_order
(
event_loop
):
ws
=
WebsocketMock
([
{
'request-id'
:
1
},
{
'request-id'
:
3
},
{
'request-id'
:
2
},
])
expected_responses
=
[
{
'request-id'
:
1
},
{
'request-id'
:
2
},
{
'request-id'
:
3
},
]
minimal_facades
=
[{
'name'
:
'Pinger'
,
'versions'
: [
1
]}]
con
=
None
try
:
with
\
mock
.
patch
(
'websockets.connect'
,
base
.
AsyncMock
(
return_value
=
ws
)), \
mock
.
patch
(
'juju.client.connection.Connection.login'
,
base
.
AsyncMock
(
return_value
=
{
'response'
: {
'facades'
:
minimal_facades
,
'server-version'
:
'3.0'
,
}}),
), \
mock
.
patch
(
'juju.client.connection.Connection._get_ssl'
), \
mock
.
patch
(
'juju.client.connection.Connection._pinger'
,
base
.
AsyncMock
()):
con
=
await
Connection
.
connect
(
'0.1.2.3:999'
)
actual_responses
=
[]
for
i
in
range
(
3
):
actual_responses
.
append
(
await
con
.
rpc
({
'version'
:
1
}))
assert
actual_responses
==
expected_responses
finally
:
if
con
:
await
con
.
close
()
@
pytest
.
mark
.
asyncio
async
def
test_bubble_redirect_exception
(
event_loop
):
ca_cert
=
"""
-----BEGIN CERTIFICATE-----
SOMECERT
-----END CERTIFICATE-----"""
ws
=
WebsocketMock
([
{
'request-id'
:
1
,
'error'
:
'redirection to alternative server required'
,
'error-code'
:
'redirection required'
,
'error-info'
: {
'ca-cert'
:
ca_cert
,
'servers'
: [[
{
'port'
:
17070
,
'scope'
:
'local-cloud'
,
'type'
:
'ipv4'
,
'value'
:
'42.42.42.42'
,
},
{
'port'
:
4242
,
'scope'
:
'public'
,
'type'
:
'ipv4'
,
'value'
:
'42.42.42.42'
,
},
]],
},
'response'
: {},
},
])
with
pytest
.
raises
(
JujuRedirectException
)
as
caught_ex
:
with
mock
.
patch
(
'websockets.connect'
,
base
.
AsyncMock
(
return_value
=
ws
)):
await
Connection
.
connect
(
'0.1.2.3:999'
)
redir_error
=
caught_ex
.
value
assert
redir_error
.
follow_redirect
is
False
assert
redir_error
.
ca_cert
==
ca_cert
assert
redir_error
.
endpoints
==
[
(
'42.42.42.42:17070'
,
ca_cert
),
(
'42.42.42.42:4242'
,
ca_cert
),
]
@
pytest
.
mark
.
asyncio
async
def
test_follow_redirect
(
event_loop
):
ca_cert
=
"""
-----BEGIN CERTIFICATE-----
SOMECERT
-----END CERTIFICATE-----"""
wsForCont1
=
WebsocketMock
([
{
'request-id'
:
1
,
'error'
:
'redirection to alternative server required'
,
'error-code'
:
'redirection required'
,
'response'
: {},
},
{
'request-id'
:
2
,
'response'
: {
'ca-cert'
:
ca_cert
,
'servers'
: [[
{
'port'
:
17070
,
'scope'
:
'local-cloud'
,
'type'
:
'ipv4'
,
'value'
:
'42.42.42.42'
,
},
{
'port'
:
4242
,
'scope'
:
'public'
,
'type'
:
'ipv4'
,
'value'
:
'42.42.42.42'
,
},
]],
},
},
])
minimal_facades
=
[{
'name'
:
'Pinger'
,
'versions'
: [
1
]}]
wsForCont2
=
WebsocketMock
([
{
'request-id'
:
1
},
{
'request-id'
:
2
},
{
'request-id'
:
3
,
'response'
: {
'result'
:
minimal_facades
,
'server-version'
:
'3.0'
,
}},
])
con
=
None
try
:
with
\
mock
.
patch
(
'websockets.connect'
,
base
.
AsyncMock
(
side_effect
=
[
wsForCont1
,
wsForCont2
])
), \
mock
.
patch
(
'juju.client.connection.Connection._get_ssl'
), \
mock
.
patch
(
'juju.client.connection.Connection._pinger'
,
base
.
AsyncMock
()):
con
=
await
Connection
.
connect
(
'0.1.2.3:999'
)
finally
:
if
con
:
assert
con
.
connect_params
()[
'endpoint'
]
==
"42.42.42.42:4242"
await
con
.
close
()
@
pytest
.
mark
.
asyncio
async
def
test_rpc_none_results
(
event_loop
):
ws
=
WebsocketMock
([
{
'request-id'
:
1
,
'response'
: {
'results'
:
None
}},
])
expected_responses
=
[
{
'request-id'
:
1
,
'response'
: {
'results'
:
None
}},
]
minimal_facades
=
[{
'name'
:
'Pinger'
,
'versions'
: [
1
]}]
con
=
None
try
:
with
\
mock
.
patch
(
'websockets.connect'
,
base
.
AsyncMock
(
return_value
=
ws
)), \
mock
.
patch
(
'juju.client.connection.Connection.login'
,
base
.
AsyncMock
(
return_value
=
{
'response'
: {
'facades'
:
minimal_facades
,
'server-version'
:
'3.0'
,
}}),
), \
mock
.
patch
(
'juju.client.connection.Connection._get_ssl'
), \
mock
.
patch
(
'juju.client.connection.Connection._pinger'
,
base
.
AsyncMock
()):
con
=
await
Connection
.
connect
(
'0.1.2.3:999'
)
actual_responses
=
[]
actual_responses
.
append
(
await
con
.
rpc
({
'version'
:
1
}))
assert
actual_responses
==
expected_responses
finally
:
if
con
:
await
con
.
close
()
Back
|
FazBrowse Home
|
New Git URL