Skip to content

Commit 702646d

Browse files
author
draveness
committed
test(runtimeclass): add e2e tests for runtimeclass scheduling
1 parent 11678fb commit 702646d

File tree

4 files changed

+167
-15
lines changed

4 files changed

+167
-15
lines changed

test/e2e/common/runtimeclass.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121
"time"
2222

23-
"k8s.io/api/core/v1"
23+
v1 "k8s.io/api/core/v1"
2424
"k8s.io/apimachinery/pkg/api/errors"
2525
apierrs "k8s.io/apimachinery/pkg/api/errors"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -37,15 +37,6 @@ import (
3737
"github.com/onsi/gomega"
3838
)
3939

40-
const (
41-
// PreconfiguredRuntimeHandler is the name of the runtime handler that is expected to be
42-
// preconfigured in the test environment.
43-
PreconfiguredRuntimeHandler = "test-handler"
44-
// DockerRuntimeHandler is a hardcoded runtime handler that is accepted by dockershim, and
45-
// treated equivalently to a nil runtime handler.
46-
DockerRuntimeHandler = "docker"
47-
)
48-
4940
var _ = ginkgo.Describe("[sig-node] RuntimeClass", func() {
5041
f := framework.NewDefaultFramework("runtimeclass")
5142

@@ -64,10 +55,7 @@ var _ = ginkgo.Describe("[sig-node] RuntimeClass", func() {
6455
// This test requires that the PreconfiguredRuntimeHandler has already been set up on nodes.
6556
ginkgo.It("should run a Pod requesting a RuntimeClass with a configured handler [NodeFeature:RuntimeHandler]", func() {
6657
// The built-in docker runtime does not support configuring runtime handlers.
67-
handler := PreconfiguredRuntimeHandler
68-
if framework.TestContext.ContainerRuntime == "docker" {
69-
handler = DockerRuntimeHandler
70-
}
58+
handler := framework.PreconfiguredRuntimeClassHandler()
7159

7260
rcName := createRuntimeClass(f, "preconfigured-handler", handler)
7361
pod := f.PodClient().Create(newRuntimeClassPod(rcName))

test/e2e/framework/framework.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"sync"
3232
"time"
3333

34-
"k8s.io/api/core/v1"
34+
v1 "k8s.io/api/core/v1"
3535
apierrors "k8s.io/apimachinery/pkg/api/errors"
3636
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3737
"k8s.io/apimachinery/pkg/labels"
@@ -884,3 +884,18 @@ func GetLogToFileFunc(file *os.File) func(format string, args ...interface{}) {
884884
writer.Flush()
885885
}
886886
}
887+
888+
const (
889+
// preconfiguredRuntimeHandler is the name of the runtime handler that is expected to be
890+
// preconfigured in the test environment.
891+
preconfiguredRuntimeHandler = "test-handler"
892+
)
893+
894+
// PreconfiguredRuntimeClassHandler returns configured runtime handler.
895+
func PreconfiguredRuntimeClassHandler() string {
896+
if TestContext.ContainerRuntime == "docker" {
897+
return TestContext.ContainerRuntime
898+
}
899+
900+
return preconfiguredRuntimeHandler
901+
}

test/e2e/node/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ go_library(
1414
"pod_gc.go",
1515
"pods.go",
1616
"pre_stop.go",
17+
"runtimeclass.go",
1718
"security_context.go",
1819
"ssh.go",
1920
"ttlafterfinished.go",
@@ -22,10 +23,13 @@ go_library(
2223
visibility = ["//visibility:public"],
2324
deps = [
2425
"//pkg/kubelet/apis/stats/v1alpha1:go_default_library",
26+
"//pkg/kubelet/runtimeclass/testing:go_default_library",
2527
"//pkg/master/ports:go_default_library",
2628
"//pkg/util/slice:go_default_library",
2729
"//staging/src/k8s.io/api/batch/v1:go_default_library",
2830
"//staging/src/k8s.io/api/core/v1:go_default_library",
31+
"//staging/src/k8s.io/api/node/v1beta1:go_default_library",
32+
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
2933
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
3034
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
3135
"//staging/src/k8s.io/apimachinery/pkg/fields:go_default_library",
@@ -46,10 +50,12 @@ go_library(
4650
"//test/e2e/framework/ssh:go_default_library",
4751
"//test/e2e/framework/volume:go_default_library",
4852
"//test/e2e/perftype:go_default_library",
53+
"//test/e2e/scheduling:go_default_library",
4954
"//test/utils:go_default_library",
5055
"//test/utils/image:go_default_library",
5156
"//vendor/github.com/onsi/ginkgo:go_default_library",
5257
"//vendor/github.com/onsi/gomega:go_default_library",
58+
"//vendor/k8s.io/utils/pointer:go_default_library",
5359
],
5460
)
5561

test/e2e/node/runtimeclass.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package node
18+
19+
import (
20+
"fmt"
21+
22+
v1 "k8s.io/api/core/v1"
23+
"k8s.io/api/node/v1beta1"
24+
apierrs "k8s.io/apimachinery/pkg/api/errors"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
runtimeclasstest "k8s.io/kubernetes/pkg/kubelet/runtimeclass/testing"
27+
"k8s.io/kubernetes/test/e2e/framework"
28+
e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
29+
"k8s.io/kubernetes/test/e2e/scheduling"
30+
imageutils "k8s.io/kubernetes/test/utils/image"
31+
utilpointer "k8s.io/utils/pointer"
32+
33+
"github.com/onsi/ginkgo"
34+
"github.com/onsi/gomega"
35+
)
36+
37+
var _ = ginkgo.Describe("[sig-node] RuntimeClass", func() {
38+
f := framework.NewDefaultFramework("runtimeclass")
39+
40+
ginkgo.It("should reject a Pod requesting a RuntimeClass with conflicting node selector", func() {
41+
scheduling := &v1beta1.Scheduling{
42+
NodeSelector: map[string]string{
43+
"foo": "conflict",
44+
},
45+
}
46+
47+
runtimeClass := newRuntimeClass(f.Namespace.Name, "conflict-runtimeclass")
48+
runtimeClass.Scheduling = scheduling
49+
rc, err := f.ClientSet.NodeV1beta1().RuntimeClasses().Create(runtimeClass)
50+
framework.ExpectNoError(err, "failed to create RuntimeClass resource")
51+
52+
pod := newRuntimeClassPod(rc.GetName())
53+
pod.Spec.NodeSelector = map[string]string{
54+
"foo": "bar",
55+
}
56+
_, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(pod)
57+
framework.ExpectError(err, "should be forbidden")
58+
gomega.Expect(apierrs.IsForbidden(err)).To(gomega.BeTrue(), "should be forbidden error")
59+
})
60+
61+
ginkgo.It("should run a Pod requesting a RuntimeClass with scheduling [NodeFeature:RuntimeHandler] ", func() {
62+
nodeName := scheduling.GetNodeThatCanRunPod(f)
63+
nodeSelector := map[string]string{
64+
"foo": "bar",
65+
"fizz": "buzz",
66+
}
67+
tolerations := []v1.Toleration{
68+
{
69+
Key: "foo",
70+
Operator: v1.TolerationOpEqual,
71+
Value: "bar",
72+
Effect: v1.TaintEffectNoSchedule,
73+
},
74+
}
75+
scheduling := &v1beta1.Scheduling{
76+
NodeSelector: nodeSelector,
77+
Tolerations: tolerations,
78+
}
79+
80+
ginkgo.By("Trying to apply a label on the found node.")
81+
for key, value := range nodeSelector {
82+
framework.AddOrUpdateLabelOnNode(f.ClientSet, nodeName, key, value)
83+
framework.ExpectNodeHasLabel(f.ClientSet, nodeName, key, value)
84+
defer framework.RemoveLabelOffNode(f.ClientSet, nodeName, key)
85+
}
86+
87+
ginkgo.By("Trying to apply taint on the found node.")
88+
taint := v1.Taint{
89+
Key: "foo",
90+
Value: "bar",
91+
Effect: v1.TaintEffectNoSchedule,
92+
}
93+
framework.AddOrUpdateTaintOnNode(f.ClientSet, nodeName, taint)
94+
framework.ExpectNodeHasTaint(f.ClientSet, nodeName, &taint)
95+
defer framework.RemoveTaintOffNode(f.ClientSet, nodeName, taint)
96+
97+
ginkgo.By("Trying to create runtimeclass and pod")
98+
runtimeClass := newRuntimeClass(f.Namespace.Name, "non-conflict-runtimeclass")
99+
runtimeClass.Scheduling = scheduling
100+
rc, err := f.ClientSet.NodeV1beta1().RuntimeClasses().Create(runtimeClass)
101+
framework.ExpectNoError(err, "failed to create RuntimeClass resource")
102+
103+
pod := newRuntimeClassPod(rc.GetName())
104+
pod.Spec.NodeSelector = map[string]string{
105+
"foo": "bar",
106+
}
107+
pod = f.PodClient().Create(pod)
108+
109+
framework.ExpectNoError(e2epod.WaitForPodNotPending(f.ClientSet, f.Namespace.Name, pod.Name))
110+
111+
// check that pod got scheduled on specified node.
112+
scheduledPod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(pod.Name, metav1.GetOptions{})
113+
framework.ExpectNoError(err)
114+
framework.ExpectEqual(nodeName, scheduledPod.Spec.NodeName)
115+
framework.ExpectEqual(nodeSelector, pod.Spec.NodeSelector)
116+
gomega.Expect(pod.Spec.Tolerations).To(gomega.ContainElement(tolerations[0]))
117+
})
118+
})
119+
120+
// newRuntimeClass returns a test runtime class.
121+
func newRuntimeClass(namespace, name string) *v1beta1.RuntimeClass {
122+
uniqueName := fmt.Sprintf("%s-%s", namespace, name)
123+
return runtimeclasstest.NewRuntimeClass(uniqueName, framework.PreconfiguredRuntimeClassHandler())
124+
}
125+
126+
// newRuntimeClassPod returns a test pod with the given runtimeClassName.
127+
func newRuntimeClassPod(runtimeClassName string) *v1.Pod {
128+
return &v1.Pod{
129+
ObjectMeta: metav1.ObjectMeta{
130+
GenerateName: fmt.Sprintf("test-runtimeclass-%s-", runtimeClassName),
131+
},
132+
Spec: v1.PodSpec{
133+
RuntimeClassName: &runtimeClassName,
134+
Containers: []v1.Container{{
135+
Name: "test",
136+
Image: imageutils.GetE2EImage(imageutils.BusyBox),
137+
Command: []string{"true"},
138+
}},
139+
RestartPolicy: v1.RestartPolicyNever,
140+
AutomountServiceAccountToken: utilpointer.BoolPtr(false),
141+
},
142+
}
143+
}

0 commit comments

Comments
 (0)