github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/plugins/juju-metadata/deleteimage_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "github.com/juju/errors" 8 gitjujutesting "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/cmd/modelcmd" 13 "github.com/juju/juju/testing" 14 ) 15 16 const deleteTestId = "tst12345" 17 18 type deleteImageSuite struct { 19 BaseCloudImageMetadataSuite 20 21 mockAPI *mockDeleteAPI 22 23 deletedIds []string 24 } 25 26 var _ = gc.Suite(&deleteImageSuite{}) 27 28 func (s *deleteImageSuite) SetUpTest(c *gc.C) { 29 s.BaseCloudImageMetadataSuite.SetUpTest(c) 30 31 s.deletedIds = []string{} 32 s.mockAPI = &mockDeleteAPI{ 33 delete: func(imageId string) error { 34 s.deletedIds = append(s.deletedIds, imageId) 35 return nil 36 }, 37 Stub: &gitjujutesting.Stub{}, 38 } 39 } 40 41 func (s *deleteImageSuite) TestDeleteImageMetadata(c *gc.C) { 42 s.assertDeleteImageMetadata(c, deleteTestId) 43 } 44 45 func (s *deleteImageSuite) TestDeleteImageMetadataNoImageId(c *gc.C) { 46 s.assertDeleteImageMetadataErr(c, "image ID must be supplied when deleting image metadata") 47 } 48 49 func (s *deleteImageSuite) TestDeleteImageMetadataManyImageIds(c *gc.C) { 50 s.assertDeleteImageMetadataErr(c, "only one image ID can be supplied as an argument to this command", deleteTestId, deleteTestId) 51 } 52 53 func (s *deleteImageSuite) TestDeleteImageMetadataFailed(c *gc.C) { 54 msg := "failed" 55 s.mockAPI.delete = func(imageId string) error { 56 return errors.New(msg) 57 } 58 s.assertDeleteImageMetadataErr(c, msg, deleteTestId) 59 s.mockAPI.CheckCallNames(c, "Delete", "Close") 60 } 61 62 func (s *deleteImageSuite) runDeleteImageMetadata(c *gc.C, args ...string) error { 63 tstDelete := &deleteImageMetadataCommand{} 64 tstDelete.newAPIFunc = func() (MetadataDeleteAPI, error) { 65 return s.mockAPI, nil 66 } 67 deleteCmd := modelcmd.Wrap(tstDelete) 68 69 _, err := testing.RunCommand(c, deleteCmd, args...) 70 return err 71 } 72 73 func (s *deleteImageSuite) assertDeleteImageMetadata(c *gc.C, id string) { 74 err := s.runDeleteImageMetadata(c, id) 75 c.Assert(err, jc.ErrorIsNil) 76 c.Assert(s.deletedIds, gc.DeepEquals, []string{id}) 77 s.mockAPI.CheckCallNames(c, "Delete", "Close") 78 } 79 80 func (s *deleteImageSuite) assertDeleteImageMetadataErr(c *gc.C, errorMsg string, args ...string) { 81 err := s.runDeleteImageMetadata(c, args...) 82 c.Assert(err, gc.ErrorMatches, errorMsg) 83 c.Assert(s.deletedIds, gc.DeepEquals, []string{}) 84 } 85 86 type mockDeleteAPI struct { 87 *gitjujutesting.Stub 88 89 delete func(imageId string) error 90 } 91 92 func (s mockDeleteAPI) Close() error { 93 s.MethodCall(s, "Close") 94 return nil 95 } 96 97 func (s mockDeleteAPI) Delete(imageId string) error { 98 s.MethodCall(s, "Delete", imageId) 99 return s.delete(imageId) 100 }