forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoped.go
97 lines (85 loc) · 2.73 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
package images
import (
"fmt"
"net/http"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
"k8s.io/kubernetes/pkg/util/wait"
"k8s.io/kubernetes/test/e2e"
exutil "github.com/openshift/origin/test/extended/util"
)
var _ = g.Describe("Router", func() {
defer g.GinkgoRecover()
var (
configPath = exutil.FixturePath("fixtures", "scoped-router.yaml")
oc = exutil.NewCLI("scoped-router", exutil.KubeConfigPath())
)
g.Describe("The HAProxy router", func() {
g.It("should serve the correct routes when scoped to a single namespace and label set", func() {
oc.SetOutputDir(exutil.TestContext.OutputDir)
g.By(fmt.Sprintf("creating a scoped router from a config file %q", configPath))
err := oc.Run("create").Args("-f", configPath).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
var routerIP string
err = wait.Poll(time.Second, 2*time.Minute, func() (bool, error) {
pod, err := oc.KubeFramework().Client.Pods(oc.KubeFramework().Namespace.Name).Get("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 valid route to respond")
err = waitForRouterOKResponse(routerURL, "first.example.com", 2*time.Minute)
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))
req, err := requestViaReverseProxy("GET", routerURL, host)
o.Expect(err).NotTo(o.HaveOccurred())
resp, err := http.DefaultClient.Do(req)
o.Expect(err).NotTo(o.HaveOccurred())
resp.Body.Close()
if resp.StatusCode != http.StatusServiceUnavailable {
e2e.Failf("should have had a 503 status code for %s", host)
}
}
})
})
})
func waitForRouterOKResponse(url, host string, timeout time.Duration) error {
return wait.Poll(time.Second, timeout, func() (bool, error) {
req, err := requestViaReverseProxy("GET", url, host)
if err != nil {
return false, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
resp.Body.Close()
if resp.StatusCode == http.StatusServiceUnavailable {
// not ready yet
return false, nil
}
if resp.StatusCode != http.StatusOK {
e2e.Logf("unexpected response: %#v", resp.StatusCode)
return false, nil
}
return true, nil
})
}
func requestViaReverseProxy(method, url, host string) (*http.Request, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
req.Host = host
return req, nil
}