Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: floppy naming pattern #403

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
tenthirtyam marked this conversation as resolved.
Show resolved Hide resolved
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
Loading