github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/boot/boottest/device.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2019 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package boottest 21 22 import ( 23 "strings" 24 25 "github.com/snapcore/snapd/boot" 26 ) 27 28 type mockDevice struct { 29 bootSnap string 30 mode string 31 uc20 bool 32 } 33 34 // MockDevice implements boot.Device. It wraps a string like 35 // <boot-snap-name>[@<mode>], no <boot-snap-name> means classic, no 36 // <mode> defaults to "run". It returns <boot-snap-name> for both 37 // Base and Kernel, for more control mock a DeviceContext. 38 func MockDevice(s string) boot.Device { 39 bootsnap, mode := snapAndMode(s) 40 return &mockDevice{ 41 bootSnap: bootsnap, 42 mode: mode, 43 } 44 } 45 46 // MockUC20Device implements boot.Device and returns true for HasModeenv. 47 func MockUC20Device(s string) boot.Device { 48 m := MockDevice(s).(*mockDevice) 49 m.uc20 = true 50 return m 51 } 52 53 func snapAndMode(str string) (snap, mode string) { 54 parts := strings.SplitN(string(str), "@", 2) 55 if len(parts) == 1 || parts[1] == "" { 56 return parts[0], "run" 57 } 58 return parts[0], parts[1] 59 } 60 61 func (d *mockDevice) Kernel() string { return d.bootSnap } 62 func (d *mockDevice) Base() string { return d.bootSnap } 63 func (d *mockDevice) Classic() bool { return d.bootSnap == "" } 64 func (d *mockDevice) RunMode() bool { return d.mode == "run" } 65 func (d *mockDevice) HasModeenv() bool { return d.uc20 }