This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrench_test.go
199 lines (160 loc) · 5.38 KB
/
wrench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package wrench_test
import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
stdtesting "testing"
"github.com/juju/loggo"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
coretesting "github.com/juju/juju/testing"
"github.com/juju/juju/wrench"
)
func TestPackage(t *stdtesting.T) {
gc.TestingT(t)
}
type wrenchSuite struct {
coretesting.BaseSuite
wrenchDir string
logWriter loggo.TestWriter
}
var _ = gc.Suite(&wrenchSuite{})
func (s *wrenchSuite) SetUpTest(c *gc.C) {
s.BaseSuite.SetUpTest(c)
// BaseSuite turns off wrench so restore the non-testing default.
wrench.SetEnabled(true)
logger := loggo.GetLogger("juju.wrench")
oldLevel := logger.LogLevel()
logger.SetLogLevel(loggo.TRACE)
c.Assert(loggo.RegisterWriter("wrench-tests", &s.logWriter), gc.IsNil)
s.AddCleanup(func(*gc.C) {
s.logWriter.Clear()
logger.SetLogLevel(oldLevel)
loggo.RemoveWriter("wrench-tests")
// Ensure the wrench is turned off when these tests are done.
wrench.SetEnabled(false)
})
}
func (s *wrenchSuite) createWrenchDir(c *gc.C) {
s.wrenchDir = c.MkDir()
s.PatchValue(wrench.WrenchDir, s.wrenchDir)
}
func (s *wrenchSuite) createWrenchFile(c *gc.C, name, content string) string {
filename := filepath.Join(s.wrenchDir, name)
err := ioutil.WriteFile(filename, []byte(content), 0700)
c.Assert(err, jc.ErrorIsNil)
return filename
}
func (s *wrenchSuite) TestIsActive(c *gc.C) {
s.createWrenchDir(c)
s.createWrenchFile(c, "foo", "bar")
c.Assert(wrench.IsActive("foo", "bar"), jc.IsTrue)
s.AssertActivationLogged(c)
}
func (s *wrenchSuite) TestIsActiveWithWhitespace(c *gc.C) {
s.createWrenchDir(c)
s.createWrenchFile(c, "foo", "\tbar ")
c.Assert(wrench.IsActive("foo", "bar"), jc.IsTrue)
s.AssertActivationLogged(c)
}
func (s *wrenchSuite) TestIsActiveMultiFeatures(c *gc.C) {
s.createWrenchDir(c)
s.createWrenchFile(c, "foo", "one\ntwo\nbar\n")
c.Assert(wrench.IsActive("foo", "bar"), jc.IsTrue)
s.AssertActivationLogged(c)
}
func (s *wrenchSuite) TestIsActiveMultiFeaturesWithMixedNewlines(c *gc.C) {
s.createWrenchDir(c)
s.createWrenchFile(c, "foo", "one\ntwo\r\nthree\nbar\n")
c.Assert(wrench.IsActive("foo", "bar"), jc.IsTrue)
s.AssertActivationLogged(c)
}
func (s *wrenchSuite) TestNotActive(c *gc.C) {
s.createWrenchDir(c)
s.createWrenchFile(c, "foo", "abc")
c.Assert(wrench.IsActive("foo", "bar"), jc.IsFalse)
s.AssertNothingLogged(c)
}
func (s *wrenchSuite) TestNoFile(c *gc.C) {
s.createWrenchDir(c)
c.Assert(wrench.IsActive("foo", "bar"), jc.IsFalse)
s.AssertFileErrorLogged(c)
}
func (s *wrenchSuite) TestMatchInOtherCategory(c *gc.C) {
s.createWrenchDir(c)
s.createWrenchFile(c, "other", "bar")
c.Assert(wrench.IsActive("foo", "bar"), jc.IsFalse)
s.AssertFileErrorLogged(c)
}
func (s *wrenchSuite) TestNoDirectory(c *gc.C) {
s.PatchValue(wrench.WrenchDir, "/does/not/exist")
c.Assert(wrench.IsActive("foo", "bar"), jc.IsFalse)
s.AssertDirErrorLogged(c)
}
func (s *wrenchSuite) TestFileNotOwnedByJujuUser(c *gc.C) {
s.createWrenchDir(c)
filename := s.createWrenchFile(c, "foo", "bar")
s.tweakOwner(c, filename)
c.Assert(wrench.IsActive("foo", "bar"), jc.IsFalse)
c.Assert(s.logWriter.Log(), jc.LogMatches, []jc.SimpleMessage{{
loggo.ERROR,
`wrench file for foo/bar has incorrect ownership - ignoring ` + filename,
}})
}
func (s *wrenchSuite) TestFilePermsTooLoose(c *gc.C) {
if runtime.GOOS == "windows" {
c.Skip("Windows is not fully POSIX compliant")
}
s.createWrenchDir(c)
filename := s.createWrenchFile(c, "foo", "bar")
err := os.Chmod(filename, 0666)
c.Assert(err, jc.ErrorIsNil)
c.Assert(wrench.IsActive("foo", "bar"), jc.IsFalse)
c.Assert(s.logWriter.Log(), jc.LogMatches, []jc.SimpleMessage{{
loggo.ERROR,
`wrench file for foo/bar should only be writable by owner - ignoring ` + filename,
}})
}
func (s *wrenchSuite) TestDirectoryNotOwnedByJujuUser(c *gc.C) {
s.createWrenchDir(c)
s.tweakOwner(c, s.wrenchDir)
c.Assert(wrench.IsActive("foo", "bar"), jc.IsFalse)
c.Assert(s.logWriter.Log(), jc.LogMatches, []jc.SimpleMessage{{
loggo.ERROR,
`wrench directory has incorrect ownership - wrench functionality disabled \(.+\)`,
}})
}
func (s *wrenchSuite) TestSetEnabled(c *gc.C) {
s.createWrenchDir(c)
s.createWrenchFile(c, "foo", "bar")
// Starts enabled.
c.Assert(wrench.IsEnabled(), jc.IsTrue)
c.Assert(wrench.IsActive("foo", "bar"), jc.IsTrue)
// Disable.
c.Assert(wrench.SetEnabled(false), jc.IsTrue)
c.Assert(wrench.IsEnabled(), jc.IsFalse)
c.Assert(wrench.IsActive("foo", "bar"), jc.IsFalse)
// Enable again.
c.Assert(wrench.SetEnabled(true), jc.IsFalse)
c.Assert(wrench.IsEnabled(), jc.IsTrue)
c.Assert(wrench.IsActive("foo", "bar"), jc.IsTrue)
}
var notJujuUid = uint32(os.Getuid() + 1)
func (s *wrenchSuite) AssertActivationLogged(c *gc.C) {
c.Assert(s.logWriter.Log(), jc.LogMatches, []jc.SimpleMessage{
{loggo.TRACE, `wrench for foo/bar is active`}})
}
func (s *wrenchSuite) AssertNothingLogged(c *gc.C) {
c.Assert(len(s.logWriter.Log()), gc.Equals, 0)
}
func (s *wrenchSuite) AssertFileErrorLogged(c *gc.C) {
c.Assert(s.logWriter.Log(), jc.LogMatches, []jc.SimpleMessage{
{loggo.TRACE, `no wrench data for foo/bar \(ignored\): ` + fileNotFound}})
}
func (s *wrenchSuite) AssertDirErrorLogged(c *gc.C) {
c.Assert(s.logWriter.Log(), jc.LogMatches, []jc.SimpleMessage{
{loggo.TRACE, `couldn't read wrench directory: ` + fileNotFound}})
}