github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/plugins/juju-metadata/validatetoolsmetadata_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "strings" 8 9 "github.com/juju/cmd" 10 jc "github.com/juju/testing/checkers" 11 "github.com/juju/utils/arch" 12 "gopkg.in/amz.v3/aws" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/cmd/modelcmd" 16 "github.com/juju/juju/environs/filestorage" 17 "github.com/juju/juju/environs/tools" 18 "github.com/juju/juju/jujuclient" 19 "github.com/juju/juju/jujuclient/jujuclienttesting" 20 coretesting "github.com/juju/juju/testing" 21 jujuversion "github.com/juju/juju/version" 22 ) 23 24 type ValidateToolsMetadataSuite struct { 25 coretesting.FakeJujuXDGDataHomeSuite 26 metadataDir string 27 store *jujuclienttesting.MemStore 28 } 29 30 var _ = gc.Suite(&ValidateToolsMetadataSuite{}) 31 32 func runValidateToolsMetadata(c *gc.C, store jujuclient.ClientStore, args ...string) (*cmd.Context, error) { 33 cmd := &validateToolsMetadataCommand{} 34 cmd.SetClientStore(store) 35 return coretesting.RunCommand(c, modelcmd.Wrap(cmd), args...) 36 } 37 38 var validateInitToolsErrorTests = []struct { 39 args []string 40 err string 41 }{ 42 { 43 args: []string{"-p", "ec2", "-s", "series", "-d", "dir"}, 44 err: `region required if provider type is specified`, 45 }, { 46 args: []string{"-p", "ec2", "-s", "series", "-r", "region"}, 47 err: `metadata directory required if provider type is specified`, 48 }, { 49 args: []string{"-s", "series", "-r", "region", "--majorminor-version", "x"}, 50 err: `invalid major version number x: .*`, 51 }, { 52 args: []string{"-s", "series", "-r", "region", "--majorminor-version", "2.x"}, 53 err: `invalid minor version number x: .*`, 54 }, { 55 args: []string{"-s", "series", "-r", "region", "--majorminor-version", "2.2.1"}, 56 err: `invalid major.minor version number 2.2.1`, 57 }, 58 } 59 60 func (s *ValidateToolsMetadataSuite) TestInitErrors(c *gc.C) { 61 for i, t := range validateInitToolsErrorTests { 62 c.Logf("test %d", i) 63 cmd := &validateToolsMetadataCommand{} 64 cmd.SetClientStore(s.store) 65 err := coretesting.InitCommand(modelcmd.Wrap(cmd), t.args) 66 c.Check(err, gc.ErrorMatches, t.err) 67 } 68 } 69 70 func (s *ValidateToolsMetadataSuite) TestInvalidProviderError(c *gc.C) { 71 _, err := runValidateToolsMetadata(c, s.store, "-p", "foo", "-s", "series", "-r", "region", "-d", "dir") 72 c.Check(err, gc.ErrorMatches, `no registered provider for "foo"`) 73 } 74 75 func (s *ValidateToolsMetadataSuite) TestUnsupportedProviderError(c *gc.C) { 76 _, err := runValidateToolsMetadata(c, s.store, "-p", "maas", "-s", "series", "-r", "region", "-d", "dir") 77 c.Check(err, gc.ErrorMatches, `maas provider does not support tools metadata validation`) 78 } 79 80 func (s *ValidateToolsMetadataSuite) makeLocalMetadata(c *gc.C, stream, version, region, series, endpoint string) error { 81 tm := []*tools.ToolsMetadata{{ 82 Version: version, 83 Arch: arch.HostArch(), 84 Release: series, 85 }} 86 targetStorage, err := filestorage.NewFileStorageWriter(s.metadataDir) 87 c.Assert(err, jc.ErrorIsNil) 88 streamMetadata := map[string][]*tools.ToolsMetadata{ 89 stream: tm, 90 } 91 err = tools.WriteMetadata(targetStorage, streamMetadata, []string{stream}, false) 92 if err != nil { 93 return err 94 } 95 return nil 96 } 97 98 func (s *ValidateToolsMetadataSuite) SetUpTest(c *gc.C) { 99 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 100 s.metadataDir = c.MkDir() 101 102 s.store = jujuclienttesting.NewMemStore() 103 cacheTestEnvConfig(c, s.store) 104 105 s.PatchEnvironment("AWS_ACCESS_KEY_ID", "access") 106 s.PatchEnvironment("AWS_SECRET_ACCESS_KEY", "secret") 107 // All of the following are recognized as fallbacks by goamz. 108 s.PatchEnvironment("AWS_ACCESS_KEY", "") 109 s.PatchEnvironment("AWS_SECRET_KEY", "") 110 s.PatchEnvironment("EC2_ACCESS_KEY", "") 111 s.PatchEnvironment("EC2_SECRET_KEY", "") 112 } 113 114 func (s *ValidateToolsMetadataSuite) setupEc2LocalMetadata(c *gc.C, region string) { 115 ec2Region, ok := aws.Regions[region] 116 if !ok { 117 c.Fatalf("unknown ec2 region %q", region) 118 } 119 endpoint := ec2Region.EC2Endpoint 120 s.makeLocalMetadata(c, "released", "1.11.4", region, "precise", endpoint) 121 } 122 123 func (s *ValidateToolsMetadataSuite) TestEc2LocalMetadataUsingEnvironment(c *gc.C) { 124 s.setupEc2LocalMetadata(c, "us-east-1") 125 ctx, err := runValidateToolsMetadata(c, s.store, "-m", "ec2-controller:ec2", "-j", "1.11.4", "-d", s.metadataDir) 126 c.Assert(err, jc.ErrorIsNil) 127 errOut := coretesting.Stdout(ctx) 128 strippedOut := strings.Replace(errOut, "\n", "", -1) 129 c.Assert(strippedOut, gc.Matches, `Matching Tools Versions:.*Resolve Metadata.*`) 130 } 131 132 func (s *ValidateToolsMetadataSuite) TestEc2LocalMetadataUsingIncompleteEnvironment(c *gc.C) { 133 // We already unset the other fallbacks recognized by goamz in SetUpTest(). 134 s.PatchEnvironment("AWS_ACCESS_KEY_ID", "") 135 s.PatchEnvironment("AWS_SECRET_ACCESS_KEY", "") 136 s.setupEc2LocalMetadata(c, "us-east-1") 137 _, err := runValidateToolsMetadata(c, s.store, "-m", "ec2-controller:ec2", "-j", "1.11.4") 138 c.Assert(err, gc.ErrorMatches, `detecting credentials.*AWS_SECRET_ACCESS_KEY not found in environment`) 139 } 140 141 func (s *ValidateToolsMetadataSuite) TestEc2LocalMetadataWithManualParams(c *gc.C) { 142 s.setupEc2LocalMetadata(c, "us-west-1") 143 ctx, err := runValidateToolsMetadata(c, s.store, 144 "-p", "ec2", "-s", "precise", "-r", "us-west-1", "-j", "1.11.4", 145 "-u", "https://ec2.us-west-1.amazonaws.com", "-d", s.metadataDir, 146 ) 147 c.Assert(err, jc.ErrorIsNil) 148 errOut := coretesting.Stdout(ctx) 149 strippedOut := strings.Replace(errOut, "\n", "", -1) 150 c.Check(strippedOut, gc.Matches, `Matching Tools Versions:.*Resolve Metadata.*`) 151 } 152 153 func (s *ValidateToolsMetadataSuite) TestEc2LocalMetadataNoMatch(c *gc.C) { 154 s.setupEc2LocalMetadata(c, "us-east-1") 155 _, err := runValidateToolsMetadata(c, s.store, 156 "-p", "ec2", "-s", "raring", "-r", "us-west-1", 157 "-u", "https://ec2.us-west-1.amazonaws.com", "-d", s.metadataDir, 158 ) 159 c.Assert(err, gc.ErrorMatches, "no matching tools(.|\n)*Resolve Metadata(.|\n)*") 160 _, err = runValidateToolsMetadata(c, s.store, 161 "-p", "ec2", "-s", "precise", "-r", "region", 162 "-u", "https://ec2.region.amazonaws.com", "-d", s.metadataDir, 163 ) 164 c.Assert(err, gc.ErrorMatches, `unknown region "region"`) 165 } 166 167 func (s *ValidateToolsMetadataSuite) TestOpenstackLocalMetadataWithManualParams(c *gc.C) { 168 s.makeLocalMetadata(c, "released", "1.11.4", "region-2", "raring", "some-auth-url") 169 ctx, err := runValidateToolsMetadata(c, s.store, 170 "-p", "openstack", "-s", "raring", "-r", "region-2", "-j", "1.11.4", 171 "-u", "some-auth-url", "-d", s.metadataDir, 172 ) 173 c.Assert(err, jc.ErrorIsNil) 174 errOut := coretesting.Stdout(ctx) 175 strippedOut := strings.Replace(errOut, "\n", "", -1) 176 c.Check(strippedOut, gc.Matches, `Matching Tools Versions:.*Resolve Metadata.*`) 177 } 178 179 func (s *ValidateToolsMetadataSuite) TestOpenstackLocalMetadataNoMatch(c *gc.C) { 180 s.makeLocalMetadata(c, "released", "1.11.4", "region-2", "raring", "some-auth-url") 181 _, err := runValidateToolsMetadata(c, s.store, 182 "-p", "openstack", "-s", "precise", "-r", "region-2", 183 "-u", "some-auth-url", "-d", s.metadataDir, 184 ) 185 c.Assert(err, gc.ErrorMatches, "no matching tools(.|\n)*Resolve Metadata(.|\n)*") 186 _, err = runValidateToolsMetadata(c, s.store, 187 "-p", "openstack", "-s", "raring", "-r", "region-3", 188 "-u", "some-auth-url", "-d", s.metadataDir, 189 ) 190 c.Assert(err, gc.ErrorMatches, "no matching tools(.|\n)*Resolve Metadata(.|\n)*") 191 } 192 193 func (s *ValidateToolsMetadataSuite) TestDefaultVersion(c *gc.C) { 194 s.makeLocalMetadata(c, "released", jujuversion.Current.String(), "region-2", "raring", "some-auth-url") 195 ctx, err := runValidateToolsMetadata(c, s.store, 196 "-p", "openstack", "-s", "raring", "-r", "region-2", 197 "-u", "some-auth-url", "-d", s.metadataDir, 198 ) 199 c.Assert(err, jc.ErrorIsNil) 200 errOut := coretesting.Stdout(ctx) 201 strippedOut := strings.Replace(errOut, "\n", "", -1) 202 c.Check(strippedOut, gc.Matches, `Matching Tools Versions:.*Resolve Metadata.*`) 203 } 204 205 func (s *ValidateToolsMetadataSuite) TestStream(c *gc.C) { 206 s.makeLocalMetadata(c, "proposed", jujuversion.Current.String(), "region-2", "raring", "some-auth-url") 207 ctx, err := runValidateToolsMetadata(c, s.store, 208 "-p", "openstack", "-s", "raring", "-r", "region-2", 209 "-u", "some-auth-url", "-d", s.metadataDir, "--stream", "proposed", 210 ) 211 c.Assert(err, jc.ErrorIsNil) 212 errOut := coretesting.Stdout(ctx) 213 strippedOut := strings.Replace(errOut, "\n", "", -1) 214 c.Check(strippedOut, gc.Matches, `Matching Tools Versions:.*Resolve Metadata.*`) 215 } 216 217 func (s *ValidateToolsMetadataSuite) TestMajorVersionMatch(c *gc.C) { 218 s.makeLocalMetadata(c, "released", "1.11.4", "region-2", "raring", "some-auth-url") 219 ctx, err := runValidateToolsMetadata(c, s.store, 220 "-p", "openstack", "-s", "raring", "-r", "region-2", 221 "-u", "some-auth-url", "-d", s.metadataDir, "--majorminor-version", "1", 222 ) 223 c.Assert(err, jc.ErrorIsNil) 224 errOut := coretesting.Stdout(ctx) 225 strippedOut := strings.Replace(errOut, "\n", "", -1) 226 c.Check(strippedOut, gc.Matches, `Matching Tools Versions:.*Resolve Metadata.*`) 227 } 228 229 func (s *ValidateToolsMetadataSuite) TestMajorMinorVersionMatch(c *gc.C) { 230 s.makeLocalMetadata(c, "released", "1.12.1", "region-2", "raring", "some-auth-url") 231 ctx, err := runValidateToolsMetadata(c, s.store, 232 "-p", "openstack", "-s", "raring", "-r", "region-2", 233 "-u", "some-auth-url", "-d", s.metadataDir, "--majorminor-version", "1.12", 234 ) 235 c.Assert(err, jc.ErrorIsNil) 236 errOut := coretesting.Stdout(ctx) 237 strippedOut := strings.Replace(errOut, "\n", "", -1) 238 c.Check(strippedOut, gc.Matches, `Matching Tools Versions:.*Resolve Metadata.*`) 239 } 240 241 func (s *ValidateToolsMetadataSuite) TestJustDirectory(c *gc.C) { 242 s.makeLocalMetadata(c, "released", jujuversion.Current.String(), "region-2", "raring", "some-auth-url") 243 ctx, err := runValidateToolsMetadata(c, s.store, 244 "-s", "raring", "-d", s.metadataDir, 245 ) 246 c.Assert(err, jc.ErrorIsNil) 247 errOut := coretesting.Stdout(ctx) 248 strippedOut := strings.Replace(errOut, "\n", "", -1) 249 c.Check(strippedOut, gc.Matches, `Matching Tools Versions:.*Resolve Metadata.*`) 250 }