-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of framework test harness (KEP-0008)
Rename Test to Case, rename Stage to Step. Fix race if a resource is updated externally while the test harness is updating the resource. Migrate InstallManifests into utils.
- Loading branch information
1 parent
61aa3db
commit 1e69cf6
Showing
32 changed files
with
2,315 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// +build build_harness | ||
|
||
package tools | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kudobuilder/kudo/pkg/test" | ||
) | ||
|
||
func init() { | ||
test.RegisterFlags() | ||
} | ||
|
||
func TestKudoFrameworks(t *testing.T) { | ||
test.HarnessFromFlags(t).Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
apiVersion: kudo.k8s.io/v1alpha1 | ||
kind: Framework | ||
metadata: | ||
name: test-framework | ||
namespace: default | ||
--- | ||
apiVersion: kudo.k8s.io/v1alpha1 | ||
kind: FrameworkVersion | ||
metadata: | ||
name: test-framework-1.0 | ||
namespace: default | ||
spec: | ||
framework: | ||
name: test-framework | ||
kind: Framework | ||
version: "1.0" | ||
parameters: | ||
- name: REPLICAS | ||
description: "Number of nginx replicas" | ||
default: "3" | ||
displayName: "Replica count" | ||
templates: | ||
deploy.yaml: | | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: nginx | ||
spec: | ||
replicas: {{REPLICAS}} | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.7.9 | ||
ports: | ||
- containerPort: 80 | ||
tasks: | ||
deploy: | ||
resources: | ||
- deploy.yaml | ||
plans: | ||
deploy: | ||
strategy: serial | ||
phases: | ||
- name: deploy | ||
strategy: parallel | ||
steps: | ||
- name: deploy | ||
tasks: | ||
- deploy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
// TestStep settings to apply to a test step. | ||
type TestStep struct { | ||
// The type meta object, should always be a GVK of kudo.k8s.io/v1alpha1/TestStep. | ||
metav1.TypeMeta `json:",inline"` | ||
// Override the default metadata. Set labels or override the test step name. | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Index int `json:"index,omitempty"` | ||
// Objects to delete at the beginning of the test step. | ||
Delete []corev1.ObjectReference `json:"delete,omitempty"` | ||
|
||
// Indicates that this is a unit test - safe to run without a real Kubernetes cluster. | ||
UnitTest bool `json:"unitTest"` | ||
|
||
// Allowed environment labels | ||
// Disallowed environment labels | ||
} | ||
|
||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
// TestAssert represents the settings needed to verify the result of a test step. | ||
type TestAssert struct { | ||
// The type meta object, should always be a GVK of kudo.k8s.io/v1alpha1/TestAssert. | ||
metav1.TypeMeta `json:",inline"` | ||
// Override the default timeout of 300 seconds (in seconds). | ||
Timeout int `json:"timeout"` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"regexp" | ||
"sort" | ||
"strconv" | ||
"testing" | ||
|
||
petname "github.com/dustinkirkland/golang-petname" | ||
testutils "github.com/kudobuilder/kudo/pkg/test/utils" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/discovery" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var testStepRegex = regexp.MustCompile(`^(\d+)-([^.]+)(.yaml)?$`) | ||
|
||
// Case contains all of the test steps and the Kubernetes client and other global configuration | ||
// for a test. | ||
type Case struct { | ||
Steps []*Step | ||
Name string | ||
Dir string | ||
|
||
Client client.Client | ||
DiscoveryClient discovery.DiscoveryInterface | ||
Logger testutils.Logger | ||
} | ||
|
||
// DeleteNamespace deletes a namespace in Kubernetes after we are done using it. | ||
func (t *Case) DeleteNamespace(namespace string) error { | ||
t.Logger.Log("Deleting namespace:", namespace) | ||
return t.Client.Delete(context.TODO(), &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: namespace, | ||
}, | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Namespace", | ||
}, | ||
}) | ||
} | ||
|
||
// CreateNamespace creates a namespace in Kubernetes to use for a test. | ||
func (t *Case) CreateNamespace(namespace string) error { | ||
t.Logger.Log("Creating namespace:", namespace) | ||
return t.Client.Create(context.TODO(), &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: namespace, | ||
}, | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Namespace", | ||
}, | ||
}) | ||
} | ||
|
||
// TestCaseFactory creates a new Go test that runs a set of test steps. | ||
func (t *Case) TestCaseFactory() func(*testing.T) { | ||
return func(test *testing.T) { | ||
t.Logger = testutils.NewTestLogger(test, t.Name) | ||
|
||
test.Parallel() | ||
|
||
ns := fmt.Sprintf("kudo-test-%s", petname.Generate(2, "-")) | ||
|
||
if err := t.CreateNamespace(ns); err != nil { | ||
test.Fatal(err) | ||
} | ||
|
||
defer t.DeleteNamespace(ns) | ||
|
||
for _, testStep := range t.Steps { | ||
testStep.Client = t.Client | ||
testStep.DiscoveryClient = t.DiscoveryClient | ||
testStep.Logger = t.Logger.WithPrefix(testStep.String()) | ||
|
||
defer testStep.Clean(ns) | ||
|
||
if errs := testStep.Run(ns); len(errs) > 0 { | ||
for _, err := range errs { | ||
test.Error(err) | ||
} | ||
|
||
test.Error(fmt.Errorf("failed in step %s", testStep.String())) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
// CollectTestStepFiles collects a map of test steps and their associated files | ||
// from a directory. | ||
func (t *Case) CollectTestStepFiles() (map[int64][]string, error) { | ||
testStepFiles := map[int64][]string{} | ||
|
||
files, err := ioutil.ReadDir(t.Dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, file := range files { | ||
matches := testStepRegex.FindStringSubmatch(file.Name()) | ||
|
||
index, err := strconv.ParseInt(matches[1], 10, 32) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if testStepFiles[index] == nil { | ||
testStepFiles[index] = []string{} | ||
} | ||
|
||
testStepPath := filepath.Join(t.Dir, file.Name()) | ||
|
||
if file.IsDir() { | ||
testStepDir, err := ioutil.ReadDir(testStepPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, testStepFile := range testStepDir { | ||
testStepFiles[index] = append(testStepFiles[index], filepath.Join( | ||
testStepPath, testStepFile.Name(), | ||
)) | ||
} | ||
} else { | ||
testStepFiles[index] = append(testStepFiles[index], testStepPath) | ||
} | ||
} | ||
|
||
return testStepFiles, nil | ||
} | ||
|
||
// LoadTestSteps loads all of the test steps for a test case. | ||
func (t *Case) LoadTestSteps() error { | ||
testStepFiles, err := t.CollectTestStepFiles() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
testSteps := []*Step{} | ||
|
||
for index, files := range testStepFiles { | ||
testStep := &Step{ | ||
Index: int(index), | ||
Asserts: []runtime.Object{}, | ||
Apply: []runtime.Object{}, | ||
Errors: []runtime.Object{}, | ||
} | ||
|
||
for _, file := range files { | ||
if err := testStep.LoadYAML(file); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
testSteps = append(testSteps, testStep) | ||
} | ||
|
||
sort.Slice(testSteps, func(i, j int) bool { | ||
return testSteps[i].Index < testSteps[j].Index | ||
}) | ||
|
||
t.Steps = testSteps | ||
return nil | ||
} |
Oops, something went wrong.