k8s.io/apiserver@v0.31.1/pkg/admission/plugin/webhook/config/kubeconfig_test.go (about)

     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 config
    18  
    19  import (
    20  	"bytes"
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  func TestLoadConfig(t *testing.T) {
    26  	testcases := []struct {
    27  		name             string
    28  		input            string
    29  		expectErr        string
    30  		expectKubeconfig string
    31  	}{
    32  		{
    33  			name:      "empty",
    34  			input:     "",
    35  			expectErr: `'Kind' is missing in ''`,
    36  		},
    37  		{
    38  			name:      "unknown kind",
    39  			input:     `{"kind":"Unknown","apiVersion":"v1"}`,
    40  			expectErr: `no kind "Unknown" is registered for version "v1"`,
    41  		},
    42  		{
    43  			name: "valid v1alpha1",
    44  			input: `
    45  kind: WebhookAdmission
    46  apiVersion: apiserver.config.k8s.io/v1alpha1
    47  kubeConfigFile: /foo
    48  `,
    49  			expectKubeconfig: "/foo",
    50  		},
    51  		{
    52  			name: "valid v1",
    53  			input: `
    54  kind: WebhookAdmissionConfiguration
    55  apiVersion: apiserver.config.k8s.io/v1
    56  kubeConfigFile: /foo
    57  `,
    58  			expectKubeconfig: "/foo",
    59  		},
    60  	}
    61  
    62  	for _, tc := range testcases {
    63  		t.Run(tc.name, func(t *testing.T) {
    64  			kubeconfig, err := LoadConfig(bytes.NewBufferString(tc.input))
    65  			if len(tc.expectErr) > 0 {
    66  				if err == nil {
    67  					t.Fatal("expected err, got none")
    68  				}
    69  				if !strings.Contains(err.Error(), tc.expectErr) {
    70  					t.Fatalf("expected err containing %q, got %v", tc.expectErr, err)
    71  				}
    72  				return
    73  			}
    74  			if err != nil {
    75  				t.Fatal(err)
    76  			}
    77  			if kubeconfig != tc.expectKubeconfig {
    78  				t.Fatalf("expected %q, got %q", tc.expectKubeconfig, kubeconfig)
    79  			}
    80  		})
    81  	}
    82  }