github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/setmeterstatus/setmeterstatus_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package setmeterstatus_test 5 6 import ( 7 stdtesting "testing" 8 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/cmd/juju/setmeterstatus" 14 "github.com/juju/juju/cmd/modelcmd" 15 jujutesting "github.com/juju/juju/juju/testing" 16 coretesting "github.com/juju/juju/testing" 17 "github.com/juju/juju/testing/factory" 18 ) 19 20 func TestPackage(t *stdtesting.T) { 21 coretesting.MgoTestPackage(t) 22 } 23 24 type MockSetMeterStatusClient struct { 25 testing.Stub 26 } 27 28 func (m *MockSetMeterStatusClient) SetMeterStatus(tag, status, info string) error { 29 m.Stub.MethodCall(m, "SetMeterStatus", tag, status, info) 30 return m.NextErr() 31 } 32 func (m *MockSetMeterStatusClient) Close() error { 33 m.Stub.MethodCall(m, "Close") 34 return nil 35 } 36 37 type SetMeterStatusSuite struct { 38 coretesting.FakeJujuXDGDataHomeSuite 39 } 40 41 var _ = gc.Suite(&SetMeterStatusSuite{}) 42 43 func (s *SetMeterStatusSuite) TestUnit(c *gc.C) { 44 client := MockSetMeterStatusClient{testing.Stub{}} 45 s.PatchValue(setmeterstatus.NewClient, func(_ modelcmd.ModelCommandBase) (setmeterstatus.SetMeterStatusClient, error) { 46 return &client, nil 47 }) 48 _, err := coretesting.RunCommand(c, setmeterstatus.New(), "metered/0", "RED") 49 c.Assert(err, jc.ErrorIsNil) 50 client.CheckCall(c, 0, "SetMeterStatus", "unit-metered-0", "RED", "") 51 } 52 53 func (s *SetMeterStatusSuite) TestService(c *gc.C) { 54 client := MockSetMeterStatusClient{testing.Stub{}} 55 s.PatchValue(setmeterstatus.NewClient, func(_ modelcmd.ModelCommandBase) (setmeterstatus.SetMeterStatusClient, error) { 56 return &client, nil 57 }) 58 _, err := coretesting.RunCommand(c, setmeterstatus.New(), "metered", "RED") 59 c.Assert(err, jc.ErrorIsNil) 60 client.CheckCall(c, 0, "SetMeterStatus", "service-metered", "RED", "") 61 } 62 63 func (s *SetMeterStatusSuite) TestNotValidServiceOrUnit(c *gc.C) { 64 client := MockSetMeterStatusClient{testing.Stub{}} 65 s.PatchValue(setmeterstatus.NewClient, func(_ modelcmd.ModelCommandBase) (setmeterstatus.SetMeterStatusClient, error) { 66 return &client, nil 67 }) 68 _, err := coretesting.RunCommand(c, setmeterstatus.New(), "!!!!!!", "RED") 69 c.Assert(err, gc.ErrorMatches, `"!!!!!!" is not a valid unit or service`) 70 } 71 72 type DebugMetricsCommandSuite struct { 73 jujutesting.JujuConnSuite 74 } 75 76 var _ = gc.Suite(&DebugMetricsCommandSuite{}) 77 78 func (s *DebugMetricsCommandSuite) TestDebugNoArgs(c *gc.C) { 79 _, err := coretesting.RunCommand(c, setmeterstatus.New()) 80 c.Assert(err, gc.ErrorMatches, `you need to specify an entity \(service or unit\) and a status`) 81 } 82 83 func (s *DebugMetricsCommandSuite) TestUnits(c *gc.C) { 84 charm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "mysql", URL: "local:quantal/mysql"}) 85 service := s.Factory.MakeService(c, &factory.ServiceParams{Charm: charm}) 86 unit := s.Factory.MakeUnit(c, &factory.UnitParams{Service: service, SetCharmURL: true}) 87 _, err := coretesting.RunCommand(c, setmeterstatus.New(), unit.Name(), "RED", "--info", "foobar") 88 c.Assert(err, jc.ErrorIsNil) 89 status, err := unit.GetMeterStatus() 90 c.Assert(err, jc.ErrorIsNil) 91 c.Assert(status.Code.String(), gc.Equals, "RED") 92 c.Assert(status.Info, gc.Equals, "foobar") 93 } 94 95 func (s *DebugMetricsCommandSuite) TestService(c *gc.C) { 96 charm := s.Factory.MakeCharm(c, &factory.CharmParams{Name: "mysql", URL: "local:quantal/mysql"}) 97 service := s.Factory.MakeService(c, &factory.ServiceParams{Charm: charm}) 98 unit0, err := service.AddUnit() 99 c.Assert(err, jc.ErrorIsNil) 100 unit1, err := service.AddUnit() 101 c.Assert(err, jc.ErrorIsNil) 102 _, err = coretesting.RunCommand(c, setmeterstatus.New(), "mysql", "RED", "--info", "foobar") 103 c.Assert(err, jc.ErrorIsNil) 104 status, err := unit0.GetMeterStatus() 105 c.Assert(err, jc.ErrorIsNil) 106 c.Assert(status.Code.String(), gc.Equals, "RED") 107 c.Assert(status.Info, gc.Equals, "foobar") 108 109 status, err = unit1.GetMeterStatus() 110 c.Assert(err, jc.ErrorIsNil) 111 c.Assert(status.Code.String(), gc.Equals, "RED") 112 c.Assert(status.Info, gc.Equals, "foobar") 113 }