~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/environs/tools/marshal_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package tools_test
 
5
 
 
6
import (
 
7
        "encoding/json"
 
8
        "time"
 
9
 
 
10
        jc "github.com/juju/testing/checkers"
 
11
        gc "gopkg.in/check.v1"
 
12
 
 
13
        "github.com/juju/juju/environs/tools"
 
14
)
 
15
 
 
16
var _ = gc.Suite(&marshalSuite{})
 
17
 
 
18
type marshalSuite struct {
 
19
        streamMetadata map[string][]*tools.ToolsMetadata
 
20
}
 
21
 
 
22
func (s *marshalSuite) SetUpTest(c *gc.C) {
 
23
        s.streamMetadata = map[string][]*tools.ToolsMetadata{
 
24
                "released": releasedToolMetadataForTesting,
 
25
                "proposed": proposedToolMetadataForTesting,
 
26
        }
 
27
}
 
28
 
 
29
func (s *marshalSuite) TestLargeNumber(c *gc.C) {
 
30
        metadata := map[string][]*tools.ToolsMetadata{
 
31
                "released": {
 
32
                        {
 
33
                                Release:  "saucy",
 
34
                                Version:  "1.2.3.4",
 
35
                                Arch:     "arm",
 
36
                                Size:     9223372036854775807,
 
37
                                Path:     "/somewhere/over/the/rainbow.tar.gz",
 
38
                                FileType: "tar.gz",
 
39
                        }},
 
40
        }
 
41
        _, _, products, err := tools.MarshalToolsMetadataJSON(metadata, time.Now())
 
42
        c.Assert(err, jc.ErrorIsNil)
 
43
        c.Assert(string(products["released"]), jc.Contains, `"size": 9223372036854775807`)
 
44
}
 
45
 
 
46
var expectedIndex = `{
 
47
    "index": {
 
48
        "com.ubuntu.juju:proposed:tools": {
 
49
            "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
 
50
            "format": "products:1.0",
 
51
            "datatype": "content-download",
 
52
            "path": "streams/v1/com.ubuntu.juju-proposed-tools.json",
 
53
            "products": [
 
54
                "com.ubuntu.juju:14.04:arm64",
 
55
                "com.ubuntu.juju:14.10:ppc64el"            
 
56
            ]
 
57
        },
 
58
        "com.ubuntu.juju:released:tools": {
 
59
            "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
 
60
            "format": "products:1.0",
 
61
            "datatype": "content-download",
 
62
            "path": "streams/v1/com.ubuntu.juju-released-tools.json",
 
63
            "products": [
 
64
                "com.ubuntu.juju:12.04:amd64",
 
65
                "com.ubuntu.juju:12.04:arm",
 
66
                "com.ubuntu.juju:13.10:arm"            
 
67
            ]
 
68
        }
 
69
    },
 
70
    "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
 
71
    "format": "index:1.0"
 
72
}`
 
73
 
 
74
var expectedLegacyIndex = `{
 
75
    "index": {
 
76
        "com.ubuntu.juju:released:tools": {
 
77
            "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
 
78
            "format": "products:1.0",
 
79
            "datatype": "content-download",
 
80
            "path": "streams/v1/com.ubuntu.juju-released-tools.json",
 
81
            "products": [
 
82
                "com.ubuntu.juju:12.04:amd64",
 
83
                "com.ubuntu.juju:12.04:arm",
 
84
                "com.ubuntu.juju:13.10:arm"            
 
85
            ]
 
86
        }
 
87
    },
 
88
    "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
 
89
    "format": "index:1.0"
 
90
}`
 
