github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/payload/context/register_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package context
     5  
     6  import (
     7  	"io/ioutil"
     8  	"path/filepath"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/errors"
    12  	"github.com/juju/testing"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  	"gopkg.in/juju/charm.v6"
    16  
    17  	"github.com/juju/juju/payload"
    18  )
    19  
    20  type registerSuite struct {
    21  	testing.IsolationSuite
    22  
    23  	hookCtx *stubRegisterContext
    24  	command RegisterCmd
    25  }
    26  
    27  var _ = gc.Suite(&registerSuite{})
    28  
    29  func (s *registerSuite) SetUpTest(c *gc.C) {
    30  	s.IsolationSuite.SetUpTest(c)
    31  
    32  	s.hookCtx = &stubRegisterContext{}
    33  
    34  	s.command = RegisterCmd{
    35  		hookContextFunc: func() (Component, error) {
    36  			return s.hookCtx, nil
    37  		},
    38  	}
    39  }
    40  
    41  func (s *registerSuite) TestInitNilArgs(c *gc.C) {
    42  	err := s.command.Init(nil)
    43  	c.Assert(err, gc.NotNil)
    44  }
    45  
    46  func (s *registerSuite) TestInitTooFewArgs(c *gc.C) {
    47  	err := s.command.Init([]string{"foo", "bar"})
    48  	c.Assert(err, gc.NotNil)
    49  }
    50  
    51  func (s *registerSuite) TestInit(c *gc.C) {
    52  	err := s.command.Init([]string{"type", "class", "id"})
    53  	c.Assert(err, jc.ErrorIsNil)
    54  	c.Assert(s.command.typ, gc.Equals, "type")
    55  	c.Assert(s.command.class, gc.Equals, "class")
    56  	c.Assert(s.command.id, gc.Equals, "id")
    57  	c.Assert(s.command.labels, gc.HasLen, 0)
    58  }
    59  
    60  func (s *registerSuite) TestInitWithLabels(c *gc.C) {
    61  	err := s.command.Init([]string{"type", "class", "id", "tag1", "tag 2"})
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	c.Assert(s.command.typ, gc.Equals, "type")
    64  	c.Assert(s.command.class, gc.Equals, "class")
    65  	c.Assert(s.command.id, gc.Equals, "id")
    66  	c.Assert(s.command.labels, gc.DeepEquals, []string{"tag1", "tag 2"})
    67  }
    68  
    69  func (s *registerSuite) TestRun(c *gc.C) {
    70  	err := s.command.Init([]string{"type", "class", "id", "tag1", "tag 2"})
    71  	c.Assert(err, jc.ErrorIsNil)
    72  
    73  	ctx := setupMetadata(c)
    74  	err = s.command.Run(ctx)
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	c.Check(s.hookCtx.flushed, jc.IsTrue)
    77  	c.Check(s.hookCtx.payload, jc.DeepEquals, payload.Payload{
    78  		PayloadClass: charm.PayloadClass{
    79  			Name: "class",
    80  			Type: "type",
    81  		},
    82  		ID:     "id",
    83  		Status: payload.StateRunning,
    84  		Labels: []string{"tag1", "tag 2"},
    85  		Unit:   "a-application/0",
    86  	})
    87  	// TODO (natefinch): we need to do something with the labels
    88  }
    89  
    90  func (s *registerSuite) TestRunUnknownClass(c *gc.C) {
    91  	err := s.command.Init([]string{"type", "badclass", "id"})
    92  	c.Assert(err, jc.ErrorIsNil)
    93  
    94  	ctx := setupMetadata(c)
    95  	err = s.command.Run(ctx)
    96  	c.Assert(err, gc.ErrorMatches, "payload \"badclass\" not found in metadata.yaml")
    97  }
    98  
    99  func (s *registerSuite) TestRunUnknownType(c *gc.C) {
   100  	err := s.command.Init([]string{"badtype", "class", "id"})
   101  	c.Assert(err, jc.ErrorIsNil)
   102  
   103  	ctx := setupMetadata(c)
   104  	err = s.command.Run(ctx)
   105  	c.Assert(err, gc.ErrorMatches, "incorrect type \"badtype\" for payload \"class\", expected \"type\"")
   106  }
   107  
   108  func (s *registerSuite) TestRunTrackErr(c *gc.C) {
   109  	s.hookCtx.trackerr = errors.Errorf("boo")
   110  	err := s.command.Init([]string{"type", "class", "id", "tag1", "tag 2"})
   111  	c.Assert(err, jc.ErrorIsNil)
   112  
   113  	ctx := setupMetadata(c)
   114  	err = s.command.Run(ctx)
   115  	c.Assert(err, gc.ErrorMatches, "boo")
   116  }
   117  
   118  func (s *registerSuite) TestRunFlushErr(c *gc.C) {
   119  	s.hookCtx.flusherr = errors.Errorf("boo")
   120  	err := s.command.Init([]string{"type", "class", "id", "tag1", "tag 2"})
   121  	c.Assert(err, jc.ErrorIsNil)
   122  
   123  	ctx := setupMetadata(c)
   124  	err = s.command.Run(ctx)
   125  	c.Assert(err, gc.ErrorMatches, "boo")
   126  }
   127  
   128  type stubRegisterContext struct {
   129  	Component
   130  	payload  payload.Payload
   131  	flushed  bool
   132  	trackerr error
   133  	flusherr error
   134  }
   135  
   136  func (f *stubRegisterContext) Track(pl payload.Payload) error {
   137  	f.payload = pl
   138  	return f.trackerr
   139  }
   140  
   141  func (f *stubRegisterContext) Flush() error {
   142  	f.flushed = true
   143  	return f.flusherr
   144  }
   145  
   146  func setupMetadata(c *gc.C) *cmd.Context {
   147  	dir := c.MkDir()
   148  	path := filepath.Join(dir, "metadata.yaml")
   149  	ioutil.WriteFile(path, []byte(metadataContents), 0660)
   150  	return &cmd.Context{Dir: dir}
   151  }
   152  
   153  const metadataContents = `name: ducksay
   154  summary: Testing charm payload management
   155  maintainer: juju@canonical.com <Juju>
   156  description: |
   157    Testing payloads
   158  subordinate: false
   159  payloads:
   160    class:
   161      type: type
   162      lifecycle: ["restart"]
   163  `