forked from tam7t/droplan
/
peers_test.go
166 lines (139 loc) · 4.94 KB
/
peers_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
package main
import (
"testing"
"github.com/digitalocean/godo"
. "github.com/franela/goblin"
)
func TestPeers(t *testing.T) {
g := Goblin(t)
g.Describe(`SortDroplets`, func() {
region := &godo.Region{Slug: `nyc1`}
droplet := godo.Droplet{Region: region}
var output map[string][]string
g.BeforeEach(func() {
output = SortDroplets([]godo.Droplet{droplet})
})
g.Describe(`without a private network`, func() {
g.Before(func() {
droplet.Networks = &godo.Networks{
V4: []godo.NetworkV4{
godo.NetworkV4{IPAddress: `192.168.0.0`, Type: `public`},
},
}
})
g.It(`is not included in the output`, func() {
_, exists := output[region.Slug]
g.Assert(exists).Equal(false)
})
})
g.Describe(`with a private network`, func() {
g.Before(func() {
droplet.Networks = &godo.Networks{
V4: []godo.NetworkV4{
godo.NetworkV4{IPAddress: `192.168.0.0`, Type: `private`},
},
}
})
g.It(`is included in the output`, func() {
g.Assert(output[region.Slug]).Equal([]string{`192.168.0.0`})
})
})
})
g.Describe(`DropletList`, func() {
var sds *stubDropletService
g.BeforeEach(func() {
sds = &stubDropletService{}
})
g.Describe(`with no droplets`, func() {
g.BeforeEach(func() {
sds.list = func(a *godo.ListOptions) ([]godo.Droplet, *godo.Response, error) {
resp := &godo.Response{}
resp.Links = nil
return []godo.Droplet{}, resp, nil
}
})
g.It(`returns no droplets`, func() {
drops, err := DropletList(sds)
g.Assert(drops).Equal([]godo.Droplet{})
g.Assert(err).Equal(nil)
})
})
g.Describe(`with a single page of droplets`, func() {
g.BeforeEach(func() {
sds.list = func(a *godo.ListOptions) ([]godo.Droplet, *godo.Response, error) {
resp := &godo.Response{}
resp.Links = nil
return []godo.Droplet{{Name: `foobar`}}, resp, nil
}
})
g.It(`returns the droplets`, func() {
drops, err := DropletList(sds)
g.Assert(drops).Equal([]godo.Droplet{{Name: `foobar`}})
g.Assert(err).Equal(nil)
})
})
g.Describe(`with a multiple pages of droplets`, func() {
g.BeforeEach(func() {
sds.list = func(a *godo.ListOptions) ([]godo.Droplet, *godo.Response, error) {
resp := &godo.Response{}
drops := []godo.Droplet{}
if a.Page == 0 {
resp.Links = &godo.Links{Pages: &godo.Pages{Next: `http://example.com/droplets?page=2`, Last: `http://example.com/droplets?page=2`}}
drops = append(drops, godo.Droplet{Name: `firstPage`})
} else {
resp.Links = &godo.Links{Pages: &godo.Pages{Prev: `http://example.com/droplets?page=1`}}
drops = append(drops, godo.Droplet{Name: `secondPage`})
}
return drops, resp, nil
}
})
g.It(`returns the droplets`, func() {
drops, err := DropletList(sds)
g.Assert(drops).Equal([]godo.Droplet{{Name: `firstPage`}, {Name: `secondPage`}})
g.Assert(err).Equal(nil)
})
})
})
}
type stubDropletService struct {
list func(*godo.ListOptions) ([]godo.Droplet, *godo.Response, error)
get func(int) (*godo.Droplet, *godo.Response, error)
create func(*godo.DropletCreateRequest) (*godo.Droplet, *godo.Response, error)
createMultiple func(*godo.DropletMultiCreateRequest) ([]godo.Droplet, *godo.Response, error)
delete func(int) (*godo.Response, error)
kernels func(int, *godo.ListOptions) ([]godo.Kernel, *godo.Response, error)
snapshots func(int, *godo.ListOptions) ([]godo.Image, *godo.Response, error)
backups func(int, *godo.ListOptions) ([]godo.Image, *godo.Response, error)
actions func(int, *godo.ListOptions) ([]godo.Action, *godo.Response, error)
neighbors func(int) ([]godo.Droplet, *godo.Response, error)
}
func (sds *stubDropletService) List(a *godo.ListOptions) ([]godo.Droplet, *godo.Response, error) {
return sds.list(a)
}
func (sds *stubDropletService) Get(a int) (*godo.Droplet, *godo.Response, error) {
return sds.get(a)
}
func (sds *stubDropletService) Create(a *godo.DropletCreateRequest) (*godo.Droplet, *godo.Response, error) {
return sds.create(a)
}
func (sds *stubDropletService) CreateMultiple(a *godo.DropletMultiCreateRequest) ([]godo.Droplet, *godo.Response, error) {
return sds.createMultiple(a)
}
func (sds *stubDropletService) Delete(a int) (*godo.Response, error) {
return sds.delete(a)
}
func (sds *stubDropletService) Kernels(a int, b *godo.ListOptions) ([]godo.Kernel, *godo.Response, error) {
return sds.kernels(a, b)
}
func (sds *stubDropletService) Snapshots(a int, b *godo.ListOptions) ([]godo.Image, *godo.Response, error) {
return sds.snapshots(a, b)
}
func (sds *stubDropletService) Backups(a int, b *godo.ListOptions) ([]godo.Image, *godo.Response, error) {
return sds.backups(a, b)
}
func (sds *stubDropletService) Actions(a int, b *godo.ListOptions) ([]godo.Action, *godo.Response, error) {
return sds.actions(a, b)
}
func (sds *stubDropletService) Neighbors(a int) ([]godo.Droplet, *godo.Response, error) {
return sds.neighbors(a)
}