mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-09-16 15:58:25 +00:00
Create test utils for crafting kubeconfig strings
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
36
internal/testutil/kubeconfigbuilder.go
Normal file
36
internal/testutil/kubeconfigbuilder.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Name string `yaml:"name,omitempty"`
|
||||
Namespace string `yaml:"namespace,omitempty"`
|
||||
}
|
||||
|
||||
func Ctx(name string) *Context { return &Context{Name: name} }
|
||||
|
||||
type Kubeconfig map[string]interface{}
|
||||
|
||||
func KC() *Kubeconfig {
|
||||
return &Kubeconfig{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Config"}
|
||||
}
|
||||
|
||||
func (k *Kubeconfig) Set(key string, v interface{}) *Kubeconfig { (*k)[key] = v; return k }
|
||||
func (k *Kubeconfig) WithCurrentCtx(s string) *Kubeconfig { (*k)["current-context"] = s; return k }
|
||||
func (k *Kubeconfig) WithCtxs(c ...*Context) *Kubeconfig { (*k)["contexts"] = c; return k }
|
||||
|
||||
func (k *Kubeconfig) ToYAML(t *testing.T) string {
|
||||
t.Helper()
|
||||
var v strings.Builder
|
||||
if err := yaml.NewEncoder(&v).Encode(*k); err != nil {
|
||||
t.Fatalf("failed to encode mock kubeconfig: %v", err)
|
||||
}
|
||||
return v.String()
|
||||
}
|
Reference in New Issue
Block a user