forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
scoped.go
221 lines (193 loc) · 7.67 KB
/
scoped.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package images
import (
"fmt"
"net/http"
"strconv"
"strings"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/wait"
e2e "k8s.io/kubernetes/test/e2e/framework"
exutil "github.com/openshift/origin/test/extended/util"
)
const changeTimeoutSeconds = 3 * 60
var _ = g.Describe("[networking][router] openshift routers", func() {
defer g.GinkgoRecover()
var (
configPath = exutil.FixturePath("testdata", "scoped-router.yaml")
oc = exutil.NewCLI("scoped-router", exutil.KubeConfigPath())
)
g.BeforeEach(func() {
// defer oc.Run("delete").Args("-f", configPath).Execute()
err := oc.AsAdmin().Run("adm").Args("policy", "add-cluster-role-to-user", "system:router", oc.Username()).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.Run("create").Args("-f", configPath).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
})
g.Describe("The HAProxy router", func() {
g.It("should serve the correct routes when scoped to a single namespace and label set", func() {
defer func() {
// This should be done if the test fails but
// for now always dump the logs.
// if g.CurrentGinkgoTestDescription().Failed
dumpScopedRouterLogs(oc, g.CurrentGinkgoTestDescription().FullTestText)
}()
oc.SetOutputDir(exutil.TestContext.OutputDir)
ns := oc.KubeFramework().Namespace.Name
execPodName := exutil.CreateExecPodOrFail(oc.AdminKubeClient().Core(), ns, "execpod")
defer func() { oc.AdminKubeClient().Core().Pods(ns).Delete(execPodName, kapi.NewDeleteOptions(1)) }()
g.By(fmt.Sprintf("creating a scoped router from a config file %q", configPath))
var routerIP string
err := wait.Poll(time.Second, changeTimeoutSeconds*time.Second, func() (bool, error) {
pod, err := oc.KubeFramework().ClientSet.Core().Pods(oc.KubeFramework().Namespace.Name).Get("scoped-router")
if err != nil {
return false, err
}
if len(pod.Status.PodIP) == 0 {
return false, nil
}
routerIP = pod.Status.PodIP
return true, nil
})
o.Expect(err).NotTo(o.HaveOccurred())
// router expected to listen on port 80
routerURL := fmt.Sprintf("http://%s", routerIP)
g.By("waiting for the healthz endpoint to respond")
healthzURI := fmt.Sprintf("http://%s:1936/healthz", routerIP)
err = waitForRouterOKResponseExec(ns, execPodName, healthzURI, routerIP, changeTimeoutSeconds)
if err != nil {
dumpScopedRouterLogs(oc, fmt.Sprintf("%s - %s", g.CurrentGinkgoTestDescription().TestText, "waiting for the healthz endpoint to respond"))
}
o.Expect(err).NotTo(o.HaveOccurred())
g.By("waiting for the valid route to respond")
err = waitForRouterOKResponseExec(ns, execPodName, routerURL, "first.example.com", changeTimeoutSeconds)
o.Expect(err).NotTo(o.HaveOccurred())
for _, host := range []string{"second.example.com", "third.example.com"} {
g.By(fmt.Sprintf("checking that %s does not match a route", host))
err = expectRouteStatusCodeExec(ns, execPodName, routerURL, host, http.StatusServiceUnavailable)
o.Expect(err).NotTo(o.HaveOccurred())
}
})
g.It("should override the route host with a custom value", func() {
defer func() {
// This should be done if the test fails but
// for now always dump the logs.
// if g.CurrentGinkgoTestDescription().Failed
dumpScopedRouterLogs(oc, g.CurrentGinkgoTestDescription().FullTestText)
}()
oc.SetOutputDir(exutil.TestContext.OutputDir)
ns := oc.KubeFramework().Namespace.Name
execPodName := exutil.CreateExecPodOrFail(oc.AdminKubeClient().Core(), ns, "execpod")
defer func() { oc.AdminKubeClient().Core().Pods(ns).Delete(execPodName, kapi.NewDeleteOptions(1)) }()
g.By(fmt.Sprintf("creating a scoped router from a config file %q", configPath))
var routerIP string
err := wait.Poll(time.Second, changeTimeoutSeconds*time.Second, func() (bool, error) {
pod, err := oc.KubeFramework().ClientSet.Core().Pods(ns).Get("router-override")
if err != nil {
return false, err
}
if len(pod.Status.PodIP) == 0 {
return false, nil
}
routerIP = pod.Status.PodIP
return true, nil
})
o.Expect(err).NotTo(o.HaveOccurred())
// router expected to listen on port 80
routerURL := fmt.Sprintf("http://%s", routerIP)
pattern := "%s-%s.myapps.mycompany.com"
g.By("waiting for the healthz endpoint to respond")
healthzURI := fmt.Sprintf("http://%s:1936/healthz", routerIP)
err = waitForRouterOKResponseExec(ns, execPodName, healthzURI, routerIP, changeTimeoutSeconds)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("waiting for the valid route to respond")
err = waitForRouterOKResponseExec(ns, execPodName, routerURL, fmt.Sprintf(pattern, "route-1", ns), changeTimeoutSeconds)
o.Expect(err).NotTo(o.HaveOccurred())
g.By("checking that the stored domain name does not match a route")
host := "first.example.com"
err = expectRouteStatusCodeExec(ns, execPodName, routerURL, host, http.StatusServiceUnavailable)
o.Expect(err).NotTo(o.HaveOccurred())
for _, host := range []string{"route-1", "route-2"} {
host = fmt.Sprintf(pattern, host, ns)
g.By(fmt.Sprintf("checking that %s matches a route", host))
err = expectRouteStatusCodeExec(ns, execPodName, routerURL, host, http.StatusOK)
o.Expect(err).NotTo(o.HaveOccurred())
}
})
})
})
func waitForRouterOKResponseExec(ns, execPodName, url, host string, timeoutSeconds int) error {
cmd := fmt.Sprintf(`
set -e
for i in $(seq 1 %d); do
code=$( curl -s -o /dev/null -w '%%{http_code}\n' --header 'Host: %s' %q ) || rc=$?
if [[ "${rc:-0}" -eq 0 ]]; then
echo $code
if [[ $code -eq 200 ]]; then
exit 0
fi
if [[ $code -ne 503 ]]; then
exit 1
fi
else
echo "error ${rc}" 1>&2
fi
sleep 1
done
`, timeoutSeconds, host, url)
output, err := e2e.RunHostCmd(ns, execPodName, cmd)
if err != nil {
return fmt.Errorf("host command failed: %v\n%s", err, output)
}
lines := strings.Split(strings.TrimSpace(output), "\n")
if lines[len(lines)-1] != "200" {
return fmt.Errorf("last response from server was not 200:\n%s", output)
}
return nil
}
func expectRouteStatusCodeRepeatedExec(ns, execPodName, url, host string, statusCode int, times int) error {
cmd := fmt.Sprintf(`
set -e
for i in $(seq 1 %d); do
code=$( curl -s -o /dev/null -w '%%{http_code}\n' --header 'Host: %s' %q ) || rc=$?
if [[ "${rc:-0}" -eq 0 ]]; then
echo $code
if [[ $code -ne %d ]]; then
exit 1
fi
else
echo "error ${rc}" 1>&2
fi
done
`, times, host, url, statusCode)
output, err := e2e.RunHostCmd(ns, execPodName, cmd)
if err != nil {
return fmt.Errorf("host command failed: %v\n%s", err, output)
}
return nil
}
func expectRouteStatusCodeExec(ns, execPodName, url, host string, statusCode int) error {
cmd := fmt.Sprintf("curl -s -o /dev/null -w '%%{http_code}' --header 'Host: %s' %q", host, url)
output, err := e2e.RunHostCmd(ns, execPodName, cmd)
if err != nil {
return fmt.Errorf("host command failed: %v\n%s", err, output)
}
if output != strconv.Itoa(statusCode) {
return fmt.Errorf("last response from server was not %d: %s", statusCode, output)
}
return nil
}
func getAuthenticatedRouteURLViaPod(ns, execPodName, url, host, user, pass string) (string, error) {
cmd := fmt.Sprintf("curl -s -u %s:%s --header 'Host: %s' %q", user, pass, host, url)
output, err := e2e.RunHostCmd(ns, execPodName, cmd)
if err != nil {
return "", fmt.Errorf("host command failed: %v\n%s", err, output)
}
return output, nil
}
func dumpScopedRouterLogs(oc *exutil.CLI, name string) {
log, _ := e2e.GetPodLogs(oc.AdminKubeClient(), oc.KubeFramework().Namespace.Name, "scoped-router", "router")
e2e.Logf("Scoped Router test %s logs:\n %s", name, log)
}