File: api_icons_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 (213 lines) | stat: -rw-r--r-- 6,657 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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2014-2020 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 daemon_test

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"os"
	"path/filepath"

	"gopkg.in/check.v1"

	"github.com/snapcore/snapd/daemon"
	"github.com/snapcore/snapd/overlord/snapstate/backend"
	"github.com/snapcore/snapd/snap"
	"github.com/snapcore/snapd/testutil"
)

var _ = check.Suite(&iconsSuite{})

type iconsSuite struct {
	apiBaseSuite
}

func (s *iconsSuite) expectIconsReadAccess() {
	s.expectReadAccess(daemon.InterfaceOpenAccess{Interfaces: []string{"desktop-launch", "snap-interfaces-requests-control", "snap-refresh-observe"}})
}

func (s *iconsSuite) TestSnapIconGet(c *check.C) {
	s.expectIconsReadAccess()
	d := s.daemon(c)

	// have an active foo in the system
	s.mkInstalledInState(c, d, "foo", "bar", "v1", snap.R(10), true, "")

	req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil)
	c.Assert(err, check.IsNil)

	rec := httptest.NewRecorder()

	s.req(c, req, nil, actionIsExpected).ServeHTTP(rec, req)
	c.Check(rec.Code, check.Equals, 200)
	c.Check(rec.Body.String(), check.Equals, "yadda icon")
}

func (s *iconsSuite) TestSnapIconGetPriority(c *check.C) {
	s.expectIconsReadAccess()
	d := s.daemon(c)

	checkIconContents := func(expected string) {
		req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil)
		c.Assert(err, check.IsNil)

		rec := httptest.NewRecorder()

		s.req(c, req, nil, actionIsExpected).ServeHTTP(rec, req)
		c.Check(rec.Code, check.Equals, 200)
		c.Check(rec.Body.String(), check.Equals, expected)
	}

	// have an active foo in the system
	info := s.mkInstalledInState(c, d, "foo", "bar", "v1", snap.R(10), true, "")

	// have various icons for it in the package itself
	icondir := filepath.Join(info.MountDir(), "meta", "gui")
	c.Assert(os.MkdirAll(icondir, 0o755), check.IsNil)
	c.Check(os.WriteFile(filepath.Join(icondir, "icon.jpg"), []byte("I'm a jpg"), 0o644), check.IsNil)
	c.Check(os.WriteFile(filepath.Join(icondir, "icon.svg"), []byte("I'm an svg"), 0o644), check.IsNil)
	c.Check(os.WriteFile(filepath.Join(icondir, "icon.png"), []byte("I'm a png"), 0o644), check.IsNil)

	// svg should have priority
	checkIconContents("I'm an svg")

	c.Check(os.Remove(filepath.Join(icondir, "icon.svg")), check.IsNil)

	// followed by png
	checkIconContents("I'm a png")

	c.Check(os.Remove(filepath.Join(icondir, "icon.png")), check.IsNil)

	// followed by whatever's left
	checkIconContents("I'm a jpg")
}

func (s *iconsSuite) TestSnapIconGetInactive(c *check.C) {
	s.expectIconsReadAccess()
	d := s.daemon(c)

	// have an *in*active foo in the system
	s.mkInstalledInState(c, d, "foo", "bar", "v1", snap.R(10), false, "")

	req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil)
	c.Assert(err, check.IsNil)

	rec := httptest.NewRecorder()

	s.req(c, req, nil, actionIsExpected).ServeHTTP(rec, req)
	c.Check(rec.Code, check.Equals, 200)
	c.Check(rec.Body.String(), check.Equals, "yadda icon")
}

