forked from dell/gounity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
151 lines (130 loc) · 3.82 KB
/
main_test.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
package gounity
import (
"bufio"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"testing"
)
type testConfig struct {
unityEndPoint string
username string
password string
poolID string
nodeHostName string
nodeHostIP string
wwns []string
iqn string
hostIOLimitName string
nasServer string
volumeAPI *Volume
hostAPI *Host
poolAPI *Storagepool
snapAPI *Snapshot
ipinterfaceAPI *Ipinterface
fileAPI *Filesystem
metricsAPI *Metrics
}
var testConf *testConfig
func TestMain(m *testing.M) {
fmt.Println("------------In TestMain--------------")
os.Setenv("GOUNITY_DEBUG", "true")
// for this tutorial, we will hard code it to config.txt
testProp, err := readTestProperties("test.properties")
if err != nil {
panic("The system cannot find the file specified")
}
insecure, _ := strconv.ParseBool(testProp["X_CSI_UNITY_INSECURE"])
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: insecure}
if err != nil {
fmt.Println(err)
}
ctx := context.Background()
testConf = &testConfig{}
testConf.unityEndPoint = testProp["GOUNITY_ENDPOINT"]
testConf.username = testProp["X_CSI_UNITY_USER"]
testConf.password = testProp["X_CSI_UNITY_PASSWORD"]
testConf.poolID = testProp["STORAGE_POOL"]
testConf.nodeHostName = testProp["NODE_HOSTNAME"]
testConf.hostIOLimitName = testProp["HOST_IO_LIMIT_NAME"]
testConf.nodeHostIP = testProp["NODE_HOSTIP"]
testConf.nasServer = testProp["UNITY_NAS_SERVER"]
testConf.iqn = testProp["NODE_IQN"]
wwnStr := testProp["NODE_WWNS"]
os.Setenv("GOUNITY_ENDPOINT", testConf.unityEndPoint)
os.Setenv("X_CSI_UNITY_USER", testConf.username)
os.Setenv("X_CSI_UNITY_PASSWORD", testConf.password)
testConf.username = testProp["X_CSI_UNITY_USER"]
testConf.password = testProp["X_CSI_UNITY_PASSWORD"]
testClient := getTestClient(ctx, testConf.unityEndPoint, testConf.username, testConf.password, testConf.unityEndPoint, insecure)
testConf.wwns = strings.Split(wwnStr, ",")
testConf.hostAPI = NewHost(testClient)
testConf.poolAPI = NewStoragePool(testClient)
testConf.snapAPI = NewSnapshot(testClient)
testConf.volumeAPI = NewVolume(testClient)
testConf.ipinterfaceAPI = NewIPInterface(testClient)
testConf.fileAPI = NewFilesystem(testClient)
testConf.metricsAPI = NewMetrics(testClient)
code := m.Run()
fmt.Println("------------End of TestMain--------------")
os.Exit(code)
}
func getTestClient(ctx context.Context, url, username, password, endpoint string, insecure bool) *Client {
fmt.Println("Test:", url, username, password)
c, err := NewClientWithArgs(ctx, endpoint, insecure)
if err != nil {
fmt.Println(err)
}
err = c.Authenticate(ctx, &ConfigConnect{
Username: username,
Password: password,
Endpoint: url,
})
return c
}
func readTestProperties(filename string) (map[string]string, error) {
// init with some bogus data
configPropertiesMap := map[string]string{}
if len(filename) == 0 {
return nil, errors.New("Error reading properties file " + filename)
}
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
reader := bufio.NewReader(file)
for {
line, err := reader.ReadString('\n')
// check if the line has = sign
// and process the line. Ignore the rest.
if equal := strings.Index(line, "="); equal >= 0 {
if key := strings.TrimSpace(line[:equal]); len(key) > 0 {
value := ""
if len(line) > equal {
value = strings.TrimSpace(line[equal+1:])
}
// assign the config map
configPropertiesMap[key] = value
}
}
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
}
return configPropertiesMap, nil
}
func prettyPrintJSON(obj interface{}) string {
data, _ := json.Marshal(obj)
return string(data)
}