From 0da04d61ab4b70817083c8208af12397b818546a Mon Sep 17 00:00:00 2001 From: hangaoshuai Date: Wed, 22 Aug 2018 10:18:30 +0800 Subject: [PATCH] add unit test func TestToAuthenticationRequestHeaderConfig --- .../pkg/server/options/authentication_test.go | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 staging/src/k8s.io/apiserver/pkg/server/options/authentication_test.go diff --git a/staging/src/k8s.io/apiserver/pkg/server/options/authentication_test.go b/staging/src/k8s.io/apiserver/pkg/server/options/authentication_test.go new file mode 100644 index 00000000000..3b2a581f2cc --- /dev/null +++ b/staging/src/k8s.io/apiserver/pkg/server/options/authentication_test.go @@ -0,0 +1,68 @@ +/* +Copyright 2018 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" + + "k8s.io/apiserver/pkg/authentication/authenticatorfactory" +) + +func TestToAuthenticationRequestHeaderConfig(t *testing.T) { + testCases := []struct { + name string + testOptions *RequestHeaderAuthenticationOptions + expectConfig *authenticatorfactory.RequestHeaderConfig + }{ + { + name: "test when ClientCAFile is nil", + testOptions: &RequestHeaderAuthenticationOptions{ + UsernameHeaders: []string{"x-remote-user"}, + GroupHeaders: []string{"x-remote-group"}, + ExtraHeaderPrefixes: []string{"x-remote-extra-"}, + AllowedNames: []string{"kube-aggregator"}, + }, + }, + { + name: "test when ClientCAFile is not nil", + testOptions: &RequestHeaderAuthenticationOptions{ + ClientCAFile: "/testClientCAFile", + UsernameHeaders: []string{"x-remote-user"}, + GroupHeaders: []string{"x-remote-group"}, + ExtraHeaderPrefixes: []string{"x-remote-extra-"}, + AllowedNames: []string{"kube-aggregator"}, + }, + expectConfig: &authenticatorfactory.RequestHeaderConfig{ + UsernameHeaders: []string{"x-remote-user"}, + GroupHeaders: []string{"x-remote-group"}, + ExtraHeaderPrefixes: []string{"x-remote-extra-"}, + ClientCA: "/testClientCAFile", + AllowedClientNames: []string{"kube-aggregator"}, + }, + }, + } + + for _, testcase := range testCases { + t.Run(testcase.name, func(t *testing.T) { + resultConfig := testcase.testOptions.ToAuthenticationRequestHeaderConfig() + if !reflect.DeepEqual(resultConfig, testcase.expectConfig) { + t.Errorf("got RequestHeaderConfig: %#v, expected RequestHeaderConfig: %#v", resultConfig, testcase.expectConfig) + } + }) + } +}