-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagemetadata_test.go
133 lines (119 loc) · 4.96 KB
/
imagemetadata_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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package environs_test
import (
"github.com/juju/errors"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils"
gc "gopkg.in/check.v1"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/bootstrap"
"github.com/juju/juju/environs/imagemetadata"
"github.com/juju/juju/environs/simplestreams"
sstesting "github.com/juju/juju/environs/simplestreams/testing"
envtesting "github.com/juju/juju/environs/testing"
"github.com/juju/juju/juju/keys"
"github.com/juju/juju/jujuclient/jujuclienttesting"
"github.com/juju/juju/provider/dummy"
"github.com/juju/juju/testing"
coretesting "github.com/juju/juju/testing"
)
type ImageMetadataSuite struct {
coretesting.BaseSuite
}
var _ = gc.Suite(&ImageMetadataSuite{})
func (s *ImageMetadataSuite) TearDownTest(c *gc.C) {
dummy.Reset(c)
s.BaseSuite.TearDownTest(c)
}
func (s *ImageMetadataSuite) env(c *gc.C, imageMetadataURL, stream string) environs.Environ {
attrs := dummy.SampleConfig()
if stream != "" {
attrs = attrs.Merge(testing.Attrs{
"image-stream": stream,
})
}
if imageMetadataURL != "" {
attrs = attrs.Merge(testing.Attrs{
"image-metadata-url": imageMetadataURL,
})
}
env, err := bootstrap.Prepare(
envtesting.BootstrapContext(c),
jujuclienttesting.NewMemStore(),
bootstrap.PrepareParams{
ControllerConfig: testing.FakeControllerConfig(),
ControllerName: attrs["name"].(string),
ModelConfig: attrs,
Cloud: dummy.SampleCloudSpec(),
AdminSecret: "admin-secret",
},
)
c.Assert(err, jc.ErrorIsNil)
return env
}
func (s *ImageMetadataSuite) TestImageMetadataURLsNoConfigURL(c *gc.C) {
env := s.env(c, "", "")
sources, err := environs.ImageMetadataSources(env)
c.Assert(err, jc.ErrorIsNil)
sstesting.AssertExpectedSources(c, sources, []sstesting.SourceDetails{
{"https://streams.canonical.com/juju/images/releases/", keys.JujuPublicKey},
{"http://cloud-images.ubuntu.com/releases/", imagemetadata.SimplestreamsImagesPublicKey},
})
}
func (s *ImageMetadataSuite) TestImageMetadataURLs(c *gc.C) {
env := s.env(c, "config-image-metadata-url", "")
sources, err := environs.ImageMetadataSources(env)
c.Assert(err, jc.ErrorIsNil)
sstesting.AssertExpectedSources(c, sources, []sstesting.SourceDetails{
{"config-image-metadata-url/", ""},
{"https://streams.canonical.com/juju/images/releases/", keys.JujuPublicKey},
{"http://cloud-images.ubuntu.com/releases/", imagemetadata.SimplestreamsImagesPublicKey},
})
}
func (s *ImageMetadataSuite) TestImageMetadataURLsRegisteredFuncs(c *gc.C) {
environs.RegisterImageDataSourceFunc("id0", func(environs.Environ) (simplestreams.DataSource, error) {
return simplestreams.NewURLDataSource("id0", "betwixt/releases", utils.NoVerifySSLHostnames, simplestreams.DEFAULT_CLOUD_DATA, false), nil
})
environs.RegisterImageDataSourceFunc("id1", func(environs.Environ) (simplestreams.DataSource, error) {
return simplestreams.NewURLDataSource("id1", "yoink", utils.NoVerifySSLHostnames, simplestreams.SPECIFIC_CLOUD_DATA, false), nil
})
// overwrite the one previously registered against id1
environs.RegisterImageDataSourceFunc("id1", func(environs.Environ) (simplestreams.DataSource, error) {
return nil, errors.NewNotSupported(nil, "oyvey")
})
environs.RegisterUserImageDataSourceFunc("id2", func(environs.Environ) (simplestreams.DataSource, error) {
return simplestreams.NewURLDataSource("id2", "foobar", utils.NoVerifySSLHostnames, simplestreams.CUSTOM_CLOUD_DATA, false), nil
})
defer environs.UnregisterImageDataSourceFunc("id0")
defer environs.UnregisterImageDataSourceFunc("id1")
defer environs.UnregisterImageDataSourceFunc("id2")
env := s.env(c, "config-image-metadata-url", "")
sources, err := environs.ImageMetadataSources(env)
c.Assert(err, jc.ErrorIsNil)
sstesting.AssertExpectedSources(c, sources, []sstesting.SourceDetails{
{"config-image-metadata-url/", ""},
{"foobar/", ""},
{"betwixt/releases/", ""},
{"https://streams.canonical.com/juju/images/releases/", keys.JujuPublicKey},
{"http://cloud-images.ubuntu.com/releases/", imagemetadata.SimplestreamsImagesPublicKey},
})
}
func (s *ImageMetadataSuite) TestImageMetadataURLsRegisteredFuncsError(c *gc.C) {
environs.RegisterImageDataSourceFunc("id0", func(environs.Environ) (simplestreams.DataSource, error) {
return nil, errors.New("oyvey!")
})
defer environs.UnregisterImageDataSourceFunc("id0")
env := s.env(c, "config-image-metadata-url", "")
_, err := environs.ImageMetadataSources(env)
c.Assert(err, gc.ErrorMatches, "oyvey!")
}
func (s *ImageMetadataSuite) TestImageMetadataURLsNonReleaseStream(c *gc.C) {
env := s.env(c, "", "daily")
sources, err := environs.ImageMetadataSources(env)
c.Assert(err, jc.ErrorIsNil)
sstesting.AssertExpectedSources(c, sources, []sstesting.SourceDetails{
{"https://streams.canonical.com/juju/images/daily/", keys.JujuPublicKey},
{"http://cloud-images.ubuntu.com/daily/", imagemetadata.SimplestreamsImagesPublicKey},
})
}