github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/cachedimages/remove_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for info.
     3  
     4  package cachedimages_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/cmd/juju/cachedimages"
    12  	"github.com/juju/juju/testing"
    13  )
    14  
    15  type removeImageCommandSuite struct {
    16  	testing.FakeJujuXDGDataHomeSuite
    17  	mockAPI *fakeImageRemoveAPI
    18  }
    19  
    20  var _ = gc.Suite(&removeImageCommandSuite{})
    21  
    22  type fakeImageRemoveAPI struct {
    23  	kind   string
    24  	series string
    25  	arch   string
    26  }
    27  
    28  func (*fakeImageRemoveAPI) Close() error {
    29  	return nil
    30  }
    31  
    32  func (f *fakeImageRemoveAPI) DeleteImage(kind, series, arch string) error {
    33  	f.kind = kind
    34  	f.series = series
    35  	f.arch = arch
    36  	return nil
    37  }
    38  
    39  func (s *removeImageCommandSuite) SetUpTest(c *gc.C) {
    40  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
    41  	s.mockAPI = &fakeImageRemoveAPI{}
    42  	s.PatchValue(cachedimages.GetRemoveImageAPI, func(_ *cachedimages.CachedImagesCommandBase) (cachedimages.RemoveImageAPI, error) {
    43  		return s.mockAPI, nil
    44  	})
    45  }
    46  
    47  func runRemoveCommand(c *gc.C, args ...string) (*cmd.Context, error) {
    48  	return testing.RunCommand(c, cachedimages.NewRemoveCommandForTest(), args...)
    49  }
    50  
    51  func (s *removeImageCommandSuite) TestRemoveImage(c *gc.C) {
    52  	_, err := runRemoveCommand(c, "--kind", "lxd", "--series", "trusty", "--arch", "amd64")
    53  	c.Assert(err, jc.ErrorIsNil)
    54  	c.Assert(s.mockAPI.kind, gc.Equals, "lxd")
    55  	c.Assert(s.mockAPI.series, gc.Equals, "trusty")
    56  	c.Assert(s.mockAPI.arch, gc.Equals, "amd64")
    57  }
    58  
    59  func (*removeImageCommandSuite) TestTooManyArgs(c *gc.C) {
    60  	_, err := runRemoveCommand(c, "--kind", "lxd", "--series", "trusty", "--arch", "amd64", "bad")
    61  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["bad"\]`)
    62  }
    63  
    64  func (*removeImageCommandSuite) TestKindRequired(c *gc.C) {
    65  	_, err := runRemoveCommand(c, "--series", "trusty", "--arch", "amd64", "bad")
    66  	c.Assert(err, gc.ErrorMatches, `image kind must be specified`)
    67  }
    68  
    69  func (*removeImageCommandSuite) TestSeriesRequired(c *gc.C) {
    70  	_, err := runRemoveCommand(c, "--kind", "lxd", "--arch", "amd64", "bad")
    71  	c.Assert(err, gc.ErrorMatches, `image series must be specified`)
    72  }
    73  
    74  func (*removeImageCommandSuite) TestArchRequired(c *gc.C) {
    75  	_, err := runRemoveCommand(c, "--kind", "lxd", "--series", "trusty", "bad")
    76  	c.Assert(err, gc.ErrorMatches, `image architecture must be specified`)
    77  }