github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/patch/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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 patch
    21  
    22  import (
    23  	"github.com/snapcore/snapd/overlord/state"
    24  	"github.com/snapcore/snapd/snap"
    25  )
    26  
    27  // PatchesForTest returns the registered set of patches for testing purposes.
    28  func PatchesForTest() map[int][]PatchFunc {
    29  	return patches
    30  }
    31  
    32  // MockPatch1ReadType replaces patch1ReadType.
    33  func MockPatch1ReadType(f func(name string, rev snap.Revision) (snap.Type, error)) (restore func()) {
    34  	old := patch1ReadType
    35  	patch1ReadType = f
    36  	return func() { patch1ReadType = old }
    37  }
    38  
    39  // MockLevel replaces the current implemented patch level
    40  func MockLevel(lv, sublvl int) (restorer func()) {
    41  	old := Level
    42  	Level = lv
    43  	oldSublvl := Sublevel
    44  	Sublevel = sublvl
    45  	oldPatches := make(map[int][]PatchFunc)
    46  	for k, v := range patches {
    47  		oldPatches[k] = v
    48  	}
    49  
    50  	for level, sublevels := range patches {
    51  		if level > lv {
    52  			delete(patches, level)
    53  			continue
    54  		}
    55  		if level == lv && len(sublevels)-1 > sublvl {
    56  			sublevels = sublevels[:sublvl+1]
    57  			patches[level] = sublevels
    58  		}
    59  	}
    60  
    61  	return func() {
    62  		patches = oldPatches
    63  		Level = old
    64  		Sublevel = oldSublvl
    65  	}
    66  }
    67  
    68  func Patch4TaskSnapSetup(task *state.Task) (*patch4SnapSetup, error) {
    69  	return patch4T{}.taskSnapSetup(task)
    70  }
    71  
    72  func Patch4StateMap(st *state.State) (map[string]patch4SnapState, error) {
    73  	var stateMap map[string]patch4SnapState
    74  	err := st.Get("snaps", &stateMap)
    75  
    76  	return stateMap, err
    77  }
    78  
    79  func Patch6StateMap(st *state.State) (map[string]patch6SnapState, error) {
    80  	var stateMap map[string]patch6SnapState
    81  	err := st.Get("snaps", &stateMap)
    82  
    83  	return stateMap, err
    84  }
    85  
    86  func Patch6SnapSetup(task *state.Task) (patch6SnapSetup, error) {
    87  	var snapsup patch6SnapSetup
    88  	err := task.Get("snap-setup", &snapsup)
    89  	return snapsup, err
    90  }