mirror of
				https://github.com/siderolabs/talos.git
				synced 2025-11-04 10:21:13 +01:00 
			
		
		
		
	When interating with the kubeconfig it can be expected that a user may have the KUBECONFIG environment variable set, so we need to use it when appropriate. Closes #5091 Signed-off-by: Tim Jones <tim.jones@siderolabs.com>
		
			
				
	
	
		
			61 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// This Source Code Form is subject to the terms of the Mozilla Public
 | 
						|
// License, v. 2.0. If a copy of the MPL was not distributed with this
 | 
						|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 | 
						|
 | 
						|
package kubeconfig_test
 | 
						|
 | 
						|
import (
 | 
						|
	"testing"
 | 
						|
 | 
						|
	"github.com/stretchr/testify/assert"
 | 
						|
	"github.com/stretchr/testify/require"
 | 
						|
 | 
						|
	"github.com/talos-systems/talos/internal/pkg/kubeconfig"
 | 
						|
)
 | 
						|
 | 
						|
func TestSinglePath(t *testing.T) {
 | 
						|
	expectedDefaultPath, err := kubeconfig.DefaultPath()
 | 
						|
	assert.NoError(t, err)
 | 
						|
 | 
						|
	for _, tt := range []struct {
 | 
						|
		name      string
 | 
						|
		envVar    string
 | 
						|
		shouldErr bool
 | 
						|
		expected  string
 | 
						|
	}{
 | 
						|
		{
 | 
						|
			name:      "NoKUBECONFIGSet",
 | 
						|
			shouldErr: false,
 | 
						|
			expected:  expectedDefaultPath,
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name:      "UseKUBECONFIGSet",
 | 
						|
			envVar:    "/my/custom/path/to/kubeconfig",
 | 
						|
			shouldErr: false,
 | 
						|
			expected:  "/my/custom/path/to/kubeconfig",
 | 
						|
		},
 | 
						|
		{
 | 
						|
			name:      "MultiKUBECONFIGSet",
 | 
						|
			envVar:    "/my/custom/path/to/kubeconfig:/another/path/to/kubeconfig:/foo/bar/kubeconfig",
 | 
						|
			shouldErr: true,
 | 
						|
		},
 | 
						|
	} {
 | 
						|
		tt := tt
 | 
						|
 | 
						|
		t.Run(tt.name, func(t *testing.T) {
 | 
						|
			if tt.envVar != "" {
 | 
						|
				t.Setenv("KUBECONFIG", tt.envVar)
 | 
						|
			}
 | 
						|
			result, err := kubeconfig.SinglePath()
 | 
						|
 | 
						|
			if tt.shouldErr {
 | 
						|
				require.Error(t, err)
 | 
						|
			} else {
 | 
						|
				require.NoError(t, err)
 | 
						|
			}
 | 
						|
 | 
						|
			assert.Equal(t, tt.expected, result)
 | 
						|
		})
 | 
						|
	}
 | 
						|
}
 |