1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
package common
import (
"context"
"testing"
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
)
type parseBootDeviceIdentifierTest struct {
generation uint
deviceIdentifier string
controllerType string
controllerNumber uint
controllerLocation uint
failInParse bool // true if ParseBootDeviceIdentifier should return an error
haltStep bool // true if Step.Run should return Halt action
shouldCallSet bool // true if driver.SetFirstBootDevice should have been called
setDvdProps bool // true to set DvdDeviceProperties state
}
var parseIdentifierTests = [...]parseBootDeviceIdentifierTest{
{1, "IDE", "IDE", 0, 0, false, false, true, false},
{1, "idE", "IDE", 0, 0, false, false, true, false},
{1, "CD", "CD", 0, 0, false, false, false, false},
{1, "CD", "CD", 0, 0, false, false, true, true},
{1, "cD", "CD", 0, 0, false, false, false, false},
{1, "DVD", "CD", 0, 0, false, false, false, false},
{1, "DVD", "CD", 0, 0, false, false, true, true},
{1, "Dvd", "CD", 0, 0, false, false, false, false},
{1, "FLOPPY", "FLOPPY", 0, 0, false, false, true, false},
{1, "FloppY", "FLOPPY", 0, 0, false, false, true, false},
{1, "NET", "NET", 0, 0, false, false, true, false},
{1, "net", "NET", 0, 0, false, false, true, false},
{1, "", "", 0, 0, true, false, false, false},
{1, "bad", "", 0, 0, true, true, false, false},
{1, "IDE:0:0", "", 0, 0, true, true, true, false},
{1, "SCSI:0:0", "", 0, 0, true, true, true, false},
{2, "IDE", "", 0, 0, true, true, true, false},
{2, "idE", "", 0, 0, true, true, true, false},
{2, "CD", "CD", 0, 0, false, false, false, false},
{2, "CD", "CD", 0, 0, false, false, true, true},
{2, "cD", "CD", 0, 0, false, false, false, false},
{2, "DVD", "CD", 0, 0, false, false, false, false},
{2, "DVD", "CD", 0, 0, false, false, true, true},
{2, "Dvd", "CD", 0, 0, false, false, false, false},
{2, "FLOPPY", "", 0, 0, true, true, true, false},
{2, "FloppY", "", 0, 0, true, true, true, false},
{2, "NET", "NET", 0, 0, false, false, true, false},
{2, "net", "NET", 0, 0, false, false, true, false},
{2, "", "", 0, 0, true, false, false, false},
{2, "bad", "", 0, 0, true, true, false, false},
{2, "IDE:0:0", "IDE", 0, 0, false, false, true, false},
{2, "SCSI:0:0", "SCSI", 0, 0, false, false, true, false},
{2, "Ide:0:0", "IDE", 0, 0, false, false, true, false},
{2, "sCsI:0:0", "SCSI", 0, 0, false, false, true, false},
{2, "IDEscsi:0:0", "", 0, 0, true, true, false, false},
{2, "SCSIide:0:0", "", 0, 0, true, true, false, false},
{2, "IDE:0", "", 0, 0, true, true, false, false},
{2, "SCSI:0", "", 0, 0, true, true, false, false},
{2, "IDE:0:a", "", 0, 0, true, true, false, false},
{2, "SCSI:0:a", "", 0, 0, true, true, false, false},
{2, "IDE:0:653", "", 0, 0, true, true, false, false},
{2, "SCSI:-10:0", "", 0, 0, true, true, false, false},
}
func TestStepSetFirstBootDevice_impl(t *testing.T) {
var _ multistep.Step = new(StepSetFirstBootDevice)
}
func TestStepSetFirstBootDevice_ParseIdentifier(t *testing.T) {
for _, identifierTest := range parseIdentifierTests {
controllerType, controllerNumber, controllerLocation, err := ParseBootDeviceIdentifier(
identifierTest.deviceIdentifier,
identifierTest.generation)
if (err != nil) != identifierTest.failInParse {
t.Fatalf("Test %q (gen %v): failInParse: %v but err: %v", identifierTest.deviceIdentifier,
identifierTest.generation, identifierTest.failInParse, err)
}
switch {
case controllerType != identifierTest.controllerType:
t.Fatalf("Test %q (gen %v): controllerType: %q != %q", identifierTest.deviceIdentifier, identifierTest.generation,
identifierTest.controllerType, controllerType)
case controllerNumber != identifierTest.controllerNumber:
t.Fatalf("Test %q (gen %v): controllerNumber: %v != %v", identifierTest.deviceIdentifier, identifierTest.generation,
identifierTest.controllerNumber, controllerNumber)
case controllerLocation != identifierTest.controllerLocation:
t.Fatalf("Test %q (gen %v): controllerLocation: %v != %v", identifierTest.deviceIdentifier, identifierTest.generation,
identifierTest.controllerLocation, controllerLocation)
}
}
}
func TestStepSetFirstBootDevice(t *testing.T) {
step := new(StepSetFirstBootDevice)
for _, identifierTest := range parseIdentifierTests {
state := testState(t)
driver := state.Get("driver").(*DriverMock)
// requires the vmName state value
vmName := "foo"
state.Put("vmName", vmName)
// pretend that we mounted a DVD somewhere (CD:0:0)
if identifierTest.setDvdProps {
var dvdControllerProperties DvdControllerProperties
dvdControllerProperties.ControllerNumber = 0
dvdControllerProperties.ControllerLocation = 0
dvdControllerProperties.Existing = false
state.Put("os.dvd.properties", dvdControllerProperties)
}
step.Generation = identifierTest.generation
step.FirstBootDevice = identifierTest.deviceIdentifier
action := step.Run(context.Background(), state)
if (action != multistep.ActionContinue) != identifierTest.haltStep {
t.Fatalf("Test %q (gen %v): Bad action: %v", identifierTest.deviceIdentifier, identifierTest.generation, action)
}
if identifierTest.haltStep {
if _, ok := state.GetOk("error"); !ok {
t.Fatalf("Test %q (gen %v): Should have error", identifierTest.deviceIdentifier, identifierTest.generation)
}
// don't perform the remaining checks..
continue
} else {
if _, ok := state.GetOk("error"); ok {
t.Fatalf("Test %q (gen %v): Should NOT have error", identifierTest.deviceIdentifier, identifierTest.generation)
}
}
if driver.SetFirstBootDevice_Called != identifierTest.shouldCallSet {
if identifierTest.shouldCallSet {
t.Fatalf("Test %q (gen %v): Should have called SetFirstBootDevice", identifierTest.deviceIdentifier, identifierTest.generation)
}
t.Fatalf("Test %q (gen %v): Should NOT have called SetFirstBootDevice", identifierTest.deviceIdentifier, identifierTest.generation)
}
if (driver.SetFirstBootDevice_Called) &&
((driver.SetFirstBootDevice_VmName != vmName) ||
(driver.SetFirstBootDevice_ControllerType != identifierTest.controllerType) ||
(driver.SetFirstBootDevice_ControllerNumber != identifierTest.controllerNumber) ||
(driver.SetFirstBootDevice_ControllerLocation != identifierTest.controllerLocation) ||
(driver.SetFirstBootDevice_Generation != identifierTest.generation)) {
t.Fatalf("Test %q (gen %v): Called SetFirstBootDevice with unexpected arguments.", identifierTest.deviceIdentifier, identifierTest.generation)
}
}
}
|