-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain_test.go
163 lines (137 loc) · 4.09 KB
/
main_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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestInstallHandler(t *testing.T) {
installScript := "#!/bin/sh\necho \"Hello world!\""
handlers := &Handlers{
bootstrapVersion: "aaa000",
installScriptData: []byte(installScript),
}
server := httptest.NewServer(http.HandlerFunc(handlers.install))
defer server.Close()
resp, err := http.Get(server.URL)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatalf("Expected 200 status code, got: %d", resp.StatusCode)
}
// Check install script data in body
actual, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if installScript != string(actual) {
t.Errorf("Expected body '%s', got: '%s'", installScript, actual)
}
contentDisposition := resp.Header.Get("Content-Disposition")
if contentDisposition != "attachment; filename=\"install-weave-cloud.sh\"" {
t.Errorf("Expected Content-Disposition: attachment, got: '%s'", contentDisposition)
}
}
func TestBootstrapHandler(t *testing.T) {
handlers := &Handlers{
bootstrapVersion: "aaa000",
installScriptData: []byte{},
}
bootstrapHandler := http.HandlerFunc(handlers.bootstrap)
testCases := []struct {
queryString string
expectedStatusCode int
expectedLocation string
}{
{"", 400, ""},
{"?dist=darwin", 301, "https://weaveworks-launcher.s3.amazonaws.com/bootstrap/aaa000/bootstrap_darwin_amd64"},
{"?dist=linux", 301, "https://weaveworks-launcher.s3.amazonaws.com/bootstrap/aaa000/bootstrap_linux_amd64"},
{"?dist=other", 400, ""},
}
for _, tc := range testCases {
// Record request made with queryString
req, err := http.NewRequest("GET", tc.queryString, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
bootstrapHandler.ServeHTTP(rr, req)
// Check status code
if rr.Code != tc.expectedStatusCode {
t.Errorf("Expected %d status code, got: %d", tc.expectedStatusCode, rr.Code)
}
// Check redirect location
if tc.expectedLocation != "" {
location, err := rr.Result().Location()
if err != nil {
t.Fatal(err)
}
if location.String() != tc.expectedLocation {
t.Errorf("Expected location '%s', got: '%s'", tc.expectedLocation, location)
}
}
}
}
func TestAgentYAMLHandler(t *testing.T) {
data := &templateData{
CRIEndpoint: "",
}
handlers := &Handlers{
templateData: data,
}
server := httptest.NewServer(http.HandlerFunc(handlers.agentYAML))
defer server.Close()
// Set cri-endpoint param
criEndpoint := "cri-endpoint=foobar"
server.URL = fmt.Sprintf("%s/?%s", server.URL, criEndpoint)
resp, err := http.Get(server.URL)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != 200 {
t.Fatalf("Expected 200 status code, got: %d", resp.StatusCode)
}
// Check install data in body
actual, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(actual), criEndpoint) {
t.Errorf("Expected: %s in agent.yaml got: %v", criEndpoint, string(actual))
}
contentDisposition := resp.Header.Get("Content-Disposition")
if contentDisposition != "attachment" {
t.Errorf("Expected Content-Disposition: attachment, got: '%s'", contentDisposition)
}
}
func TestLoadData(t *testing.T) {
ctx := &templateData{
Scheme: "https",
LauncherHostname: "hostname.test",
CRIEndpoint: "",
}
// install.sh
installScriptData, err := loadData("./static/install.sh", ctx)
if err != nil {
t.Fatal(err)
}
installScript := string(installScriptData)
if !strings.Contains(installScript, "https://hostname.test/bootstrap?dist=$dist") {
t.Errorf("Expected 'https://hostname.test/bootstrap?dist=$dist' in install.sh")
}
if !strings.Contains(installScript, "--wc.launcher=hostname.test") {
t.Errorf("Expected '--wc.launcher=hostname.test' in install.sh")
}
// agent.yaml
agentYAMLData, err := loadData("./static/agent.yaml.in", ctx)
if err != nil {
t.Fatal(err)
}
agentYAML := string(agentYAMLData)
if !strings.Contains(agentYAML, "-agent.poll-url=https://hostname.test/k8s/agent.yaml") {
t.Errorf("Expected '-agent.poll-url=https://hostname.test/k8s/agent.yaml' in agent.yaml")
}
}