github.com/rigado/snapd@v2.42.5-go-mod+incompatible/bootloader/bootloadertest/bootloadertest.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2016 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 bootloadertest
    21  
    22  import (
    23  	"path/filepath"
    24  
    25  	"github.com/snapcore/snapd/bootloader"
    26  	"github.com/snapcore/snapd/snap"
    27  )
    28  
    29  // MockBootloader mocks the bootloader interface and records all
    30  // set/get calls.
    31  type MockBootloader struct {
    32  	BootVars map[string]string
    33  	SetErr   error
    34  	GetErr   error
    35  
    36  	name    string
    37  	bootdir string
    38  
    39  	ExtractKernelAssetsCalls []snap.PlaceInfo
    40  	RemoveKernelAssetsCalls  []snap.PlaceInfo
    41  }
    42  
    43  // ensure MockBootloader implements the Bootloader interface
    44  var _ bootloader.Bootloader = (*MockBootloader)(nil)
    45  
    46  func Mock(name, bootdir string) *MockBootloader {
    47  	return &MockBootloader{
    48  		name:    name,
    49  		bootdir: bootdir,
    50  
    51  		BootVars: make(map[string]string),
    52  	}
    53  }
    54  
    55  func (b *MockBootloader) SetBootVars(values map[string]string) error {
    56  	for k, v := range values {
    57  		b.BootVars[k] = v
    58  	}
    59  	return b.SetErr
    60  }
    61  
    62  func (b *MockBootloader) GetBootVars(keys ...string) (map[string]string, error) {
    63  	out := map[string]string{}
    64  	for _, k := range keys {
    65  		out[k] = b.BootVars[k]
    66  	}
    67  
    68  	return out, b.GetErr
    69  }
    70  
    71  func (b *MockBootloader) Name() string {
    72  	return b.name
    73  }
    74  
    75  func (b *MockBootloader) ConfigFile() string {
    76  	return filepath.Join(b.bootdir, "mockboot/mockboot.cfg")
    77  }
    78  
    79  func (b *MockBootloader) ExtractKernelAssets(s snap.PlaceInfo, snapf snap.Container) error {
    80  	b.ExtractKernelAssetsCalls = append(b.ExtractKernelAssetsCalls, s)
    81  	return nil
    82  }
    83  
    84  func (b *MockBootloader) RemoveKernelAssets(s snap.PlaceInfo) error {
    85  	b.RemoveKernelAssetsCalls = append(b.RemoveKernelAssetsCalls, s)
    86  	return nil
    87  }
    88  
    89  // SetBootKernel sets the current boot kernel string. Should be
    90  // something like "pc-kernel_1234.snap".
    91  func (b *MockBootloader) SetBootKernel(kernel string) {
    92  	b.SetBootVars(map[string]string{"snap_kernel": kernel})
    93  }
    94  
    95  // SetBootBase sets the current boot base string. Should be something
    96  // like "core_1234.snap".
    97  func (b *MockBootloader) SetBootBase(base string) {
    98  	b.SetBootVars(map[string]string{"snap_core": base})
    99  }