File: patch6_1_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 (283 lines) | stat: -rw-r--r-- 6,584 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2018 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 patch_test

import (
	"os"
	"path/filepath"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/dirs"
	"github.com/snapcore/snapd/overlord/patch"
	"github.com/snapcore/snapd/overlord/state"
	"github.com/snapcore/snapd/snap"
	"github.com/snapcore/snapd/snap/snaptest"
)

type patch61Suite struct{}

var _ = Suite(&patch61Suite{})

var statePatch6_1JSON = []byte(`
{
	"last-task-id": 999,
	"last-change-id": 99,

	"data": {
		"patch-level": 6,
		"snaps": {
			"core": {
				"sequence": [{"name": "core", "revision": "2"}],
        "flags": 1,
				"current": "2"}
			},
	  "conns": {
	    "gnome-calculator:icon-themes gtk-common-themes:icon-themes": {
				"auto": true,
				"interface": "content",
				"plug-static": {
					"content": "icon-themes",
					"default-provider": "gtk-common-themes",
					"target": "$SNAP/data-dir/icons"
				},
				"slot-static": {
					"content": "icon-themes",
					"source": {
						"read": ["$SNAP/share/icons/Adwaita"]
					}
				}
			},
			"foobar:fooplug gtk-common-themes:icon-themes": {
				"auto": true,
				"interface": "content"
			},
			"gnome-calculator:network core:network": {
				"auto": true,
				"interface": "network"
			},
			"other-snap:icon-themes gtk-common-themes:icon-themes": {
				"auto": true,
				"interface": "content",
				"undesired":true
			}
		}
	},
		"changes": {
			"1": {
				"id": "1",
				"kind": "install-snap",
				"summary": "install a snap",
				"status": 0,
				"data": {"snap-names": ["foobar"]},
				"task-ids": ["11"]
			}
	},
	"tasks": {
		"11": {
				"id": "11",
				"change": "1",
				"kind": "discard-conns",
				"summary": "",
				"status": 0,
				"data": {"snap-setup": {
								"channel": "edge",
								"flags": 1
				},
				"removed": {
				    "foobar:fooplug gtk-common-themes:icon-themes": {
				      "auto": true,
				      "interface": "content"
				    }
				}
			},
  		"halt-tasks": []
    }
	}
}`)

var mockSnap1Yaml = `
name: gnome-calculator
version: 3.28.2
summary: GNOME Calculator
grade: stable
plugs:
  icon-themes:
    default-provider: gtk-common-themes
    interface: content
    target: $SNAP/data-dir/icons
  sound-themes:
    default-provider: gtk-common-themes
    interface: content
    target: $SNAP/data-dir/sounds
slots:
  gnome-calculator:
    bus: session
    interface: dbus
    name: org.gnome.Calculator
apps:
  gnome-calculator:
    command: command-gnome-calculator.wrapper
`

var mockSnap2Yaml = `
name: foobar
version: 1
summary: FooBar snap
plugs:
  fooplug:
    interface: content
    target: $SNAP/foo-dir/icons
slots:
  fooslot:
    bus: session
    interface: dbus
    name: org.gnome.Calculator
apps:
  foo:
    command: bar
`

var mockSnap3Yaml = `
name: gtk-common-themes
version: 1
summary: gtk common themes snap
slots:
  icon-themes:
    bus: session
    interface: dbus
    name: org.gnome.Calculator
apps:
  foo:
    command: bar
`

var mockSnap4Yaml = `
name: other-snap
version: 1.1
plugs:
  icon-themes:
    default-provider: gtk-common-themes
    interface: content
    target: $SNAP/data-dir/icons
apps:
  foo:
    command: bar
`

func (s *patch61Suite) SetUpTest(c *C) {
	dirs.SetRootDir(c.MkDir())

	err := os.MkdirAll(filepath.Dir(dirs.SnapStateFile), 0755)
	c.Assert(err, IsNil)
	err = os.WriteFile(dirs.SnapStateFile, statePatch6_1JSON, 0644)
	c.Assert(err, IsNil)

	snap.MockSanitizePlugsSlots(func(*snap.Info) {})

	snaptest.MockSnapCurrent(c, mockSnap1Yaml, &snap.SideInfo{Revision: snap.R("x1")})
	snaptest.MockSnapCurrent(c, mockSnap2Yaml, &snap.SideInfo{Revision: snap.R("x1")})
	snaptest.MockSnapCurrent(c, mockSnap3Yaml, &snap.SideInfo{Revision: snap.R("x1")})
	snaptest.MockSnapCurrent(c, mockSnap4Yaml, &snap.SideInfo{Revision: snap.R("x1")})
}

func (s *patch61Suite) TestPatch61(c *C) {
	restore := patch.MockLevel(6, 1)
	defer restore()

	r, err := os.Open(dirs.SnapStateFile)
	c.Assert(err, IsNil)
	defer r.Close()
	st, err := state.ReadState(nil, r)
	c.Assert(err, IsNil)

	// repeat the patch a few times to ensure it's idempotent
	for i := 0; i < 3; i++ {
		st.Lock()
		sublevel := 0
		st.Set("patch-sublevel", sublevel)
		st.Unlock()

		c.Assert(patch.Apply(st), IsNil)
		st.Lock()

		st.Get("patch-sublevel", &sublevel)
		c.Assert(sublevel, Equals, 1)

		var conns map[string]any
		c.Assert(st.Get("conns", &conns), IsNil)
		c.Assert(conns, DeepEquals, map[string]any{
			"gnome-calculator:icon-themes gtk-common-themes:icon-themes": map[string]any{
				"auto":      true,
				"interface": "content",
				"plug-static": map[string]any{
					"content":          "icon-themes",
					"default-provider": "gtk-common-themes",
					"target":           "$SNAP/data-dir/icons",
				},
				"slot-static": map[string]any{
					"content": "icon-themes",
					"source": map[string]any{
						"read": []any{"$SNAP/share/icons/Adwaita"},
					},
				},
			},
			"foobar:fooplug gtk-common-themes:icon-themes": map[string]any{
				"auto":      true,
				"interface": "content",
				"plug-static": map[string]any{
					"target": "$SNAP/foo-dir/icons",
				},
				"slot-static": map[string]any{
					"bus":  "session",
					"name": "org.gnome.Calculator",
				},
			},
			"gnome-calculator:network core:network": map[string]any{
				"auto":      true,
				"interface": "network",
			},
			"other-snap:icon-themes gtk-common-themes:icon-themes": map[string]any{
				"undesired": true,
				"auto":      true,
				"interface": "content",
			},
		})

		var removed map[string]any
		task := st.Task("11")
		c.Assert(task, NotNil)
		c.Assert(task.Get("removed", &removed), IsNil)
		c.Assert(removed, DeepEquals, map[string]any{
			"foobar:fooplug gtk-common-themes:icon-themes": map[string]any{
				"auto":      true,
				"interface": "content",
				"plug-static": map[string]any{
					"target": "$SNAP/foo-dir/icons",
				},
				"slot-static": map[string]any{
					"bus":  "session",
					"name": "org.gnome.Calculator",
				},
			},
		})
		st.Unlock()
	}
}