OLD | NEW |
1 package testing | 1 package testing |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "io/ioutil" | 5 "io/ioutil" |
6 . "launchpad.net/gocheck" | 6 . "launchpad.net/gocheck" |
7 "launchpad.net/juju-core/charm" | 7 "launchpad.net/juju-core/charm" |
8 "launchpad.net/juju-core/environs" | 8 "launchpad.net/juju-core/environs" |
9 "launchpad.net/juju-core/environs/dummy" | 9 "launchpad.net/juju-core/environs/dummy" |
10 "launchpad.net/juju-core/juju" | 10 "launchpad.net/juju-core/juju" |
11 state "launchpad.net/juju-core/state" | 11 state "launchpad.net/juju-core/state" |
12 "launchpad.net/juju-core/testing" | 12 "launchpad.net/juju-core/testing" |
13 "os" | 13 "os" |
14 "path/filepath" | 14 "path/filepath" |
15 ) | 15 ) |
16 | 16 |
17 // JujuConnSuite provides a freshly bootstrapped juju.Conn | 17 // JujuConnSuite provides a freshly bootstrapped juju.Conn |
18 // for each test. It also includes testing.LoggingSuite. | 18 // for each test. It also includes testing.LoggingSuite. |
19 // | 19 // |
20 // It also sets up $HOME and environs.VarDir to | 20 // It also sets up RootDir to point to a directory hierarchy |
21 // temporary directories; the former is primed to | 21 // mirroring the intended juju directory structure, including |
22 // hold the dummy environments.yaml file. | 22 // the following: |
23 // | 23 // » RootDir/home/ubuntu/.juju/environments.yaml |
24 // The name of the dummy environment is "dummyenv". | 24 //» » The dummy environments.yaml file, holding |
| 25 //» » a default environment named "dummyenv" |
| 26 //» » which uses the "dummy" environment type. |
| 27 //» RootDir/var/lib/juju |
| 28 //» » An empty directory returned as DataDir - the |
| 29 //» » root of the juju data storage space. |
| 30 // $HOME is set to point to RootDir/home/ubuntu. |
25 type JujuConnSuite struct { | 31 type JujuConnSuite struct { |
26 testing.LoggingSuite | 32 testing.LoggingSuite |
27 testing.ZkSuite | 33 testing.ZkSuite |
28 » Conn *juju.Conn | 34 » Conn *juju.Conn |
29 » State *state.State | 35 » State *state.State |
30 » rootDir string // the faked-up root directory. | 36 » RootDir string // The faked-up root directory. |
31 » oldHome string | 37 » oldHome string |
32 » oldVarDir string | |
33 } | 38 } |
34 | 39 |
35 var config = []byte(` | 40 var config = []byte(` |
36 environments: | 41 environments: |
37 dummyenv: | 42 dummyenv: |
38 type: dummy | 43 type: dummy |
39 zookeeper: true | 44 zookeeper: true |
40 authorized-keys: 'i-am-a-key' | 45 authorized-keys: 'i-am-a-key' |
41 `) | 46 `) |
42 | 47 |
(...skipping 20 matching lines...) Expand all Loading... |
63 } | 68 } |
64 | 69 |
65 // Reset returns environment state to that which existed at the start of | 70 // Reset returns environment state to that which existed at the start of |
66 // the test. | 71 // the test. |
67 func (s *JujuConnSuite) Reset(c *C) { | 72 func (s *JujuConnSuite) Reset(c *C) { |
68 s.tearDownConn(c) | 73 s.tearDownConn(c) |
69 s.setUpConn(c) | 74 s.setUpConn(c) |
70 } | 75 } |
71 | 76 |
72 func (s *JujuConnSuite) setUpConn(c *C) { | 77 func (s *JujuConnSuite) setUpConn(c *C) { |
73 » if s.rootDir != "" { | 78 » if s.RootDir != "" { |
74 panic("JujuConnSuite.setUpConn without teardown") | 79 panic("JujuConnSuite.setUpConn without teardown") |
75 } | 80 } |
76 » s.rootDir = c.MkDir() | 81 » s.RootDir = c.MkDir() |
77 s.oldHome = os.Getenv("HOME") | 82 s.oldHome = os.Getenv("HOME") |
78 » home := filepath.Join(s.rootDir, "/home/ubuntu") | 83 » home := filepath.Join(s.RootDir, "/home/ubuntu") |
79 err := os.MkdirAll(home, 0777) | 84 err := os.MkdirAll(home, 0777) |
80 c.Assert(err, IsNil) | 85 c.Assert(err, IsNil) |
81 os.Setenv("HOME", home) | 86 os.Setenv("HOME", home) |
82 | 87 |
83 » s.oldVarDir = environs.VarDir | 88 » dataDir := filepath.Join(s.RootDir, "/var/lib/juju") |
84 » varDir := filepath.Join(s.rootDir, environs.VarDir) | 89 » err = os.MkdirAll(dataDir, 0777) |
85 » err = os.MkdirAll(varDir, 0777) | |
86 c.Assert(err, IsNil) | 90 c.Assert(err, IsNil) |
87 environs.VarDir = varDir | |
88 | 91 |
89 err = os.Mkdir(filepath.Join(home, ".juju"), 0777) | 92 err = os.Mkdir(filepath.Join(home, ".juju"), 0777) |
90 c.Assert(err, IsNil) | 93 c.Assert(err, IsNil) |
91 | 94 |
92 err = ioutil.WriteFile(filepath.Join(home, ".juju", "environments.yaml")
, config, 0600) | 95 err = ioutil.WriteFile(filepath.Join(home, ".juju", "environments.yaml")
, config, 0600) |
93 c.Assert(err, IsNil) | 96 c.Assert(err, IsNil) |
94 | 97 |
95 environ, err := environs.NewFromName("dummyenv") | 98 environ, err := environs.NewFromName("dummyenv") |
96 c.Assert(err, IsNil) | 99 c.Assert(err, IsNil) |
97 // sanity check we've got the correct environment. | 100 // sanity check we've got the correct environment. |
98 c.Assert(environ.Name(), Equals, "dummyenv") | 101 c.Assert(environ.Name(), Equals, "dummyenv") |
99 c.Assert(environ.Bootstrap(false), IsNil) | 102 c.Assert(environ.Bootstrap(false), IsNil) |
100 | 103 |
101 conn, err := juju.NewConnFromName("dummyenv") | 104 conn, err := juju.NewConnFromName("dummyenv") |
102 c.Assert(err, IsNil) | 105 c.Assert(err, IsNil) |
103 s.Conn = conn | 106 s.Conn = conn |
104 s.State = conn.State | 107 s.State = conn.State |
105 c.Assert(err, IsNil) | 108 c.Assert(err, IsNil) |
106 } | 109 } |
107 | 110 |
108 func (s *JujuConnSuite) tearDownConn(c *C) { | 111 func (s *JujuConnSuite) tearDownConn(c *C) { |
109 dummy.Reset() | 112 dummy.Reset() |
110 c.Assert(s.Conn.Close(), IsNil) | 113 c.Assert(s.Conn.Close(), IsNil) |
111 s.Conn = nil | 114 s.Conn = nil |
112 s.State = nil | 115 s.State = nil |
113 os.Setenv("HOME", s.oldHome) | 116 os.Setenv("HOME", s.oldHome) |
114 s.oldHome = "" | 117 s.oldHome = "" |
115 » environs.VarDir = s.oldVarDir | 118 » s.RootDir = "" |
116 » s.oldVarDir = "" | 119 } |
117 » s.rootDir = "" | 120 |
| 121 func (s *JujuConnSuite) DataDir() string { |
| 122 » if s.RootDir == "" { |
| 123 » » panic("DataDir called out of test context") |
| 124 » } |
| 125 » return filepath.Join(s.RootDir, "/var/lib/juju") |
118 } | 126 } |
119 | 127 |
120 // WriteConfig writes a juju config file to the "home" directory. | 128 // WriteConfig writes a juju config file to the "home" directory. |
121 func (s *JujuConnSuite) WriteConfig(config string) { | 129 func (s *JujuConnSuite) WriteConfig(config string) { |
122 » if s.rootDir == "" { | 130 » if s.RootDir == "" { |
123 panic("SetUpTest has not been called; will not overwrite $HOME/.
juju/environments.yaml") | 131 panic("SetUpTest has not been called; will not overwrite $HOME/.
juju/environments.yaml") |
124 } | 132 } |
125 path := filepath.Join(os.Getenv("HOME"), ".juju", "environments.yaml") | 133 path := filepath.Join(os.Getenv("HOME"), ".juju", "environments.yaml") |
126 err := ioutil.WriteFile(path, []byte(config), 0600) | 134 err := ioutil.WriteFile(path, []byte(config), 0600) |
127 if err != nil { | 135 if err != nil { |
128 panic(err) | 136 panic(err) |
129 } | 137 } |
130 } | 138 } |
131 | 139 |
132 func (s *JujuConnSuite) StateInfo(c *C) *state.Info { | 140 func (s *JujuConnSuite) StateInfo(c *C) *state.Info { |
133 return &state.Info{Addrs: []string{testing.ZkAddr}} | 141 return &state.Info{Addrs: []string{testing.ZkAddr}} |
134 } | 142 } |
135 | 143 |
136 func (s *JujuConnSuite) AddTestingCharm(c *C, name string) *state.Charm { | 144 func (s *JujuConnSuite) AddTestingCharm(c *C, name string) *state.Charm { |
137 ch := testing.Charms.Dir(name) | 145 ch := testing.Charms.Dir(name) |
138 ident := fmt.Sprintf("%s-%d", name, ch.Revision()) | 146 ident := fmt.Sprintf("%s-%d", name, ch.Revision()) |
139 curl := charm.MustParseURL("local:series/" + ident) | 147 curl := charm.MustParseURL("local:series/" + ident) |
140 sch, err := s.Conn.PutCharm(curl, testing.Charms.Path, false) | 148 sch, err := s.Conn.PutCharm(curl, testing.Charms.Path, false) |
141 c.Assert(err, IsNil) | 149 c.Assert(err, IsNil) |
142 return sch | 150 return sch |
143 } | 151 } |
OLD | NEW |