Browse Source

Increase code coverage dramatically. some fixes too.

Signed-off-by: Erik Hollensbe <github@hollensbe.org>
master
Erik Hollensbe 6 years ago
parent
commit
9dbd7bb4a2
  1. 43
      providers/vmware/disk.go
  2. 46
      providers/vmware/disk_test.go
  3. 24
      providers/vmware/fixtures_test.go
  4. 12
      providers/vmware/shared_folder.go
  5. 29
      providers/vmware/shared_folder_test.go
  6. 44
      providers/vmware/vmx.go
  7. 4
      providers/vmware/vmx_parser.go
  8. 5
      providers/vmware/vmx_parser_test.go
  9. 2
      providers/vmware/vmx_parser_testdata/example.vmx
  10. 23
      vcontrol.go
  11. 9
      vcontrol_test.go

43
providers/vmware/disk.go

@ -8,27 +8,37 @@ import (
"github.com/erikh/vcontrol/types"
)
const (
// VMXDiskSATA Disk type SATA
VMXDiskSATA types.DiskType = 0
// VMXDiskSCSI Disk type SCSI
VMXDiskSCSI = iota
// VMXDiskIDE Disk type IDE
VMXDiskIDE = iota
// VMXDiskFloppy Disk type Floppy
VMXDiskFloppy = iota
)
// DiskController is the representation of a VMware disk controller
type DiskController struct {
PCISlotNumber string
Present bool
VirtualDevice string
diskType types.DiskType
DiskType types.DiskType
}
// Disk is the representation of a disk
type Disk struct {
RootPath string // the root directory of this disk's files
present bool
diskType types.DiskType
filename string
Present bool
DiskType types.DiskType
Filename string
}
// NewDisk constructs a *Disk for use.
func NewDisk(filename string, typ types.DiskType) *Disk {
return &Disk{filename: filename, diskType: typ}
return &Disk{Filename: filename, DiskType: typ}
}
// NewDiskFromParams constructs a *Disk for use from a VMXParams provided by
@ -46,15 +56,15 @@ func NewDiskFromParams(vp VMXParams) *Disk {
for re, typ := range vmxDiskNameMap {
if re.MatchString(parts[0]) {
vmd.diskType = typ
vmd.DiskType = typ
}
}
switch parts[1] {
case "present":
vmd.present = value == "TRUE"
vmd.Present = value == "TRUE"
case "filename":
vmd.filename = value
vmd.Filename = value
}
}
@ -64,7 +74,7 @@ func NewDiskFromParams(vp VMXParams) *Disk {
// Attached returns whether or not the VM is using or will use this disk on
// next start.
func (vmd *Disk) Attached() bool {
return vmd.present
return vmd.Present
}
// Create creates a disk for use and sets the filename
@ -79,21 +89,22 @@ func (vmd *Disk) Destroy() error {
// Name returns the name of the disk.
func (vmd *Disk) Name() string {
return vmd.filename
return vmd.Filename
}
// FileName returns the fully qualified filename on the host disk.
func (vmd *Disk) FileName() string {
return filepath.Join(vmd.RootPath, vmd.filename)
return filepath.Join(vmd.RootPath, vmd.Filename)
}
// NewDiskController returns a DiskController with the paramters
// set.
func NewDiskController(slotNumber, virtualDev string, present bool) *DiskController {
func NewDiskController(slotNumber, virtualDev string, present bool, diskType types.DiskType) *DiskController {
return &DiskController{
PCISlotNumber: slotNumber,
VirtualDevice: virtualDev,
Present: present,
DiskType: diskType,
}
}
@ -111,7 +122,7 @@ func NewDiskControllerFromParams(vp VMXParams) *DiskController {
for re, typ := range vmxDiskControllerMap {
if re.MatchString(key) {
vdc.diskType = typ
vdc.DiskType = typ
}
}
@ -130,7 +141,7 @@ func NewDiskControllerFromParams(vp VMXParams) *DiskController {
// Type returns the type of disk controller
func (vdc *DiskController) Type() types.DiskType {
return vdc.diskType
return vdc.DiskType
}
// Active returns if this controller is active (or will be when started)
@ -149,7 +160,7 @@ func (vdc *DiskController) mapParams() map[string]interface{} {
func (vdc *DiskController) toParams(i int) VMXParams {
vp := VMXParams{}
diskTypeName := vmxDiskTypeMap[vdc.diskType] + fmt.Sprintf("%d", i)
diskTypeName := vmxDiskTypeMap[vdc.DiskType] + fmt.Sprintf("%d", i)
for key, value := range vdc.mapParams() {
vp[strings.Join([]string{diskTypeName, key}, ".")] = fmt.Sprintf("%v", value)

46
providers/vmware/disk_test.go

@ -3,9 +3,55 @@ package vmware
import (
"fmt"
"github.com/erikh/vcontrol/types"
. "gopkg.in/check.v1"
)
var dummyDisk = &Disk{
Filename: "test",
DiskType: VMXDiskSATA,
}
var dcParams = VMXParams{
"scsi0.pcislotnumber": "16",
"scsi0.present": "TRUE",
"scsi0.virtualdev": "lsilogic",
}
func (vs *vmwareSuite) TestNewDisk(c *C) {
c.Assert(NewDisk("test", VMXDiskSATA), DeepEquals, dummyDisk)
}
func (vs *vmwareSuite) TestNewDiskFromParams(c *C) {
c.Assert(NewDiskFromParams(dummyParams), DeepEquals, &Disk{})
}
func (vs *vmwareSuite) TestDiskInterface(c *C) {
disk := NewDisk("test", VMXDiskSATA)
c.Assert(disk.Attached(), Equals, false)
disk.Present = true
c.Assert(disk.Attached(), Equals, true)
c.Assert(disk.FileName(), Equals, "test")
disk.RootPath = "/tmp"
disk.Filename = "other"
c.Assert(disk.FileName(), Equals, "/tmp/other")
c.Assert(disk.Name(), Equals, "other")
}
func (vs *vmwareSuite) TestNewDiskControllerFromParams(c *C) {
c.Assert(NewDiskControllerFromParams(VMXParams{}), DeepEquals, &DiskController{})
c.Assert(NewDiskControllerFromParams(dummyParams), DeepEquals, &DiskController{})
c.Assert(NewDiskControllerFromParams(dcParams), DeepEquals, centosDiskControllers[1])
}
func (vs *vmwareSuite) TestNewDiskController(c *C) {
disk := NewDiskController("16", "lsilogic", true, VMXDiskSCSI)
c.Assert(disk, DeepEquals, centosDiskControllers[1])
c.Assert(disk.Type(), Equals, types.DiskType(VMXDiskSCSI))
c.Assert(disk.Active(), Equals, true)
}
func (vs *vmwareSuite) TestDiskControllerParams(c *C) {
vp := centosDiskControllers[0].(*DiskController).toParams(0)
c.Assert(vp, NotNil)

24
providers/vmware/fixtures_test.go

@ -4,23 +4,27 @@ import "github.com/erikh/vcontrol/types"
var exampleParams = VMXParams{
".encoding": "UTF-8",
"someslashes": "\\\"",
"someslashes": "\"\\asdf",
"thisisfalse": "FALSE",
"config.version": "",
"test.something": "",
}
var dummyParams = VMXParams{
"test": "true",
}
var centosSharedFolders = []types.SharedFolder{
&SharedFolder{hostPath: "/home/erikh/src/github.com/contiv/volplugin/systemtests/testdata", guestName: "-testdata", readable: true, writable: true, enabled: false, present: true},
&SharedFolder{hostPath: "/home/erikh/src/github.com/contiv/volplugin/bin", guestName: "-opt-golang-bin", readable: true, writable: true, enabled: false, present: true},
&SharedFolder{hostPath: "/home/erikh/src/github.com/contiv/volplugin", guestName: "-opt-golang-src-github.com-contiv-volplugin", readable: true, writable: true, enabled: false, present: true},
&SharedFolder{hostPath: "/home/erikh/src/github.com/contiv/volplugin/systemtests/testdata", guestName: "-testdata", readable: true, writable: true, Present: true},
&SharedFolder{hostPath: "/home/erikh/src/github.com/contiv/volplugin/bin", guestName: "-opt-golang-bin", readable: true, writable: true, Present: true},
&SharedFolder{hostPath: "/home/erikh/src/github.com/contiv/volplugin", guestName: "-opt-golang-src-github.com-contiv-volplugin", readable: true, writable: true, Present: true},
}
var centosDisks = []types.Disk{
&Disk{RootPath: "", present: true, diskType: 0x2, filename: "auto detect"},
&Disk{RootPath: "", present: true, diskType: 0x1, filename: "disk.vmdk"},
&Disk{RootPath: "", present: true, diskType: 0x1, filename: "/home/erikh/src/github.com/contiv/volplugin/.vagrant/disk-0-0.vmdk"},
&Disk{RootPath: "", present: true, diskType: 0x1, filename: "/home/erikh/src/github.com/contiv/volplugin/.vagrant/disk-0-1.vmdk"},
&Disk{RootPath: "", Present: true, DiskType: 0x2, Filename: "auto detect"},
&Disk{RootPath: "", Present: true, DiskType: 0x1, Filename: "disk.vmdk"},
&Disk{RootPath: "", Present: true, DiskType: 0x1, Filename: "/home/erikh/src/github.com/contiv/volplugin/.vagrant/disk-0-0.vmdk"},
&Disk{RootPath: "", Present: true, DiskType: 0x1, Filename: "/home/erikh/src/github.com/contiv/volplugin/.vagrant/disk-0-1.vmdk"},
}
var centosEthernets = []types.Ethernet{
@ -29,6 +33,6 @@ var centosEthernets = []types.Ethernet{
}
var centosDiskControllers = []types.DiskController{
&DiskController{PCISlotNumber: "", Present: false, VirtualDevice: "", diskType: types.DiskType(0x3)},
&DiskController{PCISlotNumber: "16", Present: true, VirtualDevice: "lsilogic", diskType: types.DiskType(0x1)},
&DiskController{PCISlotNumber: "", Present: false, VirtualDevice: "", DiskType: types.DiskType(0x3)},
&DiskController{PCISlotNumber: "16", Present: true, VirtualDevice: "lsilogic", DiskType: types.DiskType(0x1)},
}

12
providers/vmware/shared_folder.go

@ -11,18 +11,18 @@ type SharedFolder struct {
guestName string
readable bool
writable bool
enabled bool
present bool
Present bool
}
// NewSharedFolder constructs a shared folder for .
func NewSharedFolder(hostPath, guestPath string, readable, writable, enabled bool) *SharedFolder {
func NewSharedFolder(hostPath, guestPath string, readable, writable, present bool) *SharedFolder {
return &SharedFolder{
hostPath: hostPath,
guestName: toGuestName(guestPath),
readable: readable,
writable: writable,
enabled: enabled,
Present: present,
}
}
@ -42,7 +42,7 @@ func NewSharedFolderFromParams(vp VMXParams) *SharedFolder {
case "hostPath":
vsf.hostPath = value
case "present":
vsf.present = value == "true"
vsf.Present = value == "true"
case "readAccess":
vsf.readable = value == "true"
case "writeAccess":
@ -59,7 +59,7 @@ func toGuestName(guestPath string) string {
// Enabled is the enabled status in .
func (vsf *SharedFolder) Enabled() bool {
return vsf.enabled
return vsf.Present
}
// Readable returns whether or not this shared folder is configured as

29
providers/vmware/shared_folder_test.go

@ -0,0 +1,29 @@
package vmware
import (
. "gopkg.in/check.v1"
)
func (vs *vmwareSuite) TestNewSharedFolder(c *C) {
c.Assert(
NewSharedFolder(
"/home/erikh/src/github.com/contiv/volplugin/systemtests/testdata",
"-testdata",
true,
true,
true,
), DeepEquals, centosSharedFolders[0])
}
func (vs *vmwareSuite) TestNewSharedFolderFromParams(c *C) {
c.Assert(NewSharedFolderFromParams(dummyParams), DeepEquals, &SharedFolder{})
}
func (vs *vmwareSuite) TestSharedFolderAttributes(c *C) {
sf := centosSharedFolders[0]
c.Assert(sf.Enabled(), Equals, true)
c.Assert(sf.Readable(), Equals, true)
c.Assert(sf.Writable(), Equals, true)
c.Assert(sf.HostPath(), Equals, "/home/erikh/src/github.com/contiv/volplugin/systemtests/testdata")
c.Assert(sf.GuestPath(), Equals, "/testdata")
}

44
providers/vmware/vmx.go

@ -1,7 +1,6 @@
package vmware
import (
"fmt"
"regexp"
"sort"
"strings"
@ -9,17 +8,6 @@ import (
"github.com/erikh/vcontrol/types"
)
const (
// VMXDiskSATA Disk type SATA
VMXDiskSATA types.DiskType = 0
// VMXDiskSCSI Disk type SCSI
VMXDiskSCSI = iota
// VMXDiskIDE Disk type IDE
VMXDiskIDE = iota
// VMXDiskFloppy Disk type Floppy
VMXDiskFloppy = iota
)
var (
vmxEthernetRegexp = regexp.MustCompile(`^ethernet`)
vmxSharedFolderRegexp = regexp.MustCompile(`^sharedFolder`)
@ -94,38 +82,6 @@ func (v *VMX) String() string {
return v.VMName
}
func (v *VMX) toParams() VMXParams {
vp := VMXParams{}
typeMap := map[types.DiskType][]*DiskController{}
for _, cont := range v.DiskControllers() {
typeMap[cont.Type()] = append(typeMap[cont.Type()], cont.(*DiskController))
}
for _, value := range typeMap {
for i, cont := range value {
tmp := cont.toParams(i)
for innerKey, innerValue := range tmp {
vp[innerKey] = innerValue
}
}
}
return vp
}
// Config returns the configuration in VMX format.
func (vp VMXParams) Config() string {
vmxFile := headerVMX + "\n"
for key, value := range vp {
vmxFile += fmt.Sprintf("%s = %q\n", key, value)
}
return vmxFile
}
func (vp VMXParams) loadDevices(matcher func(string) bool) map[string]VMXParams {
deviceMap := map[string]VMXParams{}

4
providers/vmware/vmx_parser.go

@ -51,10 +51,6 @@ func (vp VMXParams) read(filename string) error {
inSlash = true
}
default:
if inSlash {
inSlash = false
}
newValue += string(ch)
}

5
providers/vmware/vmx_parser_test.go

@ -9,7 +9,10 @@ func (vs *vmwareSuite) TestVMXParamsRead(c *C) {
}
func (vs *vmwareSuite) TestVMXFromFile(c *C) {
vmx, err := NewVMXFromFile("vmx_parser_testdata/centos72.vmx")
vmx, err := NewVMXFromFile("foo")
c.Assert(vmx, IsNil)
c.Assert(err, NotNil)
vmx, err = NewVMXFromFile("vmx_parser_testdata/centos72.vmx")
c.Assert(err, IsNil)
c.Assert(vmx.Disks(), DeepEquals, centosDisks)
c.Assert(vmx.Ethernets(), DeepEquals, centosEthernets)

2
providers/vmware/vmx_parser_testdata/example.vmx

@ -1,6 +1,6 @@
#!/usr/bin/vmware
.encoding = "UTF-8" # commment
someslashes ="\\\""
someslashes ="\"\\asdf"
thisisfalse = "FALSE"
config.version = ""
test.something =

23
vcontrol.go

@ -2,23 +2,32 @@
package vcontrol
import (
"fmt"
"github.com/erikh/vcontrol/context"
"github.com/erikh/vcontrol/providers/vmware"
)
var potentialProviders = []context.Provider{
&vmware.Provider{},
}
// Init returns an initialized Context for using the library functions to
// control vmware and virtualbox.
func Init(environment []string) (*context.Context, error) {
ctx := &context.Context{Providers: []context.Provider{}, Environment: environment}
vm := &vmware.Provider{}
if err := vm.Available(ctx); err != nil {
return nil, err
}
if err := vm.Init(ctx); err != nil {
return nil, err
for _, prov := range potentialProviders {
if err := prov.Available(ctx); err != nil {
continue
}
if err := prov.Init(ctx); err != nil {
return nil, fmt.Errorf("Could not init available provider: %v", err)
}
ctx.Providers = append(ctx.Providers, prov)
}
ctx.Providers = append(ctx.Providers, vm)
return ctx, nil
}

9
vcontrol_test.go

@ -18,6 +18,15 @@ func TestVControl(t *T) {
TestingT(t)
}
func (vc *vcontrolSuite) TestInit(c *C) {
// FIXME this test will fail on any machine that doesn't have vmware
// installed; solution: make a mock provider
ctx, err := Init(os.Environ())
c.Assert(err, IsNil)
c.Assert(ctx.Providers[0].Name(), Equals, "vmware")
}
func (vc *vcontrolSuite) TestInitWithProviders(c *C) {
vmw := &vmware.Provider{}
ctx, err := InitWithProviders([]context.Provider{vmw}, os.Environ())

Loading…
Cancel
Save