github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/overlord/devicestate/devicestatetest/gadget.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-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 devicestatetest
    21  
    22  import (
    23  	"encoding/json"
    24  	"fmt"
    25  
    26  	. "gopkg.in/check.v1"
    27  	"gopkg.in/tomb.v2"
    28  	"gopkg.in/yaml.v2"
    29  
    30  	"github.com/snapcore/snapd/overlord/hookstate"
    31  	"github.com/snapcore/snapd/overlord/hookstate/ctlcmd"
    32  	"github.com/snapcore/snapd/overlord/snapstate"
    33  	"github.com/snapcore/snapd/overlord/state"
    34  	"github.com/snapcore/snapd/snap"
    35  	"github.com/snapcore/snapd/snap/snaptest"
    36  )
    37  
    38  type PrepareDeviceBehavior struct {
    39  	DeviceSvcURL   string
    40  	Headers        map[string]string
    41  	RegBody        map[string]string
    42  	ProposedSerial string
    43  }
    44  
    45  func MockGadget(c *C, st *state.State, name string, revision snap.Revision, pDBhv *PrepareDeviceBehavior) (restore func()) {
    46  
    47  	sideInfoGadget := &snap.SideInfo{
    48  		RealName: name,
    49  		Revision: revision,
    50  	}
    51  
    52  	snapYaml := fmt.Sprintf(`name: %q
    53  type: gadget
    54  version: gadget
    55  `, name)
    56  
    57  	if pDBhv != nil {
    58  		snapYaml += `hooks:
    59    prepare-device:
    60  `
    61  	}
    62  
    63  	snaptest.MockSnap(c, snapYaml, sideInfoGadget)
    64  	snapstate.Set(st, name, &snapstate.SnapState{
    65  		SnapType: "gadget",
    66  		Active:   true,
    67  		Sequence: []*snap.SideInfo{sideInfoGadget},
    68  		Current:  revision,
    69  	})
    70  
    71  	if pDBhv == nil {
    72  		// nothing to restore
    73  		return func() {}
    74  	}
    75  
    76  	// mock the prepare-device hook
    77  
    78  	return hookstate.MockRunHook(func(ctx *hookstate.Context, _ *tomb.Tomb) ([]byte, error) {
    79  		c.Assert(ctx.HookName(), Equals, "prepare-device")
    80  
    81  		// snapctl set the registration params
    82  		_, _, err := ctlcmd.Run(ctx, []string{"set", fmt.Sprintf("device-service.url=%q", pDBhv.DeviceSvcURL)}, 0)
    83  		c.Assert(err, IsNil)
    84  
    85  		if len(pDBhv.Headers) != 0 {
    86  			h, err := json.Marshal(pDBhv.Headers)
    87  			c.Assert(err, IsNil)
    88  			_, _, err = ctlcmd.Run(ctx, []string{"set", fmt.Sprintf("device-service.headers=%s", string(h))}, 0)
    89  			c.Assert(err, IsNil)
    90  		}
    91  
    92  		if pDBhv.ProposedSerial != "" {
    93  			_, _, err = ctlcmd.Run(ctx, []string{"set", fmt.Sprintf("registration.proposed-serial=%q", pDBhv.ProposedSerial)}, 0)
    94  			c.Assert(err, IsNil)
    95  		}
    96  
    97  		if len(pDBhv.RegBody) != 0 {
    98  			d, err := yaml.Marshal(pDBhv.RegBody)
    99  			c.Assert(err, IsNil)
   100  			_, _, err = ctlcmd.Run(ctx, []string{"set", fmt.Sprintf("registration.body=%q", d)}, 0)
   101  			c.Assert(err, IsNil)
   102  		}
   103  
   104  		return nil, nil
   105  	})
   106  }