91
 
 
92
var expectedReleasedProducts = `{
 
93
    "products": {
 
94
        "com.ubuntu.juju:12.04:amd64": {
 
95
            "version": "4.3.2.1",
 
96
            "arch": "amd64",
 
97
            "versions": {
 
98
                "19700101": {
 
99
                    "items": {
 
100
                        "4.3.2.1-precise-amd64": {
 
101
                            "release": "precise",
 
102
                            "version": "4.3.2.1",
 
103
                            "arch": "amd64",
 
104
                            "size": 0,
 
105
                            "path": "whatever.tar.gz",
 
106
                            "ftype": "tar.gz",
 
107
                            "sha256": "afb14e65c794464e378def12cbad6a96f9186d69"
 
108
                        }
 
109
                    }
 
110
                }
 
111
            }
 
112
        },
 
113
        "com.ubuntu.juju:12.04:arm": {
 
114
            "version": "1.2.3.4",
 
115
            "arch": "arm",
 
116
            "versions": {
 
117
                "19700101": {
 
118
                    "items": {
 
119
                        "1.2.3.4-precise-arm": {
 
120
                            "release": "precise",
 
121
                            "version": "1.2.3.4",
 
122
                            "arch": "arm",
 
123
                            "size": 42,
 
124
                            "path": "toenlightenment.tar.gz",
 
125
                            "ftype": "tar.gz",
 
126
                            "sha256": ""
 
127
                        }
 
128
                    }
 
129
                }
 
130
            }
 
131
        },
 
132
        "com.ubuntu.juju:13.10:arm": {
 
133
            "version": "1.2.3.4",
 
134
            "arch": "arm",
 
135
            "versions": {
 
136
                "19700101": {
 
137
                    "items": {
 
138
                        "1.2.3.4-saucy-arm": {
 
139
                            "release": "saucy",
 
140
                            "version": "1.2.3.4",
 
141
                            "arch": "arm",
 
142
                            "size": 9223372036854775807,
 
143
                            "path": "/somewhere/over/the/rainbow.tar.gz",
 
144
                            "ftype": "tar.gz",
 
145
                            "sha256": ""
 
146
                        }
 
147
                    }
 
148
                }
 
149
            }
 
150
        }
 
151
    },
 
152
    "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
 
153
    "format": "products:1.0",
 
154
    "content_id": "com.ubuntu.juju:released:tools"
 
155
}`
 
156
 
 
157
var expectedProposedProducts = `{
 
158
    "products": {
 
159
        "com.ubuntu.juju:14.04:arm64": {
 
160
            "version": "1.2-beta1",
 
161
            "arch": "arm64",
 
162
            "versions": {
 
163
                "19700101": {
 
164
                    "items": {
 
165
                        "1.2-beta1-trusty-arm64": {
 
166
                            "release": "trusty",
 
167
                            "version": "1.2-beta1",
 
168
                            "arch": "arm64",
 
169
                            "size": 42,
 
170
                            "path": "gotham.tar.gz",
 
171
                            "ftype": "tar.gz",
 
172
                            "sha256": ""
 
173
                        }
 
174
                    }
 
175
                }
 
176
            }
 
177
        },
 
178
        "com.ubuntu.juju:14.10:ppc64el": {
 
179
            "version": "1.2-alpha1",
 
180
            "arch": "ppc64el",
 
181
            "versions": {
 
182
                "19700101": {
 
183
                    "items": {
 
184
                        "1.2-alpha1-utopic-ppc64el": {
 
185
                            "release": "utopic",
 
186
                            "version": "1.2-alpha1",
 
187
                            "arch": "ppc64el",
 
188
                            "size": 9223372036854775807,
 
189
                            "path": "/funkytown.tar.gz",
 
190
                            "ftype": "tar.gz",
 
191
                            "sha256": ""
 
192
                        }
 
193
                    }
 
194
                }
 
195
            }
 
196
        }
 
197
    },
 
198
    "updated": "Thu, 01 Jan 1970 00:00:00 +0000",
 
199
    "format": "products:1.0",
 
200
    "content_id": "com.ubuntu.juju:proposed:tools"
 
201
}`
 
202
 
 
203
var releasedToolMetadataForTesting = []*tools.ToolsMetadata{
 
204
        {
 
205
                Release:  "saucy",
 
206
                Version:  "1.2.3.4",
 
207
                Arch:     "arm",
 
208
                Size:     9223372036854775807,
 
209
                Path:     "/somewhere/over/the/rainbow.tar.gz",
 
210
                FileType: "tar.gz",
 
211
        },
 
212
        {
 
213
                Release:  "precise",
 
214
                Version:  "1.2.3.4",
 
215
                Arch:     "arm",
 
216
                Size:     42,
 
217
                Path:     "toenlightenment.tar.gz",
 
218
                FileType: "tar.gz",
 
219
        },
 
220
        {
 
221
                Release:  "precise",
 
222
                Version:  "4.3.2.1",
 
223
                Arch:     "amd64",
 
224
                Path:     "whatever.tar.gz",
 
225
                FileType: "tar.gz",
 
226
                SHA256:   "afb14e65c794464e378def12cbad6a96f9186d69",
 
227
        },
 
228
        {
 
229
                Release:  "xuanhuaceratops",
 
230
                Version:  "4.3.2.1",
 
231
                Arch:     "amd64",
 
232
                Size:     42,
 
233
                Path:     "dinodance.tar.gz",
 
234
                FileType: "tar.gz",
 
235
        },
 
236
        {
 
237
                Release:  "xuanhanosaurus",
 
238
                Version:  "5.4.3.2",
 
239
                Arch:     "amd64",
 
240
                Size:     42,
 
241
                Path:     "dinodisco.tar.gz",
 
242
                FileType: "tar.gz",
 
243
        },
 
244
}
 
