github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/featuretests/cmd_juju_system_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 "fmt" 8 "strings" 9 10 "io/ioutil" 11 "path/filepath" 12 13 "github.com/juju/cmd" 14 "github.com/juju/errors" 15 jc "github.com/juju/testing/checkers" 16 gc "gopkg.in/check.v1" 17 goyaml "gopkg.in/yaml.v1" 18 19 "github.com/juju/juju/api" 20 "github.com/juju/juju/api/environmentmanager" 21 "github.com/juju/juju/cmd/envcmd" 22 "github.com/juju/juju/cmd/juju/system" 23 "github.com/juju/juju/environs/configstore" 24 "github.com/juju/juju/feature" 25 "github.com/juju/juju/juju" 26 jujutesting "github.com/juju/juju/juju/testing" 27 "github.com/juju/juju/state" 28 "github.com/juju/juju/testing" 29 "github.com/juju/juju/testing/factory" 30 ) 31 32 type cmdSystemSuite struct { 33 jujutesting.JujuConnSuite 34 } 35 36 func (s *cmdSystemSuite) SetUpTest(c *gc.C) { 37 s.SetInitialFeatureFlags(feature.JES) 38 s.JujuConnSuite.SetUpTest(c) 39 } 40 41 func (s *cmdSystemSuite) run(c *gc.C, args ...string) *cmd.Context { 42 command := system.NewSuperCommand() 43 context, err := testing.RunCommand(c, command, args...) 44 c.Assert(err, jc.ErrorIsNil) 45 return context 46 } 47 48 func (s *cmdSystemSuite) createEnv(c *gc.C, envname string, isServer bool) { 49 conn, err := juju.NewAPIState(s.AdminUserTag(c), s.Environ, api.DialOpts{}) 50 c.Assert(err, jc.ErrorIsNil) 51 s.AddCleanup(func(*gc.C) { conn.Close() }) 52 envManager := environmentmanager.NewClient(conn) 53 _, err = envManager.CreateEnvironment(s.AdminUserTag(c).Id(), nil, map[string]interface{}{ 54 "name": envname, 55 "authorized-keys": "ssh-key", 56 "state-server": isServer, 57 }) 58 c.Assert(err, jc.ErrorIsNil) 59 } 60 61 func (s *cmdSystemSuite) TestSystemListCommand(c *gc.C) { 62 context := s.run(c, "list") 63 c.Assert(testing.Stdout(context), gc.Equals, "dummyenv\n") 64 } 65 66 func (s *cmdSystemSuite) TestSystemEnvironmentsCommand(c *gc.C) { 67 c.Assert(envcmd.WriteCurrentSystem("dummyenv"), jc.ErrorIsNil) 68 s.createEnv(c, "new-env", false) 69 context := s.run(c, "environments") 70 c.Assert(testing.Stdout(context), gc.Equals, ""+ 71 "NAME OWNER LAST CONNECTION\n"+ 72 "dummyenv dummy-admin@local just now\n"+ 73 "new-env dummy-admin@local never connected\n"+ 74 "\n") 75 } 76 77 func (s *cmdSystemSuite) TestSystemLoginCommand(c *gc.C) { 78 user := s.Factory.MakeUser(c, &factory.UserParams{ 79 NoEnvUser: true, 80 Password: "super-secret", 81 }) 82 apiInfo := s.APIInfo(c) 83 serverFile := envcmd.ServerFile{ 84 Addresses: apiInfo.Addrs, 85 CACert: apiInfo.CACert, 86 Username: user.Name(), 87 Password: "super-secret", 88 } 89 serverFilePath := filepath.Join(c.MkDir(), "server.yaml") 90 content, err := goyaml.Marshal(serverFile) 91 c.Assert(err, jc.ErrorIsNil) 92 err = ioutil.WriteFile(serverFilePath, []byte(content), 0644) 93 c.Assert(err, jc.ErrorIsNil) 94 95 s.run(c, "login", "--server", serverFilePath, "just-a-system") 96 97 // Make sure that the saved server details are sufficient to connect 98 // to the api server. 99 api, err := juju.NewAPIFromName("just-a-system") 100 c.Assert(err, jc.ErrorIsNil) 101 api.Close() 102 } 103 104 func (s *cmdSystemSuite) TestCreateEnvironment(c *gc.C) { 105 c.Assert(envcmd.WriteCurrentSystem("dummyenv"), jc.ErrorIsNil) 106 // The JujuConnSuite doesn't set up an ssh key in the fake home dir, 107 // so fake one on the command line. The dummy provider also expects 108 // a config value for 'state-server'. 109 context := s.run(c, "create-environment", "new-env", "authorized-keys=fake-key", "state-server=false") 110 c.Check(testing.Stdout(context), gc.Equals, "") 111 c.Check(testing.Stderr(context), gc.Equals, ` 112 created environment "new-env" 113 dummyenv (system) -> new-env 114 `[1:]) 115 116 // Make sure that the saved server details are sufficient to connect 117 // to the api server. 118 api, err := juju.NewAPIFromName("new-env") 119 c.Assert(err, jc.ErrorIsNil) 120 api.Close() 121 } 122 123 func (s *cmdSystemSuite) TestSystemDestroy(c *gc.C) { 124 st := s.Factory.MakeEnvironment(c, &factory.EnvParams{ 125 Name: "just-a-system", 126 ConfigAttrs: testing.Attrs{"state-server": true}, 127 }) 128 129 st.Close() 130 s.run(c, "destroy", "dummyenv", "-y", "--destroy-all-environments") 131 132 store, err := configstore.Default() 133 _, err = store.ReadInfo("dummyenv") 134 c.Assert(err, jc.Satisfies, errors.IsNotFound) 135 } 136 137 func (s *cmdSystemSuite) TestRemoveBlocks(c *gc.C) { 138 c.Assert(envcmd.WriteCurrentSystem("dummyenv"), jc.ErrorIsNil) 139 s.State.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyEnvironment") 140 s.State.SwitchBlockOn(state.ChangeBlock, "TestChangeBlock") 141 142 s.run(c, "remove-blocks") 143 144 blocks, err := s.State.AllBlocksForSystem() 145 c.Assert(err, jc.ErrorIsNil) 146 c.Assert(blocks, gc.HasLen, 0) 147 } 148 149 func (s *cmdSystemSuite) TestSystemKill(c *gc.C) { 150 st := s.Factory.MakeEnvironment(c, &factory.EnvParams{ 151 Name: "foo", 152 }) 153 st.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyEnvironment") 154 st.Close() 155 156 s.run(c, "kill", "dummyenv", "-y") 157 158 store, err := configstore.Default() 159 _, err = store.ReadInfo("dummyenv") 160 c.Assert(err, jc.Satisfies, errors.IsNotFound) 161 } 162 163 func (s *cmdSystemSuite) TestListBlocks(c *gc.C) { 164 c.Assert(envcmd.WriteCurrentSystem("dummyenv"), jc.ErrorIsNil) 165 s.State.SwitchBlockOn(state.DestroyBlock, "TestBlockDestroyEnvironment") 166 s.State.SwitchBlockOn(state.ChangeBlock, "TestChangeBlock") 167 168 ctx := s.run(c, "list-blocks", "--format", "json") 169 expected := fmt.Sprintf(`[{"name":"dummyenv","env-uuid":"%s","owner-tag":"%s","blocks":["BlockDestroy","BlockChange"]}]`, 170 s.State.EnvironUUID(), s.AdminUserTag(c).String()) 171 172 strippedOut := strings.Replace(testing.Stdout(ctx), "\n", "", -1) 173 c.Check(strippedOut, gc.Equals, expected) 174 }