Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Add simple e2e tests in Go for no-op success and failure builds #329

Merged
merged 3 commits into from
Sep 5, 2018
Merged
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
31 changes: 22 additions & 9 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion test/e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ kubectl delete buildtemplates --all
# Run the tests

header "Running tests"

ko apply -R -f test/ || fail_test

header "Running Go e2e tests"
GOCACHE=off go test -tags e2e ./test/e2e/... || fail_test
Copy link
Contributor

@adrcunha adrcunha Sep 5, 2018

Choose a reason for hiding this comment

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

Use report_go_test here instead of go test so you'll get individual results on testgrid and in the gubernator report.

Also, don't fail_test here. Fail after checking the results, so you'll get all results of the test. Better run the go tests first, since you're not checking the results here (even better: move everything to a function). Something like:

local failed=0
report_go_test ... || failed=1
run_old_ugly_tests_in_shell_script || failed=1
(( failed )) && abort_test
success


# Wait for tests to finish.
tests_finished=0
for i in {1..60}; do
Expand Down
25 changes: 25 additions & 0 deletions test/e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# End-to-End testing

## Running the tests

To run all e2e tests:
```
GOCACHE=off go test -tags e2e ./test/e2e/...
Copy link
Contributor

Choose a reason for hiding this comment

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

This info seems to conflict with using count=1 as per https://golang.org/doc/go1.10#test

```

`GOCACHE=off` disables Go's test cache, so that tests results will not be
cached and the test will always run.

To run a single e2e test:

```
GOCACHE=off go test -tags e2e ./test/e2e/... -test.run=<regex>
```

## What the tests do

By default, tests use your current Kubernetes config to talk to your currently
configured cluster.

When they run tests ensure that a namespace named `build-tests` exists, then
starts running builds in it.
120 changes: 120 additions & 0 deletions test/e2e/e2e.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// +build e2e

