Path: blob/main/dev/preview/previewctl/pkg/k8s/context/k3s/k3s_test.go
2506 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package k3s56import (7"context"8"testing"910"github.com/cockroachdb/errors"11"github.com/stretchr/testify/assert"12"k8s.io/apimachinery/pkg/runtime"13"k8s.io/client-go/tools/clientcmd/api"1415pssh "github.com/gitpod-io/gitpod/previewctl/pkg/ssh"16)1718func Test_LoadK3SConfig(t *testing.T) {19type k3sExpStruct struct {20config *api.Config21err error22}23type testCase struct {24name string25cmd pssh.MockCmd26expected *k3sExpStruct27}2829testCases := []testCase{30{31name: "k3s config not found",32cmd: pssh.MockCmd{33CMD: catK3sConfigCmd,34STDOUT: []byte(""),35STDERR: []byte("cat: /etc/rancher/k3s/k3s.yaml: No such file or directory"),36Err: errors.New("some error that will be irrelevant"),37},38expected: &k3sExpStruct{39config: nil,40err: ErrK3SConfigNotFound,41},42},43{44name: "returned config",45cmd: pssh.MockCmd{46CMD: catK3sConfigCmd,47STDOUT: []byte(`48apiVersion: v149clusters:50- cluster:51certificate-authority-data: dGVzdF9kYXRh52server: https://default.kube.gitpod-dev.com:644353name: default54contexts:55- context:56cluster: default57user: default58name: default59current-context: default60kind: Config61preferences: {}62users:63- name: default64user:65client-certificate-data: dGVzdF9kYXRh66client-key-data: dGVzdF9kYXRh67`),68STDERR: nil,69Err: nil,70},71expected: &k3sExpStruct{72config: &api.Config{73Preferences: api.Preferences{74Extensions: map[string]runtime.Object{},75},76Contexts: map[string]*api.Context{77"k3s": {78Cluster: "k3s",79AuthInfo: "k3s",80Extensions: map[string]runtime.Object{},81},82},83Clusters: map[string]*api.Cluster{84"k3s": {85Server: "https://k3s.preview.gitpod-dev.com:6443",86CertificateAuthorityData: []byte("test_data"),87Extensions: map[string]runtime.Object{},88},89},90CurrentContext: "k3s",91AuthInfos: map[string]*api.AuthInfo{92"k3s": {93ClientCertificateData: []byte("test_data"),94ClientKeyData: []byte("test_data"),95Extensions: map[string]runtime.Object{},96},97},98Extensions: map[string]runtime.Object{},99},100err: nil,101},102},103}104105for _, test := range testCases {106t.Run(test.name, func(t *testing.T) {107c := &pssh.MockClient{Command: test.cmd}108k := &ConfigLoader{client: c, opts: ConfigLoaderOpts{109PreviewName: "k3s",110}}111112config, err := k.Load(context.TODO())113114assert.ErrorIs(t, test.expected.err, err)115assert.Equal(t, test.expected.config, config)116})117}118}119120121