245
 
 
246
var proposedToolMetadataForTesting = []*tools.ToolsMetadata{
 
247
        {
 
248
                Release:  "utopic",
 
249
                Version:  "1.2-alpha1",
 
250
                Arch:     "ppc64el",
 
251
                Size:     9223372036854775807,
 
252
                Path:     "/funkytown.tar.gz",
 
253
                FileType: "tar.gz",
 
254
        },
 
255
        {
 
256
                Release:  "trusty",
 
257
                Version:  "1.2-beta1",
 
258
                Arch:     "arm64",
 
259
                Size:     42,
 
260
                Path:     "gotham.tar.gz",
 
261
                FileType: "tar.gz",
 
262
        },
 
263
        {
 
264
                Release:  "xuanhuaceratops",
 
265
                Version:  "4.3.2.1",
 
266
                Arch:     "amd64",
 
267
                Size:     42,
 
268
                Path:     "dinodance.tar.gz",
 
269
                FileType: "tar.gz",
 
270
        },
 
271
        {
 
272
                Release:  "xuanhanosaurus",
 
273
                Version:  "5.4.3.2",
 
274
                Arch:     "amd64",
 
275
                Size:     42,
 
276
                Path:     "dinodisco.tar.gz",
 
277
                FileType: "tar.gz",
 
278
        },
 
279
}
 
280
 
 
281
func (s *marshalSuite) TestMarshalIndex(c *gc.C) {
 
282
        index, legacyIndex, err := tools.MarshalToolsMetadataIndexJSON(s.streamMetadata, time.Unix(0, 0).UTC())
 
283
        c.Assert(err, jc.ErrorIsNil)
 
284
        assertIndex(c, index, expectedIndex)
 
285
        assertIndex(c, legacyIndex, expectedLegacyIndex)
 
286
}
 
287
 
 
288
func assertIndex(c *gc.C, obtainedIndex []byte, expectedIndex string) {
 
289
        // Unmarshall into objects so an order independent comparison can be done.
 
290
        var obtained interface{}
 
291
        err := json.Unmarshal(obtainedIndex, &obtained)
 
292
        var expected interface{}
 
293
        err = json.Unmarshal([]byte(expectedIndex), &expected)
 
294
        c.Assert(err, jc.ErrorIsNil)
 
295
        c.Assert(obtained, jc.DeepEquals, expected)
 
296
}
 
297
 
 
298
func (s *marshalSuite) TestMarshalProducts(c *gc.C) {
 
299
        products, err := tools.MarshalToolsMetadataProductsJSON(s.streamMetadata, time.Unix(0, 0).UTC())
 
300
        c.Assert(err, jc.ErrorIsNil)
 
301
        assertProducts(c, products)
 
302
}
 
303
 
 
304
func assertProducts(c *gc.C, obtainedProducts map[string][]byte) {
 
305
        c.Assert(obtainedProducts, gc.HasLen, 2)
 
306
        c.Assert(string(obtainedProducts["released"]), gc.Equals, expectedReleasedProducts)
 
307
        c.Assert(string(obtainedProducts["proposed"]), gc.Equals, expectedProposedProducts)
 
308
}
 
309
 
 
310
func (s *marshalSuite) TestMarshal(c *gc.C) {
 
311
        index, legacyIndex, products, err := tools.MarshalToolsMetadataJSON(s.streamMetadata, time.Unix(0, 0).UTC())
 
312
        c.Assert(err, jc.ErrorIsNil)
 
313
        assertIndex(c, index, expectedIndex)
 
314
        assertIndex(c, legacyIndex, expectedLegacyIndex)
 
315
        assertProducts(c, products)
 
316
}
 
317
 
 
318
func (s *marshalSuite) TestMarshalNoReleaseStream(c *gc.C) {
 
319
        metadata := s.streamMetadata
 
320
        delete(metadata, "released")
 
321
        index, legacyIndex, products, err := tools.MarshalToolsMetadataJSON(s.streamMetadata, time.Unix(0, 0).UTC())
 
322
        c.Assert(err, jc.ErrorIsNil)
 
323
        c.Assert(legacyIndex, gc.IsNil)
 
324
        c.Assert(index, gc.NotNil)
 
325
        c.Assert(products, gc.NotNil)
 
326
}