/*
Copyright 2018 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"errors"
"fmt"
"testing"

"github.com/knative/pkg/test"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"

"github.com/knative/build/pkg/apis/build/v1alpha1"
buildversioned "github.com/knative/build/pkg/client/clientset/versioned"
buildtyped "github.com/knative/build/pkg/client/clientset/versioned/typed/build/v1alpha1"
)

type clients struct {
kubeClient *test.KubeClient
buildClient *buildClient
}

const buildTestNamespace = "build-tests"

func setup(t *testing.T) *clients {
clients, err := newClients(test.Flags.Kubeconfig, test.Flags.Cluster, buildTestNamespace)
if err != nil {
t.Fatalf("newClients: %v", err)
}

return clients
}

func newClients(configPath string, clusterName string, namespace string) (*clients, error) {
overrides := clientcmd.ConfigOverrides{}
// Override the cluster name if provided.
if clusterName != "" {
overrides.Context.Cluster = clusterName
}
cfg, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(&clientcmd.ClientConfigLoadingRules{
ExplicitPath: configPath,
}, &overrides).ClientConfig()
if err != nil {
return nil, err
}

kubeClient, err := test.NewKubeClient(configPath, clusterName)
if err != nil {
return nil, err
}

bcs, err := buildversioned.NewForConfig(cfg)
if err != nil {
return nil, err
}
buildClient := &buildClient{builds: bcs.BuildV1alpha1().Builds(namespace)}

return &clients{
kubeClient: kubeClient,
buildClient: buildClient,
}, nil
}

type buildClient struct {
builds buildtyped.BuildInterface
}

func (c *buildClient) watchBuild(name string) (*v1alpha1.Build, error) {
w, err := c.builds.Watch(metav1.SingleObject(metav1.ObjectMeta{Name: name}))
if err != nil {
return nil, err
}
for evt := range w.ResultChan() {
switch evt.Type {
case watch.Deleted:
return nil, errors.New("build deleted")
case watch.Error:
return nil, fmt.Errorf("error event: %v", evt.Object)
}

b, ok := evt.Object.(*v1alpha1.Build)
if !ok {
return nil, fmt.Errorf("object was not a Build: %v", err)
}

for _, cond := range b.Status.Conditions {
if cond.Type == v1alpha1.BuildSucceeded {
switch cond.Status {
case corev1.ConditionTrue:
return b, nil
case corev1.ConditionFalse:
return b, errors.New("build failed")
case corev1.ConditionUnknown:
continue
}
}
}
}
return nil, errors.New("watch ended before build completion")
}
122 changes: 122 additions & 0 deletions test/e2e/simple_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// +build e2e

/*
Copyright 2018 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
"flag"
"log"
"os"
"testing"

corev1 "k8s.io/api/core/v1"
kuberrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/knative/build/pkg/apis/build/v1alpha1"
"github.com/knative/pkg/test"
)

// TestMain is called by the test binary generated by "go test", and is
// responsible for setting up and tearing down the testing environment, namely
// the test namespace.
func TestMain(m *testing.M) {
flag.Parse()
clients, err := newClients(test.Flags.Kubeconfig, test.Flags.Cluster, buildTestNamespace)
if err != nil {
log.Fatalf("newClients: %v", err)
}

// Ensure the test namespace exists, by trying to create it and ignoring
// already-exists errors.
if _, err := clients.kubeClient.Kube.CoreV1().Namespaces().Create(&corev1.Namespace{
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a nice thing, we should adopt it for serving as well, instead of having to create a test space as a requirement.

ObjectMeta: metav1.ObjectMeta{
Name: buildTestNamespace,
},
}); err == nil {
log.Printf("Created namespace %q", buildTestNamespace)
} else if kuberrors.IsAlreadyExists(err) {
log.Printf("Namespace %q already exists", buildTestNamespace)
} else {
log.Fatalf("Creating namespace %q: %v", buildTestNamespace, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a suggestion

It helps debug if log msg explicitly calls out the word error
eg: log.Fatalf("Error creating namespace %q: %v", buildTestNamespace, err)

}

defer func() {
}()

code := m.Run()

// Delete the test namespace to be recreated next time.
if err := clients.kubeClient.Kube.CoreV1().Namespaces().Delete(buildTestNamespace, &metav1.DeleteOptions{}); err != nil && !kuberrors.IsNotFound(err) {
log.Fatalf("Deleting namespace %q: %v", buildTestNamespace, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar suggestion
log.Fatalf("Error deleting namespace %q: %v", buildTestNamespace, err)

}
log.Printf("Deleted namespace %q", buildTestNamespace)

os.Exit(code)
}

// TestSimpleBuild tests that a simple build that does nothing interesting
// succeeds.
func TestSimpleBuild(t *testing.T) {
clients := setup(t)

buildName := "simple-build"
if _, err := clients.buildClient.builds.Create(&v1alpha1.Build{
ObjectMeta: metav1.ObjectMeta{
Namespace: buildTestNamespace,
Name: buildName,
},
Spec: v1alpha1.BuildSpec{
Steps: []corev1.Container{{
Image: "busybox",
Args: []string{"echo", "simple"},
}},
},
}); err != nil {
t.Fatalf("Create: %v", err)
}

if _, err := clients.buildClient.watchBuild(buildName); err != nil {
t.Fatalf("watchBuild: %v", err)
}
}

// TestFailingBuild tests that a simple build that fails, fails as expected.
func TestFailingBuild(t *testing.T) {
clients := setup(t)

buildName := "failing-build"
if _, err := clients.buildClient.builds.Create(&v1alpha1.Build{
ObjectMeta: metav1.ObjectMeta{
Namespace: buildTestNamespace,
Name: buildName,
},
Spec: v1alpha1.BuildSpec{
Steps: []corev1.Container{{
Image: "busybox",
Args: []string{"false"}, // fails.
}},
},
}); err != nil {
t.Fatalf("Create: %v", err)
}

if _, err := clients.buildClient.watchBuild(buildName); err == nil {
t.Fatalf("watchBuild did not return expected error: %v", err)
}
}
Loading