github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/container/lxd/export_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lxd
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  
    10  	lxdclient "github.com/canonical/lxd/client"
    11  	"github.com/juju/clock"
    12  
    13  	"github.com/juju/juju/container"
    14  	"github.com/juju/juju/core/base"
    15  	"github.com/juju/juju/core/network"
    16  )
    17  
    18  var (
    19  	NewNICDevice        = newNICDevice
    20  	BaseRemoteAliases   = baseRemoteAliases
    21  	ErrIPV6NotSupported = errIPV6NotSupported
    22  )
    23  
    24  type patcher interface {
    25  	PatchValue(interface{}, interface{})
    26  }
    27  
    28  // PatchConnectRemote ensures that the ConnectImageRemote function always returns
    29  // the supplied (mock) image server.
    30  func PatchConnectRemote(patcher patcher, remotes map[string]lxdclient.ImageServer) {
    31  	patcher.PatchValue(&ConnectImageRemote, func(_ context.Context, spec ServerSpec) (lxdclient.ImageServer, error) {
    32  		if svr, ok := remotes[spec.Name]; ok {
    33  			return svr, nil
    34  		}
    35  		return nil, errors.New("unrecognized remote server")
    36  	})
    37  }
    38  
    39  func PatchGenerateVirtualMACAddress(patcher patcher) {
    40  	patcher.PatchValue(&network.GenerateVirtualMACAddress, func() string {
    41  		return "00:16:3e:00:00:00"
    42  	})
    43  }
    44  
    45  func PatchLXDViaSnap(patcher patcher, isSnap bool) {
    46  	patcher.PatchValue(&lxdViaSnap, func() bool { return isSnap })
    47  }
    48  
    49  func PatchHostBase(patcher patcher, b base.Base) {
    50  	patcher.PatchValue(&hostBase, func() (base.Base, error) { return b, nil })
    51  }
    52  
    53  func PatchGetSnapManager(patcher patcher, mgr SnapManager) {
    54  	patcher.PatchValue(&getSnapManager, func() SnapManager { return mgr })
    55  }
    56  
    57  func GetImageSources(mgr container.Manager) ([]ServerSpec, error) {
    58  	cMgr := mgr.(*containerManager)
    59  	_ = cMgr.ensureInitialized()
    60  	return cMgr.getImageSources()
    61  }
    62  
    63  func NetworkDevicesFromConfig(mgr container.Manager, netConfig *container.NetworkConfig) (
    64  	map[string]device, []string, error,
    65  ) {
    66  	cMgr := mgr.(*containerManager)
    67  	_ = cMgr.ensureInitialized()
    68  	return cMgr.networkDevicesFromConfig(netConfig)
    69  }
    70  
    71  func NewTestingServer(svr lxdclient.InstanceServer, clock clock.Clock) (*Server, error) {
    72  	server, err := NewServer(svr)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	server.clock = clock
    77  	return server, nil
    78  }