@@ -10,6 +10,7 @@ import (
10
10
"strings"
11
11
"testing"
12
12
"text/template"
13
+ "time"
13
14
14
15
"github.com/davrodpin/mole/alias"
15
16
)
@@ -241,6 +242,137 @@ func TestShowAll(t *testing.T) {
241
242
}
242
243
}
243
244
245
+ func TestAliasMerge (t * testing.T ) {
246
+ keepAliveInterval , _ := time .ParseDuration ("5s" )
247
+
248
+ tests := []struct {
249
+ alias alias.Alias
250
+ tunnelFlags * alias.TunnelFlags
251
+ }{
252
+ {
253
+ alias.Alias {
254
+ Verbose : false ,
255
+ Insecure : false ,
256
+ Detach : false ,
257
+ Source : []string {"127.0.0.1:80" },
258
+ Destination : []string {"172.17.0.100:8080" },
259
+
260
+ Key : "path/to/key/1" ,
261
+ KeepAliveInterval : "3s" ,
262
+ ConnectionRetries : 3 ,
263
+ WaitAndRetry : "10s" ,
264
+ SshAgent : "path/to/sshagent" ,
265
+ Timeout : "3s" ,
266
+ },
267
+ & alias.TunnelFlags {
268
+ Verbose : true ,
269
+ Insecure : true ,
270
+ Detach : true ,
271
+ Source : alias .AddressInputList ([]alias.AddressInput {alias.AddressInput {Host : "127.0.0.1" , Port : "80" }}),
272
+ Destination : alias .AddressInputList ([]alias.AddressInput {alias.AddressInput {Host : "172.17.0.100" , Port : "8080" }}),
273
+ Server : alias.AddressInput {Host : "acme.com" , Port : "22" },
274
+ Key : "path/to/key/2" ,
275
+ KeepAliveInterval : keepAliveInterval ,
276
+ ConnectionRetries : 10 ,
277
+ },
278
+ },
279
+ }
280
+
281
+ for id , test := range tests {
282
+ test .alias .Merge (test .tunnelFlags )
283
+
284
+ if test .alias .Verbose != test .tunnelFlags .Verbose {
285
+ t .Errorf ("alias verbose doesn't match on test %d: expected: %t, value: %t" , id , test .tunnelFlags .Verbose , test .alias .Verbose )
286
+ }
287
+
288
+ if test .alias .Insecure != test .tunnelFlags .Insecure {
289
+ t .Errorf ("alias insecure doesn't match on test %d: expected: %t, value: %t" , id , test .tunnelFlags .Insecure , test .alias .Insecure )
290
+ }
291
+
292
+ if test .alias .Detach != test .tunnelFlags .Detach {
293
+ t .Errorf ("alias detach doesn't match on test %d: expected: %t, value: %t" , id , test .tunnelFlags .Detach , test .alias .Detach )
294
+ }
295
+ }
296
+
297
+ }
298
+
299
+ func TestParseAlias (t * testing.T ) {
300
+ kai , _ := time .ParseDuration ("3s" )
301
+ war , _ := time .ParseDuration ("5s" )
302
+ tim , _ := time .ParseDuration ("1s" )
303
+
304
+ flags := alias.TunnelFlags {
305
+ TunnelType : "local" ,
306
+ Verbose : false ,
307
+ Insecure : false ,
308
+ Detach : false ,
309
+ Source : alias.AddressInputList {alias.AddressInput {Host : "127.0.0.1" , Port : "8080" }},
310
+ Destination : alias.AddressInputList {alias.AddressInput {Host : "172.17.0.100" , Port : "80" }},
311
+ Server : alias.AddressInput {},
312
+ Key : "path/to/key/1" ,
313
+ KeepAliveInterval : kai ,
314
+ ConnectionRetries : 3 ,
315
+ WaitAndRetry : war ,
316
+ SshAgent : "path/to/sshagent" ,
317
+ Timeout : tim ,
318
+ }
319
+
320
+ al := flags .ParseAlias ("aliasName" )
321
+
322
+ if flags .TunnelType != al .TunnelType {
323
+ t .Errorf ("tunnelType does not match: expected: %s, value: %s" , flags .TunnelType , al .TunnelType )
324
+ }
325
+
326
+ if flags .Verbose != al .Verbose {
327
+ t .Errorf ("verbose does not match: expected: %t, value: %t" , flags .Verbose , al .Verbose )
328
+ }
329
+
330
+ if flags .Insecure != al .Insecure {
331
+ t .Errorf ("insecure does not match: expected: %t, value: %t" , flags .Insecure , al .Insecure )
332
+ }
333
+
334
+ if flags .Detach != al .Detach {
335
+ t .Errorf ("detach does not match: expected: %t, value: %t" , flags .Detach , al .Detach )
336
+ }
337
+
338
+ if ! reflect .DeepEqual (flags .Source .List (), al .Source ) {
339
+ t .Errorf ("source does not match: expected: %s, value: %s" , flags .Source .List (), al .Source )
340
+ }
341
+
342
+ if ! reflect .DeepEqual (flags .Destination .List (), al .Destination ) {
343
+ t .Errorf ("destination does not match: expected: %s, value: %s" , flags .Destination .List (), al .Destination )
344
+ }
345
+
346
+ if flags .Server .String () != al .Server {
347
+ t .Errorf ("server does not match: expected: %s, value: %s" , flags .Server .String (), al .Server )
348
+ }
349
+
350
+ if flags .Key != al .Key {
351
+ t .Errorf ("key does not match: expected: %s, value: %s" , flags .Key , al .Key )
352
+ }
353
+
354
+ if flags .KeepAliveInterval .String () != al .KeepAliveInterval {
355
+ t .Errorf ("keep alive interval does not match: expected: %s, value: %s" , flags .KeepAliveInterval .String (), al .KeepAliveInterval )
356
+ }
357
+
358
+ if flags .ConnectionRetries != al .ConnectionRetries {
359
+ t .Errorf ("connection retries does not match: expected: %d, value: %d" , flags .ConnectionRetries , al .ConnectionRetries )
360
+ }
361
+
362
+ if flags .WaitAndRetry .String () != al .WaitAndRetry {
363
+ t .Errorf ("wait and retry does not match: expected: %s, value: %s" , flags .WaitAndRetry .String (), al .WaitAndRetry )
364
+ }
365
+
366
+ if flags .SshAgent != al .SshAgent {
367
+ t .Errorf ("ssh agent does not match: expected: %s, value: %s" , flags .SshAgent , al .SshAgent )
368
+ }
369
+
370
+ if flags .Timeout .String () != al .Timeout {
371
+ t .Errorf ("timeout does not match: expected: %s, value: %s" , flags .Timeout .String (), al .Timeout )
372
+ }
373
+
374
+ }
375
+
244
376
func addAlias () (* alias.Alias , error ) {
245
377
a := & alias.Alias {
246
378
Name : "alias" ,
0 commit comments