1 package host
2
3 import (
4 "testing"
5
6 "time"
7
8 "github.com/stretchr/testify/assert"
9 )
10
11 type helper interface {
12 Helper()
13 }
14
15 func EqualError(message string) assert.ErrorAssertionFunc {
16 return func(t assert.TestingT, err error, i ...interface{}) bool {
17 if help, ok := t.(helper); ok {
18 help.Helper()
19 }
20 return assert.EqualError(t, err, message, i...)
21 }
22 }
23
24 type TimeAssertionFunc func(t assert.TestingT, actual time.Time, msgAndArgs ...interface{}) bool
25
26 func TimeEqual(expected time.Time) TimeAssertionFunc {
27 return func(t assert.TestingT, actual time.Time, msgAndArgs ...interface{}) bool {
28 return assert.Equal(t, expected, actual, msgAndArgs...)
29 }
30 }
31
32 func WithinRange(start, end time.Time) TimeAssertionFunc {
33 return func(t assert.TestingT, actual time.Time, msgAndArgs ...interface{}) bool {
34 return assert.WithinRange(t, actual, start, end, msgAndArgs...)
35 }
36 }
37
38 func TestUpdateStateVNC(t *testing.T) {
39 t.Parallel()
40
41
42
43
44 testTime := time.Now().Add(48 * time.Hour).Round(time.Second).UTC()
45
46 tests := map[string]struct {
47 state *State
48 payload postVNCPayload
49 want *State
50 timeAssert TimeAssertionFunc
51 }{
52 "Update Status": {
53 state: &State{
54 VNC: VNCStates{
55 {
56 RequestID: "1",
57 Status: Requested,
58 TimeStamp: time.Now().Format(time.RFC3339),
59 },
60 },
61 },
62 payload: postVNCPayload{
63 RequestID: "1",
64 Status: Accepted,
65 },
66 want: &State{
67 VNC: VNCStates{
68 {
69 RequestID: "1",
70 Status: Accepted,
71 },
72 },
73 },
74 timeAssert: WithinRange(time.Now().Add(-(10 * time.Second)), time.Now()),
75 },
76 "Add New VNC State": {
77 state: &State{
78 VNC: VNCStates{
79 {
80 RequestID: "1",
81 Status: Requested,
82 TimeStamp: time.Now().Format(time.RFC3339),
83 },
84 },
85 },
86 payload: postVNCPayload{
87 RequestID: "2",
88 Status: Accepted,
89 },
90 want: &State{
91 VNC: VNCStates{
92 {
93 RequestID: "1",
94 Status: Requested,
95 },
96 {
97 RequestID: "2",
98 Status: Accepted,
99 },
100 },
101 },
102 timeAssert: WithinRange(time.Now().Add(-(10 * time.Second)), time.Now()),
103 },
104 "Rejected VNC State": {
105 state: &State{
106 VNC: VNCStates{
107 {
108 RequestID: "1",
109 Status: Requested,
110 TimeStamp: time.Now().Format(time.RFC3339),
111 },
112 },
113 },
114 payload: postVNCPayload{
115 RequestID: "1",
116 Status: Rejected,
117 },
118 want: &State{
119 VNC: VNCStates{
120 {
121 RequestID: "1",
122 Status: Rejected,
123 },
124 },
125 },
126 timeAssert: WithinRange(time.Now().Add(-(10 * time.Second)), time.Now()),
127 },
128 "Connected VNC State": {
129 state: &State{
130 VNC: VNCStates{
131 {
132 RequestID: "1",
133 Status: Accepted,
134 TimeStamp: time.Now().Format(time.RFC3339),
135 },
136 },
137 },
138 payload: postVNCPayload{
139 RequestID: "1",
140 Status: Connected,
141 Connections: 1,
142 TimeStamp: testTime,
143 },
144 want: &State{
145 VNC: VNCStates{
146 {
147 RequestID: "1",
148 Status: Connected,
149 Connections: 1,
150 TimeStamp: testTime.Format(time.RFC3339),
151 },
152 },
153 },
154 timeAssert: TimeEqual(testTime),
155 },
156 "Dropped VNC State": {
157 state: &State{
158 VNC: VNCStates{
159 {
160 RequestID: "1",
161 Status: Requested,
162 TimeStamp: time.Now().Format(time.RFC3339),
163 },
164 },
165 },
166 payload: postVNCPayload{
167 RequestID: "1",
168 Status: Dropped,
169 },
170 want: &State{
171 VNC: VNCStates{},
172 },
173 timeAssert: TimeEqual(time.Time{}),
174 },
175 "Remove VNC State Not Found": {
176 state: &State{
177 VNC: VNCStates{
178 {
179 RequestID: "1",
180 Status: Requested,
181 TimeStamp: time.Now().Format(time.RFC3339),
182 },
183 },
184 },
185 payload: postVNCPayload{
186 RequestID: "2",
187 Status: Dropped,
188 },
189 want: &State{
190 VNC: VNCStates{
191 {
192 RequestID: "1",
193 Status: Requested,
194 },
195 },
196 },
197 timeAssert: WithinRange(time.Now().Add(-(10 * time.Second)), time.Now()),
198 },
199 "Payload With Timestamp": {
200 state: &State{
201 VNC: VNCStates{
202 {
203 RequestID: "1",
204 Status: Requested,
205 TimeStamp: time.Now().Format(time.RFC3339),
206 },
207 },
208 },
209 payload: postVNCPayload{
210 RequestID: "1",
211 Status: Accepted,
212 TimeStamp: testTime,
213 },
214 want: &State{
215 VNC: VNCStates{
216 {
217 RequestID: "1",
218 Status: Accepted,
219 TimeStamp: testTime.Format(time.RFC3339),
220 },
221 },
222 },
223 timeAssert: TimeEqual(testTime),
224 },
225 "New Payload With Timestamp": {
226 state: &State{
227 VNC: VNCStates{},
228 },
229 payload: postVNCPayload{
230 RequestID: "1",
231 Status: Accepted,
232 TimeStamp: testTime,
233 },
234 want: &State{
235 VNC: VNCStates{
236 {
237 RequestID: "1",
238 Status: Accepted,
239 TimeStamp: testTime.Format(time.RFC3339),
240 },
241 },
242 },
243 timeAssert: TimeEqual(testTime),
244 },
245 }
246
247 for name, tc := range tests {
248 tc := tc
249 t.Run(name, func(t *testing.T) {
250 t.Parallel()
251
252 updateStateVNC(tc.state, tc.payload)
253
254 for i := range tc.state.VNC {
255 assert.Equal(t, tc.state.VNC[i].RequestID, tc.want.VNC[i].RequestID)
256 assert.Equal(t, tc.state.VNC[i].Status, tc.want.VNC[i].Status)
257 actualTime, err := time.Parse(time.RFC3339, tc.state.VNC[i].TimeStamp)
258 assert.NoError(t, err)
259 tc.timeAssert(t, actualTime)
260 }
261 })
262 }
263 }
264
View as plain text