-
Notifications
You must be signed in to change notification settings - Fork 23
/
remoteentity_test.go
106 lines (90 loc) · 3.02 KB
/
remoteentity_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
// Copyright 2019 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 RemoteEntitySerializationSuite struct {
SliceSerializationSuite
}
var _ = gc.Suite(&RemoteEntitySerializationSuite{})
func (s *RemoteEntitySerializationSuite) SetUpTest(c *gc.C) {
s.SliceSerializationSuite.SetUpTest(c)
s.importName = "remote entities"
s.sliceName = "remote-entities"
s.importFunc = func(m map[string]interface{}) (interface{}, error) {
return importRemoteEntities(m)
}
s.testFields = func(m map[string]interface{}) {
m["remote-entities"] = []interface{}{}
}
}
func minimalRemoteEntityMap() map[interface{}]interface{} {
return map[interface{}]interface{}{
"id": "ctrl-uuid-3",
"token": "rnd-token",
"macaroon": "macaroon-value",
}
}
func minimalRemoteEntity() *remoteEntity {
c := newRemoteEntity(RemoteEntityArgs{
ID: "ctrl-uuid-3",
Token: "rnd-token",
Macaroon: "macaroon-value",
})
return c
}
func (*RemoteEntitySerializationSuite) TestNew(c *gc.C) {
e := minimalRemoteEntity()
c.Check(e.ID(), gc.Equals, "ctrl-uuid-3")
c.Check(e.Token(), gc.Equals, "rnd-token")
c.Check(e.Macaroon(), gc.Equals, "macaroon-value")
}
func (*RemoteEntitySerializationSuite) TestBadSchema1(c *gc.C) {
container := map[string]interface{}{
"version": 1,
"remote-entities": []interface{}{1234},
}
_, err := importRemoteEntities(container)
c.Assert(err, gc.ErrorMatches, `remote entities version schema check failed: remote-entities\[0\]: expected map, got int\(1234\)`)
}
func (*RemoteEntitySerializationSuite) TestBadSchema2(c *gc.C) {
m := minimalRemoteEntityMap()
m["token"] = true
container := map[string]interface{}{
"version": 1,
"remote-entities": []interface{}{m},
}
_, err := importRemoteEntities(container)
c.Assert(err, gc.ErrorMatches, `remote entity 0 v1 schema check failed: token: expected string, got bool\(true\)`)
}
func (*RemoteEntitySerializationSuite) TestMinimalMatches(c *gc.C) {
bytes, err := yaml.Marshal(minimalRemoteEntityMap())
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, minimalRemoteEntityMap())
}
func (s *RemoteEntitySerializationSuite) TestRoundTrip(c *gc.C) {
rIn := minimalRemoteEntity()
rOut := s.exportImport(c, rIn)
c.Assert(rOut, jc.DeepEquals, rIn)
}
func (s *RemoteEntitySerializationSuite) exportImport(c *gc.C, remoteEntityIn *remoteEntity) *remoteEntity {
remoteEntitiesIn := &remoteEntities{
Version: 1,
RemoteEntities: []*remoteEntity{remoteEntityIn},
}
bytes, err := yaml.Marshal(remoteEntitiesIn)
c.Assert(err, jc.ErrorIsNil)
var source map[string]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
remoteEntitiesOut, err := importRemoteEntities(source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(remoteEntitiesOut, gc.HasLen, 1)
return remoteEntitiesOut[0]
}