Skip to content

Commit

Permalink
Implement test framework to test orchestration
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheng Pan committed Aug 30, 2019
1 parent 294afe3 commit 1100614
Show file tree
Hide file tree
Showing 8 changed files with 614 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ test-integration:

.PHONY: test-e2e-single-az
test-e2e-single-az:
AWS_REGION=us-west-2 AWS_AVAILABILITY_ZONES=us-west-2a GINKGO_FOCUS="\[ebs-csi-e2e\] \[single-az\]" ./hack/run-e2e-test
#AWS_REGION=us-west-2 AWS_AVAILABILITY_ZONES=us-west-2a GINKGO_FOCUS="\[ebs-csi-e2e\] \[single-az\]" ./hack/run-e2e-test
TESTCONFIG=./tester/test-config.yaml go run tester/cmd/main.go

.PHONY: test-e2e-multi-az
test-e2e-multi-az:
Expand Down
17 changes: 17 additions & 0 deletions tester/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"os"

"github.com/kubernetes-sigs/aws-ebs-csi-driver/tester"
)

func main() {
test := tester.NewTester("")
err := test.Start()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
105 changes: 105 additions & 0 deletions tester/framework.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package tester

import (
"fmt"
"os"

"github.com/kubernetes-sigs/aws-ebs-csi-driver/tester/pkg"
yaml "gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/util/rand"
)

type Tester struct {
init pkg.Step
build pkg.Step
up pkg.Step
install pkg.Step
test pkg.Step
uninstall pkg.Step
tearDown pkg.Step
}

func NewTester(configPath string) *Tester {
if configPath == "" {
configPath = readTestConfigPath()
}
testConfig, err := os.Open(configPath)
if err != nil {
panic(err)
}
var config *pkg.TestConfig
err = yaml.NewDecoder(testConfig).Decode(&config)
if err != nil {
panic(err)
}

testId := fmt.Sprintf("%d", rand.Int()%10000)
clusterCreator, err := pkg.NewClusterCreator(config, "/tmp/ebs-e2e-test", testId)
if err != nil {
panic(err)
}

return &Tester{
init: createStepOrPanic(clusterCreator.Init),
build: scriptStep(config.BuildScript, testId),
up: createStepOrPanic(clusterCreator.Up),
install: scriptStep(config.InstallScript, testId),
test: scriptStep(config.TestScript, testId),
uninstall: scriptStep(config.UninstallScript, testId),
tearDown: createStepOrPanic(clusterCreator.TearDown),
}
}

func (t *Tester) Start() error {
var err error
err = t.init.Run()
if err != nil {
return err
}

err = t.build.Run()
if err != nil {
return err
}

err = t.up.Run()
if err != nil {
return err
}

err = t.install.Run()
if err != nil {
tErr := t.tearDown.Run()
if tErr != nil {
fmt.Printf("failed to tear down cluster: %v", tErr)
}
return err
}

err = t.test.Run()
t.uninstall.Run()
t.tearDown.Run()

return err
}

func readTestConfigPath() string {
path := os.Getenv("TESTCONFIG")
if len(path) == 0 {
return "test-config.yaml"
}

return path
}

func createStepOrPanic(f func() (pkg.Step, error)) pkg.Step {
step, err := f()
if err != nil {
panic(err)
}
return step
}

func scriptStep(script string, testId string) pkg.Step {
return &pkg.TestStep{Script: script, TestId: testId}
}
35 changes: 35 additions & 0 deletions tester/pkg/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package pkg

import (
"fmt"
)

type ClusterCreator interface {
// Initialize the creator such as downloading dependencies
Init() (Step, error)

// Create and wait for cluster creation
Up() (Step, error)

// Teardown the cluster
TearDown() (Step, error)
}

func NewClusterCreator(config *TestConfig, testDir string, testId string) (ClusterCreator, error) {
cluster := config.Cluster

if cluster.Kops == nil && cluster.Eks == nil {
return nil, fmt.Errorf("TestConfig.Cluster is not set")
}

if cluster.Kops != nil && cluster.Eks != nil {
return nil, fmt.Errorf("Both Kops and Eks cluster is set")
}

if cluster.Kops != nil {
return NewKopsClusterCreator(cluster.Kops, testDir, testId), nil
}

// TODO: add for EKS cluster
return nil, nil
}
45 changes: 45 additions & 0 deletions tester/pkg/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package pkg

type TestConfig struct {
Cluster *Cluster `yaml:"cluster"`
Region string `yaml:"region"`
BuildScript string `yaml:"build"`
InstallScript string `yaml:"install"`
UninstallScript string `yaml:"uninstall"`
TestScript string `yaml:"test"`
}

type Cluster struct {
Kops *KopsCluster `yaml:"kops"`
Eks *EksCluster `yaml:"eks"`
}

type KopsCluster struct {
Region string `yaml:"region"`
Zones string `yaml:"zones"`
NodeCount int `yaml:"nodeCount"`
NodeSize string `yaml:"nodeSize"`
KubernetesVersion string `yaml:"kubernetesVersion"`
FeatureGates string `yaml:"featureGates"`
IamPolicies string `yaml:"iamPolicies"`
//KubeApiServer KubeApiServer `yaml:"kubeApiServer"`
//Kubelet Kubelet `yaml:"kubelet"`
}

type KubeApiServer struct {
FeatureGates FeatureGates `yaml:"featureGates"`
}

type FeatureGates struct {
CSIDriverRegistry bool `yaml:"CSIDriverRegistry"`
CSINodeInfo bool `yaml:"CSINodeInfo"`
CSIBlockVolume bool `yaml:"CSIBlockVolume"`
VolumeSnapshotDataSource bool `yaml:"VolumeSnapshotDataSource"`
}

type Kubelet struct {
FeatureGates FeatureGates
}

type EksCluster struct {
}
Loading

0 comments on commit 1100614

Please sign in to comment.