Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add e2e tests for Windows #4139

Merged
merged 2 commits into from
Aug 11, 2021
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
8 changes: 8 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ use `TEST_RUNTIME_ARCH` environment variable to specify the target hardware arch
You can also use
[all of flags defined in `knative/pkg/test`](https://github.com/knative/pkg/tree/master/test#flags).
To include tests for Windows, you need to specify the `windows_e2e` build tag. For example:
```shell
go test -v -count=1 -tags=e2e,windows_e2e -timeout=20m ./test
```
Please note that in order to run Windows tests there must be at least one Windows node available in the target Kubernetes cluster.
### Flags
- By default the e2e tests against the current cluster in `~/.kube/config` using
Expand Down
184 changes: 184 additions & 0 deletions test/windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// +build e2e,windows_e2e

/*
Copyright 2021 The Tekton 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 test

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knativetest "knative.dev/pkg/test"
)

var (
ignoreTerminatedFields = cmpopts.IgnoreFields(corev1.ContainerStateTerminated{}, "StartedAt", "FinishedAt", "ContainerID")
ignoreStepFields = cmpopts.IgnoreFields(v1beta1.StepState{}, "ImageID")
)

func TestWindows(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

c, namespace := setup(ctx, t)
t.Parallel()

knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)

taskRunName := "windows-taskrun"
t.Logf("Creating TaskRun in namespace %s", namespace)

taskRun := &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{Name: taskRunName, Namespace: namespace},
Spec: v1beta1.TaskRunSpec{
TaskSpec: &v1beta1.TaskSpec{
Steps: []v1beta1.Step{{
Container: corev1.Container{
Image: "mcr.microsoft.com/windows/nanoserver:1809",
Command: []string{"cmd.exe"},
Args: []string{"/c", "echo hello"},
},
}},
},
PodTemplate: &v1beta1.PodTemplate{
NodeSelector: map[string]string{"kubernetes.io/os": "windows"},
},
},
}
if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create TaskRun: %s", err)
}

t.Logf("Waiting for TaskRun in namespace %s to finish", namespace)
if err := WaitForTaskRunState(ctx, c, taskRunName, TaskRunSucceed(taskRunName), "TaskRunSucceeded"); err != nil {
t.Errorf("Error waiting for TaskRun to finish: %s", err)
}

taskrun, err := c.TaskRunClient.Get(ctx, taskRunName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Couldn't get expected TaskRun %s: %s", taskRunName, err)
}

expectedStepState := []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 0,
Reason: "Completed",
},
},
Name: "unnamed-0",
ContainerName: "step-unnamed-0",
}}
if d := cmp.Diff(expectedStepState, taskrun.Status.Steps, ignoreTerminatedFields, ignoreStepFields); d != "" {
t.Fatalf("-want, +got: %v", d)
}
}

func TestWindowsFailure(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

c, namespace := setup(ctx, t)
t.Parallel()

knativetest.CleanupOnInterrupt(func() { tearDown(ctx, t, c, namespace) }, t.Logf)
defer tearDown(ctx, t, c, namespace)

taskRunName := "failing-windows-taskrun"
t.Logf("Creating TaskRun in namespace %s", namespace)

taskRun := &v1beta1.TaskRun{
ObjectMeta: metav1.ObjectMeta{Name: taskRunName, Namespace: namespace},
Spec: v1beta1.TaskRunSpec{
TaskSpec: &v1beta1.TaskSpec{
Steps: []v1beta1.Step{{
Container: corev1.Container{
Image: "mcr.microsoft.com/windows/nanoserver:1809",
Command: []string{"cmd.exe"},
Args: []string{"/c", "echo hello"},
}}, {
Container: corev1.Container{
Image: "mcr.microsoft.com/windows/nanoserver:1809",
Command: []string{"cmd.exe"},
Args: []string{"/c", "exit 1"},
}}, {
Container: corev1.Container{
Image: "mcr.microsoft.com/windows/nanoserver:1809",
Command: []string{"cmd.exe"},
Args: []string{"/c", "ping localhost -n 5"},
}},
},
},
PodTemplate: &v1beta1.PodTemplate{
NodeSelector: map[string]string{"kubernetes.io/os": "windows"},
},
},
}
if _, err := c.TaskRunClient.Create(ctx, taskRun, metav1.CreateOptions{}); err != nil {
t.Fatalf("Failed to create TaskRun: %s", err)
}

t.Logf("Waiting for TaskRun in namespace %s to fail", namespace)
if err := WaitForTaskRunState(ctx, c, taskRunName, TaskRunFailed(taskRunName), "TaskRunFailed"); err != nil {
t.Errorf("Error waiting for TaskRun to finish: %s", err)
}

taskrun, err := c.TaskRunClient.Get(ctx, taskRunName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Couldn't get expected TaskRun %s: %s", taskRunName, err)
}

expectedStepState := []v1beta1.StepState{{
ContainerState: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 0,
Reason: "Completed",
},
},
Name: "unnamed-0",
ContainerName: "step-unnamed-0",
}, {
ContainerState: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 1,
Reason: "Error",
},
},
Name: "unnamed-1",
ContainerName: "step-unnamed-1",
}, {
ContainerState: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 1,
Reason: "Error",
},
},
Name: "unnamed-2",
ContainerName: "step-unnamed-2",
}}
if d := cmp.Diff(expectedStepState, taskrun.Status.Steps, ignoreTerminatedFields, ignoreStepFields); d != "" {
t.Fatalf("-want, +got: %v", d)
}
}