From d5febacc912692af13ba749d501722da34614d26 Mon Sep 17 00:00:00 2001 From: Ti Zhou Date: Thu, 12 Oct 2017 17:17:15 +0800 Subject: [PATCH 1/3] Added more unit tests for kube-scheduler. --- .../app/options/options_test.go | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 plugin/cmd/kube-scheduler/app/options/options_test.go diff --git a/plugin/cmd/kube-scheduler/app/options/options_test.go b/plugin/cmd/kube-scheduler/app/options/options_test.go new file mode 100644 index 00000000000..bdaac971a7d --- /dev/null +++ b/plugin/cmd/kube-scheduler/app/options/options_test.go @@ -0,0 +1,103 @@ +/* +Copyright 2017 The Kubernetes 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 options + +import ( + "reflect" + "testing" + "time" + + "github.com/spf13/pflag" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/diff" + "k8s.io/kubernetes/pkg/apis/componentconfig" +) + +func TestAddFlags(t *testing.T) { + f := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError) + s := NewSchedulerServer() + s.AddFlags(f) + + args := []string{ + "--address=192.168.4.20", + "--algorithm-provider=FooProvider", + "--contention-profiling=true", + "--failure-domains=kubernetes.io/hostname", + "--hard-pod-affinity-symmetric-weight=0", + "--kube-api-burst=80", + "--kube-api-content-type=application/vnd.kubernetes.protobuf", + "--kube-api-qps=40.0", + "--kubeconfig=/foo/bar", + "--leader-elect=true", + "--leader-elect-lease-duration=20s", + "--leader-elect-renew-deadline=15s", + "--leader-elect-resource-lock=endpoints", + "--leader-elect-retry-period=3s", + "--lock-object-name=foo", + "--lock-object-namespace=bar", + "--master=192.168.4.20", + "--policy-config-file=/foo/bar", + "--policy-configmap=foo", + "--policy-configmap-namespace=bar", + "--port=10000", + "--profiling=false", + "--scheduler-name=foo", + "--use-legacy-policy-config=true", + } + + f.Parse(args) + + expected := &SchedulerServer{ + KubeSchedulerConfiguration: componentconfig.KubeSchedulerConfiguration{ + Port: 10000, + Address: "192.168.4.20", + AlgorithmProvider: "FooProvider", + PolicyConfigFile: "/foo/bar", + EnableContentionProfiling: true, + EnableProfiling: false, + + ContentType: "application/vnd.kubernetes.protobuf", + KubeAPIQPS: 40.0, + KubeAPIBurst: 80, + SchedulerName: "foo", + LeaderElection: componentconfig.LeaderElectionConfiguration{ + ResourceLock: "endpoints", + LeaderElect: true, + LeaseDuration: metav1.Duration{Duration: 20 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 3 * time.Second}, + }, + + LockObjectNamespace: "bar", + LockObjectName: "foo", + + PolicyConfigMapName: "foo", + PolicyConfigMapNamespace: "bar", + UseLegacyPolicyConfig: true, + + HardPodAffinitySymmetricWeight: 0, + FailureDomains: "kubernetes.io/hostname", + }, + Kubeconfig: "/foo/bar", + Master: "192.168.4.20", + } + + if !reflect.DeepEqual(expected, s) { + t.Errorf("Got different run options than expected.\nDifference detected on:\n%s", diff.ObjectReflectDiff(expected, s)) + } +} From 2a910ff12c80a5fb8e44d4fbaf94dd5a30dfc043 Mon Sep 17 00:00:00 2001 From: Ti Zhou Date: Fri, 13 Oct 2017 10:40:47 +0800 Subject: [PATCH 2/3] Tweak kube-schuduler unit test cases. --- .../app/options/options_test.go | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/plugin/cmd/kube-scheduler/app/options/options_test.go b/plugin/cmd/kube-scheduler/app/options/options_test.go index bdaac971a7d..bd6172ccb37 100644 --- a/plugin/cmd/kube-scheduler/app/options/options_test.go +++ b/plugin/cmd/kube-scheduler/app/options/options_test.go @@ -21,11 +21,11 @@ import ( "testing" "time" - "github.com/spf13/pflag" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/diff" "k8s.io/kubernetes/pkg/apis/componentconfig" + + "github.com/spf13/pflag" ) func TestAddFlags(t *testing.T) { @@ -42,21 +42,21 @@ func TestAddFlags(t *testing.T) { "--kube-api-burst=80", "--kube-api-content-type=application/vnd.kubernetes.protobuf", "--kube-api-qps=40.0", - "--kubeconfig=/foo/bar", + "--kubeconfig=/foo/bar/kubeconfig", "--leader-elect=true", "--leader-elect-lease-duration=20s", "--leader-elect-renew-deadline=15s", "--leader-elect-resource-lock=endpoints", "--leader-elect-retry-period=3s", - "--lock-object-name=foo", - "--lock-object-namespace=bar", + "--lock-object-name=test-lock-object-name", + "--lock-object-namespace=test-lock-object-ns", "--master=192.168.4.20", - "--policy-config-file=/foo/bar", - "--policy-configmap=foo", - "--policy-configmap-namespace=bar", + "--policy-config-file=/foo/bar/policyconfig", + "--policy-configmap=test-policy-configmap", + "--policy-configmap-namespace=test-policy-configmap-ns", "--port=10000", "--profiling=false", - "--scheduler-name=foo", + "--scheduler-name=test-scheduler-name", "--use-legacy-policy-config=true", } @@ -67,14 +67,14 @@ func TestAddFlags(t *testing.T) { Port: 10000, Address: "192.168.4.20", AlgorithmProvider: "FooProvider", - PolicyConfigFile: "/foo/bar", + PolicyConfigFile: "/foo/bar/policyconfig", EnableContentionProfiling: true, EnableProfiling: false, ContentType: "application/vnd.kubernetes.protobuf", KubeAPIQPS: 40.0, KubeAPIBurst: 80, - SchedulerName: "foo", + SchedulerName: "test-scheduler-name", LeaderElection: componentconfig.LeaderElectionConfiguration{ ResourceLock: "endpoints", LeaderElect: true, @@ -83,17 +83,17 @@ func TestAddFlags(t *testing.T) { RetryPeriod: metav1.Duration{Duration: 3 * time.Second}, }, - LockObjectNamespace: "bar", - LockObjectName: "foo", + LockObjectNamespace: "test-lock-object-ns", + LockObjectName: "test-lock-object-name", - PolicyConfigMapName: "foo", - PolicyConfigMapNamespace: "bar", + PolicyConfigMapName: "test-policy-configmap", + PolicyConfigMapNamespace: "test-policy-configmap-ns", UseLegacyPolicyConfig: true, HardPodAffinitySymmetricWeight: 0, FailureDomains: "kubernetes.io/hostname", }, - Kubeconfig: "/foo/bar", + Kubeconfig: "/foo/bar/kubeconfig", Master: "192.168.4.20", } From cb7d08f2c481965424ddb81cb14da34583a4d096 Mon Sep 17 00:00:00 2001 From: Ti Zhou Date: Fri, 13 Oct 2017 17:15:27 +0800 Subject: [PATCH 3/3] Update BUILD file by executing update-bazel.sh --- plugin/cmd/kube-scheduler/app/options/BUILD | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugin/cmd/kube-scheduler/app/options/BUILD b/plugin/cmd/kube-scheduler/app/options/BUILD index 5d036472025..2724e9da73c 100644 --- a/plugin/cmd/kube-scheduler/app/options/BUILD +++ b/plugin/cmd/kube-scheduler/app/options/BUILD @@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"]) load( "@io_bazel_rules_go//go:def.bzl", "go_library", + "go_test", ) go_library( @@ -34,3 +35,15 @@ filegroup( srcs = [":package-srcs"], tags = ["automanaged"], ) + +go_test( + name = "go_default_test", + srcs = ["options_test.go"], + library = ":go_default_library", + deps = [ + "//pkg/apis/componentconfig:go_default_library", + "//vendor/github.com/spf13/pflag:go_default_library", + "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + "//vendor/k8s.io/apimachinery/pkg/util/diff:go_default_library", + ], +)