Merge lp:~themue/juju-core/016-juju-home into lp:~juju/juju-core/trunk

Proposed by Frank Mueller
Status: Merged
Approved by: William Reade
Approved revision: no longer in the source branch.
Merged at revision: 1013
Proposed branch: lp:~themue/juju-core/016-juju-home
Merge into: lp:~juju/juju-core/trunk
Diff against target: 130 lines (+121/-0)
2 files modified
environs/config/home.go (+56/-0)
environs/config/home_test.go (+65/-0)
To merge this branch: bzr merge lp:~themue/juju-core/016-juju-home
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+153742@code.launchpad.net

Description of the change

config: added functions to access juju home

This first CL introduces functions for the access to
$JUJU_HOME - if set - or $HOME/.juju as fallback. The
functions return the directory, allow to create a path
to a file/dir in it, set it for testing purposes and
restore it afterwards. A second CL will then migrate
all manual access to $HOME/.juju today to the usage
of the new API.

https://codereview.appspot.com/7774044/

To post a comment you must log in.
Revision history for this message
Dimiter Naydenov (dimitern) wrote :

LGTM with a trivial suggestion.

https://codereview.appspot.com/7774044/diff/1/environs/config/home_test.go
File environs/config/home_test.go (right):

https://codereview.appspot.com/7774044/diff/1/environs/config/home_test.go#newcode26
environs/config/home_test.go:26: config.RestoreJujuHome()
probably you don't need this in both setup and teardown

https://codereview.appspot.com/7774044/

Revision history for this message
William Reade (fwereade) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'environs/config/home.go'
2--- environs/config/home.go 1970-01-01 00:00:00 +0000
3+++ environs/config/home.go 2013-03-18 10:29:22 +0000
4@@ -0,0 +1,56 @@
5+package config
6+
7+import (
8+ "os"
9+ "path/filepath"
10+ "sync"
11+)
12+
13+// jujuHome stores the path to the juju configuration
14+// folder defined by $JUJU_HOME or default ~/.juju.
15+var (
16+ jujuHomeMu sync.Mutex
17+ jujuHome string
18+)
19+
20+func init() {
21+ RestoreJujuHome()
22+}
23+
24+// JujuHome returns the current juju home.
25+func JujuHome() string {
26+ return jujuHome
27+}
28+
29+// JujuHomePath returns the path to a file in the
30+// current juju home.
31+func JujuHomePath(names ...string) string {
32+ all := append([]string{jujuHome}, names...)
33+ return filepath.Join(all...)
34+}
35+
36+// SetTestJujuHome allows to set the value of juju home for test
37+// purposes. It returns the current juju home.
38+func SetTestJujuHome(home string) string {
39+ jujuHomeMu.Lock()
40+ defer jujuHomeMu.Unlock()
41+
42+ current := jujuHome
43+ jujuHome = home
44+ return current
45+}
46+
47+// RestoreJujuHome (re)initializes the juju home after it may
48+// have been changed for testing purposes. It returns the
49+// juju home.
50+func RestoreJujuHome() string {
51+ jujuHomeMu.Lock()
52+ defer jujuHomeMu.Unlock()
53+
54+ jujuHome = os.Getenv("JUJU_HOME")
55+ if jujuHome == "" {
56+ home := os.Getenv("HOME")
57+ jujuHome = filepath.Join(home, ".juju")
58+ }
59+ return jujuHome
60+}
61
62=== added file 'environs/config/home_test.go'
63--- environs/config/home_test.go 1970-01-01 00:00:00 +0000
64+++ environs/config/home_test.go 2013-03-18 10:29:22 +0000
65@@ -0,0 +1,65 @@
66+package config_test
67+
68+import (
69+ . "launchpad.net/gocheck"
70+ "os"
71+ "path/filepath"
72+
73+ "launchpad.net/juju-core/environs/config"
74+)
75+
76+type JujuHomeSuite struct {
77+ origJujuHome string
78+}
79+
80+var _ = Suite(&JujuHomeSuite{})
81+
82+func (s *JujuHomeSuite) SetUpSuite(c *C) {
83+ s.origJujuHome = config.JujuHome()
84+}
85+
86+func (s *JujuHomeSuite) TearDownSuite(c *C) {
87+ config.RestoreJujuHome()
88+}
89+
90+func (s *JujuHomeSuite) SetUpTest(c *C) {
91+ config.RestoreJujuHome()
92+}
93+
94+func (s *JujuHomeSuite) TearDownTest(c *C) {
95+ err := os.Setenv("JUJU_HOME", s.origJujuHome)
96+ c.Assert(err, IsNil)
97+}
98+
99+func (s *JujuHomeSuite) TestStandardHome(c *C) {
100+ home := os.Getenv("HOME")
101+ err := os.Setenv("JUJU_HOME", "")
102+ c.Assert(err, IsNil)
103+ jujuHome := config.RestoreJujuHome()
104+ newJujuHome := filepath.Join(home, ".juju")
105+ c.Assert(jujuHome, Equals, newJujuHome)
106+ jujuHome = config.JujuHome()
107+ c.Assert(jujuHome, Equals, newJujuHome)
108+ testJujuHome := c.MkDir()
109+ err = os.Setenv("JUJU_HOME", testJujuHome)
110+ jujuHome = config.RestoreJujuHome()
111+ c.Assert(jujuHome, Equals, testJujuHome)
112+ jujuHome = config.JujuHome()
113+ c.Assert(jujuHome, Equals, testJujuHome)
114+}
115+
116+func (s *JujuHomeSuite) TestHomePath(c *C) {
117+ envPath := config.JujuHomePath("environments.yaml")
118+ c.Assert(envPath, Equals, filepath.Join(s.origJujuHome, "environments.yaml"))
119+}
120+
121+func (s *JujuHomeSuite) TestTestHome(c *C) {
122+ jujuHome := config.JujuHome()
123+ testJujuHome := c.MkDir()
124+ origJujuHome := config.SetTestJujuHome(testJujuHome)
125+ c.Assert(jujuHome, Equals, origJujuHome)
126+ jujuHome = config.JujuHome()
127+ c.Assert(jujuHome, Equals, testJujuHome)
128+ jujuHome = config.RestoreJujuHome()
129+ c.Assert(jujuHome, Equals, origJujuHome)
130+}

Subscribers

People subscribed via source and target branches