talos/internal/pkg/kubeconfig/kubeconfig_test.go
Tim Jones 95d900de77
feat: use kubeconfig env var
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>
2022-03-31 15:30:26 +02:00

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)
})
}
}