github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/featuretests/cmdjuju_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package featuretests
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/juju/charm.v6-unstable"
    10  
    11  	"github.com/juju/juju/cmd/juju/model"
    12  	"github.com/juju/juju/cmd/juju/service"
    13  	"github.com/juju/juju/constraints"
    14  	"github.com/juju/juju/instance"
    15  	jujutesting "github.com/juju/juju/juju/testing"
    16  	"github.com/juju/juju/state"
    17  	"github.com/juju/juju/testing"
    18  )
    19  
    20  // cmdJujuSuite tests the connectivity of juju commands.  These tests
    21  // go from the command line, api client, api server, db. The db changes
    22  // are then checked.  Only one test for each command is done here to
    23  // check connectivity.  Exhaustive unit tests are at each layer.
    24  type cmdJujuSuite struct {
    25  	jujutesting.JujuConnSuite
    26  }
    27  
    28  func uint64p(val uint64) *uint64 {
    29  	return &val
    30  }
    31  
    32  func (s *cmdJujuSuite) TestSetConstraints(c *gc.C) {
    33  	_, err := testing.RunCommand(c, model.NewModelSetConstraintsCommand(), "mem=4G", "cpu-power=250")
    34  	c.Assert(err, jc.ErrorIsNil)
    35  
    36  	cons, err := s.State.ModelConstraints()
    37  	c.Assert(err, jc.ErrorIsNil)
    38  	c.Assert(cons, gc.DeepEquals, constraints.Value{
    39  		CpuPower: uint64p(250),
    40  		Mem:      uint64p(4096),
    41  	})
    42  }
    43  
    44  func (s *cmdJujuSuite) TestGetConstraints(c *gc.C) {
    45  	svc := s.AddTestingService(c, "svc", s.AddTestingCharm(c, "dummy"))
    46  	err := svc.SetConstraints(constraints.Value{CpuCores: uint64p(64)})
    47  	c.Assert(err, jc.ErrorIsNil)
    48  
    49  	context, err := testing.RunCommand(c, service.NewServiceGetConstraintsCommand(), "svc")
    50  	c.Assert(testing.Stdout(context), gc.Equals, "cpu-cores=64\n")
    51  	c.Assert(testing.Stderr(context), gc.Equals, "")
    52  }
    53  
    54  func (s *cmdJujuSuite) TestServiceSet(c *gc.C) {
    55  	ch := s.AddTestingCharm(c, "dummy")
    56  	svc := s.AddTestingService(c, "dummy-service", ch)
    57  
    58  	_, err := testing.RunCommand(c, service.NewSetCommand(), "dummy-service",
    59  		"username=hello", "outlook=hello@world.tld")
    60  	c.Assert(err, jc.ErrorIsNil)
    61  
    62  	expect := charm.Settings{
    63  		"username": "hello",
    64  		"outlook":  "hello@world.tld",
    65  	}
    66  
    67  	settings, err := svc.ConfigSettings()
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	c.Assert(settings, gc.DeepEquals, expect)
    70  }
    71  
    72  func (s *cmdJujuSuite) TestServiceUnset(c *gc.C) {
    73  	ch := s.AddTestingCharm(c, "dummy")
    74  	svc := s.AddTestingService(c, "dummy-service", ch)
    75  
    76  	settings := charm.Settings{
    77  		"username": "hello",
    78  		"outlook":  "hello@world.tld",
    79  	}
    80  
    81  	err := svc.UpdateConfigSettings(settings)
    82  	c.Assert(err, jc.ErrorIsNil)
    83  
    84  	_, err = testing.RunCommand(c, service.NewSetCommand(), "--to-default", "dummy-service", "username")
    85  	c.Assert(err, jc.ErrorIsNil)
    86  
    87  	expect := charm.Settings{
    88  		"outlook": "hello@world.tld",
    89  	}
    90  	settings, err = svc.ConfigSettings()
    91  	c.Assert(err, jc.ErrorIsNil)
    92  	c.Assert(settings, gc.DeepEquals, expect)
    93  }
    94  
    95  func (s *cmdJujuSuite) TestServiceGet(c *gc.C) {
    96  	expected := `charm: dummy
    97  service: dummy-service
    98  settings:
    99    outlook:
   100      default: true
   101      description: No default outlook.
   102      type: string
   103    skill-level:
   104      default: true
   105      description: A number indicating skill.
   106      type: int
   107    title:
   108      default: true
   109      description: A descriptive title used for the service.
   110      type: string
   111      value: My Title
   112    username:
   113      default: true
   114      description: The name of the initial account (given admin permissions).
   115      type: string
   116      value: admin001
   117  `
   118  	ch := s.AddTestingCharm(c, "dummy")
   119  	s.AddTestingService(c, "dummy-service", ch)
   120  
   121  	context, err := testing.RunCommand(c, service.NewGetCommand(), "dummy-service")
   122  	c.Assert(err, jc.ErrorIsNil)
   123  	c.Assert(testing.Stdout(context), gc.Equals, expected)
   124  }
   125  
   126  func (s *cmdJujuSuite) TestServiceAddUnitExistingContainer(c *gc.C) {
   127  	ch := s.AddTestingCharm(c, "dummy")
   128  	svc := s.AddTestingService(c, "some-service-name", ch)
   129  
   130  	machine, err := s.State.AddMachine("quantal", state.JobHostUnits)
   131  	c.Assert(err, jc.ErrorIsNil)
   132  	template := state.MachineTemplate{
   133  		Series: "quantal",
   134  		Jobs:   []state.MachineJob{state.JobHostUnits},
   135  	}
   136  	container, err := s.State.AddMachineInsideMachine(template, machine.Id(), instance.LXC)
   137  	c.Assert(err, jc.ErrorIsNil)
   138  
   139  	_, err = testing.RunCommand(c, service.NewAddUnitCommand(), "some-service-name",
   140  		"--to", container.Id())
   141  	c.Assert(err, jc.ErrorIsNil)
   142  
   143  	units, err := svc.AllUnits()
   144  	c.Assert(err, jc.ErrorIsNil)
   145  	mid, err := units[0].AssignedMachineId()
   146  	c.Assert(err, jc.ErrorIsNil)
   147  	c.Assert(mid, gc.Equals, container.Id())
   148  }