github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/cmd/juju/unset_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  
     9  	gc "launchpad.net/gocheck"
    10  
    11  	"launchpad.net/juju-core/charm"
    12  	"launchpad.net/juju-core/cmd"
    13  	"launchpad.net/juju-core/juju/testing"
    14  	"launchpad.net/juju-core/state"
    15  	coretesting "launchpad.net/juju-core/testing"
    16  )
    17  
    18  type UnsetSuite struct {
    19  	testing.JujuConnSuite
    20  	svc *state.Service
    21  	dir string
    22  }
    23  
    24  var _ = gc.Suite(&UnsetSuite{})
    25  
    26  func (s *UnsetSuite) SetUpTest(c *gc.C) {
    27  	s.JujuConnSuite.SetUpTest(c)
    28  	ch := s.AddTestingCharm(c, "dummy")
    29  	svc := s.AddTestingService(c, "dummy-service", ch)
    30  	s.svc = svc
    31  	s.dir = c.MkDir()
    32  	setupConfigFile(c, s.dir)
    33  }
    34  
    35  func (s *UnsetSuite) TestUnsetOptionOneByOneSuccess(c *gc.C) {
    36  	// Set options as preparation.
    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  
    45  	// Unset one by one.
    46  	assertUnsetSuccess(c, s.dir, s.svc, []string{"username"}, charm.Settings{
    47  		"outlook": "hello@world.tld",
    48  	})
    49  	assertUnsetSuccess(c, s.dir, s.svc, []string{"outlook"}, charm.Settings{})
    50  }
    51  
    52  func (s *UnsetSuite) TestUnsetOptionMultipleAtOnceSuccess(c *gc.C) {
    53  	// Set options as preparation.
    54  	assertSetSuccess(c, s.dir, s.svc, []string{
    55  		"username=hello",
    56  		"outlook=hello@world.tld",
    57  	}, charm.Settings{
    58  		"username": "hello",
    59  		"outlook":  "hello@world.tld",
    60  	})
    61  
    62  	// Unset multiple options at once.
    63  	assertUnsetSuccess(c, s.dir, s.svc, []string{"username", "outlook"}, charm.Settings{})
    64  }
    65  
    66  func (s *UnsetSuite) TestUnsetOptionFail(c *gc.C) {
    67  	assertUnsetFail(c, s.dir, []string{}, "error: no configuration options specified\n")
    68  	assertUnsetFail(c, s.dir, []string{"invalid"}, "error: unknown option \"invalid\"\n")
    69  	assertUnsetFail(c, s.dir, []string{"username=bar"}, "error: unknown option \"username=bar\"\n")
    70  	assertUnsetFail(c, s.dir, []string{
    71  		"username",
    72  		"outlook",
    73  		"invalid",
    74  	}, "error: unknown option \"invalid\"\n")
    75  }
    76  
    77  // assertUnsetSuccess unsets configuration options and checks the expected settings.
    78  func assertUnsetSuccess(c *gc.C, dir string, svc *state.Service, args []string, expect charm.Settings) {
    79  	ctx := coretesting.ContextForDir(c, dir)
    80  	code := cmd.Main(&UnsetCommand{}, ctx, append([]string{"dummy-service"}, args...))
    81  	c.Check(code, gc.Equals, 0)
    82  	settings, err := svc.ConfigSettings()
    83  	c.Assert(err, gc.IsNil)
    84  	c.Assert(settings, gc.DeepEquals, expect)
    85  }
    86  
    87  // assertUnsetFail unsets configuration options and checks the expected error.
    88  func assertUnsetFail(c *gc.C, dir string, args []string, err string) {
    89  	ctx := coretesting.ContextForDir(c, dir)
    90  	code := cmd.Main(&UnsetCommand{}, ctx, append([]string{"dummy-service"}, args...))
    91  	c.Check(code, gc.Not(gc.Equals), 0)
    92  	c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Matches, err)
    93  }