github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/plugins/juju-metadata/addimage_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "fmt" 8 "regexp" 9 10 "github.com/juju/cmd" 11 "github.com/juju/errors" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/apiserver/params" 16 "github.com/juju/juju/testing" 17 ) 18 19 type addImageSuite struct { 20 BaseCloudImageMetadataSuite 21 22 data []params.CloudImageMetadata 23 24 mockAPI *mockAddAPI 25 } 26 27 var _ = gc.Suite(&addImageSuite{}) 28 29 var emptyMetadata = []params.CloudImageMetadata{} 30 31 func (s *addImageSuite) SetUpTest(c *gc.C) { 32 s.BaseCloudImageMetadataSuite.SetUpTest(c) 33 34 s.data = emptyMetadata 35 36 s.mockAPI = &mockAddAPI{} 37 s.mockAPI.add = func(metadata []params.CloudImageMetadata) error { 38 s.data = append(s.data, metadata...) 39 return nil 40 } 41 s.PatchValue(&getImageMetadataAddAPI, func(c *addImageMetadataCommand) (MetadataAddAPI, error) { 42 return s.mockAPI, nil 43 }) 44 } 45 46 func (s *addImageSuite) TestAddImageMetadata(c *gc.C) { 47 s.assertValidAddImageMetadata(c, constructTestImageMetadata()) 48 } 49 50 func (s *addImageSuite) TestAddImageMetadataWithStream(c *gc.C) { 51 m := constructTestImageMetadata() 52 m.Stream = "streamV" 53 s.assertValidAddImageMetadata(c, m) 54 } 55 56 func (s *addImageSuite) TestAddImageMetadataWithRegion(c *gc.C) { 57 m := constructTestImageMetadata() 58 m.Region = "region" 59 s.assertValidAddImageMetadata(c, m) 60 } 61 62 func (s *addImageSuite) TestAddImageMetadataAWS(c *gc.C) { 63 m := constructTestImageMetadata() 64 m.VirtType = "vType" 65 m.RootStorageType = "sType" 66 s.assertValidAddImageMetadata(c, m) 67 } 68 69 func (s *addImageSuite) TestAddImageMetadataAWSWithSize(c *gc.C) { 70 m := constructTestImageMetadata() 71 m.VirtType = "vType" 72 m.RootStorageType = "sType" 73 74 size := uint64(100) 75 m.RootStorageSize = &size 76 77 s.assertValidAddImageMetadata(c, m) 78 } 79 80 func (s *addImageSuite) TestAddImageMetadataFailed(c *gc.C) { 81 msg := "failed" 82 s.mockAPI.add = func(metadata []params.CloudImageMetadata) error { 83 return errors.New(msg) 84 } 85 86 s.assertAddImageMetadataErr(c, constructTestImageMetadata(), msg) 87 } 88 89 func (s *addImageSuite) TestAddImageMetadataNoImageId(c *gc.C) { 90 m := constructTestImageMetadata() 91 m.ImageId = "" 92 93 s.assertAddImageMetadataErr(c, m, "image id must be supplied when adding image metadata") 94 } 95 96 func (s *addImageSuite) TestAddImageMetadataNoSeries(c *gc.C) { 97 m := constructTestImageMetadata() 98 m.Series = "" 99 // Series will default to config default, for e.g. "trusty" 100 s.assertValidAddImageMetadata(c, m) 101 } 102 103 func (s *addImageSuite) TestAddImageMetadataInvalidSeries(c *gc.C) { 104 m := constructTestImageMetadata() 105 m.Series = "blah" 106 107 s.assertAddImageMetadataErr(c, m, regexp.QuoteMeta(`unknown version for series: "blah"`)) 108 } 109 110 func (s *addImageSuite) TestAddImageMetadataNoArch(c *gc.C) { 111 m := constructTestImageMetadata() 112 m.Arch = "" 113 114 s.assertValidAddImageMetadata(c, m) 115 } 116 117 func (s *addImageSuite) assertValidAddImageMetadata(c *gc.C, m params.CloudImageMetadata) { 118 args := getAddImageMetadataCmdFlags(c, m) 119 120 _, err := runAddImageMetadata(c, args...) 121 c.Assert(err, jc.ErrorIsNil) 122 123 // Need to make sure that defaults are populated 124 if m.Arch == "" { 125 m.Arch = "amd64" 126 } 127 if m.Stream == "" { 128 m.Stream = "released" 129 } 130 131 c.Assert(s.data, gc.DeepEquals, []params.CloudImageMetadata{m}) 132 } 133 134 func runAddImageMetadata(c *gc.C, args ...string) (*cmd.Context, error) { 135 return testing.RunCommand(c, newAddImageMetadataCommand(), args...) 136 } 137 138 func (s *addImageSuite) assertAddImageMetadataErr(c *gc.C, m params.CloudImageMetadata, msg string) { 139 args := getAddImageMetadataCmdFlags(c, m) 140 _, err := runAddImageMetadata(c, args...) 141 c.Assert(err, gc.ErrorMatches, fmt.Sprintf(".*%v.*", msg)) 142 c.Assert(s.data, gc.DeepEquals, emptyMetadata) 143 } 144 145 func constructTestImageMetadata() params.CloudImageMetadata { 146 return params.CloudImageMetadata{ 147 ImageId: "im-33333", 148 Series: "trusty", 149 Arch: "arch", 150 Source: "custom", 151 } 152 } 153 154 func getAddImageMetadataCmdFlags(c *gc.C, data params.CloudImageMetadata) []string { 155 args := []string{} 156 157 addFlag := func(flag, value, defaultValue string) { 158 if value != "" { 159 args = append(args, flag, value) 160 } else { 161 if defaultValue != "" { 162 args = append(args, flag, defaultValue) 163 } 164 } 165 } 166 167 addFlag("--series", data.Series, "") 168 addFlag("--region", data.Region, "") 169 addFlag("--arch", data.Arch, "amd64") 170 addFlag("--virt-type", data.VirtType, "") 171 addFlag("--storage-type", data.RootStorageType, "") 172 addFlag("--stream", data.Stream, "released") 173 174 if data.RootStorageSize != nil { 175 args = append(args, "--storage-size", fmt.Sprintf("%d", *data.RootStorageSize)) 176 } 177 178 // image id is an argument 179 if data.ImageId != "" { 180 args = append(args, data.ImageId) 181 } 182 return args 183 } 184 185 type mockAddAPI struct { 186 add func(metadata []params.CloudImageMetadata) error 187 } 188 189 func (s mockAddAPI) Close() error { 190 return nil 191 } 192 193 func (s mockAddAPI) Save(metadata []params.CloudImageMetadata) error { 194 return s.add(metadata) 195 }