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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package kvm
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  
    10  	"github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/core/base"
    15  	"github.com/juju/juju/core/paths"
    16  	"github.com/juju/juju/packaging"
    17  	"github.com/juju/juju/packaging/dependency"
    18  )
    19  
    20  type initialisationInternalSuite struct {
    21  	testing.IsolationSuite
    22  }
    23  
    24  var _ = gc.Suite(&initialisationInternalSuite{})
    25  
    26  func (initialisationInternalSuite) TestCreatePool(c *gc.C) {
    27  	tmpDir, err := os.MkdirTemp("", "juju-initialisationInternalSuite")
    28  	defer func() {
    29  		err = os.RemoveAll(tmpDir)
    30  		if err != nil {
    31  			c.Errorf("failed to remove tmpDir: %s\n", err)
    32  		}
    33  	}()
    34  	c.Check(err, jc.ErrorIsNil)
    35  	pathfinder := func(_ paths.OS) string {
    36  		return tmpDir
    37  	}
    38  	stub := runStub{}
    39  	chown := func(string) error { return nil }
    40  	err = ensurePool(nil, pathfinder, stub.Run, chown)
    41  	c.Check(err, jc.ErrorIsNil)
    42  	c.Assert(stub.Calls(), jc.DeepEquals, []string{
    43  		fmt.Sprintf(" virsh pool-define-as juju-pool dir - - - - %s/kvm/guests", tmpDir),
    44  		" virsh pool-build juju-pool",
    45  		" virsh pool-start juju-pool",
    46  		" virsh pool-autostart juju-pool",
    47  	})
    48  }
    49  
    50  func (initialisationInternalSuite) TestStartPool(c *gc.C) {
    51  	tmpDir, err := os.MkdirTemp("", "juju-initialisationInternalSuite")
    52  	defer func() {
    53  		err = os.RemoveAll(tmpDir)
    54  		if err != nil {
    55  			c.Errorf("failed to remove tmpDir: %s\n", err)
    56  		}
    57  	}()
    58  	c.Check(err, jc.ErrorIsNil)
    59  	pathfinder := func(_ paths.OS) string {
    60  		return tmpDir
    61  	}
    62  	poolInfo := &libvirtPool{Name: "juju-pool", Autostart: "no", State: "inactive"}
    63  	stub := runStub{}
    64  	chown := func(string) error { return nil }
    65  	err = ensurePool(poolInfo, pathfinder, stub.Run, chown)
    66  	c.Check(err, jc.ErrorIsNil)
    67  	c.Assert(stub.Calls(), jc.DeepEquals, []string{
    68  		" virsh pool-build juju-pool",
    69  		" virsh pool-start juju-pool",
    70  		" virsh pool-autostart juju-pool",
    71  	})
    72  }
    73  
    74  func (initialisationInternalSuite) TestAutoStartPool(c *gc.C) {
    75  	tmpDir, err := os.MkdirTemp("", "juju-initialisationInternalSuite")
    76  	defer func() {
    77  		err = os.RemoveAll(tmpDir)
    78  		if err != nil {
    79  			c.Errorf("failed to remove tmpDir: %s\n", err)
    80  		}
    81  	}()
    82  	c.Check(err, jc.ErrorIsNil)
    83  	pathfinder := func(_ paths.OS) string {
    84  		return tmpDir
    85  	}
    86  	poolInfo := &libvirtPool{Name: "juju-pool", Autostart: "no", State: "running"}
    87  	stub := runStub{}
    88  	chown := func(string) error { return nil }
    89  	err = ensurePool(poolInfo, pathfinder, stub.Run, chown)
    90  	c.Check(err, jc.ErrorIsNil)
    91  	c.Assert(stub.Calls(), jc.DeepEquals, []string{
    92  		" virsh pool-autostart juju-pool",
    93  	})
    94  }
    95  
    96  func (initialisationInternalSuite) TestRequiredPackagesAMD64(c *gc.C) {
    97  	got, err := dependency.KVM("amd64").PackageList(base.MustParseBaseFromString("ubuntu@20.04"))
    98  	c.Assert(err, gc.IsNil)
    99  	assertPkgListMatches(c, got, []string{"qemu-kvm", "qemu-utils", "genisoimage", "libvirt-daemon-system", "libvirt-clients"})
   100  }
   101  
   102  func (initialisationInternalSuite) TestRequiredPackagesARM64(c *gc.C) {
   103  	got, err := dependency.KVM("amd64").PackageList(base.MustParseBaseFromString("ubuntu@20.04"))
   104  	c.Assert(err, gc.IsNil)
   105  	assertPkgListMatches(c, got, []string{"qemu-kvm", "qemu-utils", "genisoimage", "libvirt-daemon-system", "libvirt-clients"})
   106  }
   107  
   108  func assertPkgListMatches(c *gc.C, got []packaging.Package, exp []string) {
   109  	c.Check(len(got), gc.Equals, len(exp))
   110  	for i := 0; i < len(got); i++ {
   111  		c.Assert(got[i].Name, gc.Equals, exp[i])
   112  	}
   113  }