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