File: service_scope_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 (82 lines) | stat: -rw-r--r-- 3,122 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
// -*- 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"
	. "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)
	}
}