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