|
1 | 1 | package templates
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "crypto/tls" |
| 5 | + "encoding/json" |
4 | 6 | "fmt"
|
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "os/exec" |
| 10 | + "time" |
5 | 11 |
|
6 | 12 | g "github.com/onsi/ginkgo"
|
7 | 13 | o "github.com/onsi/gomega"
|
8 | 14 |
|
| 15 | + kerrors "k8s.io/apimachinery/pkg/api/errors" |
| 16 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | + "k8s.io/apimachinery/pkg/util/wait" |
| 18 | + "k8s.io/client-go/pkg/apis/extensions" |
| 19 | + kapi "k8s.io/kubernetes/pkg/api" |
| 20 | + kapiv1 "k8s.io/kubernetes/pkg/api/v1" |
| 21 | + kexternalclientset "k8s.io/kubernetes/pkg/client/clientset_generated/clientset" |
| 22 | + e2e "k8s.io/kubernetes/test/e2e/framework" |
| 23 | + intframework "k8s.io/kubernetes/test/integration/framework" |
| 24 | + |
9 | 25 | authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
|
| 26 | + osbclient "github.com/openshift/origin/pkg/openservicebroker/client" |
| 27 | + templateapi "github.com/openshift/origin/pkg/template/apis/template" |
10 | 28 | userapi "github.com/openshift/origin/pkg/user/apis/user"
|
11 | 29 | exutil "github.com/openshift/origin/test/extended/util"
|
12 |
| - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
13 |
| - kapi "k8s.io/kubernetes/pkg/api" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + tsbNS = "openshift-template-service-broker" |
| 34 | + tsbHost = "apiserver." + tsbNS + ".svc" |
14 | 35 | )
|
15 | 36 |
|
16 | 37 | func createUser(cli *exutil.CLI, name, role string) *userapi.User {
|
@@ -58,3 +79,96 @@ func setUser(cli *exutil.CLI, user *userapi.User) {
|
58 | 79 | cli.ChangeUser(user.Name)
|
59 | 80 | }
|
60 | 81 | }
|
| 82 | + |
| 83 | +func EnsureTSB(tsbOC *exutil.CLI) (osbclient.Client, *exec.Cmd) { |
| 84 | + exists := true |
| 85 | + if _, err := tsbOC.AdminKubeClient().Extensions().DaemonSets(tsbNS).Get("apiserver", metav1.GetOptions{}); err != nil { |
| 86 | + if !kerrors.IsNotFound(err) { |
| 87 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 88 | + } |
| 89 | + exists = false |
| 90 | + } |
| 91 | + |
| 92 | + if !exists { |
| 93 | + e2e.Logf("Installing TSB onto the cluster for testing") |
| 94 | + _, _, err := tsbOC.AsAdmin().WithoutNamespace().Run("create", "namespace").Args(tsbNS).Outputs() |
| 95 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 96 | + configPath := exutil.FixturePath("..", "..", "examples", "templateservicebroker", "templateservicebroker-template.yaml") |
| 97 | + stdout, _, err := tsbOC.WithoutNamespace().Run("process").Args("-f", configPath).Outputs() |
| 98 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 99 | + err = tsbOC.WithoutNamespace().AsAdmin().Run("create").Args("-f", "-").InputString(stdout).Execute() |
| 100 | + // this is weird, but we have to ignore this error in case some of this already exists (race between tests and the like) |
| 101 | + //o.Expect(err).NotTo(o.HaveOccurred()) |
| 102 | + err = WaitForDaemonSetStatus(tsbOC.AdminKubeClient(), &extensions.DaemonSet{ObjectMeta: metav1.ObjectMeta{Name: "apiserver", Namespace: tsbNS}}) |
| 103 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 104 | + } |
| 105 | + |
| 106 | + // we're trying to test the TSB, not the service. We're outside all the normal networks. Run a portforward to a particular |
| 107 | + // pod and test that |
| 108 | + pods, err := tsbOC.AdminKubeClient().Core().Pods(tsbNS).List(metav1.ListOptions{}) |
| 109 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 110 | + var pod *kapiv1.Pod |
| 111 | + for i := range pods.Items { |
| 112 | + currPod := pods.Items[i] |
| 113 | + for _, cond := range currPod.Status.Conditions { |
| 114 | + if cond.Type == kapiv1.PodReady && cond.Status == kapiv1.ConditionTrue { |
| 115 | + pod = &currPod |
| 116 | + break |
| 117 | + } else { |
| 118 | + out, _ := json.Marshal(currPod.Status) |
| 119 | + e2e.Logf("%v %v", currPod.Name, string(out)) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + if pod == nil { |
| 124 | + e2e.Failf("no ready pod found") |
| 125 | + } |
| 126 | + port, err := intframework.FindFreeLocalPort() |
| 127 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 128 | + portForwardCmd, _, _, err := tsbOC.AsAdmin().WithoutNamespace().Run("port-forward").Args("-n="+tsbNS, pod.Name, fmt.Sprintf("%d:8443", port)).Background() |
| 129 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 130 | + |
| 131 | + svc, err := tsbOC.AdminKubeClient().Core().Services(tsbNS).Get("apiserver", metav1.GetOptions{}) |
| 132 | + o.Expect(err).NotTo(o.HaveOccurred()) |
| 133 | + tsbclient := osbclient.NewClient(&http.Client{Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}, fmt.Sprintf("https://localhost:%d%s", port, templateapi.ServiceBrokerRoot)) |
| 134 | + |
| 135 | + // wait to get back healthy from the tsb |
| 136 | + healthResponse := "" |
| 137 | + err = wait.Poll(e2e.Poll, 2*time.Minute, func() (bool, error) { |
| 138 | + resp, err := tsbclient.Client().Get("https://" + svc.Spec.ClusterIP + "/healthz") |
| 139 | + if err != nil { |
| 140 | + return false, err |
| 141 | + } |
| 142 | + defer resp.Body.Close() |
| 143 | + content, _ := ioutil.ReadAll(resp.Body) |
| 144 | + healthResponse = string(content) |
| 145 | + if resp.StatusCode == http.StatusOK { |
| 146 | + return true, nil |
| 147 | + } |
| 148 | + return false, nil |
| 149 | + }) |
| 150 | + if err != nil { |
| 151 | + o.Expect(fmt.Errorf("error waiting for the TSB to be healthy: %v: %v", healthResponse, err)).NotTo(o.HaveOccurred()) |
| 152 | + } |
| 153 | + |
| 154 | + return tsbclient, portForwardCmd |
| 155 | +} |
| 156 | + |
| 157 | +// Waits for the daemonset to have at least one ready pod |
| 158 | +func WaitForDaemonSetStatus(c kexternalclientset.Interface, d *extensions.DaemonSet) error { |
| 159 | + err := wait.Poll(e2e.Poll, 5*time.Minute, func() (bool, error) { |
| 160 | + var err error |
| 161 | + ds, err := c.Extensions().DaemonSets(d.Namespace).Get(d.Name, metav1.GetOptions{}) |
| 162 | + if err != nil { |
| 163 | + return false, err |
| 164 | + } |
| 165 | + if ds.Status.NumberReady > 0 { |
| 166 | + return true, nil |
| 167 | + } |
| 168 | + return false, nil |
| 169 | + }) |
| 170 | + if err != nil { |
| 171 | + return fmt.Errorf("error waiting for ds %q status to match expectation: %v", d.Name, err) |
| 172 | + } |
| 173 | + return nil |
| 174 | +} |
0 commit comments