Skip to content

Commit b704800

Browse files
committed
Support to init k8s cluster
1 parent a40fe97 commit b704800

File tree

6 files changed

+119
-48
lines changed

6 files changed

+119
-48
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ RUN apk add --no-cache \
3333
COPY entrypoint.sh /entrypoint.sh
3434
COPY --from=builder /workspace/atest /usr/bin/atest
3535
COPY --from=hd /usr/local/bin/hd /usr/local/bin/hd
36-
RUN hd i kubernetes-sigs/kubectl
36+
RUN hd i kubernetes-sigs/kubectl && \
37+
hd i k3d
3738

3839
ENTRYPOINT ["/entrypoint.sh"]

action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,20 @@ inputs:
55
description: 'The pattern of the items'
66
required: true
77
default: 'testcase-*.yaml'
8+
kustomization:
9+
description: 'The kustomization file path'
10+
required: true
11+
waitNamespace:
12+
description: 'waitNamespace'
13+
required: true
14+
waitResource:
15+
description: 'waitResource'
16+
required: true
817
runs:
918
using: 'docker'
1019
image: 'Dockerfile'
1120
args:
1221
- ${{ inputs.pattern }}
22+
- ${{ inputs.kustomization }}
23+
- ${{ inputs.waitNamespace }}
24+
- ${{ inputs.waitResource }}

cmd/init.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"github.com/linuxsuren/api-testing/pkg/exec"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
type initOption struct {
9+
kustomization string
10+
waitNamespace string
11+
waitResource string
12+
}
13+
14+
func createInitCommand() (cmd *cobra.Command) {
15+
opt := &initOption{}
16+
cmd = &cobra.Command{
17+
Use: "init",
18+
Long: "Support to init Kubernetes cluster with kustomization, and wait it with command: kubectl wait",
19+
RunE: opt.runE,
20+
}
21+
22+
flags := cmd.Flags()
23+
flags.StringVarP(&opt.kustomization, "kustomization", "k", "", "The kustomization file path")
24+
flags.StringVarP(&opt.waitNamespace, "wait-namespace", "", "", "")
25+
flags.StringVarP(&opt.waitResource, "wait-resource", "", "", "")
26+
return
27+
}
28+
29+
func (o *initOption) runE(cmd *cobra.Command, args []string) (err error) {
30+
if o.kustomization != "" {
31+
if err = exec.RunCommand("kubectl", "apply", "-k", o.kustomization, "--wait=true"); err != nil {
32+
return
33+
}
34+
}
35+
36+
if o.waitNamespace != "" && o.waitResource != "" {
37+
if err = exec.RunCommand("kubectl", "wait", "-n", o.waitNamespace, o.waitResource, "--for", "condition=Available=True", "--timeout=900s"); err != nil {
38+
return
39+
}
40+
}
41+
return
42+
}

cmd/root.go

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,18 @@
11
package main
22

33
import (
4-
"github.com/linuxsuren/api-testing/pkg/runner"
5-
"github.com/linuxsuren/api-testing/pkg/testing"
64
"github.com/spf13/cobra"
75
"os"
8-
"path"
9-
"path/filepath"
106
)
117

12-
type option struct {
13-
pattern string
14-
}
15-
168
func main() {
17-
opt := &option{}
18-
199
cmd := &cobra.Command{
20-
Use: "atest",
21-
RunE: opt.runE,
10+
Use: "atest",
2211
}
23-
24-
// set flags
25-
flags := cmd.Flags()
26-
flags.StringVarP(&opt.pattern, "pattern", "p", "testcase-*.yaml",
27-
"The file pattern which try to execute the test cases")
12+
cmd.AddCommand(createInitCommand(), createRunCommand())
2813

2914
// run command
3015
if err := cmd.Execute(); err != nil {
3116
os.Exit(1)
3217
}
3318
}
34-
35-
func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
36-
var files []string
37-
if files, err = filepath.Glob(o.pattern); err == nil {
38-
for i := range files {
39-
item := files[i]
40-
41-
var testcase *testing.TestCase
42-
if testcase, err = testing.Parse(item); err != nil {
43-
return
44-
}
45-
46-
setRelativeDir(item, testcase)
47-
48-
if err = runner.RunTestCase(testcase); err != nil {
49-
return
50-
}
51-
}
52-
}
53-
return
54-
}
55-
56-
func setRelativeDir(configFile string, testcase *testing.TestCase) {
57-
dir := filepath.Dir(configFile)
58-
59-
for i := range testcase.Prepare.Kubernetes {
60-
testcase.Prepare.Kubernetes[i] = path.Join(dir, testcase.Prepare.Kubernetes[i])
61-
}
62-
}

cmd/run.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"github.com/linuxsuren/api-testing/pkg/runner"
5+
"github.com/linuxsuren/api-testing/pkg/testing"
6+
"github.com/spf13/cobra"
7+
"path"
8+
"path/filepath"
9+
)
10+
11+
type runOption struct {
12+
pattern string
13+
}
14+
15+
func createRunCommand() (cmd *cobra.Command) {
16+
opt := &runOption{}
17+
cmd = &cobra.Command{
18+
Use: "run",
19+
RunE: opt.runE,
20+
}
21+
22+
// set flags
23+
flags := cmd.Flags()
24+
flags.StringVarP(&opt.pattern, "pattern", "p", "testcase-*.yaml",
25+
"The file pattern which try to execute the test cases")
26+
return
27+
}
28+
29+
func (o *runOption) runE(cmd *cobra.Command, args []string) (err error) {
30+
var files []string
31+
if files, err = filepath.Glob(o.pattern); err == nil {
32+
for i := range files {
33+
item := files[i]
34+
35+
var testcase *testing.TestCase
36+
if testcase, err = testing.Parse(item); err != nil {
37+
return
38+
}
39+
40+
setRelativeDir(item, testcase)
41+
42+
if err = runner.RunTestCase(testcase); err != nil {
43+
return
44+
}
45+
}
46+
}
47+
return
48+
}
49+
50+
func setRelativeDir(configFile string, testcase *testing.TestCase) {
51+
dir := filepath.Dir(configFile)
52+
53+
for i := range testcase.Prepare.Kubernetes {
54+
testcase.Prepare.Kubernetes[i] = path.Join(dir, testcase.Prepare.Kubernetes[i])
55+
}
56+
}

entrypoint.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#!/bin/sh
22

3-
atest -p "$1"
3+
k3d cluster create
4+
k3d cluster list
5+
6+
atest init -k $2 --wait-namespace $3 --wait-resource $4
7+
atest run -p "$1"

0 commit comments

Comments
 (0)