File: service_scope_test.go

package info (click to toggle)
snapd 2.74.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,428 kB
  • sloc: sh: 16,966; ansic: 16,788; python: 11,332; makefile: 1,897; exp: 190; awk: 58; xml: 22
file content (138 lines) | stat: -rw-r--r-- 4,718 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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2024 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 clientutil_test

import (
	"github.com/snapcore/snapd/client"
	"github.com/snapcore/snapd/client/clientutil"
	"github.com/snapcore/snapd/snap"

	. "gopkg.in/check.v1"
)

type serviceScopeSuite struct{}

var _ = Suite(&serviceScopeSuite{})

func (s *serviceScopeSuite) TestScopes(c *C) {
	tests := []struct {
		opts     clientutil.ServiceScopeOptions
		expected client.ScopeSelector
	}{
		// when expected is nil it means both scopes
		{clientutil.ServiceScopeOptions{}, nil},
		{clientutil.ServiceScopeOptions{User: true}, client.ScopeSelector{"user"}},
		{clientutil.ServiceScopeOptions{Usernames: "all"}, client.ScopeSelector{"user"}},
		{clientutil.ServiceScopeOptions{System: true}, client.ScopeSelector{"system"}},
		{clientutil.ServiceScopeOptions{User: true, System: true}, nil},
		{clientutil.ServiceScopeOptions{Usernames: "all", System: true}, nil},
	}

	for _, t := range tests {
		c.Check(t.opts.Scope(), DeepEquals, t.expected)
	}
}

func (s *serviceScopeSuite) TestUsers(c *C) {
	tests := []struct {
		opts     clientutil.ServiceScopeOptions
		expected client.UserSelector
	}{
		{clientutil.ServiceScopeOptions{}, client.UserSelector{Names: []string{}, Selector: client.UserSelectionList}},
		{clientutil.ServiceScopeOptions{User: true}, client.UserSelector{Selector: client.UserSelectionSelf}},
		{clientutil.ServiceScopeOptions{Usernames: "all"}, client.UserSelector{Selector: client.UserSelectionAll}},
		{clientutil.ServiceScopeOptions{System: true}, client.UserSelector{Names: []string{}, Selector: client.UserSelectionList}},
		{clientutil.ServiceScopeOptions{User: true, System: true}, client.UserSelector{Selector: client.UserSelectionSelf}},
		{clientutil.ServiceScopeOptions{Usernames: "all", System: true}, client.UserSelector{Selector: client.UserSelectionAll}},
	}

	for _, t := range tests {
		c.Check(t.opts.Users(), DeepEquals, t.expected)
	}
}

func (s *serviceScopeSuite) TestInvalidOptions(c *C) {
	tests := []struct {
		opts     clientutil.ServiceScopeOptions
		expected string
	}{
		{clientutil.ServiceScopeOptions{Usernames: "foo"}, `only "all" is supported as a value for --users`},
		{clientutil.ServiceScopeOptions{User: true, System: true}, `--system and --user cannot be used in conjunction with each other`},
		{clientutil.ServiceScopeOptions{Usernames: "all", User: true}, `--user and --users cannot be used in conjunction with each other`},
	}

	for _, t := range tests {
		c.Check(t.opts.Validate(), ErrorMatches, t.expected)
	}
}

func (s *serviceScopeSuite) TestFmtServiceStatus(c *C) {
	out := clientutil.FmtServiceStatus(&client.AppInfo{
		Snap: "test-snap",
		Name: "bar",
	}, clientutil.FmtServiceStatusOptions{})
	c.Check(out, Equals, "test-snap.bar\tdisabled\tinactive\t-")

	out = clientutil.FmtServiceStatus(&client.AppInfo{
		Snap:    "test-snap",
		Name:    "bar",
		Active:  true,
		Enabled: true,
	}, clientutil.FmtServiceStatusOptions{})
	c.Check(out, Equals, "test-snap.bar\tenabled\tactive\t-")

	out = clientutil.FmtServiceStatus(&client.AppInfo{
		Snap:        "test-snap",
		Name:        "bar",
		Active:      true,
		Enabled:     true,
		DaemonScope: snap.UserDaemon,
	}, clientutil.FmtServiceStatusOptions{})
	c.Check(out, Equals, "test-snap.bar\tenabled\tactive\t-")

	out = clientutil.FmtServiceStatus(&client.AppInfo{
		Snap:        "test-snap",
		Name:        "bar",
		Active:      true,
		Enabled:     true,
		DaemonScope: snap.UserDaemon,
	}, clientutil.FmtServiceStatusOptions{
		IsUserGlobal: true,
	})
	c.Check(out, Equals, "test-snap.bar\tenabled\t-\t-")

	out = clientutil.FmtServiceStatus(&client.AppInfo{
		Snap:    "test-snap_foo",
		Name:    "bar",
		Active:  true,
		Enabled: true,
	}, clientutil.FmtServiceStatusOptions{})
	c.Check(out, Equals, "test-snap_foo.bar\tenabled\tactive\t-")

	out = clientutil.FmtServiceStatus(&client.AppInfo{
		Snap:    "test-snap_foo",
		Name:    "bar",
		Active:  true,
		Enabled: true,
	}, clientutil.FmtServiceStatusOptions{
		DropSnapInstanceKey: true,
	})
	c.Check(out, Equals, "test-snap.bar\tenabled\tactive\t-")
}