...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/util/config/joinconfiguration_test.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/util/config

     1  /*
     2  Copyright 2018 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  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/lithammer/dedent"
    25  )
    26  
    27  func TestLoadJoinConfigurationFromFile(t *testing.T) {
    28  	// Create temp folder for the test case
    29  	tmpdir, err := os.MkdirTemp("", "")
    30  	if err != nil {
    31  		t.Fatalf("Couldn't create tmpdir: %v", err)
    32  	}
    33  	defer os.RemoveAll(tmpdir)
    34  
    35  	// cfgFiles is in cluster_test.go
    36  	var tests = []struct {
    37  		name         string
    38  		fileContents string
    39  		expectErr    bool
    40  	}{
    41  		{
    42  			name:      "empty file causes error",
    43  			expectErr: true,
    44  		},
    45  		{
    46  			name: "Invalid v1beta3 causes error",
    47  			fileContents: dedent.Dedent(`
    48  				apiVersion: kubeadm.k8s.io/v1beta3
    49  				kind: JoinConfiguration
    50  			`),
    51  			expectErr: true,
    52  		},
    53  		{
    54  			name: "valid v1beta3 is loaded",
    55  			fileContents: dedent.Dedent(`
    56  				apiVersion: kubeadm.k8s.io/v1beta3
    57  				kind: JoinConfiguration
    58  				caCertPath: /etc/kubernetes/pki/ca.crt
    59  				nodeRegistration:
    60  				  criSocket: "unix:///var/run/unknown.sock"
    61  				discovery:
    62  				  bootstrapToken:
    63  				    apiServerEndpoint: kube-apiserver:6443
    64  				    token: abcdef.0123456789abcdef
    65  				    unsafeSkipCAVerification: true
    66  				  timeout: 5m0s
    67  				  tlsBootstrapToken: abcdef.0123456789abcdef
    68  			`),
    69  		},
    70  	}
    71  
    72  	for _, rt := range tests {
    73  		t.Run(rt.name, func(t2 *testing.T) {
    74  			cfgPath := filepath.Join(tmpdir, rt.name)
    75  			err := os.WriteFile(cfgPath, []byte(rt.fileContents), 0644)
    76  			if err != nil {
    77  				t.Errorf("Couldn't create file: %v", err)
    78  				return
    79  			}
    80  
    81  			opts := LoadOrDefaultConfigurationOptions{
    82  				SkipCRIDetect: true,
    83  			}
    84  
    85  			obj, err := LoadJoinConfigurationFromFile(cfgPath, opts)
    86  			if rt.expectErr {
    87  				if err == nil {
    88  					t.Error("Unexpected success")
    89  				}
    90  			} else {
    91  				if err != nil {
    92  					t.Errorf("Error reading file: %v", err)
    93  					return
    94  				}
    95  
    96  				if obj == nil {
    97  					t.Error("Unexpected nil return value")
    98  				}
    99  			}
   100  		})
   101  	}
   102  }
   103  

View as plain text