File: common_test.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (158 lines) | stat: -rw-r--r-- 5,073 bytes parent folder | download | duplicates (2)
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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2017 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 main_test

import (
	"bytes"
	"context"
	"os"
	"path/filepath"

	. "gopkg.in/check.v1"

	update "github.com/snapcore/snapd/cmd/snap-update-ns"
	"github.com/snapcore/snapd/cmd/snaplock"
	"github.com/snapcore/snapd/dirs"
	"github.com/snapcore/snapd/osutil"
	"github.com/snapcore/snapd/sandbox/cgroup"
)

type commonSuite struct {
	dir   string
	upCtx *update.CommonProfileUpdateContext
}

var _ = Suite(&commonSuite{})

func (s *commonSuite) SetUpTest(c *C) {
	s.dir = c.MkDir()
	s.upCtx = update.NewCommonProfileUpdateContext("foo", false,
		filepath.Join(s.dir, "current.fstab"),
		filepath.Join(s.dir, "desired.fstab"))
}

func (s *commonSuite) TestInstanceName(c *C) {
	c.Check(s.upCtx.InstanceName(), Equals, "foo")
}

func (s *commonSuite) TestLock(c *C) {
	// Mock away real freezer code, allowing test code to return an error when freezing.
	var freezingError error
	restore := cgroup.MockFreezing(func(context.Context, string) error { return freezingError }, func(string) error { return nil })
	defer restore()
	// Mock system directories, we use the lock directory.
	dirs.SetRootDir(s.dir)
	defer dirs.SetRootDir("")

	// We will use 2nd lock for our testing.
	testLock, err := snaplock.OpenLock(s.upCtx.InstanceName())
	c.Assert(err, IsNil)
	defer testLock.Close()

	// When fromSnapConfine is false we acquire our own lock.
	s.upCtx.SetFromSnapConfine(false)
	c.Check(s.upCtx.FromSnapConfine(), Equals, false)
	unlock, err := s.upCtx.Lock()
	c.Assert(err, IsNil)
	// The lock is acquired now. We should not be able to get another lock.
	c.Check(testLock.TryLock(), Equals, osutil.ErrAlreadyLocked)
	// We can release the original lock now and see our test lock working.
	unlock()
	c.Assert(testLock.TryLock(), IsNil)

	// When fromSnapConfine is true we test existing lock but don't grab one.
	s.upCtx.SetFromSnapConfine(true)
	c.Check(s.upCtx.FromSnapConfine(), Equals, true)
	err = testLock.Lock()
	c.Assert(err, IsNil)
	unlock, err = s.upCtx.Lock()
	c.Assert(err, IsNil)
	unlock()

	// When the test lock is unlocked the common update helper reports an error
	// since it was expecting the lock to be held. Oh, and the lock is not leaked.
	testLock.Unlock()
	unlock, err = s.upCtx.Lock()
	c.Check(err, ErrorMatches, `mount namespace of snap "foo" is not locked but --from-snap-confine was used`)
	c.Check(unlock, IsNil)
	c.Assert(testLock.TryLock(), IsNil)

	// When freezing fails the lock acquired internally is not leaked.
	freezingError = errTesting
	s.upCtx.SetFromSnapConfine(false)
	c.Check(s.upCtx.FromSnapConfine(), Equals, false)
	testLock.Unlock()
	unlock, err = s.upCtx.Lock()
	c.Check(err, Equals, errTesting)
	c.Check(unlock, IsNil)
	c.Check(testLock.TryLock(), IsNil)
}

func (s *commonSuite) TestLoadDesiredProfile(c *C) {
	upCtx := s.upCtx
	text := "tmpfs /tmp tmpfs defaults 0 0\n"

	// Ask the common profile update helper to read the desired profile.
	profile, err := upCtx.LoadCurrentProfile()
	c.Assert(err, IsNil)

	// A profile that is not present on disk just reads as a valid empty profile.
	c.Check(profile.Entries, HasLen, 0)

	// Write a desired user mount profile for snap "foo".
	path := upCtx.DesiredProfilePath()
	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
	c.Assert(os.WriteFile(path, []byte(text), 0644), IsNil)

	// Ask the common profile update helper to read the desired profile.
	profile, err = upCtx.LoadDesiredProfile()
	c.Assert(err, IsNil)
	builder := &bytes.Buffer{}
	profile.WriteTo(builder)

	// The profile is returned unchanged.
	c.Check(builder.String(), Equals, text)
}

func (s *commonSuite) TestLoadCurrentProfile(c *C) {
	upCtx := s.upCtx
	text := "tmpfs /tmp tmpfs defaults 0 0\n"

	// Ask the common profile update helper to read the current profile.
	profile, err := upCtx.LoadCurrentProfile()
	c.Assert(err, IsNil)

	// A profile that is not present on disk just reads as a valid empty profile.
	c.Check(profile.Entries, HasLen, 0)

	// Write a current user mount profile for snap "foo".
	path := upCtx.CurrentProfilePath()
	c.Assert(os.MkdirAll(filepath.Dir(path), 0755), IsNil)
	c.Assert(os.WriteFile(path, []byte(text), 0644), IsNil)

	// Ask the common profile update helper to read the current profile.
	profile, err = upCtx.LoadCurrentProfile()
	c.Assert(err, IsNil)
	builder := &bytes.Buffer{}
	profile.WriteTo(builder)

	// The profile is returned unchanged.
	c.Check(builder.String(), Equals, text)
}