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