forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
191 lines (181 loc) · 5.02 KB
/
parse_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
191
package main
import (
"fmt"
"testing"
"github.com/taskcluster/taskcluster/v47/clients/client-go/tcqueue"
)
func TestNoTaskNoScopes(t *testing.T) {
routes, address, err := ParseCommandArgs(
[]string{
"--root-url", "https://tc-tests.example.com",
"--client-id", "abc",
"--access-token", "ghi",
},
false,
)
if err != nil {
t.Fatalf("%v", err)
}
if routes.Credentials.AuthorizedScopes != nil {
t.Fatalf("Was expecting no AuthorizedScopes restriction, but got: %v", routes.Credentials.AuthorizedScopes)
}
if address != ":8080" {
t.Fatalf("Was expecting address ':8080', but got address '%v'.", address)
}
}
func TestNondefaultPort(t *testing.T) {
_, address, err := ParseCommandArgs(
[]string{
"--root-url", "https://tc-tests.example.com",
"--client-id", "abc",
"--access-token", "ghi",
"--port", "12345",
},
false,
)
if err != nil {
t.Fatalf("%v", err)
}
if address != ":12345" {
t.Fatalf("Was expecting address ':12345', but got address '%v'.", address)
}
}
func TestWithTwoScopes(t *testing.T) {
routes, address, err := ParseCommandArgs(
[]string{
"--root-url", "https://tc-tests.example.com",
"--client-id", "abc",
"--certificate", "def",
"--access-token", "ghi",
"--port", "12345",
// scopes
"a:b:c",
"d:e:f",
},
false,
)
if err != nil {
t.Fatalf("%v", err)
}
if address != ":12345" {
t.Fatalf("Was expecting address ':12345', but got address '%v'.", address)
}
if clientID := routes.Credentials.ClientID; clientID != "abc" {
t.Fatalf("Was expecting client id 'abc', but got client id '%v'.", clientID)
}
if cert := routes.Credentials.Certificate; cert != "def" {
t.Fatalf("Was expecting certificate 'def', but got certificate '%v'.", cert)
}
if accessToken := routes.Credentials.AccessToken; accessToken != "ghi" {
t.Fatalf("Was expecting access token 'ghi', but got access token '%v'.", accessToken)
}
if scopes := routes.Credentials.AuthorizedScopes; len(scopes) != 2 || (scopes[0]+" "+scopes[1] != "a:b:c d:e:f" && scopes[1]+" "+scopes[0] != "a:b:c d:e:f") {
t.Fatalf("Was expecting authorized scopes to contain 'a:b:c' and 'd:e:f', but instead got: '%v'", scopes)
}
}
func withFakeTask(expectedTaskID string, fakeTask *tcqueue.TaskDefinitionResponse) func() {
oldGetTask := getTask
getTask = func(rootURL string, taskID string) (task *tcqueue.TaskDefinitionResponse, err error) {
if taskID == expectedTaskID {
task = fakeTask
} else {
err = fmt.Errorf("Task not found")
}
return
}
return func() {
getTask = oldGetTask
}
}
func TestWithTaskWithNoScopes(t *testing.T) {
defer withFakeTask("abc", &tcqueue.TaskDefinitionResponse{
Scopes: nil,
})()
routes, _, err := ParseCommandArgs(
[]string{
"--task-id", "abc",
"--root-url", "https://tc-tests.example.com",
"--client-id", "abc",
"--certificate", "def",
"--access-token", "ghi",
},
false,
)
if err != nil {
t.Fatalf("%v", err)
}
if scopes := routes.Credentials.AuthorizedScopes; scopes == nil || len(scopes) > 0 {
t.Fatalf("Was expecting authorized scopes to be an empty non-nil array, but instead got: '%v'", scopes)
}
}
func TestWithTaskWithScopes(t *testing.T) {
exampleScope := "queue:get-artifact:taskcluster-proxy-test/512-random-bytes"
defer withFakeTask("abc", &tcqueue.TaskDefinitionResponse{
Scopes: []string{exampleScope},
})()
routes, _, err := ParseCommandArgs(
[]string{
"--task-id", "abc",
"--root-url", "https://tc-tests.example.com",
"--client-id", "abc",
"--certificate", "def",
"--access-token", "ghi",
},
false,
)
if err != nil {
t.Fatalf("%v", err)
}
if scopes := routes.Credentials.AuthorizedScopes; len(scopes) != 1 || scopes[0] != exampleScope {
t.Fatalf("Was expecting authorized scopes to be the single scope %v, but instead got: %v", exampleScope, scopes)
}
}
func TestWithInterface(t *testing.T) {
_, address, err := ParseCommandArgs(
[]string{
"--root-url", "https://tc-tests.example.com",
"--client-id", "abc",
"--certificate", "def",
"--access-token", "ghi",
"--port", "12345",
"--ip-address", "172.17.0.44",
},
false,
)
if err != nil {
t.Fatalf("%v", err)
}
if address != "172.17.0.44:12345" {
t.Fatalf("Was expecting address '172.17.0.44:12345', but got address '%v'.", address)
}
}
func TestBadPort(t *testing.T) {
_, _, err := ParseCommandArgs(
[]string{
"--port", "-12345",
"--ip-address", "172.17.0.44",
},
false,
)
if err == nil {
t.Fatalf("Was expecting an error!")
}
if err.Error() != "Port -12345 is not in range [0,65535]" {
t.Fatalf("Was expecting error to say 'Port -12345 is not in range [0,65535]' but it says: %v", err)
}
}
func TestBadIPAddress(t *testing.T) {
_, _, err := ParseCommandArgs(
[]string{
"--port", "12345",
"--ip-address", "172.17.0.44.66",
},
false,
)
if err == nil {
t.Fatalf("Was expecting an error!")
}
if err.Error() != "Invalid IPv4/IPv6 address specified - cannot parse: 172.17.0.44.66" {
t.Fatalf("Was expecting error to say 'Invalid IPv4/IPv6 address specified - cannot parse: 172.17.0.44.66' but it says: %v", err)
}
}