forked from opensciencegrid/stashcp
-
Notifications
You must be signed in to change notification settings - Fork 6
/
director_test.go
194 lines (164 loc) · 6.5 KB
/
director_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
192
193
194
package stashcp
import(
"testing"
"net/http"
"net/http/httptest"
"bytes"
"io/ioutil"
"os"
"github.com/stretchr/testify/assert"
namespaces "github.com/htcondor/osdf-client/v6/namespaces"
)
func TestHeaderParser(t *testing.T) {
header1 := "namespace=/foo/bar, issuer = https://get-your-tokens.org, readhttps=False"
newMap1 := HeaderParser(header1)
assert.Equal(t, "/foo/bar", newMap1["namespace"])
assert.Equal(t, "https://get-your-tokens.org", newMap1["issuer"])
assert.Equal(t, "False", newMap1["readhttps"])
header2 := ""
newMap2 := HeaderParser(header2)
assert.Equal(t, map[string]string{}, newMap2)
}
func TestGetCachesFromDirectorResponse(t *testing.T) {
// Construct the Director's Response, comprising headers and a body
directorHeaders := make(map[string][]string)
directorHeaders["Link"] = []string{"<my-cache.edu:8443>; rel=\"duplicate\"; pri=1, <another-cache.edu:8443>; rel=\"duplicate\"; pri=2"}
directorBody := []byte(`{"key": "value"}`)
directorResponse := &http.Response{
StatusCode: 307,
Header: directorHeaders,
Body: ioutil.NopCloser(bytes.NewReader(directorBody)),
}
// Call the function in question
caches, err := GetCachesFromDirectorResponse(directorResponse, true)
// Test for expected outputs
assert.NoError(t, err, "Error getting caches from the Director's response")
assert.Equal(t, "my-cache.edu:8443", caches[0].EndpointUrl)
assert.Equal(t, 1, caches[0].Priority)
assert.Equal(t, true, caches[0].AuthedReq)
assert.Equal(t, "another-cache.edu:8443", caches[1].EndpointUrl)
assert.Equal(t, 2, caches[1].Priority)
assert.Equal(t, true, caches[1].AuthedReq)
}
func TestCreateNsFromDirectorResp(t *testing.T) {
//Craft the Director's response
directorHeaders := make(map[string][]string)
directorHeaders["Link"] = []string{"<my-cache.edu:8443>; rel=\"duplicate\"; pri=1, <another-cache.edu:8443>; rel=\"duplicate\"; pri=2"}
directorHeaders["X-Osdf-Namespace"] = []string{"namespace=/foo/bar, readhttps=True, use-token-on-read=True"}
directorHeaders["X-Osdf-Authorization"] = []string{"issuer=https://get-your-tokens.org, base-path=/foo/bar"}
directorBody := []byte(`{"key": "value"}`)
directorResponse := &http.Response{
StatusCode: 307,
Header: directorHeaders,
Body: ioutil.NopCloser(bytes.NewReader(directorBody)),
}
// Create a namespace instance to test against
cache1 := namespaces.DirectorCache{
EndpointUrl: "my-cache.edu:8443",
Priority: 1,
AuthedReq: true,
}
cache2 := namespaces.DirectorCache{
EndpointUrl: "another-cache.edu:8443",
Priority: 2,
AuthedReq: true,
}
caches := []namespaces.DirectorCache{}
caches = append(caches, cache1)
caches = append(caches, cache2)
constructedNamespace := &namespaces.Namespace{
SortedDirectorCaches: caches,
Path: "/foo/bar",
Issuer: "https://get-your-tokens.org",
ReadHTTPS: true,
UseTokenOnRead: true,
}
// Call the function in question
var ns namespaces.Namespace
err := CreateNsFromDirectorResp(directorResponse, &ns)
// Test for expected outputs
assert.NoError(t, err, "Error creating Namespace from Director response")
assert.Equal(t, constructedNamespace.SortedDirectorCaches, ns.SortedDirectorCaches)
assert.Equal(t, constructedNamespace.Path, ns.Path)
assert.Equal(t, constructedNamespace.Issuer, ns.Issuer)
assert.Equal(t, constructedNamespace.ReadHTTPS, ns.ReadHTTPS)
assert.Equal(t, constructedNamespace.UseTokenOnRead, ns.UseTokenOnRead)
}
func TestNewTransferDetailsUsingDirector(t *testing.T) {
os.Setenv("http_proxy", "http://proxy.edu:3128")
// Construct the input caches
// Cache with http
nonAuthCache := namespaces.DirectorCache{
ResourceName: "mycache",
EndpointUrl: "my-cache-url:8000",
Priority: 99,
AuthedReq: false,
}
// Cache with https
authCache := namespaces.DirectorCache{
ResourceName: "mycache",
EndpointUrl: "my-cache-url:8443",
Priority: 99,
AuthedReq: true,
}
// Case 1: cache with http
transfers := NewTransferDetailsUsingDirector(nonAuthCache, nonAuthCache.AuthedReq)
assert.Equal(t, 2, len(transfers))
assert.Equal(t, "my-cache-url:8000", transfers[0].Url.Host)
assert.Equal(t, "http", transfers[0].Url.Scheme)
assert.Equal(t, true, transfers[0].Proxy)
assert.Equal(t, 2, len(transfers))
assert.Equal(t, "my-cache-url:8000", transfers[1].Url.Host)
assert.Equal(t, "http", transfers[1].Url.Scheme)
assert.Equal(t, false, transfers[1].Proxy)
// Case 2: cache with https
transfers = NewTransferDetailsUsingDirector(authCache, authCache.AuthedReq)
assert.Equal(t, 1, len(transfers))
assert.Equal(t, "my-cache-url:8443", transfers[0].Url.Host)
assert.Equal(t, "https", transfers[0].Url.Scheme)
assert.Equal(t, false, transfers[0].Proxy)
// Case 3: cache without port with http
nonAuthCache.EndpointUrl = "my-cache-url"
transfers = NewTransferDetailsUsingDirector(nonAuthCache, nonAuthCache.AuthedReq)
assert.Equal(t, 2, len(transfers))
assert.Equal(t, "my-cache-url:8000", transfers[0].Url.Host)
assert.Equal(t, "http", transfers[0].Url.Scheme)
assert.Equal(t, true, transfers[0].Proxy)
assert.Equal(t, "my-cache-url:8000", transfers[1].Url.Host)
assert.Equal(t, "http", transfers[1].Url.Scheme)
assert.Equal(t, false, transfers[1].Proxy)
// Case 4. cache without port with https
authCache.EndpointUrl = "my-cache-url"
transfers = NewTransferDetailsUsingDirector(authCache, authCache.AuthedReq)
assert.Equal(t, 2, len(transfers))
assert.Equal(t, "my-cache-url:8444", transfers[0].Url.Host)
assert.Equal(t, "https", transfers[0].Url.Scheme)
assert.Equal(t, false, transfers[0].Proxy)
assert.Equal(t, "my-cache-url:8443", transfers[1].Url.Host)
assert.Equal(t, "https", transfers[1].Url.Scheme)
assert.Equal(t, false, transfers[1].Proxy)
}
func TestQueryDirector(t *testing.T) {
// Construct a local server that we can poke with QueryDirector
expectedLocation := "http://redirect.com"
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Location", expectedLocation)
w.WriteHeader(http.StatusFound)
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
// Call QueryDirector with the test server URL and a source path
actualResp, err := QueryDirector("/foo/bar", server.URL)
if err != nil {
t.Fatal(err)
}
// Check the Location header
actualLocation := actualResp.Header.Get("Location")
if actualLocation != expectedLocation {
t.Errorf("Expected Location header %q, but got %q", expectedLocation, actualLocation)
}
// Check the HTTP status code
if actualResp.StatusCode != http.StatusFound {
t.Errorf("Expected HTTP status code %d, but got %d", http.StatusFound, actualResp.StatusCode)
}
}