forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagemetadata_test.go
118 lines (104 loc) · 4 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
// 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/config"
"github.com/juju/juju/environs/configstore"
"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/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()
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,
})
}
cfg, err := config.New(config.NoDefaults, attrs)
c.Assert(err, jc.ErrorIsNil)
env, err := environs.Prepare(cfg, envtesting.BootstrapContext(c), configstore.NewMem())
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, []string{
"http://cloud-images.ubuntu.com/releases/",
})
}
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, []string{
"config-image-metadata-url/", "http://cloud-images.ubuntu.com/releases/",
})
}
func (s *ImageMetadataSuite) TestImageMetadataURLsRegisteredFuncs(c *gc.C) {
environs.RegisterImageDataSourceFunc("id0", func(environs.Environ) (simplestreams.DataSource, error) {
return simplestreams.NewURLDataSource("id0", "betwixt/releases", utils.NoVerifySSLHostnames), nil
})
environs.RegisterImageDataSourceFunc("id1", func(environs.Environ) (simplestreams.DataSource, error) {
return simplestreams.NewURLDataSource("id1", "yoink", utils.NoVerifySSLHostnames), 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), 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, []string{
"config-image-metadata-url/",
"foobar/",
"betwixt/releases/",
"http://cloud-images.ubuntu.com/releases/",
})
}
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, []string{
"http://cloud-images.ubuntu.com/daily/",
})
}