|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package workload |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + _ "embed" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + fwext "github.com/aws/aws-k8s-tester/internal/e2e" |
| 13 | + batchv1 "k8s.io/api/batch/v1" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "sigs.k8s.io/e2e-framework/klient/wait" |
| 16 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 17 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + //go:embed manifests/single-node-test-workload.yaml |
| 22 | + workloadSingleNodeManifest []byte |
| 23 | + renderedWorkloadSingleNodeManifest []byte |
| 24 | +) |
| 25 | + |
| 26 | +type workloadSingleNodeManifestTplVars struct { |
| 27 | + WorkloadTestCommand string |
| 28 | + WorkloadTestImage string |
| 29 | + WorkloadTestName string |
| 30 | +} |
| 31 | + |
| 32 | +func TestWorkload(t *testing.T) { |
| 33 | + singleNode := features.New("single-node"). |
| 34 | + WithLabel("suite", "workload"). |
| 35 | + Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 36 | + if *workloadTestCommand == "" { |
| 37 | + t.Fatal(fmt.Errorf("workloadTestCommand must be set to run workload test")) |
| 38 | + } |
| 39 | + if *workloadTestImage == "" { |
| 40 | + t.Fatal(fmt.Errorf("workloadTestImage must be set to run workload test")) |
| 41 | + } |
| 42 | + if *workloadTestName == "" { |
| 43 | + t.Fatal(fmt.Errorf("workloadTestName must be set to run workload test")) |
| 44 | + } |
| 45 | + var err error |
| 46 | + renderedWorkloadSingleNodeManifest, err = fwext.RenderManifests(workloadSingleNodeManifest, workloadSingleNodeManifestTplVars{ |
| 47 | + WorkloadTestCommand: *workloadTestCommand, |
| 48 | + WorkloadTestImage: *workloadTestImage, |
| 49 | + WorkloadTestName: *workloadTestName, |
| 50 | + }) |
| 51 | + if err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + t.Log("Applying single node manifest") |
| 55 | + err = fwext.ApplyManifests(cfg.Client().RESTConfig(), renderedWorkloadSingleNodeManifest) |
| 56 | + if err != nil { |
| 57 | + t.Fatal(err) |
| 58 | + } |
| 59 | + t.Log("Manifest applied successfully") |
| 60 | + return ctx |
| 61 | + }). |
| 62 | + Assess("Single node test Job succeeds", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 63 | + job := &batchv1.Job{ |
| 64 | + ObjectMeta: metav1.ObjectMeta{Name: *workloadTestName, Namespace: "default"}, |
| 65 | + } |
| 66 | + t.Log("Waiting for single node job to complete") |
| 67 | + err := wait.For(fwext.NewConditionExtension(cfg.Client().Resources()).JobSucceeded(job), |
| 68 | + wait.WithContext(ctx), |
| 69 | + wait.WithTimeout(time.Minute*20), |
| 70 | + ) |
| 71 | + if err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + return ctx |
| 75 | + }). |
| 76 | + Teardown(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 77 | + log, err := fwext.GetJobLogs(cfg.Client().RESTConfig(), &batchv1.Job{ |
| 78 | + ObjectMeta: metav1.ObjectMeta{Name: *workloadTestName, Namespace: "default"}, |
| 79 | + }) |
| 80 | + if err != nil { |
| 81 | + t.Error(err) |
| 82 | + } else { |
| 83 | + t.Log(fmt.Sprintf("Test log for %s:", *workloadTestName)) |
| 84 | + t.Log(log) |
| 85 | + } |
| 86 | + err = fwext.DeleteManifests(cfg.Client().RESTConfig(), renderedWorkloadSingleNodeManifest) |
| 87 | + if err != nil { |
| 88 | + t.Error(err) |
| 89 | + } |
| 90 | + return ctx |
| 91 | + }). |
| 92 | + Feature() |
| 93 | + |
| 94 | + testenv.Test(t, singleNode) |
| 95 | +} |
0 commit comments