github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/environs/tools/urls_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package tools_test 5 6 import ( 7 "strings" 8 9 jc "github.com/juju/testing/checkers" 10 gc "launchpad.net/gocheck" 11 12 "github.com/juju/juju/environs" 13 "github.com/juju/juju/environs/config" 14 "github.com/juju/juju/environs/configstore" 15 sstesting "github.com/juju/juju/environs/simplestreams/testing" 16 "github.com/juju/juju/environs/storage" 17 "github.com/juju/juju/environs/tools" 18 "github.com/juju/juju/provider/dummy" 19 "github.com/juju/juju/testing" 20 coretesting "github.com/juju/juju/testing" 21 ) 22 23 type URLsSuite struct { 24 coretesting.BaseSuite 25 } 26 27 var _ = gc.Suite(&URLsSuite{}) 28 29 func (s *URLsSuite) TearDownTest(c *gc.C) { 30 dummy.Reset() 31 s.BaseSuite.TearDownTest(c) 32 } 33 34 func (s *URLsSuite) env(c *gc.C, toolsMetadataURL string) environs.Environ { 35 attrs := dummy.SampleConfig() 36 if toolsMetadataURL != "" { 37 attrs = attrs.Merge(testing.Attrs{ 38 "tools-metadata-url": toolsMetadataURL, 39 }) 40 } 41 cfg, err := config.New(config.NoDefaults, attrs) 42 c.Assert(err, gc.IsNil) 43 env, err := environs.Prepare(cfg, testing.Context(c), configstore.NewMem()) 44 c.Assert(err, gc.IsNil) 45 return env 46 } 47 48 func (s *URLsSuite) TestToolsURLsNoConfigURL(c *gc.C) { 49 env := s.env(c, "") 50 sources, err := tools.GetMetadataSources(env) 51 c.Assert(err, gc.IsNil) 52 // Put a file in tools since the dummy storage provider requires a 53 // file to exist before the URL can be found. This is to ensure it behaves 54 // the same way as MAAS. 55 err = env.Storage().Put("tools/dummy", strings.NewReader("dummy"), 5) 56 c.Assert(err, gc.IsNil) 57 privateStorageURL, err := env.Storage().URL("tools") 58 c.Assert(err, gc.IsNil) 59 sstesting.AssertExpectedSources(c, sources, []string{ 60 privateStorageURL, "https://streams.canonical.com/juju/tools/"}) 61 } 62 63 func (s *URLsSuite) TestToolsSources(c *gc.C) { 64 env := s.env(c, "config-tools-metadata-url") 65 sources, err := tools.GetMetadataSources(env) 66 c.Assert(err, gc.IsNil) 67 // Put a file in tools since the dummy storage provider requires a 68 // file to exist before the URL can be found. This is to ensure it behaves 69 // the same way as MAAS. 70 err = env.Storage().Put("tools/dummy", strings.NewReader("dummy"), 5) 71 c.Assert(err, gc.IsNil) 72 privateStorageURL, err := env.Storage().URL("tools") 73 c.Assert(err, gc.IsNil) 74 sstesting.AssertExpectedSources(c, sources, []string{ 75 "config-tools-metadata-url/", privateStorageURL, "https://streams.canonical.com/juju/tools/"}) 76 haveExpectedSources := false 77 for _, source := range sources { 78 if allowRetry, ok := storage.TestingGetAllowRetry(source); ok { 79 haveExpectedSources = true 80 c.Assert(allowRetry, jc.IsFalse) 81 } 82 } 83 c.Assert(haveExpectedSources, jc.IsTrue) 84 } 85 86 func (s *URLsSuite) TestToolsSourcesWithRetry(c *gc.C) { 87 env := s.env(c, "") 88 sources, err := tools.GetMetadataSourcesWithRetries(env, true) 89 c.Assert(err, gc.IsNil) 90 haveExpectedSources := false 91 for _, source := range sources { 92 if allowRetry, ok := storage.TestingGetAllowRetry(source); ok { 93 haveExpectedSources = true 94 c.Assert(allowRetry, jc.IsTrue) 95 } 96 } 97 c.Assert(haveExpectedSources, jc.IsTrue) 98 c.Assert(haveExpectedSources, jc.IsTrue) 99 } 100 101 func (s *URLsSuite) TestToolsURL(c *gc.C) { 102 for source, expected := range map[string]string{ 103 "": "", 104 "foo": "file://foo/tools", 105 "/home/foo": "file:///home/foo/tools", 106 "file://foo": "file://foo", 107 "http://foo": "http://foo", 108 } { 109 URL, err := tools.ToolsURL(source) 110 c.Assert(err, gc.IsNil) 111 c.Assert(URL, gc.Equals, expected) 112 } 113 }