func (s *iconsSuite) TestSnapIconGetNoIcon(c *check.C) {
	s.expectIconsReadAccess()
	d := s.daemon(c)

	info := s.mkInstalledInState(c, d, "foo", "bar", "v1", snap.R(10), true, "")

	// NO ICON!
	err := os.RemoveAll(filepath.Join(info.MountDir(), "meta", "gui", "icon.svg"))
	c.Assert(err, check.IsNil)

	req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil)
	c.Assert(err, check.IsNil)

	rec := httptest.NewRecorder()

	s.req(c, req, nil, actionIsExpected).ServeHTTP(rec, req)
	c.Check(rec.Code/100, check.Equals, 4)
}

func (s *iconsSuite) TestSnapIconGetFallback(c *check.C) {
	s.expectIconsReadAccess()
	d := s.daemon(c)

	const snapName = "foo"
	const snapID = "foo-id" // ID is "<name>-id" for apiBaseSuite

	info := s.mkInstalledInState(c, d, snapName, "bar", "v1", snap.R(10), true, "")

	// NO ICON IN SNAP!
	err := os.RemoveAll(filepath.Join(info.MountDir(), "meta", "gui", "icon.svg"))
	c.Assert(err, check.IsNil)

	// but fallback icon installed
	fallbackIcon := backend.IconInstallFilename(snapID)
	c.Assert(os.MkdirAll(filepath.Dir(fallbackIcon), 0o755), check.IsNil)
	c.Check(os.WriteFile(fallbackIcon, []byte("I'm from the store"), 0o644), check.IsNil)

	req, err := http.NewRequest("GET", fmt.Sprintf("/v2/icons/%s/icon", snapName), nil)
	c.Assert(err, check.IsNil)

	rec := httptest.NewRecorder()

	s.req(c, req, nil, actionIsExpected).ServeHTTP(rec, req)
	c.Check(rec.Code, check.Equals, 200)
	c.Check(rec.Body.String(), check.Equals, "I'm from the store")
}

func (s *iconsSuite) TestSnapIconGetUnasserted(c *check.C) {
	s.expectIconsReadAccess()
	d := s.daemon(c)

	const snapName = "foo"
	const snapID = "foo-id" // ID is "<name>-id" for apiBaseSuite

	// no developer, fake revision
	info := s.mkInstalledInState(c, d, snapName, "", "v1", snap.R(-1), true, "")

	// NO ICON IN SNAP!
	err := os.RemoveAll(filepath.Join(info.MountDir(), "meta", "gui", "icon.svg"))
	c.Assert(err, check.IsNil)

	// but fallback icon installed (even though it wouldn't be)
	fallbackIcon := backend.IconInstallFilename(snapID)
	c.Assert(os.MkdirAll(filepath.Dir(fallbackIcon), 0o755), check.IsNil)
	c.Check(os.WriteFile(fallbackIcon, []byte("I'm from the store"), 0o644), check.IsNil)

	req, err := http.NewRequest("GET", fmt.Sprintf("/v2/icons/%s/icon", snapName), nil)
	c.Assert(err, check.IsNil)

	rec := httptest.NewRecorder()

	s.req(c, req, nil, actionIsExpected).ServeHTTP(rec, req)
	c.Check(rec.Code, check.Equals, 404)
	c.Check(rec.Body.String(), testutil.Contains, "local snap has no icon")
}

func (s *iconsSuite) TestSnapIconGetNoApp(c *check.C) {
	s.expectIconsReadAccess()
	s.daemon(c)

	req, err := http.NewRequest("GET", "/v2/icons/foo/icon", nil)
	c.Assert(err, check.IsNil)

	rec := httptest.NewRecorder()

	s.req(c, req, nil, actionIsExpected).ServeHTTP(rec, req)
	c.Check(rec.Code, check.Equals, 404)
}

func (s *iconsSuite) TestNotInstalledSnapIcon(c *check.C) {
	info := &snap.Info{SuggestedName: "notInstalledSnap", Media: []snap.MediaInfo{{Type: "icon", URL: "icon.svg"}}}
	iconfile := daemon.SnapIcon(info, "notInstalledSnapID")
	c.Check(iconfile, check.Equals, "")
}