Merge lp:~fgimenez/snappy/test-writable-paths into lp:~snappy-dev/snappy/snappy-moved-to-github

Proposed by Federico Gimenez on 2015-07-17
Status: Merged
Approved by: Leo Arias on 2015-07-17
Approved revision: 590
Merged at revision: 590
Proposed branch: lp:~fgimenez/snappy/test-writable-paths
Merge into: lp:~snappy-dev/snappy/snappy-moved-to-github
Diff against target: 110 lines (+106/-0)
1 file modified
_integration-tests/tests/latest/writablePaths_test.go (+106/-0)
To merge this branch: bzr merge lp:~fgimenez/snappy/test-writable-paths
Reviewer Review Type Date Requested Status
Leo Arias 2015-07-17 Approve on 2015-07-17
Review via email: mp+265110@code.launchpad.net

Commit Message

Checks that the directories listed in /etc/system-image/writable-paths are writable

Description of the Change

Checks that the directories listed in /etc/system-image/writable-paths are writable.

For reading the file with the writable paths uses a bufio.Scanner, so that we don't need to load the entire file contents in memory.

It also includes a custom checker, IsWritable

To post a comment you must log in.
588. By Federico Gimenez on 2015-07-17

Proper removal of created files and message for creation error

589. By Federico Gimenez on 2015-07-17

path variable name

Leo Arias (elopio) wrote :

Really good stuff

review: Approve
590. By Federico Gimenez on 2015-07-17

Returning named parameters

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file '_integration-tests/tests/latest/writablePaths_test.go'
2--- _integration-tests/tests/latest/writablePaths_test.go 1970-01-01 00:00:00 +0000
3+++ _integration-tests/tests/latest/writablePaths_test.go 2015-07-17 16:16:14 +0000
4@@ -0,0 +1,106 @@
5+// -*- Mode: Go; indent-tabs-mode: t -*-
6+
7+/*
8+ * Copyright (C) 2015 Canonical Ltd
9+ *
10+ * This program is free software: you can redistribute it and/or modify
11+ * it under the terms of the GNU General Public License version 3 as
12+ * published by the Free Software Foundation.
13+ *
14+ * This program is distributed in the hope that it will be useful,
15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+ * GNU General Public License for more details.
18+ *
19+ * You should have received a copy of the GNU General Public License
20+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
21+ *
22+ */
23+
24+package latest
25+
26+import (
27+ "bufio"
28+ "fmt"
29+ "os"
30+ "os/exec"
31+ "path/filepath"
32+ "strings"
33+
34+ . "../common"
35+
36+ check "gopkg.in/check.v1"
37+)
38+
39+const writablePathsListFile = "/etc/system-image/writable-paths"
40+
41+var _ = check.Suite(&writablePathsSuite{})
42+
43+type writablePathsSuite struct {
44+ SnappySuite
45+}
46+
47+var IsWritable check.Checker = &isWritable{}
48+
49+type isWritable struct {
50+}
51+
52+func (is *isWritable) Info() *check.CheckerInfo {
53+ return &check.CheckerInfo{Name: "IsWritable", Params: []string{"path"}}
54+}
55+
56+func (is *isWritable) Check(params []interface{}, names []string) (result bool, error string) {
57+ if path, ok := params[0].(string); ok {
58+ filename := filepath.Join(path, "tmpfile")
59+
60+ cmd := exec.Command("sudo", "touch", filename)
61+ rmCmd := exec.Command("sudo", "rm", filename)
62+ defer rmCmd.Run()
63+
64+ if _, err := cmd.CombinedOutput(); err == nil {
65+ result = true
66+ } else {
67+ error = fmt.Sprintf("Error creating file %s", filename)
68+ }
69+ } else {
70+ error = fmt.Sprintf("First param of checker %v is of type %T and it should be a string", params[0], params[0])
71+ }
72+ return result, error
73+}
74+
75+func (s *writablePathsSuite) TestWritablePathsAreWritable(c *check.C) {
76+ for writablePath := range generateWritablePaths(c) {
77+ c.Logf("Checking if %s is writable", writablePath)
78+ c.Check(writablePath, IsWritable)
79+ }
80+}
81+
82+func generateWritablePaths(c *check.C) chan string {
83+ ch := make(chan string)
84+
85+ go func() {
86+ file, err := os.Open(writablePathsListFile)
87+
88+ c.Assert(err, check.IsNil,
89+ check.Commentf("Error reading writable files list %s", writablePathsListFile))
90+
91+ defer file.Close()
92+
93+ reader := bufio.NewReader(file)
94+ scanner := bufio.NewScanner(reader)
95+
96+ scanner.Split(bufio.ScanLines)
97+
98+ for scanner.Scan() {
99+ fields := strings.Fields(scanner.Text())
100+ if len(fields) > 0 && fields[0] != "#" {
101+ if src, err := os.Stat(fields[0]); err == nil && src.IsDir() {
102+ ch <- fields[0]
103+ }
104+ }
105+ }
106+ close(ch)
107+ }()
108+
109+ return ch
110+}

Subscribers

People subscribed via source and target branches