-
Notifications
You must be signed in to change notification settings - Fork 5
/
vpns_test.go
209 lines (173 loc) · 4.18 KB
/
vpns_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package oneandone
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
)
var (
set_vpn sync.Once
vpn_name string
vpn_desc string
test_vpn *VPN
)
const vpn_dc_code = "US"
// Helper functions
func create_vpn() *VPN {
rand.Seed(time.Now().UnixNano())
rint := rand.Intn(9999)
vpn_name = fmt.Sprintf("Test_VPN_%d", rint)
vpn_desc = fmt.Sprintf("Test_VPN_%d description", rint)
var datacenter Datacenter
dcs, err := api.ListDatacenters()
if len(dcs) > 0 {
for _, dc := range dcs {
if dc.CountryCode == vpn_dc_code {
datacenter = dc
}
}
}
fmt.Printf("Creating VPN '%s'...\n", vpn_name)
vpn_id, vpn, err := api.CreateVPN(vpn_name, vpn_desc, datacenter.Id)
if err != nil {
fmt.Printf("Unable to create a VPN. Error: %s", err.Error())
return nil
}
if vpn_id == "" || vpn.Id == "" {
fmt.Printf("Unable to create VPN '%s'.", vpn_name)
return nil
}
api.WaitForState(vpn, "ACTIVE", 10, 60)
return vpn
}
func set_vpn_once() {
test_vpn = create_vpn()
}
// /vpns tests
func TestCreateVPN(t *testing.T) {
set_vpn.Do(set_vpn_once)
if test_vpn == nil {
t.Errorf("CreateVPN failed.")
} else {
if test_vpn.Name != vpn_name {
t.Errorf("Wrong name of the VPN.")
}
if test_vpn.Description != vpn_desc {
t.Errorf("Wrong VPN description.")
}
if test_vpn.Datacenter == nil {
t.Errorf("Missing VPN Data Center.")
} else if test_vpn.Datacenter.CountryCode != vpn_dc_code {
t.Errorf("Wrong VPN Data Center.")
}
}
}
func TestGetVPN(t *testing.T) {
set_vpn.Do(set_vpn_once)
fmt.Printf("Getting VPN '%s'...\n", vpn_name)
vpn, err := api.GetVPN(test_vpn.Id)
if err != nil {
t.Errorf("GetVPN failed. Error: " + err.Error())
return
}
if vpn.Id != test_vpn.Id {
t.Errorf("Wrong VPN ID.")
}
if len(test_vpn.IPs) == 0 {
t.Errorf("Missing VPN IPs.")
}
}
func TestListVPNs(t *testing.T) {
set_vpn.Do(set_vpn_once)
fmt.Println("Listing all VPNs...")
res, err := api.ListVPNs()
if err != nil {
t.Errorf("ListVPNs failed. Error: " + err.Error())
}
if len(res) == 0 {
t.Errorf("No VPN found.")
}
res, err = api.ListVPNs(1, 1, "", "", "id,name")
if err != nil {
t.Errorf("ListVPNs with parameter options failed. Error: " + err.Error())
return
}
if len(res) == 0 {
t.Errorf("No VPN found.")
}
if len(res) > 1 {
t.Errorf("Wrong number of objects per page.")
}
if res[0].Id == "" {
t.Errorf("Filtering parameters failed.")
}
if res[0].Name == "" {
t.Errorf("Filtering parameters failed.")
}
if res[0].State != "" {
t.Errorf("Filtering parameters failed.")
}
// Test for error response
res, err = api.ListVPNs(0, 0, "", 1, "")
if res != nil || err == nil {
t.Errorf("ListVPNs failed to handle incorrect argument type.")
}
res, err = api.ListVPNs(0, 0, "", test_vpn.Name, "")
if err != nil {
t.Errorf("ListVPNs with parameter options failed. Error: " + err.Error())
return
}
if len(res) != 1 {
t.Errorf("Search parameter failed.")
}
if res[0].Name != test_vpn.Name {
t.Errorf("Search parameter failed.")
}
}
func TestGetVPNConfigFile(t *testing.T) {
//set_vpn.Do(set_vpn_once)
fmt.Printf("Getting VPN's configuration...\n")
_, err := api.GetVPNConfigFile("C:\\"+test_vpn.Name, "6EDFEFFDB31745780E4D132C7E6ABFF1")
if err != nil {
t.Errorf("GetVPNConfigFile failed. Error: " + err.Error())
return
}
}
func TestModifyVPN(t *testing.T) {
set_vpn.Do(set_vpn_once)
fmt.Printf("Modifying VPN '%s'...\n", test_vpn.Id)
new_name := test_vpn.Name + "_updated"
new_desc := test_vpn.Description + " updated"
vpn, err := api.ModifyVPN(test_vpn.Id, new_name, new_desc)
if err != nil {
t.Errorf("ModifyVPN failed. Error: " + err.Error())
return
}
if vpn.Id != test_vpn.Id {
t.Errorf("Wrong VPN ID.")
}
if vpn.Name != new_name {
t.Errorf("Wrong VPN name.")
}
if vpn.Description != new_desc {
t.Errorf("Wrong VPN description.")
}
test_vpn = vpn
}
func TestDeleteVPN(t *testing.T) {
set_vpn.Do(set_vpn_once)
fmt.Printf("Deleting VPN '%s'...\n", test_vpn.Name)
vpn, err := api.DeleteVPN(test_vpn.Id)
if err != nil {
t.Errorf("DeleteVPN failed. Error: " + err.Error())
return
}
api.WaitUntilDeleted(vpn)
vpn, err = api.GetVPN(vpn.Id)
if vpn != nil {
t.Errorf("Unable to delete the VPN.")
} else {
test_vpn = nil
}
}