-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain_test.go
159 lines (138 loc) · 4.07 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
152
153
154
155
156
157
158
159
/*
Copyright © 2019-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gounity
import (
"bufio"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"testing"
"github.com/dell/gounity/mocks"
)
type testConfig struct {
unityEndPoint string
username string
password string
poolID string
nodeHostName string
nodeHostIP string
wwns []string
iqn string
hostIOLimitName string
nasServer string
tenant string
hostList []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_template")
if err != nil {
panic("The system cannot find the file specified")
}
insecure, _ := strconv.ParseBool(testProp["X_CSI_UNITY_INSECURE"])
/* #nosec G402 */
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: insecure}
if err != nil {
fmt.Println(err)
}
testConf = &testConfig{}
testConf.unityEndPoint = "https://mock-endpoint"
testConf.username = "user"
testConf.password = "password"
testConf.poolID = "pool_3"
testConf.nodeHostName = "Unit-test-host-20231023052923"
testConf.hostIOLimitName = "Autotyre"
testConf.nodeHostIP = "10.20.30.40"
testConf.nasServer = "nas_1"
testConf.iqn = "iqn.1996-04.de.suse:01:f8298e544dc"
wwnStr := ""
hostListStr := "Unit-test-host-20231023052923"
testConf.tenant = "tenant_1"
os.Setenv("GOUNITY_ENDPOINT", testConf.unityEndPoint)
os.Setenv("X_CSI_UNITY_USER", testConf.username)
os.Setenv("X_CSI_UNITY_PASSWORD", testConf.password)
testClient := getTestClient()
testConf.wwns = strings.Split(wwnStr, ",")
testConf.hostList = strings.Split(hostListStr, ",")
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() *Client {
return &Client{
api: &mocks.Client{},
configConnect: &ConfigConnect{},
}
}
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)
}