forked from digitalocean/doctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpcs_test.go
184 lines (155 loc) · 4.41 KB
/
vpcs_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
package commands
import (
"testing"
"github.com/digitalocean/doctl"
"github.com/digitalocean/doctl/do"
"github.com/digitalocean/godo"
"github.com/stretchr/testify/assert"
)
var (
testVPC = do.VPC{
VPC: &godo.VPC{
Name: "vpc-name",
RegionSlug: "nyc1",
Description: "vpc description",
IPRange: "10.116.0.0/20",
}}
testVPCList = do.VPCs{
testVPC,
}
)
func TestVPCsCommand(t *testing.T) {
cmd := VPCs()
assert.NotNil(t, cmd)
assertCommandNames(t, cmd, "get", "list", "create", "update", "delete")
}
func TestVPCGet(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
vpcUUID := "e819b321-a9a1-4078-b437-8e6b8bf13530"
tm.vpcs.EXPECT().Get(vpcUUID).Return(&testVPC, nil)
config.Args = append(config.Args, vpcUUID)
err := RunVPCGet(config)
assert.NoError(t, err)
})
}
func TestVPCGetNoID(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
err := RunVPCGet(config)
assert.Error(t, err)
})
}
func TestVPCList(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
tm.vpcs.EXPECT().List().Return(testVPCList, nil)
err := RunVPCList(config)
assert.NoError(t, err)
})
}
func TestVPCCreate(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
r := godo.VPCCreateRequest{
Name: "vpc-name",
RegionSlug: "nyc1",
Description: "vpc description",
IPRange: "10.116.0.0/20",
}
tm.vpcs.EXPECT().Create(&r).Return(&testVPC, nil)
config.Doit.Set(config.NS, doctl.ArgVPCName, "vpc-name")
config.Doit.Set(config.NS, doctl.ArgRegionSlug, "nyc1")
config.Doit.Set(config.NS, doctl.ArgVPCDescription, "vpc description")
config.Doit.Set(config.NS, doctl.ArgVPCIPRange, "10.116.0.0/20")
err := RunVPCCreate(config)
assert.NoError(t, err)
})
}
func TestVPCUpdate(t *testing.T) {
tests := []struct {
desc string
setup func(*CmdConfig)
expectedVPCId string
expectedRequest []godo.VPCSetField
}{
{
desc: "update vpc name",
setup: func(in *CmdConfig) {
in.Args = append(in.Args, "vpc-uuid")
in.Doit.Set(in.NS, doctl.ArgVPCName, "update-vpc-name-test")
},
expectedVPCId: "vpc-uuid",
expectedRequest: []godo.VPCSetField{
godo.VPCSetName("update-vpc-name-test"),
},
},
{
desc: "update vpc name and description",
setup: func(in *CmdConfig) {
in.Args = append(in.Args, "vpc-uuid")
in.Doit.Set(in.NS, doctl.ArgVPCName, "update-vpc-name-test")
in.Doit.Set(in.NS, doctl.ArgVPCDescription, "i am a new desc")
},
expectedVPCId: "vpc-uuid",
expectedRequest: []godo.VPCSetField{
godo.VPCSetName("update-vpc-name-test"),
godo.VPCSetDescription("i am a new desc"),
},
},
{
desc: "update vpc name and description and set to default",
setup: func(in *CmdConfig) {
in.Args = append(in.Args, "vpc-uuid")
in.Doit.Set(in.NS, doctl.ArgVPCName, "update-vpc-name-test")
in.Doit.Set(in.NS, doctl.ArgVPCDescription, "i am a new desc")
in.Doit.Set(in.NS, doctl.ArgVPCDefault, true)
},
expectedVPCId: "vpc-uuid",
expectedRequest: []godo.VPCSetField{
godo.VPCSetName("update-vpc-name-test"),
godo.VPCSetDescription("i am a new desc"),
godo.VPCSetDefault(),
},
},
{
desc: "update only default",
setup: func(in *CmdConfig) {
in.Args = append(in.Args, "vpc-uuid")
in.Doit.Set(in.NS, doctl.ArgVPCDefault, true)
},
expectedVPCId: "vpc-uuid",
expectedRequest: []godo.VPCSetField{
godo.VPCSetDefault(),
},
},
}
for _, tt := range tests {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
if tt.setup != nil {
tt.setup(config)
}
tm.vpcs.EXPECT().PartialUpdate(tt.expectedVPCId, tt.expectedRequest).Return(&testVPC, nil)
err := RunVPCUpdate(config)
assert.NoError(t, err)
})
}
}
func TestVPCUpdatefNoID(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
err := RunVPCUpdate(config)
assert.Error(t, err)
})
}
func TestVPCDelete(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
vpcUUID := "e819b321-a9a1-4078-b437-8e6b8bf13530"
tm.vpcs.EXPECT().Delete(vpcUUID).Return(nil)
config.Args = append(config.Args, vpcUUID)
config.Doit.Set(config.NS, doctl.ArgForce, true)
err := RunVPCDelete(config)
assert.NoError(t, err)
})
}
func TestVPCDeleteNoID(t *testing.T) {
withTestClient(t, func(config *CmdConfig, tm *tcMocks) {
err := RunVPCDelete(config)
assert.Error(t, err)
})
}