Skip to content

Commit 0d4d0f9

Browse files
committed
feat: add workload test case for external tests
Signed-off-by: Patrick J.P. Culp <jpculp@amazon.com>
1 parent 78811ac commit 0d4d0f9

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

test/cases/workload/main_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//go:build e2e
2+
3+
package workload
4+
5+
import (
6+
"context"
7+
"flag"
8+
"log"
9+
"os"
10+
"os/signal"
11+
"testing"
12+
13+
"sigs.k8s.io/e2e-framework/pkg/env"
14+
"sigs.k8s.io/e2e-framework/pkg/envconf"
15+
)
16+
17+
var (
18+
testenv env.Environment
19+
workloadTestCommand *string
20+
workloadTestImage *string
21+
workloadTestName *string
22+
)
23+
24+
func TestMain(m *testing.M) {
25+
workloadTestCommand = flag.String("workloadTestCommand", "", "command for workload test")
26+
workloadTestImage = flag.String("workloadTestImage", "", "image for workload test")
27+
workloadTestName = flag.String("workloadTestName", "workload-test", "name for workload test")
28+
cfg, err := envconf.NewFromFlags()
29+
if err != nil {
30+
log.Fatalf("failed to initialize test environment: %v", err)
31+
}
32+
testenv = env.NewWithConfig(cfg)
33+
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
34+
defer cancel()
35+
testenv = testenv.WithContext(ctx)
36+
37+
testenv.Setup(func(ctx context.Context, config *envconf.Config) (context.Context, error) {
38+
log.Println("Starting workload test suite...")
39+
return ctx, nil
40+
})
41+
42+
os.Exit(testenv.Run(m))
43+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
kind: Job
2+
apiVersion: batch/v1
3+
metadata:
4+
name: {{.WorkloadTestName}}
5+
labels:
6+
app: {{.WorkloadTestName}}
7+
spec:
8+
template:
9+
metadata:
10+
labels:
11+
app: {{.WorkloadTestName}}
12+
spec:
13+
containers:
14+
- name: {{.WorkloadTestName}}
15+
image: {{.WorkloadTestImage}}
16+
command: ["/bin/bash", "-c"]
17+
args: ["{{.WorkloadTestCommand}}"]
18+
imagePullPolicy: Always
19+
restartPolicy: Never
20+
backoffLimit: 4
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)