|
| 1 | +// helper is a package that offers e2e test helpers. It's a badly named package. |
| 2 | +// If it grows we should refactor it into something a little more manageable |
| 3 | +// but it's fine for now. |
| 4 | +package helper |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + appsv1 "k8s.io/api/apps/v1" |
| 16 | + corev1 "k8s.io/api/core/v1" |
| 17 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 18 | + controllerruntimeclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 19 | + "sigs.k8s.io/e2e-framework/klient/k8s/resources" |
| 20 | + "sigs.k8s.io/e2e-framework/klient/wait" |
| 21 | + "sigs.k8s.io/e2e-framework/klient/wait/conditions" |
| 22 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 23 | +) |
| 24 | + |
| 25 | +const debugDeploymentName = "debugy" |
| 26 | + |
| 27 | +// EnsureDebugContainer ensures that the helper debug container is installed right namespace. This allows us to make requests from inside the cluster |
| 28 | +// regardless of the external network configuration. |
| 29 | +func EnsureDebugContainer(t *testing.T, ctx context.Context, cfg *envconf.Config, namespace string) { |
| 30 | + t.Helper() |
| 31 | + |
| 32 | + client, err := cfg.NewClient() |
| 33 | + if err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | + |
| 37 | + // Deploy a debug container so that we can test that the app is available later |
| 38 | + deployment := newDebugDeployment(namespace, debugDeploymentName, 1, debugDeploymentName) |
| 39 | + if err = client.Resources().Create(ctx, deployment); controllerruntimeclient.IgnoreAlreadyExists(err) != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + |
| 43 | + err = wait.For( |
| 44 | + conditions.New(client.Resources()). |
| 45 | + DeploymentConditionMatch(deployment, appsv1.DeploymentAvailable, corev1.ConditionTrue), |
| 46 | + wait.WithTimeout(time.Minute*5)) |
| 47 | + if err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// PostToSpinApp is a crude function for using the debug pod to post to a spin app |
| 53 | +// within the cluster. It allows customization of the route, but all requests |
| 54 | +// are currently `POST`. |
| 55 | +func PostToSpinApp(t *testing.T, ctx context.Context, cfg *envconf.Config, namespace, spinAppName, route, body string) (string, int, error) { |
| 56 | + t.Helper() |
| 57 | + |
| 58 | + client, err := cfg.NewClient() |
| 59 | + if err != nil { |
| 60 | + t.Fatal(err) |
| 61 | + } |
| 62 | + |
| 63 | + // Find the debug pod |
| 64 | + pods := &corev1.PodList{} |
| 65 | + err = client.Resources(namespace).List(ctx, pods, resources.WithLabelSelector("app=pod-exec")) |
| 66 | + if err != nil || pods.Items == nil || len(pods.Items) == 0 { |
| 67 | + return "", -1, fmt.Errorf("failed to get debug pods: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + debugPod := pods.Items[0] |
| 71 | + |
| 72 | + podName := debugPod.Name |
| 73 | + |
| 74 | + command := []string{"curl", "-s", "-m", "5", "-w", "\n%{http_code}\n", "http://" + spinAppName + "." + namespace + route, "--data", body, "-o", "-"} |
| 75 | + |
| 76 | + var stdout, stderr bytes.Buffer |
| 77 | + if err := client.Resources().ExecInPod(ctx, namespace, podName, debugDeploymentName, command, &stdout, &stderr); err != nil { |
| 78 | + t.Logf("Curl Spin App failed, err: %v.\nstdout:\n%s\n\nstderr:\n%s\n", err, stdout.String(), stderr.String()) |
| 79 | + return "", -1, err |
| 80 | + } |
| 81 | + |
| 82 | + parts := strings.SplitN(stdout.String(), "\n", 2) |
| 83 | + if len(parts) != 2 { |
| 84 | + t.Fatalf("Curl Spin App failed, unexpected response format: %s", &stdout) |
| 85 | + } |
| 86 | + |
| 87 | + strStatus := strings.Trim(parts[1], "\n") |
| 88 | + statusCode, err := strconv.Atoi(strStatus) |
| 89 | + if err != nil { |
| 90 | + t.Logf("error parsing status code: %v", err) |
| 91 | + return parts[0], statusCode, err |
| 92 | + } |
| 93 | + t.Logf("Curl Spin App response: %s, status code: %d, err: %v", parts[0], statusCode, err) |
| 94 | + return parts[0], statusCode, nil |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +func newDebugDeployment(namespace string, name string, replicas int32, containerName string) *appsv1.Deployment { |
| 99 | + labels := map[string]string{"app": "pod-exec"} |
| 100 | + return &appsv1.Deployment{ |
| 101 | + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace}, |
| 102 | + Spec: appsv1.DeploymentSpec{ |
| 103 | + Replicas: &replicas, |
| 104 | + Selector: &metav1.LabelSelector{ |
| 105 | + MatchLabels: labels, |
| 106 | + }, |
| 107 | + Template: corev1.PodTemplateSpec{ |
| 108 | + ObjectMeta: metav1.ObjectMeta{Labels: labels}, |
| 109 | + Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: containerName, Image: "nginx"}}}, |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | +} |
0 commit comments