Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions test/cases/workload/main_test.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just want to nit the naming, "workload" is pretty broad, could we pick a clear name with a theme like "jobTemplate"? I'm really open to suggestions but i want to make sure this is sane/immediately readable

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build e2e

package workload

import (
"context"
"flag"
"log"
"os"
"os/signal"
"testing"

"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
)

var (
testenv env.Environment
workloadTestCommand *string
workloadTestImage *string
workloadTestName *string
)

func TestMain(m *testing.M) {
workloadTestCommand = flag.String("workloadTestCommand", "", "command for workload test")
workloadTestImage = flag.String("workloadTestImage", "", "image for workload test")
workloadTestName = flag.String("workloadTestName", "workload-test", "name for workload test")
cfg, err := envconf.NewFromFlags()
if err != nil {
log.Fatalf("failed to initialize test environment: %v", err)
}
testenv = env.NewWithConfig(cfg)
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
testenv = testenv.WithContext(ctx)

testenv.Setup(func(ctx context.Context, config *envconf.Config) (context.Context, error) {
log.Println("Starting workload test suite...")
return ctx, nil
})

os.Exit(testenv.Run(m))
}
20 changes: 20 additions & 0 deletions test/cases/workload/manifests/single-node-test-workload.yaml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this template is actually so minimal i feel like its worth just using the go type for?

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
kind: Job
apiVersion: batch/v1
metadata:
name: {{.WorkloadTestName}}
labels:
app: {{.WorkloadTestName}}
spec:
template:
metadata:
labels:
app: {{.WorkloadTestName}}
spec:
containers:
- name: {{.WorkloadTestName}}
image: {{.WorkloadTestImage}}
command: ["/bin/bash", "-c"]
args: ["{{.WorkloadTestCommand}}"]
imagePullPolicy: Always
restartPolicy: Never
backoffLimit: 4
95 changes: 95 additions & 0 deletions test/cases/workload/workload_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//go:build e2e

package workload

import (
"context"
_ "embed"
"fmt"
"testing"
"time"

fwext "github.com/aws/aws-k8s-tester/internal/e2e"
batchv1 "k8s.io/api/batch/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/e2e-framework/klient/wait"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
)

var (
//go:embed manifests/single-node-test-workload.yaml
workloadSingleNodeManifest []byte
renderedWorkloadSingleNodeManifest []byte
)

type workloadSingleNodeManifestTplVars struct {
WorkloadTestCommand string
WorkloadTestImage string
WorkloadTestName string
}

func TestWorkload(t *testing.T) {
singleNode := features.New("single-node").
WithLabel("suite", "workload").
Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
if *workloadTestCommand == "" {
t.Fatal(fmt.Errorf("workloadTestCommand must be set to run workload test"))
}
if *workloadTestImage == "" {
t.Fatal(fmt.Errorf("workloadTestImage must be set to run workload test"))
}
if *workloadTestName == "" {
t.Fatal(fmt.Errorf("workloadTestName must be set to run workload test"))
}
Comment on lines +36 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just shortening

Suggested change
if *workloadTestCommand == "" {
t.Fatal(fmt.Errorf("workloadTestCommand must be set to run workload test"))
}
if *workloadTestImage == "" {
t.Fatal(fmt.Errorf("workloadTestImage must be set to run workload test"))
}
if *workloadTestName == "" {
t.Fatal(fmt.Errorf("workloadTestName must be set to run workload test"))
}
if *workloadTestCommand == "" {
t.Fatal("workloadTestCommand must be set to run workload test")
}
if *workloadTestImage == "" {
t.Fatal("workloadTestImage must be set to run workload test")
}
if *workloadTestName == "" {
t.Fatal("workloadTestName must be set to run workload test")
}

var err error
renderedWorkloadSingleNodeManifest, err = fwext.RenderManifests(workloadSingleNodeManifest, workloadSingleNodeManifestTplVars{
WorkloadTestCommand: *workloadTestCommand,
WorkloadTestImage: *workloadTestImage,
WorkloadTestName: *workloadTestName,
})
if err != nil {
t.Fatal(err)
}
t.Log("Applying single node manifest")
err = fwext.ApplyManifests(cfg.Client().RESTConfig(), renderedWorkloadSingleNodeManifest)
if err != nil {
t.Fatal(err)
}
t.Log("Manifest applied successfully")
return ctx
}).
Assess("Single node test Job succeeds", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{Name: *workloadTestName, Namespace: "default"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is there a default namespace enum in corev1 we can use just for posterity?

}
t.Log("Waiting for single node job to complete")
err := wait.For(fwext.NewConditionExtension(cfg.Client().Resources()).JobSucceeded(job),
wait.WithContext(ctx),
wait.WithTimeout(time.Minute*20),
)
if err != nil {
t.Fatal(err)
}
return ctx
}).
Teardown(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
log, err := fwext.GetJobLogs(cfg.Client().RESTConfig(), &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{Name: *workloadTestName, Namespace: "default"},
})
if err != nil {
t.Error(err)
} else {
t.Log(fmt.Sprintf("Test log for %s:", *workloadTestName))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.Log(fmt.Sprintf("Test log for %s:", *workloadTestName))
t.Logf("Test log for %s:", *workloadTestName)

t.Log(log)
}
err = fwext.DeleteManifests(cfg.Client().RESTConfig(), renderedWorkloadSingleNodeManifest)
if err != nil {
t.Error(err)
}
return ctx
}).
Feature()

testenv.Test(t, singleNode)
}
Loading