File: dirs_test.go

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (231 lines) | stat: -rw-r--r-- 5,753 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2025 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package syscheck_test

import (
	"fmt"
	"os"
	"path/filepath"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/dirs"
	"github.com/snapcore/snapd/dirs/dirstest"
	"github.com/snapcore/snapd/release"
	"github.com/snapcore/snapd/syscheck"
	"github.com/snapcore/snapd/testutil"
)

type dirsSuite struct {
	testutil.BaseTest
}

var _ = Suite(&dirsSuite{})

func (s *dirsSuite) SetUpTest(c *C) {
	s.BaseTest.SetUpTest(c)

	s.AddCleanup(release.MockReleaseInfo(&release.OS{
		ID: "funkylunix",
	}))
	s.AddCleanup(func() { dirs.SetRootDir("") })
}

func (s *dirsSuite) TestHappy(c *C) {
	dir := c.MkDir()

	dirstest.MustMockCanonicalSnapMountDir(dir)
	dirs.SetRootDir(dir)
	c.Check(dirs.SnapMountDirDetectionOutcome(), IsNil)

	c.Check(syscheck.CheckSnapMountDir(), IsNil)
}

func (s *dirsSuite) TestUndetermined(c *C) {
	d := c.MkDir()
	// pretend we have a relative symlink
	c.Assert(os.Symlink("foo/bar", filepath.Join(d, "/snap")), IsNil)
	dirs.SetRootDir(d)
	c.Logf("dir: %v", dirs.SnapMountDir)

	c.Check(dirs.SnapMountDirDetectionOutcome(), NotNil)

	c.Check(syscheck.CheckSnapMountDir(), ErrorMatches, "cannot resolve snap mount directory: .*")
}

var (
	known = []struct {
		ID           string
		IDLike       []string
		canonicalDir bool
	}{
		{"fedora", nil, false},
		{"rhel", []string{"fedora"}, false},
		{"centos", []string{"fedora"}, false},
		{"ubuntu", []string{"debian"}, true},
		{"debian", nil, true},
		{"suse", nil, true},
		{"yocto", nil, true},
		{"arch", []string{"archlinux"}, false},
		{"archlinux", nil, false},
		{"altlinux", nil, false},
	}

	knownSpecial = []struct {
		ID  string
		dir string
	}{
		{"ubuntucoreinitramfs", dirs.DefaultSnapMountDir},
	}
)

func (s *dirsSuite) TestMountDirKnownDistroHappy(c *C) {
	defer dirs.SetRootDir("")

	for _, tc := range known {
		c.Logf("happy case %+v", tc)
		func() {
			defer release.MockReleaseInfo(&release.OS{ID: tc.ID, IDLike: tc.IDLike})()
			d := c.MkDir()
			if tc.canonicalDir {
				dirstest.MustMockCanonicalSnapMountDir(d)
			} else {
				dirstest.MustMockAltSnapMountDir(d)
			}
			dirs.SetRootDir(d)

			c.Check(syscheck.CheckSnapMountDir(), IsNil)
		}()
	}
}

func (s *dirsSuite) TestMountDirKnownDistroMismatch(c *C) {
	defer dirs.SetRootDir("")

	for _, tc := range known {
		c.Logf("mismatch case %+v", tc)
		func() {
			defer release.MockReleaseInfo(&release.OS{ID: tc.ID, IDLike: tc.IDLike})()
			d := c.MkDir()
			// do the complete opposite so that the mount directory does not
			// have the expected value based on what we know about distributions
			// packaging snapd
			if tc.canonicalDir {
				dirstest.MustMockAltSnapMountDir(d)
			} else {
				dirstest.MustMockCanonicalSnapMountDir(d)
			}
			dirs.SetRootDir(d)

			c.Check(syscheck.CheckSnapMountDir(), ErrorMatches,
				fmt.Sprintf("unexpected snap mount directory /.*snap on %s", tc.ID))
		}()
	}
}

func (s *dirsSuite) TestMountDirKnownDistroSpecial(c *C) {
	defer dirs.SetRootDir("")

	for _, tc := range knownSpecial {
		c.Logf("distro special case %+v", tc)
		func() {
			defer release.MockReleaseInfo(&release.OS{ID: tc.ID})()
			d := c.MkDir()
			dirs.SetRootDir(d)

			c.Check(syscheck.CheckSnapMountDir(), IsNil)
		}()
	}
}

func (s *dirsSuite) TestLibExecDirDefault(c *C) {
	defer dirs.SetRootDir("")

	for _, tc := range syscheck.DefaultLibExecDirDistros {
		c.Logf("distro libexecdir default case %+v", tc)
		func() {
			defer release.MockReleaseInfo(&release.OS{ID: tc})()
			d := c.MkDir()
			dirstest.MustMockDefaultLibExecDir(d)
			dirs.SetRootDir(d)

			c.Check(syscheck.CheckLibExecDir(), IsNil)
		}()
	}
}

func (s *dirsSuite) TestLibExecDirAlt(c *C) {
	defer dirs.SetRootDir("")

	for _, tc := range syscheck.AltLibExecDirDistros {
		c.Logf("distro libexecdir alt case %+v", tc)
		func() {
			defer release.MockReleaseInfo(&release.OS{ID: tc})()
			d := c.MkDir()
			dirstest.MustMockAltLibExecDir(d)
			dirs.SetRootDir(d)

			c.Check(syscheck.CheckLibExecDir(), IsNil)
		}()
	}
}

func (s *dirsSuite) TestLibExecDirUnexpected(c *C) {
	defer dirs.SetRootDir("")

	for _, tc := range append(syscheck.DefaultLibExecDirDistros, syscheck.AltLibExecDirDistros...) {
		c.Logf("distro mismatch case %+v", tc)
		func() {
			defer release.MockReleaseInfo(&release.OS{ID: tc})()
			d := c.MkDir()
			switch tc {
			case "fedora", "opensuse-tumbleweed", "opensuse-slowroll":
				dirstest.MustMockDefaultLibExecDir(d)
			default:
				dirstest.MustMockAltLibExecDir(d)
			}
			dirs.SetRootDir(d)

			c.Check(syscheck.CheckLibExecDir(), ErrorMatches, "unexpected snapd tooling directory /usr/lib(exec)?/snapd on "+tc)
		}()
	}
}

func (s *dirsSuite) TestLibExecDirUnknownDistro(c *C) {
	defer dirs.SetRootDir("")

	func() {
		defer release.MockReleaseInfo(&release.OS{ID: "not-fedora"})()
		d := c.MkDir()
		dirstest.MustMockDefaultLibExecDir(d)
		dirs.SetRootDir(d)

		c.Check(syscheck.CheckLibExecDir(), IsNil)
	}()

	func() {
		defer release.MockReleaseInfo(&release.OS{ID: "not-ubuntu"})()
		d := c.MkDir()
		dirstest.MustMockAltLibExecDir(d)
		dirs.SetRootDir(d)

		c.Check(syscheck.CheckLibExecDir(), IsNil)
	}()
}