forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charmconfigs_test.go
159 lines (131 loc) · 4.08 KB
/
charmconfigs_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
// Copyright 2024 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 CharmConfigsSerializationSuite struct {
SerializationSuite
}
var _ = gc.Suite(&CharmConfigsSerializationSuite{})
func (s *CharmConfigsSerializationSuite) SetUpTest(c *gc.C) {
s.importName = "charmConfigs"
s.importFunc = func(m map[string]interface{}) (interface{}, error) {
return importCharmConfigs(m)
}
}
func (s *CharmConfigsSerializationSuite) TestNewCharmConfigs(c *gc.C) {
args := CharmConfigsArgs{
Configs: map[string]CharmConfig{
"foo": charmConfig{
Description_: "description",
Type_: "string",
Default_: "default",
},
},
}
metadata := newCharmConfigs(args)
c.Assert(metadata.Configs(), gc.DeepEquals, map[string]CharmConfig{
"foo": charmConfig{
Description_: "description",
Type_: "string",
Default_: "default",
},
})
}
func minimalCharmConfigsMap() map[interface{}]interface{} {
return map[interface{}]interface{}{
"version": 1,
"configs": map[interface{}]interface{}{},
}
}
func minimalCharmConfigsArgs() CharmConfigsArgs {
return CharmConfigsArgs{}
}
func minimalCharmConfigs() *charmConfigs {
return newCharmConfigs(minimalCharmConfigsArgs())
}
func maximalCharmConfigsMap() map[interface{}]interface{} {
return map[interface{}]interface{}{
"version": 1,
"configs": map[interface{}]interface{}{
"foo": map[interface{}]interface{}{
"description": "description",
"type": "string",
"default": "default",
},
},
}
}
func maximalCharmConfigsArgs() CharmConfigsArgs {
return CharmConfigsArgs{
Configs: map[string]CharmConfig{
"foo": charmConfig{
Description_: "description",
Type_: "string",
Default_: "default",
},
},
}
}
func maximalCharmConfigs() *charmConfigs {
return newCharmConfigs(maximalCharmConfigsArgs())
}
func (s *CharmConfigsSerializationSuite) TestMinimalMatches(c *gc.C) {
bytes, err := yaml.Marshal(minimalCharmConfigs())
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, minimalCharmConfigsMap())
}
func (s *CharmConfigsSerializationSuite) TestMaximalMatches(c *gc.C) {
bytes, err := yaml.Marshal(maximalCharmConfigs())
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, maximalCharmConfigsMap())
}
func (s *CharmConfigsSerializationSuite) TestMinimalParsingSerializedData(c *gc.C) {
initial := minimalCharmConfigs()
bytes, err := yaml.Marshal(initial)
c.Assert(err, jc.ErrorIsNil)
var source map[string]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
instance, err := importCharmConfigs(source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(instance, jc.DeepEquals, initial)
}
func (s *CharmConfigsSerializationSuite) TestMaximalParsingSerializedData(c *gc.C) {
initial := maximalCharmConfigs()
bytes, err := yaml.Marshal(initial)
c.Assert(err, jc.ErrorIsNil)
var source map[string]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
instance, err := importCharmConfigs(source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(instance, jc.DeepEquals, initial)
}
func (s *CharmConfigsSerializationSuite) exportImportVersion(c *gc.C, origin_ *charmConfigs, version int) *charmConfigs {
origin_.Version_ = version
bytes, err := yaml.Marshal(origin_)
c.Assert(err, jc.ErrorIsNil)
var source map[string]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
origin, err := importCharmConfigs(source)
c.Assert(err, jc.ErrorIsNil)
return origin
}
func (s *CharmConfigsSerializationSuite) TestV1ParsingReturnsLatest(c *gc.C) {
args := maximalCharmConfigsArgs()
originV1 := newCharmConfigs(args)
originLatest := *originV1
originResult := s.exportImportVersion(c, originV1, 1)
c.Assert(*originResult, jc.DeepEquals, originLatest)
}