This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket_test.go
190 lines (162 loc) · 5.61 KB
/
websocket_test.go
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
package main
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"gitlab.com/ignitionrobotics/web/cloudsim/pkg/simulations"
"gitlab.com/ignitionrobotics/web/ign-go"
igntest "gitlab.com/ignitionrobotics/web/ign-go/testhelpers"
sim "gitlab.com/ignitionrobotics/web/subt/simulations"
"sync"
"testing"
"time"
)
func TestWebsocketAddressSuite(t *testing.T) {
suite.Run(t, &WebsocketAddressTestSuite{})
}
type WebsocketAddressTestSuite struct {
suite.Suite
singleSimCircuit string
singleSimGroupID string
multiSimCircuit string
multiSimGroupID string
createSimURI string
websocketURI string
jwtTeamAUser1 *testJWT
jwtTeamBUser1 *testJWT
jwtSysAdmin *testJWT
}
func (suite *WebsocketAddressTestSuite) SetupSuite() {
suite.singleSimCircuit = "Virtual Stix"
suite.multiSimCircuit = "Urban Practice 3"
suite.createSimURI = "/1.0/simulations"
suite.websocketURI = "/1.0/simulations/%s/websocket"
suite.jwtTeamAUser1 = newJWT(createJWTForIdentity(suite.T(), "TeamAUser1"))
suite.jwtTeamBUser1 = newJWT(createJWTForIdentity(suite.T(), "TeamBUser1"))
suite.jwtSysAdmin = getDefaultTestJWT()
// Initialize the database. Note that this is only performed once for the entire suite.
setup()
// Create simulations.
suite.singleSimGroupID = suite.setSimulationToRunning(suite.requestSimulation(suite.singleSimCircuit, suite.jwtTeamAUser1))
suite.multiSimGroupID = suite.setSimulationToRunning(suite.getChildSimGroupID(
suite.setSimulationToRunning(suite.requestSimulation(suite.multiSimCircuit, suite.jwtTeamAUser1)),
))
}
// requestSimulation requests a simulation launch
func (suite *WebsocketAddressTestSuite) requestSimulation(circuit string, ownerJWT *testJWT) string {
// A WaitGroup is set to wait until the async worker finishes launching the simulation
var wg sync.WaitGroup
if circuit == suite.singleSimCircuit {
wg.Add(1)
} else if circuit == suite.multiSimCircuit {
// HACK This is very tightly coupled to the number of children set in the test circuit
wg.Add(6)
} else {
suite.Fail("Please use one of the circuits in this test suite.")
}
// Use the notify signal to mark the worker as done
cb := func(poolEvent sim.PoolEvent, groupID string, result interface{}, err error) {
if poolEvent == sim.PoolStartSimulation {
wg.Done()
}
}
sim.SimServImpl.(*sim.Service).SetPoolEventsListener(cb)
createSubtForm := map[string]string{
"name": "sim1",
"owner": "TeamA",
"circuit": circuit,
"robot_name": "X1",
"robot_type": "X1_SENSOR_CONFIG_1",
"robot_image": "infrastructureascode/aws-cli:latest",
}
var groupID string
invokeURITestMultipartPOST(
suite.T(),
uriTest{
testDesc: "WebSocket Address Test -- Creating simulation deployment",
URL: suite.createSimURI,
jwtGen: ownerJWT,
expErrMsg: nil,
ignoreErrorBody: false,
ignoreOptionsCall: false,
},
createSubtForm,
func(bslice *[]byte, resp *igntest.AssertResponse) {
var dep sim.SimulationDeployment
assert.NoError(suite.T(), json.Unmarshal(*bslice, &dep))
groupID = *dep.GroupID
},
)
// Wait up to 5 seconds for the async worker to finish
suite.False(wgTimeoutWait(&wg, 5*time.Second))
return groupID
}
// testWebsocketAddress tests that a specific user has access to a simulation's websocket address.
func (suite *WebsocketAddressTestSuite) testWebsocketAddress(testDesc string, groupID string, requestJWT *testJWT,
expErrMsg *ign.ErrMsg) {
testURI := uriTest{
testDesc: testDesc,
URL: fmt.Sprintf(suite.websocketURI, groupID),
jwtGen: requestJWT,
expErrMsg: expErrMsg,
ignoreErrorBody: false,
}
suite.Run(testURI.testDesc, func() {
invokeURITest(suite.T(), testURI, func(bslice *[]byte, resp *igntest.AssertResponse) {
var wsResp sim.WebsocketAddressResponse
suite.NoError(json.Unmarshal(*bslice, &wsResp))
// Validate the address structure
suite.True(sim.IsWebsocketAddress(wsResp.Address, &groupID))
// Validate that a token was included
suite.NotEmpty(wsResp.Token)
})
})
}
func (suite *WebsocketAddressTestSuite) getChildSimGroupID(groupID string) string {
return fmt.Sprintf("%s-c-1", groupID)
}
func (suite *WebsocketAddressTestSuite) setSimulationToRunning(groupID string) string {
// Update status to running
suite.Require().NoError(sim.SimServImpl.(*sim.Service).ServiceAdaptor.UpdateStatus(
simulations.GroupID(groupID),
simulations.StatusRunning),
)
return groupID
}
func (suite *WebsocketAddressTestSuite) TestWebsocketAddressUser() {
suite.testWebsocketAddress(
"WebSocket Address Test -- User TeamA should get websocket address",
suite.singleSimGroupID,
suite.jwtTeamAUser1,
nil,
)
suite.testWebsocketAddress(
"WebSocket Address Test -- User TeamB shouldn't get websocket address from TeamA",
suite.singleSimGroupID,
suite.jwtTeamBUser1,
ign.NewErrorMessage(ign.ErrorUnauthorized),
)
}
func (suite *WebsocketAddressTestSuite) TestWebsocketAddressAdmin() {
suite.testWebsocketAddress(
"WebSocket Address Test -- Admin should get websocket address for every simulation",
suite.singleSimGroupID,
suite.jwtSysAdmin,
nil,
)
}
func (suite *WebsocketAddressTestSuite) TestWebsocketAddressChildSimulations() {
suite.testWebsocketAddress(
"WebSocket Address Test -- User TeamA should not get websocket address for child simulations",
suite.multiSimGroupID,
suite.jwtTeamAUser1,
ign.NewErrorMessage(ign.ErrorUnauthorized),
)
suite.testWebsocketAddress(
"WebSocket Address Test -- Admin should get websocket address for child simulations",
suite.multiSimGroupID,
suite.jwtSysAdmin,
nil,
)
}