forked from redhat-developer/odo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configs.go
178 lines (163 loc) · 5.23 KB
/
configs.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package testingutil
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"testing"
"github.com/ghodss/yaml"
"github.com/openshift/odo/pkg/preference"
"github.com/pkg/errors"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)
// This value can be provided to set a seperate directory for users 'homedir' resolution
// note for mocking purpose ONLY
var customHomeDir = os.Getenv("CUSTOM_HOMEDIR")
// ConfigDetails struct holds configuration details(odo and/or kube config)
type ConfigDetails struct {
FileName string
Config interface{}
ConfigPathEnv string
}
// getConfFolder generates a mock config folder for the unit testing
func getConfFolder() (string, error) {
var confLocation string
// If custom home dir is set, skip checking for user.Current to place config
if len(customHomeDir) != 0 {
confLocation = customHomeDir
} else {
currentUser, err := user.Current()
if err != nil {
return "", err
}
confLocation = currentUser.HomeDir
}
dir, err := ioutil.TempDir(confLocation, ".odo")
if err != nil {
return "", err
}
return dir, nil
}
// setupTempConfigFile takes config file name - confFile and creates it for unit testing
// The invocation of setupTempConfigFile puts the onus of invoking the configCleanUp as well
func setupTempConfigFile(confFile string) (*os.File, error) {
confFolder, err := getConfFolder()
if err != nil {
return nil, err
}
tmpfile, err := ioutil.TempFile(confFolder, confFile)
if err != nil {
return nil, errors.Wrapf(err, "unable to create test config file")
}
return tmpfile, nil
}
// setupEnv takes odoConfigFile name and sets env var ODOCONFIG to odoConfigFile
// The config logic relies on this env var(if present) to read and/or write config
func setupEnv(envName string, odoconfigfile string) error {
err := os.Setenv(envName, odoconfigfile)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("unable to set %s to %s", envName, odoconfigfile))
}
return nil
}
// SetUp sets up the odo and kube config files and returns respective conf file pointers and error
func SetUp(odoConfigDetails ConfigDetails, kubeConfigDetails ConfigDetails) (*os.File, *os.File, error) {
odoConfigFile, err := setUpConfig(odoConfigDetails.FileName, odoConfigDetails.Config, odoConfigDetails.ConfigPathEnv)
if err != nil {
return odoConfigFile, nil, err
}
kubeConfigFile, err := setUpConfig(kubeConfigDetails.FileName, kubeConfigDetails.Config, kubeConfigDetails.ConfigPathEnv)
return odoConfigFile, kubeConfigFile, err
}
// setUpConfig sets up mock config
// Parameters:
// conf: the config object to write to the mock config file
// testFile: the name of the mock config file
// configEnvName: Name of env variable that corresponds to config file
// Returns:
// file handler for the mock config file
// error if any
func setUpConfig(testFile string, conf interface{}, configEnvName string) (*os.File, error) {
foundConfigType := false
var err error
var data []byte
if conf, ok := conf.(preference.PreferenceInfo); ok {
data, err = yaml.Marshal(conf.Preference)
foundConfigType = true
}
if conf, ok := conf.(clientcmdapi.Config); ok {
data, err = yaml.Marshal(conf)
foundConfigType = true
}
if conf, ok := conf.(string); ok {
data = []byte(conf)
foundConfigType = true
}
if err != nil {
return nil, errors.Wrap(err, "unable to create mock config file")
}
if !foundConfigType {
return nil, fmt.Errorf("Config %+v not of recognisable type", conf)
}
configFile, err := setupTempConfigFile(testFile)
if err != nil {
return nil, errors.Wrap(err, "unable to create mock config file")
}
if conf != nil {
if _, err := configFile.Write(data); err != nil {
return nil, errors.Wrapf(err, "unable to write config %+v to mock config file %s", conf, configFile.Name())
}
}
return configFile, setupEnv(configEnvName, configFile.Name())
}
// CleanupEnv cleans up the mock config file and anything that SetupEnv generated
// Parameters:
// configFile: the mock config file handler
// t: testing pointer to log errors if any
func CleanupEnv(confFiles []*os.File, t *testing.T) {
for _, confFile := range confFiles {
if confFile == nil {
continue
}
if err := confFile.Close(); err != nil {
t.Errorf("failed to cleanup the test env. Error: %v", err)
}
os.Remove(confFile.Name())
}
}
// FakeOdoConfig returns mock odo config
// It takes a confPath which is the path to the config
func FakeOdoConfig(confPath string, needNamePrefix bool, namePrefix string) preference.PreferenceInfo {
odoConfig := preference.PreferenceInfo{
Filename: confPath,
Preference: preference.Preference{},
}
if needNamePrefix {
odoConfig.OdoSettings = preference.OdoSettings{
NamePrefix: &namePrefix,
}
}
return odoConfig
}
// FakeKubeClientConfig returns mock kube client config
func FakeKubeClientConfig() string {
return `apiVersion: v1
clusters:
- cluster:
insecure-skip-tls-verify: true
server: https://192.168.42.237:8443
name: 192-168-42-237:8443
contexts:
- context:
cluster: 192-168-42-237:8443
namespace: testing
user: developer/192-168-42-237:8443
name: myproject/192-168-42-237:8443/developer
current-context: myproject/192-168-42-237:8443/developer
kind: Config
preferences: {}
users:
- name: developer/192-168-42-237:8443
user:
token: C0E6Gkmi3n_Se2QKx6Unw3Y3Zu4mJHgzdrMVK0DsDwc`
}