github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/container/kvm/wrappedcmds_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  	"errors"
     8  	"io/ioutil"
     9  	"path/filepath"
    10  
    11  	"github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/container/kvm/libvirt"
    16  )
    17  
    18  type libvirtInternalSuite struct {
    19  	testing.IsolationSuite
    20  }
    21  
    22  var _ = gc.Suite(&libvirtInternalSuite{})
    23  
    24  func (libvirtInternalSuite) TestWriteMetadata(c *gc.C) {
    25  	d := c.MkDir()
    26  
    27  	err := writeMetadata(d)
    28  	c.Check(err, jc.ErrorIsNil)
    29  	b, err := ioutil.ReadFile(filepath.Join(d, metadata))
    30  	c.Check(err, jc.ErrorIsNil)
    31  	c.Assert(string(b), gc.Matches, `{"instance-id": ".*-.*-.*-.*"}`)
    32  }
    33  
    34  func (libvirtInternalSuite) TestWriteDomainXMLSucceeds(c *gc.C) {
    35  	d := c.MkDir()
    36  
    37  	stub := &runStub{}
    38  
    39  	p := CreateMachineParams{
    40  		Hostname: "host00",
    41  		runCmd:   stub.Run,
    42  		disks: []libvirt.DiskInfo{
    43  			diskInfo{
    44  				source: "/path-ds",
    45  				driver: "raw"},
    46  			diskInfo{
    47  				source: "/path",
    48  				driver: "qcow2"},
    49  		},
    50  	}
    51  
    52  	got, err := writeDomainXML(d, p)
    53  	c.Assert(err, jc.ErrorIsNil)
    54  	c.Assert(got, gc.Matches, `/tmp/check-.*/\d+/host00.xml`)
    55  }
    56  
    57  func (libvirtInternalSuite) TestWriteDomainXMLMissingValidSystemDisk(c *gc.C) {
    58  	d := c.MkDir()
    59  
    60  	stub := &runStub{}
    61  
    62  	p := CreateMachineParams{
    63  		Hostname: "host00",
    64  		runCmd:   stub.Run,
    65  		disks: []libvirt.DiskInfo{
    66  			diskInfo{
    67  				source: "/path-ds",
    68  				driver: "raw"},
    69  			diskInfo{
    70  				source: "/path",
    71  				driver: "raw"},
    72  		},
    73  	}
    74  
    75  	got, err := writeDomainXML(d, p)
    76  	c.Assert(err, gc.ErrorMatches, "missing system disk")
    77  	c.Assert(got, gc.Matches, "")
    78  }
    79  
    80  func (libvirtInternalSuite) TestWriteDomainXMLMissingOneDisk(c *gc.C) {
    81  	d := c.MkDir()
    82  
    83  	stub := &runStub{}
    84  
    85  	p := CreateMachineParams{
    86  		Hostname: "host00",
    87  		runCmd:   stub.Run,
    88  		disks: []libvirt.DiskInfo{
    89  			diskInfo{
    90  				source: "/path-ds",
    91  				driver: "raw"},
    92  		},
    93  	}
    94  
    95  	got, err := writeDomainXML(d, p)
    96  	c.Assert(err, gc.ErrorMatches, "got 1 disks, need at least 2")
    97  	c.Assert(got, gc.Matches, "")
    98  }
    99  
   100  func (libvirtInternalSuite) TestWriteDomainXMLMissingBothDisk(c *gc.C) {
   101  	d := c.MkDir()
   102  
   103  	stub := &runStub{}
   104  
   105  	p := CreateMachineParams{
   106  		Hostname: "host00",
   107  		runCmd:   stub.Run,
   108  		disks:    []libvirt.DiskInfo{},
   109  	}
   110  
   111  	got, err := writeDomainXML(d, p)
   112  	c.Assert(err, gc.ErrorMatches, "got 0 disks, need at least 2")
   113  	c.Assert(got, gc.Matches, "")
   114  }
   115  
   116  func (libvirtInternalSuite) TestWriteDomainXMLNoHostname(c *gc.C) {
   117  	d := c.MkDir()
   118  
   119  	stub := &runStub{}
   120  
   121  	p := CreateMachineParams{
   122  		runCmd: stub.Run,
   123  		disks: []libvirt.DiskInfo{
   124  			diskInfo{
   125  				source: "/path-ds",
   126  				driver: "raw"},
   127  			diskInfo{
   128  				source: "/path",
   129  				driver: "qcow"},
   130  		},
   131  	}
   132  
   133  	got, err := writeDomainXML(d, p)
   134  	c.Assert(err, gc.ErrorMatches, "missing required hostname")
   135  	c.Assert(got, gc.Matches, "")
   136  }
   137  
   138  func (libvirtInternalSuite) TestPoolInfoSuccess(c *gc.C) {
   139  	output := `
   140  Name:           juju-pool
   141  UUID:           06ebee2d-6bd0-4f47-a7dc-dea555fdaa3b
   142  State:          running
   143  Persistent:     yes
   144  Autostart:      yes
   145  Capacity:       35.31 GiB
   146  Allocation:     3.54 GiB
   147  Available:      31.77 GiB
   148  `
   149  	stub := runStub{output: output}
   150  	got, err := poolInfo(stub.Run)
   151  	c.Check(err, jc.ErrorIsNil)
   152  	c.Assert(got, jc.DeepEquals, &libvirtPool{Name: "juju-pool", Autostart: "yes", State: "running"})
   153  
   154  }
   155  
   156  func (libvirtInternalSuite) TestPoolInfoNoPool(c *gc.C) {
   157  	stub := runStub{err: errors.New("boom")}
   158  	got, err := poolInfo(stub.Run)
   159  	c.Assert(err, jc.ErrorIsNil)
   160  	c.Assert(got, gc.IsNil)
   161  }