|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/replicatedhq/troubleshoot/pkg/convert" |
| 14 | + appsv1 "k8s.io/api/apps/v1" |
| 15 | + corev1 "k8s.io/api/core/v1" |
| 16 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 18 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 19 | +) |
| 20 | + |
| 21 | +func TestPendingPod(t *testing.T) { |
| 22 | + supportBundleName := "pod-deployment" |
| 23 | + deploymentName := "test-pending-deployment" |
| 24 | + containerName := "curl" |
| 25 | + feature := features.New("Pending Pod Test"). |
| 26 | + Setup(func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { |
| 27 | + deployment := newDeployment(c.Namespace(), deploymentName, 1, containerName) |
| 28 | + client, err := c.NewClient() |
| 29 | + if err != nil { |
| 30 | + t.Fatal(err) |
| 31 | + } |
| 32 | + if err = client.Resources().Create(ctx, deployment); err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + |
| 36 | + return ctx |
| 37 | + }). |
| 38 | + Assess("check support bundle catch pending pod", func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { |
| 39 | + var out bytes.Buffer |
| 40 | + var results []*convert.Result |
| 41 | + |
| 42 | + tarPath := fmt.Sprintf("%s.tar.gz", supportBundleName) |
| 43 | + targetFile := fmt.Sprintf("%s/analysis.json", supportBundleName) |
| 44 | + |
| 45 | + cmd := exec.Command("../../../bin/support-bundle", "spec/pod.yaml", "--interactive=false", fmt.Sprintf("-o=%s", supportBundleName)) |
| 46 | + cmd.Stdout = &out |
| 47 | + err := cmd.Run() |
| 48 | + if err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + |
| 52 | + analysisJSON, err := readFileFromTar(tarPath, targetFile) |
| 53 | + if err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + |
| 57 | + err = json.Unmarshal(analysisJSON, &results) |
| 58 | + if err != nil { |
| 59 | + t.Fatal(err) |
| 60 | + } |
| 61 | + |
| 62 | + for _, result := range results { |
| 63 | + if strings.Contains(result.Insight.Detail, deploymentName) { |
| 64 | + return ctx |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + t.Fatal("Pending pod not found") |
| 69 | + defer func() { |
| 70 | + err := os.Remove(fmt.Sprintf("%s.tar.gz", supportBundleName)) |
| 71 | + if err != nil { |
| 72 | + t.Fatal("Error remove file:", err) |
| 73 | + } |
| 74 | + }() |
| 75 | + return ctx |
| 76 | + }).Feature() |
| 77 | + testenv.Test(t, feature) |
| 78 | +} |
| 79 | + |
| 80 | +func newDeployment(namespace string, name string, replicas int32, containerName string) *appsv1.Deployment { |
| 81 | + labels := map[string]string{"app": "pending-test"} |
| 82 | + return &appsv1.Deployment{ |
| 83 | + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace}, |
| 84 | + Spec: appsv1.DeploymentSpec{ |
| 85 | + Replicas: &replicas, |
| 86 | + Selector: &metav1.LabelSelector{ |
| 87 | + MatchLabels: labels, |
| 88 | + }, |
| 89 | + Template: corev1.PodTemplateSpec{ |
| 90 | + ObjectMeta: metav1.ObjectMeta{Labels: labels}, |
| 91 | + Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: containerName, Image: "nginx", Command: []string{"wge", "-O", "/work-dir/index.html", "https://www.wikipedia.org"}}}}, |
| 92 | + }, |
| 93 | + }, |
| 94 | + } |
| 95 | +} |
0 commit comments