forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remotespace_test.go
150 lines (134 loc) · 4.27 KB
/
remotespace_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
// Copyright 2017 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
)
type RemoteSpaceSerializationSuite struct {
SliceSerializationSuite
}
var _ = gc.Suite(&RemoteSpaceSerializationSuite{})
func (s *RemoteSpaceSerializationSuite) SetUpTest(c *gc.C) {
s.SliceSerializationSuite.SetUpTest(c)
s.importName = "remote spaces"
s.sliceName = "spaces"
s.importFunc = func(m map[string]interface{}) (interface{}, error) {
return importRemoteSpaces(m)
}
s.testFields = func(m map[string]interface{}) {
m["spaces"] = []interface{}{}
}
}
func minimalRemoteSpaceMap() map[interface{}]interface{} {
return map[interface{}]interface{}{
"cloud-type": "gce",
"name": "private",
"provider-id": "juju-space-private",
"provider-attributes": map[interface{}]interface{}{
"project": "gothic",
},
"subnets": map[interface{}]interface{}{
"version": 3,
"subnets": []interface{}{map[interface{}]interface{}{
"cidr": "2.3.4.0/24",
"subnet-id": "",
"is-public": false,
"space-id": "",
"space-name": "a-space",
"vlan-tag": 64,
"provider-id": "juju-subnet-1",
"availability-zones": []interface{}{"az1", "az2"},
"provider-space-id": "juju-space-private",
"provider-network-id": "network-1",
}},
},
}
}
func minimalRemoteSpace() *remoteSpace {
space := newRemoteSpace(RemoteSpaceArgs{
CloudType: "gce",
Name: "private",
ProviderId: "juju-space-private",
ProviderAttributes: map[string]interface{}{
"project": "gothic",
},
})
space.AddSubnet(SubnetArgs{
CIDR: "2.3.4.0/24",
SpaceName: "a-space",
VLANTag: 64,
ProviderId: "juju-subnet-1",
AvailabilityZones: []string{"az1", "az2"},
ProviderSpaceId: "juju-space-private",
ProviderNetworkId: "network-1",
})
return space
}
func (*RemoteSpaceSerializationSuite) TestNew(c *gc.C) {
r := minimalRemoteSpace()
c.Check(r.CloudType(), gc.Equals, "gce")
c.Check(r.Name(), gc.Equals, "private")
c.Check(r.ProviderId(), gc.Equals, "juju-space-private")
c.Check(r.ProviderAttributes(), gc.DeepEquals, map[string]interface{}{
"project": "gothic",
})
c.Check(r.Subnets(), gc.DeepEquals, []Subnet{
newSubnet(SubnetArgs{
CIDR: "2.3.4.0/24",
SpaceName: "a-space",
VLANTag: 64,
ProviderId: "juju-subnet-1",
AvailabilityZones: []string{"az1", "az2"},
ProviderSpaceId: "juju-space-private",
ProviderNetworkId: "network-1",
}),
})
}
func (*RemoteSpaceSerializationSuite) TestBadSchema1(c *gc.C) {
container := map[string]interface{}{
"version": 1,
"spaces": []interface{}{1234},
}
_, err := importRemoteSpaces(container)
c.Assert(err, gc.ErrorMatches, `remote spaces version schema check failed: spaces\[0\]: expected map, got int\(1234\)`)
}
func (*RemoteSpaceSerializationSuite) TestBadSchema2(c *gc.C) {
m := minimalRemoteSpaceMap()
m["provider-attributes"] = "blah"
container := map[string]interface{}{
"version": 1,
"spaces": []interface{}{m},
}
_, err := importRemoteSpaces(container)
c.Assert(err, gc.ErrorMatches, `remote space 0 v1 schema check failed: provider-attributes: expected map, got string\("blah"\)`)
}
func (*RemoteSpaceSerializationSuite) TestMinimalMatches(c *gc.C) {
bytes, err := yaml.Marshal(minimalRemoteSpace())
c.Assert(err, jc.ErrorIsNil)
var source map[interface{}]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(source, jc.DeepEquals, minimalRemoteSpaceMap())
}
func (s *RemoteSpaceSerializationSuite) TestRoundTrip(c *gc.C) {
rIn := minimalRemoteSpace()
rOut := s.exportImport(c, rIn)
c.Assert(rOut, jc.DeepEquals, rIn)
}
func (s *RemoteSpaceSerializationSuite) exportImport(c *gc.C, spaceIn *remoteSpace) *remoteSpace {
spacesIn := &remoteSpaces{
Version: 1,
Spaces: []*remoteSpace{spaceIn},
}
bytes, err := yaml.Marshal(spacesIn)
c.Assert(err, jc.ErrorIsNil)
var source map[string]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
spacesOut, err := importRemoteSpaces(source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(spacesOut, gc.HasLen, 1)
return spacesOut[0]
}