forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboot_test.go
5398 lines (4630 loc) · 172 KB
/
boot_test.go
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2022 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 boot_test
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/asserts/assertstest"
"github.com/snapcore/snapd/boot"
"github.com/snapcore/snapd/boot/boottest"
"github.com/snapcore/snapd/bootloader"
"github.com/snapcore/snapd/bootloader/bootloadertest"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil/kcmdline"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/secboot"
"github.com/snapcore/snapd/seed"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/strutil"
"github.com/snapcore/snapd/testutil"
"github.com/snapcore/snapd/timings"
)
func TestBoot(t *testing.T) { TestingT(t) }
type baseBootenvSuite struct {
testutil.BaseTest
rootdir string
bootdir string
cmdlineFile string
}
func (s *baseBootenvSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
restore := release.MockOnClassic(false)
s.AddCleanup(restore)
s.rootdir = c.MkDir()
dirs.SetRootDir(s.rootdir)
s.AddCleanup(func() { dirs.SetRootDir("") })
restore = snap.MockSanitizePlugsSlots(func(snapInfo *snap.Info) {})
s.AddCleanup(restore)
s.bootdir = filepath.Join(s.rootdir, "boot")
s.cmdlineFile = filepath.Join(c.MkDir(), "cmdline")
restore = kcmdline.MockProcCmdline(s.cmdlineFile)
s.AddCleanup(restore)
}
func (s *baseBootenvSuite) forceBootloader(bloader bootloader.Bootloader) {
bootloader.Force(bloader)
s.AddCleanup(func() { bootloader.Force(nil) })
}
func (s *baseBootenvSuite) stampSealedKeys(c *C, rootdir string) {
stamp := filepath.Join(dirs.SnapFDEDirUnder(rootdir), "sealed-keys")
c.Assert(os.MkdirAll(filepath.Dir(stamp), 0755), IsNil)
err := ioutil.WriteFile(stamp, nil, 0644)
c.Assert(err, IsNil)
}
func (s *baseBootenvSuite) mockCmdline(c *C, cmdline string) {
c.Assert(ioutil.WriteFile(s.cmdlineFile, []byte(cmdline), 0644), IsNil)
}
// mockAssetsCache mocks the listed assets in the boot assets cache by creating
// an empty file for each.
func mockAssetsCache(c *C, rootdir, bootloaderName string, cachedAssets []string) {
p := filepath.Join(dirs.SnapBootAssetsDirUnder(rootdir), bootloaderName)
err := os.MkdirAll(p, 0755)
c.Assert(err, IsNil)
for _, cachedAsset := range cachedAssets {
err = ioutil.WriteFile(filepath.Join(p, cachedAsset), nil, 0644)
c.Assert(err, IsNil)
}
}
type bootenvSuite struct {
baseBootenvSuite
bootloader *bootloadertest.MockBootloader
}
var _ = Suite(&bootenvSuite{})
func (s *bootenvSuite) SetUpTest(c *C) {
s.baseBootenvSuite.SetUpTest(c)
s.bootloader = bootloadertest.Mock("mock", c.MkDir())
s.forceBootloader(s.bootloader)
}
type baseBootenv20Suite struct {
baseBootenvSuite
kern1 snap.PlaceInfo
kern2 snap.PlaceInfo
ukern1 snap.PlaceInfo
ukern2 snap.PlaceInfo
base1 snap.PlaceInfo
base2 snap.PlaceInfo
gadget1 snap.PlaceInfo
gadget2 snap.PlaceInfo
normalDefaultState *bootenv20Setup
normalTryingKernelState *bootenv20Setup
}
func (s *baseBootenv20Suite) SetUpTest(c *C) {
s.baseBootenvSuite.SetUpTest(c)
var err error
s.kern1, err = snap.ParsePlaceInfoFromSnapFileName("pc-kernel_1.snap")
c.Assert(err, IsNil)
s.kern2, err = snap.ParsePlaceInfoFromSnapFileName("pc-kernel_2.snap")
c.Assert(err, IsNil)
s.ukern1, err = snap.ParsePlaceInfoFromSnapFileName("pc-kernel_x1.snap")
c.Assert(err, IsNil)
s.ukern2, err = snap.ParsePlaceInfoFromSnapFileName("pc-kernel_x2.snap")
c.Assert(err, IsNil)
s.base1, err = snap.ParsePlaceInfoFromSnapFileName("core20_1.snap")
c.Assert(err, IsNil)
s.base2, err = snap.ParsePlaceInfoFromSnapFileName("core20_2.snap")
c.Assert(err, IsNil)
s.gadget1, err = snap.ParsePlaceInfoFromSnapFileName("pc_1.snap")
c.Assert(err, IsNil)
s.gadget2, err = snap.ParsePlaceInfoFromSnapFileName("pc_2.snap")
c.Assert(err, IsNil)
// default boot state for robustness tests, etc.
s.normalDefaultState = &bootenv20Setup{
modeenv: &boot.Modeenv{
// base is base1
Base: s.base1.Filename(),
// no try base
TryBase: "",
// base status is default
BaseStatus: boot.DefaultStatus,
// gadget is gadget1
Gadget: s.gadget1.Filename(),
// current kernels is just kern1
CurrentKernels: []string{s.kern1.Filename()},
// operating mode is run
Mode: "run",
// RecoverySystem is unset, as it should be during run mode
RecoverySystem: "",
},
// enabled kernel is kern1
kern: s.kern1,
// no try kernel enabled
tryKern: nil,
// kernel status is default
kernStatus: boot.DefaultStatus,
}
// state for after trying a new kernel for robustness tests, etc.
s.normalTryingKernelState = &bootenv20Setup{
modeenv: &boot.Modeenv{
// operating mode is run
Mode: "run",
// base is base1
Base: s.base1.Filename(),
// no try base
TryBase: "",
// base status is default
BaseStatus: boot.DefaultStatus,
// gadget is gadget2
Gadget: s.gadget2.Filename(),
// current kernels is kern1 + kern2
CurrentKernels: []string{s.kern1.Filename(), s.kern2.Filename()},
},
// enabled kernel is kern1
kern: s.kern1,
// try kernel is kern2
tryKern: s.kern2,
// kernel status is trying
kernStatus: boot.TryingStatus,
}
s.mockCmdline(c, "snapd_recovery_mode=run")
}
type bootenv20Suite struct {
baseBootenv20Suite
bootloader *bootloadertest.MockExtractedRunKernelImageBootloader
}
type bootenv20EnvRefKernelSuite struct {
baseBootenv20Suite
bootloader *bootloadertest.MockBootloader
}
type bootenv20RebootBootloaderSuite struct {
baseBootenv20Suite
bootloader *bootloadertest.MockRebootBootloader
}
var _ = Suite(&bootenv20Suite{})
var _ = Suite(&bootenv20EnvRefKernelSuite{})
var _ = Suite(&bootenv20RebootBootloaderSuite{})
func (s *bootenv20Suite) SetUpTest(c *C) {
s.baseBootenv20Suite.SetUpTest(c)
s.bootloader = bootloadertest.Mock("mock", c.MkDir()).WithExtractedRunKernelImage()
s.forceBootloader(s.bootloader)
}
func (s *bootenv20EnvRefKernelSuite) SetUpTest(c *C) {
s.baseBootenv20Suite.SetUpTest(c)
s.bootloader = bootloadertest.Mock("mock", c.MkDir())
s.forceBootloader(s.bootloader)
}
func (s *bootenv20RebootBootloaderSuite) SetUpTest(c *C) {
s.baseBootenv20Suite.SetUpTest(c)
s.bootloader = bootloadertest.Mock("mock", c.MkDir()).WithRebootBootloader()
s.forceBootloader(s.bootloader)
}
type bootenv20Setup struct {
modeenv *boot.Modeenv
kern snap.PlaceInfo
tryKern snap.PlaceInfo
kernStatus string
}
func setupUC20Bootenv(c *C, bl bootloader.Bootloader, opts *bootenv20Setup) (restore func()) {
var cleanups []func()
// write the modeenv
if opts.modeenv != nil {
c.Assert(opts.modeenv.WriteTo(""), IsNil)
// this isn't strictly necessary since the modeenv will be written to
// the test's private dir anyways, but it's nice to have so we can write
// multiple modeenvs from a single test and just call the restore
// function in between the parts of the test that use different modeenvs
r := func() {
defaultModeenv := &boot.Modeenv{Mode: "run"}
c.Assert(defaultModeenv.WriteTo(""), IsNil)
}
cleanups = append(cleanups, r)
}
// set the status
origEnv, err := bl.GetBootVars("kernel_status")
c.Assert(err, IsNil)
err = bl.SetBootVars(map[string]string{"kernel_status": opts.kernStatus})
c.Assert(err, IsNil)
cleanups = append(cleanups, func() {
err := bl.SetBootVars(origEnv)
c.Assert(err, IsNil)
})
// check what kind of real mock bootloader we have to use different methods
// to set the kernel snaps are if they're non-nil
switch vbl := bl.(type) {
case *bootloadertest.MockExtractedRunKernelImageBootloader:
// then we can use the advanced methods on it
if opts.kern != nil {
r := vbl.SetEnabledKernel(opts.kern)
cleanups = append(cleanups, r)
}
if opts.tryKern != nil {
r := vbl.SetEnabledTryKernel(opts.tryKern)
cleanups = append(cleanups, r)
}
// don't count any calls to SetBootVars made thus far
vbl.SetBootVarsCalls = 0
case *bootloadertest.MockBootloader:
// for non-extracted, we need to use the bootenv to set the current kernels
r := setupUC20MockBootloaderEnv(c, bl, opts)
cleanups = append(cleanups, r)
// don't count any calls to SetBootVars made thus far
vbl.SetBootVarsCalls = 0
case *bootloadertest.MockRebootBootloader:
// for non-extracted, we need to use the bootenv to set the current kernels
r := setupUC20MockBootloaderEnv(c, bl, opts)
cleanups = append(cleanups, r)
// don't count any calls to SetBootVars made thus far
vbl.SetBootVarsCalls = 0
default:
c.Fatalf("unsupported bootloader %T", bl)
}
return func() {
for _, r := range cleanups {
r()
}
}
}
func setupUC20MockBootloaderEnv(c *C, bl bootloader.Bootloader, opts *bootenv20Setup) (restore func()) {
origEnv, err := bl.GetBootVars("snap_kernel", "snap_try_kernel")
c.Assert(err, IsNil)
m := make(map[string]string, 2)
if opts.kern != nil {
m["snap_kernel"] = opts.kern.Filename()
} else {
m["snap_kernel"] = ""
}
if opts.tryKern != nil {
m["snap_try_kernel"] = opts.tryKern.Filename()
} else {
m["snap_try_kernel"] = ""
}
err = bl.SetBootVars(m)
c.Assert(err, IsNil)
return func() {
err := bl.SetBootVars(origEnv)
c.Assert(err, IsNil)
}
}
func (s *bootenvSuite) TestInUseClassic(c *C) {
classicDev := boottest.MockDevice("")
// make bootloader.Find fail but shouldn't matter
bootloader.ForceError(errors.New("broken bootloader"))
inUse, err := boot.InUse(snap.TypeBase, classicDev)
c.Assert(err, IsNil)
c.Check(inUse("core18", snap.R(41)), Equals, false)
}
func (s *bootenvSuite) TestInUseIrrelevantTypes(c *C) {
coreDev := boottest.MockDevice("some-snap")
// make bootloader.Find fail but shouldn't matter
bootloader.ForceError(errors.New("broken bootloader"))
inUse, err := boot.InUse(snap.TypeGadget, coreDev)
c.Assert(err, IsNil)
c.Check(inUse("gadget", snap.R(41)), Equals, false)
}
func (s *bootenvSuite) TestInUse(c *C) {
coreDev := boottest.MockDevice("some-snap")
for _, t := range []struct {
bootVarKey string
bootVarValue string
snapName string
snapRev snap.Revision
inUse bool
}{
// in use
{"snap_kernel", "kernel_41.snap", "kernel", snap.R(41), true},
{"snap_try_kernel", "kernel_82.snap", "kernel", snap.R(82), true},
{"snap_core", "core_21.snap", "core", snap.R(21), true},
{"snap_try_core", "core_42.snap", "core", snap.R(42), true},
// not in use
{"snap_core", "core_111.snap", "core", snap.R(21), false},
{"snap_try_core", "core_111.snap", "core", snap.R(21), false},
{"snap_kernel", "kernel_111.snap", "kernel", snap.R(1), false},
{"snap_try_kernel", "kernel_111.snap", "kernel", snap.R(1), false},
} {
typ := snap.TypeBase
if t.snapName == "kernel" {
typ = snap.TypeKernel
}
s.bootloader.BootVars[t.bootVarKey] = t.bootVarValue
inUse, err := boot.InUse(typ, coreDev)
c.Assert(err, IsNil)
c.Assert(inUse(t.snapName, t.snapRev), Equals, t.inUse, Commentf("unexpected result: %s %s %v", t.snapName, t.snapRev, t.inUse))
}
}
func (s *bootenvSuite) TestInUseEphemeral(c *C) {
coreDev := boottest.MockDevice("some-snap@install")
// make bootloader.Find fail but shouldn't matter
bootloader.ForceError(errors.New("broken bootloader"))
inUse, err := boot.InUse(snap.TypeBase, coreDev)
c.Assert(err, IsNil)
c.Check(inUse("whatever", snap.R(0)), Equals, true)
}
func (s *bootenvSuite) TestInUseUnhappy(c *C) {
coreDev := boottest.MockDevice("some-snap")
// make GetVars fail
s.bootloader.GetErr = errors.New("zap")
_, err := boot.InUse(snap.TypeKernel, coreDev)
c.Check(err, ErrorMatches, `cannot get boot variables: zap`)
// make bootloader.Find fail
bootloader.ForceError(errors.New("broken bootloader"))
_, err = boot.InUse(snap.TypeKernel, coreDev)
c.Check(err, ErrorMatches, `cannot get boot settings: broken bootloader`)
}
func (s *bootenvSuite) TestCurrentBootNameAndRevision(c *C) {
coreDev := boottest.MockDevice("some-snap")
s.bootloader.BootVars["snap_core"] = "core_2.snap"
s.bootloader.BootVars["snap_kernel"] = "canonical-pc-linux_2.snap"
current, err := boot.GetCurrentBoot(snap.TypeOS, coreDev)
c.Check(err, IsNil)
c.Check(current.SnapName(), Equals, "core")
c.Check(current.SnapRevision(), Equals, snap.R(2))
current, err = boot.GetCurrentBoot(snap.TypeKernel, coreDev)
c.Check(err, IsNil)
c.Check(current.SnapName(), Equals, "canonical-pc-linux")
c.Check(current.SnapRevision(), Equals, snap.R(2))
s.bootloader.BootVars["snap_mode"] = boot.TryingStatus
_, err = boot.GetCurrentBoot(snap.TypeKernel, coreDev)
c.Check(err, Equals, boot.ErrBootNameAndRevisionNotReady)
}
func (s *bootenv20Suite) TestCurrentBoot20NameAndRevision(c *C) {
coreDev := boottest.MockUC20Device("", nil)
c.Assert(coreDev.HasModeenv(), Equals, true)
r := setupUC20Bootenv(
c,
s.bootloader,
s.normalDefaultState,
)
defer r()
current, err := boot.GetCurrentBoot(snap.TypeBase, coreDev)
c.Check(err, IsNil)
c.Check(current.SnapName(), Equals, s.base1.SnapName())
c.Check(current.SnapRevision(), Equals, snap.R(1))
current, err = boot.GetCurrentBoot(snap.TypeKernel, coreDev)
c.Check(err, IsNil)
c.Check(current.SnapName(), Equals, s.kern1.SnapName())
c.Check(current.SnapRevision(), Equals, snap.R(1))
s.bootloader.BootVars["kernel_status"] = boot.TryingStatus
_, err = boot.GetCurrentBoot(snap.TypeKernel, coreDev)
c.Check(err, Equals, boot.ErrBootNameAndRevisionNotReady)
}
// only difference between this test and TestCurrentBoot20NameAndRevision is the
// base bootloader which doesn't support ExtractedRunKernelImageBootloader.
func (s *bootenv20EnvRefKernelSuite) TestCurrentBoot20NameAndRevision(c *C) {
coreDev := boottest.MockUC20Device("", nil)
c.Assert(coreDev.HasModeenv(), Equals, true)
r := setupUC20Bootenv(
c,
s.bootloader,
s.normalDefaultState,
)
defer r()
current, err := boot.GetCurrentBoot(snap.TypeKernel, coreDev)
c.Assert(err, IsNil)
c.Assert(current.SnapName(), Equals, s.kern1.SnapName())
c.Assert(current.SnapRevision(), Equals, snap.R(1))
}
func (s *bootenvSuite) TestCurrentBootNameAndRevisionUnhappy(c *C) {
coreDev := boottest.MockDevice("some-snap")
_, err := boot.GetCurrentBoot(snap.TypeKernel, coreDev)
c.Check(err, ErrorMatches, `cannot get name and revision of kernel \(snap_kernel\): boot variable unset`)
_, err = boot.GetCurrentBoot(snap.TypeOS, coreDev)
c.Check(err, ErrorMatches, `cannot get name and revision of boot base \(snap_core\): boot variable unset`)
_, err = boot.GetCurrentBoot(snap.TypeBase, coreDev)
c.Check(err, ErrorMatches, `cannot get name and revision of boot base \(snap_core\): boot variable unset`)
_, err = boot.GetCurrentBoot(snap.TypeApp, coreDev)
c.Check(err, ErrorMatches, `internal error: no boot state handling for snap type "app"`)
// validity check
s.bootloader.BootVars["snap_kernel"] = "kernel_41.snap"
current, err := boot.GetCurrentBoot(snap.TypeKernel, coreDev)
c.Check(err, IsNil)
c.Check(current.SnapName(), Equals, "kernel")
c.Check(current.SnapRevision(), Equals, snap.R(41))
// make GetVars fail
s.bootloader.GetErr = errors.New("zap")
_, err = boot.GetCurrentBoot(snap.TypeKernel, coreDev)
c.Check(err, ErrorMatches, "cannot get boot variables: zap")
s.bootloader.GetErr = nil
// make bootloader.Find fail
bootloader.ForceError(errors.New("broken bootloader"))
_, err = boot.GetCurrentBoot(snap.TypeKernel, coreDev)
c.Check(err, ErrorMatches, "cannot get boot settings: broken bootloader")
}
func (s *bootenvSuite) TestSnapTypeParticipatesInBoot(c *C) {
classicDev := boottest.MockDevice("")
legacyCoreDev := boottest.MockDevice("some-snap")
coreDev := boottest.MockUC20Device("", nil)
coreDevInstallMode := boottest.MockUC20Device("install", nil)
for _, typ := range []snap.Type{
snap.TypeKernel,
snap.TypeOS,
snap.TypeBase,
} {
c.Check(boot.SnapTypeParticipatesInBoot(typ, classicDev), Equals, false)
c.Check(boot.SnapTypeParticipatesInBoot(typ, legacyCoreDev), Equals, true)
c.Check(boot.SnapTypeParticipatesInBoot(typ, coreDev), Equals, true)
c.Check(boot.SnapTypeParticipatesInBoot(typ, coreDevInstallMode), Equals, true)
}
classicWithModesDev := boottest.MockClassicWithModesDevice("", nil)
c.Check(boot.SnapTypeParticipatesInBoot(snap.TypeKernel, classicWithModesDev), Equals, true)
c.Check(boot.SnapTypeParticipatesInBoot(snap.TypeOS, classicWithModesDev), Equals, false)
c.Check(boot.SnapTypeParticipatesInBoot(snap.TypeBase, classicWithModesDev), Equals, false)
classicWithModesDevInstallMode := boottest.MockClassicWithModesDevice("install", nil)
c.Check(boot.SnapTypeParticipatesInBoot(snap.TypeKernel, classicWithModesDevInstallMode), Equals, true)
}
func (s *bootenvSuite) TestParticipant(c *C) {
info := &snap.Info{}
info.RealName = "some-snap"
coreDev := boottest.MockDevice("some-snap")
classicDev := boottest.MockDevice("")
bp := boot.Participant(info, snap.TypeApp, coreDev)
c.Check(bp.IsTrivial(), Equals, true)
for _, typ := range []snap.Type{
snap.TypeKernel,
snap.TypeOS,
snap.TypeBase,
} {
bp = boot.Participant(info, typ, classicDev)
c.Check(bp.IsTrivial(), Equals, true)
bp = boot.Participant(info, typ, coreDev)
c.Check(bp.IsTrivial(), Equals, false)
c.Check(bp, DeepEquals, boot.NewCoreBootParticipant(info, typ, coreDev))
}
}
func (s *bootenvSuite) TestParticipantBaseWithModel(c *C) {
core := &snap.Info{SideInfo: snap.SideInfo{RealName: "core"}, SnapType: snap.TypeOS}
core18 := &snap.Info{SideInfo: snap.SideInfo{RealName: "core18"}, SnapType: snap.TypeBase}
core20 := &snap.Info{SideInfo: snap.SideInfo{RealName: "core20"}, SnapType: snap.TypeBase}
type tableT struct {
with *snap.Info
model string
nop bool
}
table := []tableT{
{
with: core,
model: "",
nop: true,
}, {
with: core,
model: "core",
nop: false,
}, {
with: core,
model: "core18",
nop: true,
},
{
with: core18,
model: "",
nop: true,
},
{
with: core18,
model: "core",
nop: true,
},
{
with: core18,
model: "core18",
nop: false,
},
{
with: core18,
model: "core18@install",
nop: true,
},
{
with: core,
model: "core@install",
nop: true,
},
{
with: core20,
model: "core@run",
nop: true,
},
}
for i, t := range table {
dev := boottest.MockDevice(t.model)
bp := boot.Participant(t.with, t.with.Type(), dev)
c.Check(bp.IsTrivial(), Equals, t.nop, Commentf("%d", i))
if !t.nop {
c.Check(bp, DeepEquals, boot.NewCoreBootParticipant(t.with, t.with.Type(), dev))
}
}
}
func (s *bootenvSuite) TestParticipantGadgetWithModel(c *C) {
gadget := &snap.Info{SideInfo: snap.SideInfo{RealName: "pc"}, SnapType: snap.TypeGadget}
type tableT struct {
with *snap.Info
model string
nop bool
}
table := []tableT{
{
with: gadget,
model: "",
nop: true,
}, {
with: gadget,
model: "pc",
nop: true,
}, {
with: gadget,
model: "pc@run",
nop: false,
}, {
with: gadget,
model: "other-gadget",
nop: true,
},
{
with: gadget,
model: "pc@install",
nop: true,
},
}
for i, t := range table {
dev := boottest.MockDevice(t.model)
bp := boot.Participant(t.with, t.with.Type(), dev)
c.Check(bp.IsTrivial(), Equals, t.nop, Commentf("%d", i))
if !t.nop {
c.Check(bp, DeepEquals, boot.NewCoreBootParticipant(t.with, t.with.Type(), dev))
}
}
}
func (s *bootenvSuite) TestKernelWithModel(c *C) {
info := &snap.Info{}
info.RealName = "kernel"
type tableT struct {
model string
nop bool
krn boot.BootKernel
}
table := []tableT{
{
model: "other-kernel",
nop: true,
krn: boot.Trivial{},
}, {
model: "kernel",
nop: false,
krn: boot.NewCoreKernel(info, boottest.MockDevice("kernel")),
}, {
model: "",
nop: true,
krn: boot.Trivial{},
}, {
model: "kernel@install",
nop: true,
krn: boot.Trivial{},
},
}
for _, t := range table {
dev := boottest.MockDevice(t.model)
krn := boot.Kernel(info, snap.TypeKernel, dev)
c.Check(krn.IsTrivial(), Equals, t.nop)
c.Check(krn, DeepEquals, t.krn)
}
}
func (s *bootenvSuite) TestParticipantClassicWithModesWithModel(c *C) {
modelHdrs := map[string]interface{}{
"type": "model",
"authority-id": "brand",
"series": "16",
"brand-id": "brand",
"model": "baz-3000",
"architecture": "amd64",
"classic": "true",
"distribution": "ubuntu",
"base": "core22",
"snaps": []interface{}{
map[string]interface{}{
"name": "kernel",
"id": "pclinuxdidididididididididididid",
"type": "kernel",
},
map[string]interface{}{
"name": "gadget",
"id": "pcididididididididididididididid",
"type": "gadget",
},
},
}
model := assertstest.FakeAssertion(modelHdrs).(*asserts.Model)
classicWithModesDev := boottest.MockClassicWithModesDevice("", model)
tests := []struct {
name string
typ snap.Type
nonTrivial bool
}{
{"some-snap", snap.TypeApp, false},
{"core22", snap.TypeBase, false},
{"kernel", snap.TypeKernel, true},
{"gadget", snap.TypeGadget, true},
}
for _, t := range tests {
info := &snap.Info{}
info.RealName = t.name
bp := boot.Participant(info, t.typ, classicWithModesDev)
if !t.nonTrivial {
c.Check(bp.IsTrivial(), Equals, true)
} else {
c.Check(bp, DeepEquals, boot.NewCoreBootParticipant(info, t.typ, classicWithModesDev))
}
}
}
func (s *bootenvSuite) TestMarkBootSuccessfulKernelStatusTryingNoTryKernelSnapCleansUp(c *C) {
coreDev := boottest.MockDevice("some-snap")
// set all the same vars as if we were doing trying, except don't set a try
// kernel
err := s.bootloader.SetBootVars(map[string]string{
"snap_kernel": "kernel_41.snap",
"snap_mode": boot.TryingStatus,
})
c.Assert(err, IsNil)
// mark successful
err = boot.MarkBootSuccessful(coreDev)
c.Assert(err, IsNil)
// check that the bootloader variables were cleaned
expected := map[string]string{
"snap_mode": boot.DefaultStatus,
"snap_kernel": "kernel_41.snap",
"snap_try_kernel": "",
}
m, err := s.bootloader.GetBootVars("snap_mode", "snap_try_kernel", "snap_kernel")
c.Assert(err, IsNil)
c.Assert(m, DeepEquals, expected)
// do it again, verify it's still okay
err = boot.MarkBootSuccessful(coreDev)
c.Assert(err, IsNil)
m2, err := s.bootloader.GetBootVars("snap_mode", "snap_try_kernel", "snap_kernel")
c.Assert(err, IsNil)
c.Assert(m2, DeepEquals, expected)
}
func (s *bootenvSuite) TestMarkBootSuccessfulTryKernelKernelStatusDefaultCleansUp(c *C) {
coreDev := boottest.MockDevice("some-snap")
// set an errant snap_try_kernel
err := s.bootloader.SetBootVars(map[string]string{
"snap_kernel": "kernel_41.snap",
"snap_try_kernel": "kernel_42.snap",
"snap_mode": boot.DefaultStatus,
})
c.Assert(err, IsNil)
// mark successful
err = boot.MarkBootSuccessful(coreDev)
c.Assert(err, IsNil)
// check that the bootloader variables were cleaned
expected := map[string]string{
"snap_mode": boot.DefaultStatus,
"snap_kernel": "kernel_41.snap",
"snap_try_kernel": "",
}
m, err := s.bootloader.GetBootVars("snap_mode", "snap_try_kernel", "snap_kernel")
c.Assert(err, IsNil)
c.Assert(m, DeepEquals, expected)
// do it again, verify it's still okay
err = boot.MarkBootSuccessful(coreDev)
c.Assert(err, IsNil)
m2, err := s.bootloader.GetBootVars("snap_mode", "snap_try_kernel", "snap_kernel")
c.Assert(err, IsNil)
c.Assert(m2, DeepEquals, expected)
}
func (s *bootenv20Suite) TestCoreKernel20(c *C) {
coreDev := boottest.MockUC20Device("", nil)
c.Assert(coreDev.HasModeenv(), Equals, true)
r := setupUC20Bootenv(
c,
s.bootloader,
s.normalDefaultState,
)
defer r()
// get the boot kernel from our kernel snap
bootKern := boot.Kernel(s.kern1, snap.TypeKernel, coreDev)
// can't use FitsTypeOf with coreKernel here, cause that causes an import
// loop as boottest imports boot and coreKernel is unexported
c.Assert(bootKern.IsTrivial(), Equals, false)
// extract the kernel assets from the coreKernel
// the container here doesn't really matter since it's just being passed
// to the mock bootloader method anyways
kernelContainer := snaptest.MockContainer(c, nil)
err := bootKern.ExtractKernelAssets(kernelContainer)
c.Assert(err, IsNil)
// make sure that the bootloader was told to extract some assets
c.Assert(s.bootloader.ExtractKernelAssetsCalls, DeepEquals, []snap.PlaceInfo{s.kern1})
// now remove the kernel assets and ensure that we get those calls
err = bootKern.RemoveKernelAssets()
c.Assert(err, IsNil)
// make sure that the bootloader was told to remove assets
c.Assert(s.bootloader.RemoveKernelAssetsCalls, DeepEquals, []snap.PlaceInfo{s.kern1})
}
func (s *bootenv20Suite) TestCoreParticipant20SetNextSameKernelSnap(c *C) {
coreDev := boottest.MockUC20Device("", nil)
c.Assert(coreDev.HasModeenv(), Equals, true)
r := setupUC20Bootenv(
c,
s.bootloader,
s.normalDefaultState,
)
defer r()
// get the boot kernel participant from our kernel snap
bootKern := boot.Participant(s.kern1, snap.TypeKernel, coreDev)
// make sure it's not a trivial boot participant
c.Assert(bootKern.IsTrivial(), Equals, false)
// make the kernel used on next boot
rebootRequired, err := bootKern.SetNextBoot(boot.NextBootContext{BootWithoutTry: false})
c.Assert(err, IsNil)
c.Assert(rebootRequired, Equals, boot.RebootInfo{RebootRequired: false})
// make sure that the bootloader was asked for the current kernel
_, nKernelCalls := s.bootloader.GetRunKernelImageFunctionSnapCalls("Kernel")
c.Assert(nKernelCalls, Equals, 1)
// ensure that kernel_status is still empty
c.Assert(s.bootloader.BootVars["kernel_status"], Equals, boot.DefaultStatus)
// there was no attempt to enable a kernel
_, enableKernelCalls := s.bootloader.GetRunKernelImageFunctionSnapCalls("EnableTryKernel")
c.Assert(enableKernelCalls, Equals, 0)
// the modeenv is still the same as well
m2, err := boot.ReadModeenv("")
c.Assert(err, IsNil)
c.Assert(m2.CurrentKernels, DeepEquals, []string{s.kern1.Filename()})
// finally we didn't call SetBootVars on the bootloader because nothing
// changed
c.Assert(s.bootloader.SetBootVarsCalls, Equals, 0)
}
func (s *bootenv20EnvRefKernelSuite) TestCoreParticipant20SetNextSameKernelSnap(c *C) {
coreDev := boottest.MockUC20Device("", nil)
c.Assert(coreDev.HasModeenv(), Equals, true)
r := setupUC20Bootenv(
c,
s.bootloader,
s.normalDefaultState,
)
defer r()
// get the boot kernel participant from our kernel snap
bootKern := boot.Participant(s.kern1, snap.TypeKernel, coreDev)
// make sure it's not a trivial boot participant
c.Assert(bootKern.IsTrivial(), Equals, false)
// make the kernel used on next boot
rebootRequired, err := bootKern.SetNextBoot(boot.NextBootContext{BootWithoutTry: false})
c.Assert(err, IsNil)
c.Assert(rebootRequired, Equals, boot.RebootInfo{RebootRequired: false})
// ensure that bootenv is unchanged
m, err := s.bootloader.GetBootVars("kernel_status", "snap_kernel", "snap_try_kernel")
c.Assert(err, IsNil)
c.Assert(m, DeepEquals, map[string]string{
"kernel_status": boot.DefaultStatus,
"snap_kernel": s.kern1.Filename(),
"snap_try_kernel": "",
})
// the modeenv is still the same as well
m2, err := boot.ReadModeenv("")
c.Assert(err, IsNil)
c.Assert(m2.CurrentKernels, DeepEquals, []string{s.kern1.Filename()})
// finally we didn't call SetBootVars on the bootloader because nothing
// changed
c.Assert(s.bootloader.SetBootVarsCalls, Equals, 0)
}
func (s *bootenv20Suite) TestCoreParticipant20SetNextNewKernelSnap(c *C) {
coreDev := boottest.MockUC20Device("", nil)
c.Assert(coreDev.HasModeenv(), Equals, true)
r := setupUC20Bootenv(
c,
s.bootloader,
s.normalDefaultState,
)
defer r()
// get the boot kernel participant from our new kernel snap
bootKern := boot.Participant(s.kern2, snap.TypeKernel, coreDev)
// make sure it's not a trivial boot participant
c.Assert(bootKern.IsTrivial(), Equals, false)
// make the kernel used on next boot
rebootRequired, err := bootKern.SetNextBoot(boot.NextBootContext{BootWithoutTry: false})
c.Assert(err, IsNil)
c.Assert(rebootRequired, DeepEquals, boot.RebootInfo{
RebootRequired: true,
BootloaderOptions: &bootloader.Options{
Role: bootloader.RoleRunMode,
},