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