|
| 1 | +package images |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + g "github.com/onsi/ginkgo" |
| 8 | + o "github.com/onsi/gomega" |
| 9 | + |
| 10 | + kapi "k8s.io/kubernetes/pkg/api" |
| 11 | + kapierrs "k8s.io/kubernetes/pkg/api/errors" |
| 12 | + e2e "k8s.io/kubernetes/test/e2e/framework" |
| 13 | + |
| 14 | + exutil "github.com/openshift/origin/test/extended/util" |
| 15 | +) |
| 16 | + |
| 17 | +var _ = g.Describe("[Conformance][networking][router] openshift routers", func() { |
| 18 | + defer g.GinkgoRecover() |
| 19 | + var ( |
| 20 | + oc = exutil.NewCLI("router-metrics", exutil.KubeConfigPath()) |
| 21 | + |
| 22 | + username, password, execPodName, ns string |
| 23 | + ) |
| 24 | + |
| 25 | + g.BeforeEach(func() { |
| 26 | + dc, err := oc.AdminClient().DeploymentConfigs("default").Get("router") |
| 27 | + if kapierrs.IsNotFound(err) { |
| 28 | + g.Skip("no router installed on the cluster") |
| 29 | + return |
| 30 | + } |
| 31 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 32 | + env := dc.Spec.Template.Spec.Containers[0].Env |
| 33 | + username, password = findEnvVar(env, "STATS_USERNAME"), findEnvVar(env, "STATS_PASSWORD") |
| 34 | + |
| 35 | + ns = oc.KubeFramework().Namespace.Name |
| 36 | + }) |
| 37 | + |
| 38 | + g.Describe("The HAProxy router", func() { |
| 39 | + g.It("should expose a health check on the metrics port", func() { |
| 40 | + execPodName = exutil.CreateExecPodOrFail(oc.AdminKubeClient().Core(), ns, "execpod") |
| 41 | + defer func() { oc.AdminKubeClient().Core().Pods(ns).Delete(execPodName, kapi.NewDeleteOptions(1)) }() |
| 42 | + |
| 43 | + g.By("listening on the health port") |
| 44 | + err := expectURLStatusCodeExec(ns, execPodName, "http://router.default.svc:1935/healthz", 200) |
| 45 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 46 | + }) |
| 47 | + |
| 48 | + g.It("should expose prometheus metrics", func() { |
| 49 | + execPodName = exutil.CreateExecPodOrFail(oc.AdminKubeClient().Core(), ns, "execpod") |
| 50 | + defer func() { oc.AdminKubeClient().Core().Pods(ns).Delete(execPodName, kapi.NewDeleteOptions(1)) }() |
| 51 | + |
| 52 | + g.By("preventing access without a username and password") |
| 53 | + err := expectURLStatusCodeExec(ns, execPodName, "http://router.default.svc:1935/metrics", 403) |
| 54 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 55 | + |
| 56 | + g.By("at /metrics endpoint") |
| 57 | + results, err := getAuthenticatedURLViaPod(ns, execPodName, "http://router.default.svc:1935/metrics", username, password) |
| 58 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 59 | + o.Expect(results).To(o.ContainSubstring("haproxy_server_max_sessions{")) |
| 60 | + o.Expect(results).To(o.ContainSubstring("openshift_build_info{")) |
| 61 | + o.Expect(results).To(o.ContainSubstring("haproxy_process_max_fds ")) |
| 62 | + o.Expect(results).To(o.ContainSubstring("haproxy_up 1")) |
| 63 | + }) |
| 64 | + |
| 65 | + g.It("should expose the profiling endpoints", func() { |
| 66 | + execPodName = exutil.CreateExecPodOrFail(oc.AdminKubeClient().Core(), ns, "execpod") |
| 67 | + defer func() { oc.AdminKubeClient().Core().Pods(ns).Delete(execPodName, kapi.NewDeleteOptions(1)) }() |
| 68 | + |
| 69 | + g.By("preventing access without a username and password") |
| 70 | + err := expectURLStatusCodeExec(ns, execPodName, "http://router.default.svc:1935/debug/pprof/heap", 403) |
| 71 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 72 | + |
| 73 | + g.By("at /debug/pprof") |
| 74 | + results, err := getAuthenticatedURLViaPod(ns, execPodName, "http://router.default.svc:1935/debug/pprof/heap", username, password) |
| 75 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 76 | + o.Expect(results).To(o.ContainSubstring("# runtime.MemStats")) |
| 77 | + }) |
| 78 | + }) |
| 79 | +}) |
| 80 | + |
| 81 | +func findEnvVar(vars []kapi.EnvVar, key string) string { |
| 82 | + for _, v := range vars { |
| 83 | + if v.Name == key { |
| 84 | + return v.Value |
| 85 | + } |
| 86 | + } |
| 87 | + return "" |
| 88 | +} |
| 89 | + |
| 90 | +func expectURLStatusCodeExec(ns, execPodName, url string, statusCode int) error { |
| 91 | + cmd := fmt.Sprintf("curl -s -o /dev/null -w '%%{http_code}' %q", url) |
| 92 | + output, err := e2e.RunHostCmd(ns, execPodName, cmd) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("host command failed: %v\n%s", err, output) |
| 95 | + } |
| 96 | + if output != strconv.Itoa(statusCode) { |
| 97 | + return fmt.Errorf("last response from server was not %d: %s", statusCode, output) |
| 98 | + } |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func getAuthenticatedURLViaPod(ns, execPodName, url, user, pass string) (string, error) { |
| 103 | + cmd := fmt.Sprintf("curl -s -u %s:%s %q", user, pass, url) |
| 104 | + output, err := e2e.RunHostCmd(ns, execPodName, cmd) |
| 105 | + if err != nil { |
| 106 | + return "", fmt.Errorf("host command failed: %v\n%s", err, output) |
| 107 | + } |
| 108 | + return output, nil |
| 109 | +} |
0 commit comments