This repository has been archived by the owner on Sep 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple e2e tests in Go for no-op success and failure builds (#329)
* Add simple e2e tests for no-op success and failure builds * Add license headers * Add go tests to e2e-tests.sh
- Loading branch information
1 parent
4dfa5da
commit c043adb
Showing
71 changed files
with
5,025 additions
and
2,248 deletions.
There are no files selected for viewing
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
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,25 @@ | ||
# End-to-End testing | ||
|
||
## Running the tests | ||
|
||
To run all e2e tests: | ||
``` | ||
GOCACHE=off go test -tags e2e ./test/e2e/... | ||
``` | ||
|
||
`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. |
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,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") | ||
} |
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,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{ | ||
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) | ||
} | ||
|
||
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) | ||
} | ||
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) | ||
} | ||
} |
Oops, something went wrong.