github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/cmd/juju/set_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"bytes"
     8  	"io/ioutil"
     9  
    10  	gc "launchpad.net/gocheck"
    11  
    12  	"github.com/juju/juju/charm"
    13  	"github.com/juju/juju/cmd"
    14  	"github.com/juju/juju/cmd/envcmd"
    15  	"github.com/juju/juju/juju/testing"
    16  	"github.com/juju/juju/state"
    17  	coretesting "github.com/juju/juju/testing"
    18  )
    19  
    20  type SetSuite struct {
    21  	testing.JujuConnSuite
    22  	svc *state.Service
    23  	dir string
    24  }
    25  
    26  var _ = gc.Suite(&SetSuite{})
    27  
    28  func (s *SetSuite) SetUpTest(c *gc.C) {
    29  	s.JujuConnSuite.SetUpTest(c)
    30  	ch := s.AddTestingCharm(c, "dummy")
    31  	svc := s.AddTestingService(c, "dummy-service", ch)
    32  	s.svc = svc
    33  	s.dir = c.MkDir()
    34  	setupConfigFile(c, s.dir)
    35  }
    36  
    37  func (s *SetSuite) TestSetOptionSuccess(c *gc.C) {
    38  	assertSetSuccess(c, s.dir, s.svc, []string{
    39  		"username=hello",
    40  		"outlook=hello@world.tld",
    41  	}, charm.Settings{
    42  		"username": "hello",
    43  		"outlook":  "hello@world.tld",
    44  	})
    45  	assertSetSuccess(c, s.dir, s.svc, []string{
    46  		"username=hello=foo",
    47  	}, charm.Settings{
    48  		"username": "hello=foo",
    49  		"outlook":  "hello@world.tld",
    50  	})
    51  
    52  }
    53  
    54  func (s *SetSuite) TestSetOptionFail(c *gc.C) {
    55  	assertSetFail(c, s.dir, []string{"foo", "bar"}, "error: invalid option: \"foo\"\n")
    56  	assertSetFail(c, s.dir, []string{"=bar"}, "error: invalid option: \"=bar\"\n")
    57  }
    58  
    59  func (s *SetSuite) TestSetConfig(c *gc.C) {
    60  	assertSetFail(c, s.dir, []string{
    61  		"--config",
    62  		"missing.yaml",
    63  	}, "error.*no such file or directory\n")
    64  
    65  	assertSetSuccess(c, s.dir, s.svc, []string{
    66  		"--config",
    67  		"testconfig.yaml",
    68  	}, charm.Settings{
    69  		"username":    "admin001",
    70  		"skill-level": int64(9000),
    71  	})
    72  }
    73  
    74  // assertSetSuccess sets configuration options and checks the expected settings.
    75  func assertSetSuccess(c *gc.C, dir string, svc *state.Service, args []string, expect charm.Settings) {
    76  	ctx := coretesting.ContextForDir(c, dir)
    77  	code := cmd.Main(envcmd.Wrap(&SetCommand{}), ctx, append([]string{"dummy-service"}, args...))
    78  	c.Check(code, gc.Equals, 0)
    79  	settings, err := svc.ConfigSettings()
    80  	c.Assert(err, gc.IsNil)
    81  	c.Assert(settings, gc.DeepEquals, expect)
    82  }
    83  
    84  // assertSetFail sets configuration options and checks the expected error.
    85  func assertSetFail(c *gc.C, dir string, args []string, err string) {
    86  	ctx := coretesting.ContextForDir(c, dir)
    87  	code := cmd.Main(envcmd.Wrap(&SetCommand{}), ctx, append([]string{"dummy-service"}, args...))
    88  	c.Check(code, gc.Not(gc.Equals), 0)
    89  	c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Matches, err)
    90  }
    91  
    92  // setupConfigFile creates a configuration file for testing set
    93  // with the --config argument specifying a configuration file.
    94  func setupConfigFile(c *gc.C, dir string) string {
    95  	ctx := coretesting.ContextForDir(c, dir)
    96  	path := ctx.AbsPath("testconfig.yaml")
    97  	content := []byte("dummy-service:\n  skill-level: 9000\n  username: admin001\n\n")
    98  	err := ioutil.WriteFile(path, content, 0666)
    99  	c.Assert(err, gc.IsNil)
   100  	return path
   101  }