github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/payload/context/unregister_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package context_test
     5  
     6  import (
     7  	"bytes"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/payload"
    16  	"github.com/juju/juju/payload/context"
    17  	coretesting "github.com/juju/juju/testing"
    18  )
    19  
    20  type unregisterSuite struct {
    21  	testing.IsolationSuite
    22  
    23  	stub    *testing.Stub
    24  	compCtx *stubUnregisterContext
    25  	ctx     *cmd.Context
    26  }
    27  
    28  var _ = gc.Suite(&unregisterSuite{})
    29  
    30  func (s *unregisterSuite) SetUpTest(c *gc.C) {
    31  	s.IsolationSuite.SetUpTest(c)
    32  
    33  	s.stub = &testing.Stub{}
    34  	s.compCtx = &stubUnregisterContext{stub: s.stub}
    35  	s.ctx = coretesting.Context(c)
    36  }
    37  
    38  func (s *unregisterSuite) Component(name string) (context.Component, error) {
    39  	s.stub.AddCall("Component", name)
    40  	if err := s.stub.NextErr(); err != nil {
    41  		return nil, errors.Trace(err)
    42  	}
    43  
    44  	return s.compCtx, nil
    45  }
    46  
    47  func (s *unregisterSuite) TestHelp(c *gc.C) {
    48  	unregister, err := context.NewUnregisterCmd(s)
    49  	c.Assert(err, jc.ErrorIsNil)
    50  	code := cmd.Main(unregister, s.ctx, []string{"--help"})
    51  	c.Assert(code, gc.Equals, 0)
    52  
    53  	c.Check(s.ctx.Stdout.(*bytes.Buffer).String(), gc.Equals, `
    54  Usage: payload-unregister <class> <id>
    55  
    56  Summary:
    57  stop tracking a payload
    58  
    59  Details:
    60  "payload-unregister" is used while a hook is running to let Juju know
    61  that a payload has been manually stopped. The <class> and <id> provided
    62  must match a payload that has been previously registered with juju using
    63  payload-register.
    64  `[1:])
    65  }
    66  
    67  func (s *unregisterSuite) TestRunOkay(c *gc.C) {
    68  	unregister, err := context.NewUnregisterCmd(s)
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	err = unregister.Init([]string{"spam", "eggs"})
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	err = unregister.Run(s.ctx)
    73  	c.Assert(err, jc.ErrorIsNil)
    74  
    75  	s.stub.CheckCalls(c, []testing.StubCall{{
    76  		FuncName: "Component",
    77  		Args: []interface{}{
    78  			payload.ComponentName,
    79  		},
    80  	}, {
    81  		FuncName: "Untrack",
    82  		Args: []interface{}{
    83  			"spam",
    84  			"eggs",
    85  		},
    86  	}, {
    87  		FuncName: "Flush",
    88  	}})
    89  }
    90  
    91  type stubUnregisterContext struct {
    92  	context.Component
    93  	stub *testing.Stub
    94  }
    95  
    96  func (s stubUnregisterContext) Untrack(class, id string) error {
    97  	s.stub.AddCall("Untrack", class, id)
    98  	if err := s.stub.NextErr(); err != nil {
    99  		return errors.Trace(err)
   100  	}
   101  
   102  	return nil
   103  }
   104  
   105  func (s stubUnregisterContext) Flush() error {
   106  	s.stub.AddCall("Flush")
   107  	if err := s.stub.NextErr(); err != nil {
   108  		return errors.Trace(err)
   109  	}
   110  
   111  	return nil
   112  }