internal/cli/agent_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
package cli import ( "strings" "testing" "git.j3s.sh/cascade/internal/agent" ) func TestParseFlagAddress(t *testing.T) { type c struct { inputAddr string expectIP string expectPort int expectErrStr string } cases := []c{ { inputAddr: "127.0.0.1", expectIP: "127.0.0.1", expectPort: 8301, }, { inputAddr: "127.0.0.1:6969", expectIP: "127.0.0.1", expectPort: 6969, }, { inputAddr: "192.168.0.4:420", expectIP: "192.168.0.4", expectPort: 420, }, { inputAddr: "127.0.0.3:", expectIP: "127.0.0.3", expectPort: 8301, }, // error cases { inputAddr: "", expectIP: "0.0.0.0", expectPort: 8301, expectErrStr: "Error parsing blank address", }, { inputAddr: ":1234", expectIP: "0.0.0.0", expectPort: 8301, expectErrStr: "Error parsing blank address", }, { inputAddr: "127.0.0.1:abcd", expectIP: "0.0.0.0", expectPort: 8301, expectErrStr: "Error parsing port", }, { inputAddr: "127.0.0.256:6969", expectIP: "0.0.0.0", expectPort: 8301, expectErrStr: "Error parsing address", }, } for _, tc := range cases { addr := agent.DefaultConfig().SerfBindAddr expectErr := tc.expectErrStr != "" err := parseFlagAddress(tc.inputAddr, addr) if expectErr && err == nil { t.Errorf("Expected error '%s', but received none", tc.expectErrStr) } if !expectErr && err != nil { t.Errorf("Unexpected error: '%s'", err) } // errors we expect are unwrapped here if expectErr && err != nil { if !strings.Contains(err.Error(), tc.expectErrStr) { t.Errorf("Expected error '%s', got '%s'", tc.expectErrStr, err) } } if tc.expectIP != addr.IP.String() { t.Errorf("Expected IP '%s', got '%s'", tc.expectIP, addr.IP.String()) } if tc.expectPort != addr.Port { t.Errorf("Expected port '%d', got '%d'", tc.expectPort, addr.Port) } } }