-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbootstrap_test.go
More file actions
91 lines (85 loc) · 2.3 KB
/
bootstrap_test.go
File metadata and controls
91 lines (85 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package bootstrap
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/client-go/tools/clientcmd/api"
)
func TestBootstrap(t *testing.T) {
cases := []struct {
name string
bootstrapFunc func(ctx context.Context, config *Config) (*api.Config, error)
expectKubeconfig func(t *testing.T, path string)
expectedError error
}{
{
name: "bootstrapping returns an error",
bootstrapFunc: func(ctx context.Context, config *Config) (*api.Config, error) {
return nil, errors.New("failed to bootstrap")
},
expectedError: errors.New("failed to bootstrap"),
},
{
name: "bootstrapping returns no error and no kubeconfig data",
bootstrapFunc: func(ctx context.Context, config *Config) (*api.Config, error) {
return nil, nil
},
expectKubeconfig: func(t *testing.T, path string) {
_, err := os.Stat(path)
assert.True(t, os.IsNotExist(err))
},
expectedError: nil,
},
{
name: "bootstrapping returns kubeconfig data without error",
bootstrapFunc: func(ctx context.Context, config *Config) (*api.Config, error) {
return &api.Config{
Clusters: map[string]*api.Cluster{
"default-cluster": {
Server: "server",
},
},
AuthInfos: map[string]*api.AuthInfo{
"default-auth": {
ClientCertificate: "cert",
ClientKey: "key",
},
},
Contexts: map[string]*api.Context{
"default-context": {
Cluster: "default-cluster",
AuthInfo: "default-auth",
},
},
CurrentContext: "default-context",
}, nil
},
expectKubeconfig: func(t *testing.T, path string) {
info, err := os.Stat(path)
assert.NoError(t, err)
assert.NotZero(t, info.Size())
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ctx := context.Background()
kubeconfigPath := filepath.Join(t.TempDir(), "kubeconfig")
bootstrapFunc = c.bootstrapFunc
err := Bootstrap(ctx, &Config{KubeconfigPath: kubeconfigPath})
if c.expectedError != nil {
assert.EqualError(t, err, c.expectedError.Error())
} else {
assert.NoError(t, err)
}
if c.expectKubeconfig != nil {
c.expectKubeconfig(t, kubeconfigPath)
}
})
}
}