github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/environs/tools/urls_test.go (about) 1 // Copyright 2012, 2013, 2014 Canonical Ltd. 2 // Copyright 2014 Cloudbase Solutions SRL 3 // Licensed under the AGPLv3, see LICENCE file for details. 4 5 package tools_test 6 7 import ( 8 "fmt" 9 10 "github.com/juju/errors" 11 jc "github.com/juju/testing/checkers" 12 "github.com/juju/utils" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/environs" 16 "github.com/juju/juju/environs/config" 17 "github.com/juju/juju/environs/configstore" 18 "github.com/juju/juju/environs/simplestreams" 19 sstesting "github.com/juju/juju/environs/simplestreams/testing" 20 envtesting "github.com/juju/juju/environs/testing" 21 "github.com/juju/juju/environs/tools" 22 "github.com/juju/juju/provider/dummy" 23 "github.com/juju/juju/testing" 24 coretesting "github.com/juju/juju/testing" 25 ) 26 27 type URLsSuite struct { 28 coretesting.BaseSuite 29 } 30 31 var _ = gc.Suite(&URLsSuite{}) 32 33 func (s *URLsSuite) TearDownTest(c *gc.C) { 34 dummy.Reset() 35 36 s.BaseSuite.TearDownTest(c) 37 } 38 39 func (s *URLsSuite) env(c *gc.C, toolsMetadataURL string) environs.Environ { 40 attrs := dummy.SampleConfig() 41 if toolsMetadataURL != "" { 42 attrs = attrs.Merge(testing.Attrs{ 43 "agent-metadata-url": toolsMetadataURL, 44 }) 45 } 46 cfg, err := config.New(config.NoDefaults, attrs) 47 c.Assert(err, jc.ErrorIsNil) 48 env, err := environs.Prepare(cfg, envtesting.BootstrapContext(c), configstore.NewMem()) 49 c.Assert(err, jc.ErrorIsNil) 50 return env 51 } 52 53 func (s *URLsSuite) TestToolsURLsNoConfigURL(c *gc.C) { 54 env := s.env(c, "") 55 sources, err := tools.GetMetadataSources(env) 56 c.Assert(err, jc.ErrorIsNil) 57 sstesting.AssertExpectedSources(c, sources, []string{"https://streams.canonical.com/juju/tools/"}) 58 } 59 60 func (s *URLsSuite) TestToolsSources(c *gc.C) { 61 env := s.env(c, "config-tools-metadata-url") 62 sources, err := tools.GetMetadataSources(env) 63 c.Assert(err, jc.ErrorIsNil) 64 sstesting.AssertExpectedSources(c, sources, []string{ 65 "config-tools-metadata-url/", "https://streams.canonical.com/juju/tools/"}) 66 } 67 68 func (s *URLsSuite) TestToolsMetadataURLsRegisteredFuncs(c *gc.C) { 69 tools.RegisterToolsDataSourceFunc("id0", func(environs.Environ) (simplestreams.DataSource, error) { 70 return simplestreams.NewURLDataSource("id0", "betwixt/releases", utils.NoVerifySSLHostnames), nil 71 }) 72 tools.RegisterToolsDataSourceFunc("id1", func(environs.Environ) (simplestreams.DataSource, error) { 73 return simplestreams.NewURLDataSource("id1", "yoink", utils.NoVerifySSLHostnames), nil 74 }) 75 // overwrite the one previously registered against id1 76 tools.RegisterToolsDataSourceFunc("id1", func(environs.Environ) (simplestreams.DataSource, error) { 77 // NotSupported errors do not cause GetMetadataSources to fail, 78 // they just cause the datasource function to be ignored. 79 return nil, errors.NewNotSupported(nil, "oyvey") 80 }) 81 defer tools.UnregisterToolsDataSourceFunc("id0") 82 defer tools.UnregisterToolsDataSourceFunc("id1") 83 84 env := s.env(c, "config-tools-metadata-url") 85 sources, err := tools.GetMetadataSources(env) 86 c.Assert(err, jc.ErrorIsNil) 87 sstesting.AssertExpectedSources(c, sources, []string{ 88 "config-tools-metadata-url/", 89 "betwixt/releases/", 90 "https://streams.canonical.com/juju/tools/", 91 }) 92 } 93 94 func (s *URLsSuite) TestToolsMetadataURLsRegisteredFuncsError(c *gc.C) { 95 tools.RegisterToolsDataSourceFunc("id0", func(environs.Environ) (simplestreams.DataSource, error) { 96 // Non-NotSupported errors cause GetMetadataSources to fail. 97 return nil, errors.New("oyvey!") 98 }) 99 defer tools.UnregisterToolsDataSourceFunc("id0") 100 101 env := s.env(c, "config-tools-metadata-url") 102 _, err := tools.GetMetadataSources(env) 103 c.Assert(err, gc.ErrorMatches, "oyvey!") 104 } 105 106 func (s *URLsSuite) TestToolsURL(c *gc.C) { 107 var toolsTests = []struct { 108 in string 109 expected string 110 expectedErr error 111 }{{ 112 in: "", 113 expected: "", 114 expectedErr: nil, 115 }, { 116 in: "file://foo", 117 expected: "file://foo", 118 expectedErr: nil, 119 }, { 120 in: "http://foo", 121 expected: "http://foo", 122 expectedErr: nil, 123 }, { 124 in: "foo", 125 expected: "", 126 expectedErr: fmt.Errorf("foo is not an absolute path"), 127 }} 128 toolsTests = append(toolsTests, toolsTestsPlatformSpecific...) 129 for i, t := range toolsTests { 130 c.Logf("Test %d:", i) 131 132 out, err := tools.ToolsURL(t.in) 133 c.Assert(err, gc.DeepEquals, t.expectedErr) 134 c.Assert(out, gc.Equals, t.expected) 135 } 136 }