github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/worker/uniter/runner/jujuc/pod-spec-set_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc_test
     5  
     6  import (
     7  	"bytes"
     8  	"io/ioutil"
     9  	"path/filepath"
    10  
    11  	"github.com/juju/cmd"
    12  	"github.com/juju/cmd/cmdtesting"
    13  	jc "github.com/juju/testing/checkers"
    14  	gc "gopkg.in/check.v1"
    15  
    16  	"github.com/juju/juju/worker/uniter/runner/jujuc"
    17  )
    18  
    19  type ContainerspecSetSuite struct {
    20  	ContextSuite
    21  }
    22  
    23  var _ = gc.Suite(&ContainerspecSetSuite{})
    24  
    25  var containerSpecYaml = `
    26  containerspec:
    27    foo: bar
    28  `[1:]
    29  
    30  var containerSpecSetInitTests = []struct {
    31  	args []string
    32  	err  string
    33  }{
    34  	{[]string{"--file", "file", "extra"}, `unrecognized args: \["extra"\]`},
    35  }
    36  
    37  func (s *ContainerspecSetSuite) TestContainerSpecSetInit(c *gc.C) {
    38  	for i, t := range containerSpecSetInitTests {
    39  		c.Logf("test %d: %#v", i, t.args)
    40  		hctx := s.GetHookContext(c, -1, "")
    41  		com, err := jujuc.NewCommand(hctx, "pod-spec-set")
    42  		c.Assert(err, jc.ErrorIsNil)
    43  		cmdtesting.TestInit(c, jujuc.NewJujucCommandWrappedForTest(com), t.args, t.err)
    44  	}
    45  }
    46  
    47  func (s *ContainerspecSetSuite) TestHelp(c *gc.C) {
    48  	hctx := s.GetHookContext(c, -1, "")
    49  	com, err := jujuc.NewCommand(hctx, "pod-spec-set")
    50  	c.Assert(err, jc.ErrorIsNil)
    51  	ctx := cmdtesting.Context(c)
    52  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, []string{"--help"})
    53  	c.Assert(code, gc.Equals, 0)
    54  	expectedHelp := "" +
    55  		"Usage: pod-spec-set [options] --file <pod spec file>\n" +
    56  		"\n" +
    57  		"Summary:\n" +
    58  		"set pod spec information\n" +
    59  		"\n" +
    60  		"Options:\n" +
    61  		"--file  (= -)\n" +
    62  		"    file containing pod spec\n" +
    63  		"\n" +
    64  		"Details:\n" +
    65  		"Sets configuration data to use for a pod.\n" +
    66  		"The spec applies to all units for the application.\n"
    67  
    68  	c.Assert(bufferString(ctx.Stdout), gc.Equals, expectedHelp)
    69  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
    70  }
    71  
    72  func (s *ContainerspecSetSuite) TestContainerSpecSetNoData(c *gc.C) {
    73  	hctx := s.GetHookContext(c, -1, "")
    74  	com, err := jujuc.NewCommand(hctx, "pod-spec-set")
    75  	c.Assert(err, jc.ErrorIsNil)
    76  	ctx := cmdtesting.Context(c)
    77  
    78  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, nil)
    79  	c.Check(code, gc.Equals, 1)
    80  	c.Assert(bufferString(
    81  		ctx.Stderr), gc.Matches,
    82  		".*no pod spec specified: pipe pod spec to command, or specify a file with --file\n")
    83  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
    84  }
    85  
    86  func (s *ContainerspecSetSuite) TestContainerSpecSet(c *gc.C) {
    87  	s.assertContainerSpecSet(c, "specfile.yaml")
    88  }
    89  
    90  func (s *ContainerspecSetSuite) TestContainerSpecSetStdIn(c *gc.C) {
    91  	s.assertContainerSpecSet(c, "-")
    92  }
    93  
    94  func (s *ContainerspecSetSuite) assertContainerSpecSet(c *gc.C, filename string) {
    95  	hctx := s.GetHookContext(c, -1, "")
    96  	com, args, ctx := s.initCommand(c, hctx, containerSpecYaml, filename)
    97  	code := cmd.Main(jujuc.NewJujucCommandWrappedForTest(com), ctx, args)
    98  	c.Check(code, gc.Equals, 0)
    99  	c.Assert(bufferString(ctx.Stderr), gc.Equals, "")
   100  	c.Assert(bufferString(ctx.Stdout), gc.Equals, "")
   101  	c.Assert(hctx.info.ContainerSpec, gc.Equals, containerSpecYaml)
   102  }
   103  
   104  func (s *ContainerspecSetSuite) initCommand(
   105  	c *gc.C, hctx jujuc.Context, yaml string, filename string,
   106  ) (cmd.Command, []string, *cmd.Context) {
   107  	com, err := jujuc.NewCommand(hctx, "pod-spec-set")
   108  	c.Assert(err, jc.ErrorIsNil)
   109  	ctx := cmdtesting.Context(c)
   110  
   111  	var args []string
   112  	if filename == "-" {
   113  		ctx.Stdin = bytes.NewBufferString(yaml)
   114  	} else if filename != "" {
   115  		filename = filepath.Join(c.MkDir(), filename)
   116  		args = append(args, "--file", filename)
   117  		err := ioutil.WriteFile(filename, []byte(yaml), 0644)
   118  		c.Assert(err, jc.ErrorIsNil)
   119  	}
   120  	return jujuc.NewJujucCommandWrappedForTest(com), args, ctx
   121  }