Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(275)

Unified Diff: juju/testing/conn.go

Issue 6484051: cmd/juju: simplify testing infrastructure.
Patch Set: cmd/juju: simplify testing infrastructure. Created 12 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cmd/juju/unexpose_test.go ('k') | worker/uniter/uniter_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: juju/testing/conn.go
=== modified file 'juju/testing/conn.go'
--- juju/testing/conn.go 2012-08-23 15:13:31 +0000
+++ juju/testing/conn.go 2012-08-29 13:13:38 +0000
@@ -2,24 +2,45 @@
import (
"fmt"
+ "io/ioutil"
. "launchpad.net/gocheck"
"launchpad.net/juju-core/charm"
+ "launchpad.net/juju-core/environs"
"launchpad.net/juju-core/environs/dummy"
"launchpad.net/juju-core/juju"
state "launchpad.net/juju-core/state"
"launchpad.net/juju-core/testing"
"net/url"
+ "os"
+ "path/filepath"
)
// JujuConnSuite provides a freshly bootstrapped juju.Conn
// for each test. It also includes testing.LoggingSuite.
+//
+// It also sets up $HOME and environs.VarDir to
+// temporary directories; the former is primed to
+// hold the dummy environments.yaml file.
+//
+// The name of the dummy environment is "dummyenv".
type JujuConnSuite struct {
testing.LoggingSuite
testing.ZkSuite
- Conn *juju.Conn
- State *state.State
+ Conn *juju.Conn
+ State *state.State
+ rootDir string // the faked-up root directory.
+ oldHome string
+ oldVarDir string
}
+var config = []byte(`
+environments:
+ dummyenv:
+ type: dummy
+ zookeeper: true
+ authorized-keys: 'i-am-a-key'
+`)
+
func (s *JujuConnSuite) SetUpTest(c *C) {
s.LoggingSuite.SetUpTest(c)
s.setUpConn(c)
@@ -37,6 +58,64 @@
s.setUpConn(c)
}
+func (s *JujuConnSuite) setUpConn(c *C) {
+ if s.rootDir != "" {
+ panic("JujuConnSuite.setUpConn without teardown")
+ }
+ s.rootDir = c.MkDir()
+ s.oldHome = os.Getenv("HOME")
+ home := filepath.Join(s.rootDir, "/home/ubuntu")
+ err := os.MkdirAll(home, 0777)
+ c.Assert(err, IsNil)
+ os.Setenv("HOME", home)
+
+ s.oldVarDir = environs.VarDir
+ varDir := filepath.Join(s.rootDir, environs.VarDir)
+ err = os.MkdirAll(varDir, 0777)
+ c.Assert(err, IsNil)
+ environs.VarDir = varDir
+
+ err = os.Mkdir(filepath.Join(home, ".juju"), 0777)
+ c.Assert(err, IsNil)
+
+ err = ioutil.WriteFile(filepath.Join(home, ".juju", "environments.yaml"), config, 0600)
+ c.Assert(err, IsNil)
+ conn, err := juju.NewConn("dummyenv")
+ c.Assert(err, IsNil)
+
+ // sanity check we've got the correct environment.
+ c.Assert(conn.Environ.Name(), Equals, "dummyenv")
+
+ c.Assert(conn.Bootstrap(false), IsNil)
+ s.Conn = conn
+ s.State, err = conn.State()
+ c.Assert(err, IsNil)
+}
+
+func (s *JujuConnSuite) tearDownConn(c *C) {
+ dummy.Reset()
+ c.Assert(s.Conn.Close(), IsNil)
+ s.Conn = nil
+ s.State = nil
+ os.Setenv("HOME", s.oldHome)
+ s.oldHome = ""
+ environs.VarDir = s.oldVarDir
+ s.oldVarDir = ""
+ s.rootDir = ""
+}
+
+// WriteConfig writes a juju config file to the "home" directory.
+func (s *JujuConnSuite) WriteConfig(config string) {
+ if s.rootDir == "" {
+ panic("SetUpTest has not been called; will not overwrite $HOME/.juju/environments.yaml")
+ }
+ path := filepath.Join(os.Getenv("HOME"), ".juju", "environments.yaml")
+ err := ioutil.WriteFile(path, []byte(config), 0600)
+ if err != nil {
+ panic(err)
+ }
+}
+
func (s *JujuConnSuite) StateInfo(c *C) *state.Info {
return &state.Info{Addrs: []string{testing.ZkAddr}}
}
@@ -51,26 +130,3 @@
c.Assert(err, IsNil)
return sch
}
-
-func (s *JujuConnSuite) setUpConn(c *C) {
- config := map[string]interface{}{
- "name": "foo",
- "type": "dummy",
- "zookeeper": true,
- "authorized-keys": "i-am-a-key",
- }
- conn, err := juju.NewConnFromAttrs(config)
- c.Assert(err, IsNil)
-
- c.Assert(conn.Bootstrap(false), IsNil)
- s.Conn = conn
- s.State, err = conn.State()
- c.Assert(err, IsNil)
-}
-
-func (s *JujuConnSuite) tearDownConn(c *C) {
- dummy.Reset()
- c.Assert(s.Conn.Close(), IsNil)
- s.Conn = nil
- s.State = nil
-}
« no previous file with comments | « cmd/juju/unexpose_test.go ('k') | worker/uniter/uniter_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b