Skip to content

Commit

Permalink
fix: floppy naming pattern
Browse files Browse the repository at this point in the history
- Updates the naming pattern for a generated floppy from the status `packer-tmp-created-floppy.flp` to a pattern of `packer-##########.flp`. This naming pattern matches the one used by packer-sdk for generated ISOs. This helps avoid conflicts with other floppy images that might be uploaded.
- Updates the tests to account for this change from the static name to the new pattern.

Ref: #93

Signed-off-by: Ryan Johnson <[email protected]>
  • Loading branch information
tenthirtyam committed Apr 11, 2024
1 parent 212e655 commit 86db58a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
20 changes: 15 additions & 5 deletions builder/vsphere/common/step_add_floppy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package common
import (
"context"
"fmt"
"math/rand"
"time"

"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
Expand Down Expand Up @@ -59,7 +61,7 @@ func (s *StepAddFloppy) Run(_ context.Context, state multistep.StateBag) multist
d := state.Get("driver").(driver.Driver)

if floppyPath, ok := state.GetOk("floppy_path"); ok {
ui.Say("Uploading created floppy image")
ui.Say("Uploading floppy image...")

ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
Expand All @@ -72,14 +74,22 @@ func (s *StepAddFloppy) Run(_ context.Context, state multistep.StateBag) multist
return multistep.ActionHalt
}

uploadPath := fmt.Sprintf("%v/packer-tmp-created-floppy.flp", vmDir)
// Create a new random number generator
src := rand.NewSource(time.Now().UnixNano())
r := rand.New(src)

// Generate a unique ID for the floppy image using the packer-##########.flp.
// This helps avoid conflicts with other floppy images that might be uploaded.
// This naming pattern matches the one used by packer-sdk for generated ISOs.
uniqueID := r.Int63n(9000000000) + 1000000000
uploadPath := fmt.Sprintf("%v/packer-%d.flp", vmDir, uniqueID)
if err := ds.UploadFile(floppyPath.(string), uploadPath, s.Host, s.SetHostForDatastoreUploads); err != nil {
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("uploaded_floppy_path", uploadPath)

ui.Say("Adding generated Floppy...")
ui.Say("Adding generated floppy image...")
floppyIMGPath := ds.ResolvePath(uploadPath)
err = vm.AddFloppy(floppyIMGPath)
if err != nil {
Expand All @@ -89,7 +99,7 @@ func (s *StepAddFloppy) Run(_ context.Context, state multistep.StateBag) multist
}

if s.Config.FloppyIMGPath != "" {
ui.Say("Adding Floppy image...")
ui.Say("Adding floppy image...")
err := vm.AddFloppy(s.Config.FloppyIMGPath)
if err != nil {
state.Put("error", err)
Expand All @@ -111,7 +121,7 @@ func (s *StepAddFloppy) Cleanup(state multistep.StateBag) {
d := state.Get("driver").(driver.Driver)

if UploadedFloppyPath, ok := state.GetOk("uploaded_floppy_path"); ok {
ui.Say("Deleting Floppy image ...")
ui.Say("Deleting floppy image...")

ds, err := d.FindDatastore(s.Datastore, s.Host)
if err != nil {
Expand Down
20 changes: 12 additions & 8 deletions builder/vsphere/common/step_add_floppy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package common
import (
"context"
"fmt"
"regexp"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -33,7 +34,7 @@ func TestStepAddFloppy_Run(t *testing.T) {
{
name: "Add floppy from state floppy path",
floppyPath: "floppy/path",
uploadedPath: "vm/dir/packer-tmp-created-floppy.flp",
uploadedPath: "vm/dir/packer-*.flp",
step: &StepAddFloppy{
Config: new(FloppyConfig),
Datastore: "datastore",
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestStepAddFloppy_Run(t *testing.T) {
expectedDsMock: &driver.DatastoreMock{
UploadFileCalled: true,
UploadFileSrc: "floppy/path",
UploadFileDst: "vm/dir/packer-tmp-created-floppy.flp",
UploadFileDst: "vm/dir/packer-*.flp",
UploadFileHost: "host",
UploadFileSetHost: true,
ResolvePathCalled: true,
Expand Down Expand Up @@ -151,7 +152,7 @@ func TestStepAddFloppy_Run(t *testing.T) {
expectedDsMock: &driver.DatastoreMock{
UploadFileCalled: true,
UploadFileSrc: "floppy/path",
UploadFileDst: "vm/dir/packer-tmp-created-floppy.flp",
UploadFileDst: "vm/dir/packer-*.flp",
UploadFileHost: "host",
UploadFileSetHost: true,
},
Expand All @@ -161,7 +162,7 @@ func TestStepAddFloppy_Run(t *testing.T) {
{
name: "State floppy path - vm fail to add floppy",
floppyPath: "floppy/path",
uploadedPath: "vm/dir/packer-tmp-created-floppy.flp",
uploadedPath: "vm/dir/packer-*.flp",
step: &StepAddFloppy{
Config: new(FloppyConfig),
Datastore: "datastore",
Expand Down Expand Up @@ -191,7 +192,7 @@ func TestStepAddFloppy_Run(t *testing.T) {
expectedDsMock: &driver.DatastoreMock{
UploadFileCalled: true,
UploadFileSrc: "floppy/path",
UploadFileDst: "vm/dir/packer-tmp-created-floppy.flp",
UploadFileDst: "vm/dir/packer-*.flp",
UploadFileHost: "host",
UploadFileSetHost: true,
ResolvePathCalled: true,
Expand Down Expand Up @@ -268,9 +269,12 @@ func TestStepAddFloppy_Run(t *testing.T) {
}
}

uploadedPath, _ := state.Get("uploaded_floppy_path").(string)
if uploadedPath != c.uploadedPath {
t.Fatalf("Unexpected uploaded path state %s", uploadedPath)
if c.driverMock.DatastoreMock.UploadFileDst != "" {
pattern := regexp.MustCompile(`vm/dir/packer-(\d{10}|tmp-created-floppy)\.flp`)
if !pattern.MatchString(c.driverMock.DatastoreMock.UploadFileDst) {
t.Fatalf("unexpected UploadFileDst %v", c.driverMock.DatastoreMock.UploadFileDst)
}
c.driverMock.DatastoreMock.UploadFileDst = "vm/dir/packer-*.flp"
}

if diff := cmp.Diff(c.vmMock, c.expectedVmMock,
Expand Down

0 comments on commit 86db58a

Please sign in to comment.