Skip to content
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
8 changes: 4 additions & 4 deletions builder/vmware/common/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func ReadNetmapConfig(path string) (NetworkMap, error) {
if err != nil {
return nil, err
}
defer fd.Close()
defer func() { _ = fd.Close() }()
return ReadNetworkMap(fd)
}

Expand All @@ -442,7 +442,7 @@ func ReadDhcpConfig(path string) (DhcpConfiguration, error) {
if err != nil {
return nil, err
}
defer fd.Close()
defer func() { _ = fd.Close() }()
return ReadDhcpConfiguration(fd)
}

Expand All @@ -452,7 +452,7 @@ func readVMXConfig(path string) (map[string]string, error) {
if err != nil {
return map[string]string{}, err
}
defer f.Close()
defer func() { _ = f.Close() }()

vmxBytes, err := io.ReadAll(f)
if err != nil {
Expand Down Expand Up @@ -730,7 +730,7 @@ func (d *VmwareDriver) HostIP(state multistep.StateBag) (string, error) {
func getHostIPForBridgedNetwork() (string, error) {
// Determine the source IP used for default-route communications.
if conn, err := net.Dial("udp4", "1.1.1.1:53"); err == nil {
defer conn.Close()
defer func() { _ = conn.Close() }()
if addr, ok := conn.LocalAddr().(*net.UDPAddr); ok {
log.Printf("[INFO] Discovered host IP address for bridged network using default route: %s", addr.IP)
return addr.IP.String(), nil
Expand Down
2 changes: 1 addition & 1 deletion builder/vmware/common/driver_fusion.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (d *FusionDriver) Verify() error {
if err != nil {
return nil, err
}
defer fd.Close()
defer func() { _ = fd.Close() }()

return ReadNetworkingConfig(fd)
}
Expand Down
10 changes: 5 additions & 5 deletions builder/vmware/common/driver_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ parameters : map[default-lease-time:1800 max-lease-time:7200]
if err != nil {
t.Fatalf("Failed to open dhcpd.conf sample: %s", err)
}
defer f.Close()
defer func() { _ = f.Close() }()

config, err := ReadDhcpConfiguration(f)
if err != nil {
Expand Down Expand Up @@ -422,7 +422,7 @@ func TestParserReadNetworkMap(t *testing.T) {
if err != nil {
t.Fatalf("Unable to open netmap.conf sample: %s", err)
}
defer f.Close()
defer func() { _ = f.Close() }()

netmap, err := ReadNetworkMap(f)
if err != nil {
Expand Down Expand Up @@ -760,7 +760,7 @@ func TestParserReadDhcpdLeases(t *testing.T) {
if err != nil {
t.Fatalf("Unable to open dhcpd.leases sample: %s", err)
}
defer f.Close()
defer func() { _ = f.Close() }()

results, err := ReadDhcpdLeaseEntries(f)
if err != nil {
Expand Down Expand Up @@ -945,7 +945,7 @@ func TestParserReadAppleDhcpdLeases(t *testing.T) {
if err != nil {
t.Fatalf("Unable to open dhcpd.leases sample: %s", err)
}
defer f.Close()
defer func() { _ = f.Close() }()

results, err := ReadAppleDhcpdLeaseEntries(f)
if err != nil {
Expand Down Expand Up @@ -1211,7 +1211,7 @@ func TestParserReadNetworingConfig(t *testing.T) {
if err != nil {
t.Fatalf("Unable to open networking-example sample: %s", err)
}
defer f.Close()
defer func() { _ = f.Close() }()

config, err := ReadNetworkingConfig(f)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion builder/vmware/common/driver_workstation.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ func checkNetmapConfExists() (NetworkNameMapper, error) {
return nil, err
}

defer fd.Close()
defer func() { _ = fd.Close() }()

// Pass the handle to the networking configuration parser.
return ReadNetworkingConfig(fd)
Expand Down
19 changes: 11 additions & 8 deletions builder/vmware/common/ssh_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func TestSSHConfigPrepare_SSHPrivateKey(t *testing.T) {
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.Remove(tf.Name())
defer tf.Close()
defer func() { _ = os.Remove(tf.Name()) }()
defer func() { _ = tf.Close() }()

if _, err := tf.Write([]byte("HELLO!")); err != nil {
t.Fatalf("err: %s", err)
Expand All @@ -72,12 +72,15 @@ func TestSSHConfigPrepare_SSHPrivateKey(t *testing.T) {
}

// Test good contents
//nolint
tf.Seek(0, 0)
//nolint
tf.Truncate(0)
//nolint
tf.Write([]byte(testPem))
if _, err := tf.Seek(0, 0); err != nil {
t.Fatalf("err: %s", err)
}
if err := tf.Truncate(0); err != nil {
t.Fatalf("err: %s", err)
}
if _, err := tf.Write([]byte(testPem)); err != nil {
t.Fatalf("err: %s", err)
}
c = testSSHConfig()
c.Comm.SSHPrivateKeyFile = tf.Name()
errs = c.Prepare(interpolate.NewContext())
Expand Down
16 changes: 8 additions & 8 deletions builder/vmware/common/step_clean_vmx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestStepCleanVMX(t *testing.T) {
step := new(StepCleanVMX)

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()
state.Put("vmx_path", vmxPath)

// Test the run
Expand All @@ -38,7 +38,7 @@ func TestStepCleanVMX_floppyPath(t *testing.T) {
step := new(StepCleanVMX)

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()
if err := os.WriteFile(vmxPath, []byte(testVMXFloppyPath), 0644); err != nil { //nolint:gosec
t.Fatalf("err: %s", err)
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestStepCleanVMX_isoPath(t *testing.T) {
step := new(StepCleanVMX)

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()
if err := os.WriteFile(vmxPath, []byte(testVMXISOPath), 0644); err != nil { //nolint:gosec
t.Fatalf("err: %s", err)
}
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestStepCleanVMX_ethernet(t *testing.T) {
}

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()
if err := os.WriteFile(vmxPath, []byte(testVMXEthernet), 0644); err != nil { //nolint:gosec
t.Fatalf("err: %s", err)
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestStepCleanVMX_toolsCDROM(t *testing.T) {
step := new(StepCleanVMX)

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()
if err := os.WriteFile(vmxPath, []byte(testVMXToolsCDROM), 0644); err != nil { //nolint:gosec
t.Fatalf("err: %s", err)
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestStepCleanVMX_toolsCDROMOnly(t *testing.T) {
step := new(StepCleanVMX)

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()
if err := os.WriteFile(vmxPath, []byte(testVMXToolsCDROMOnly), 0644); err != nil { //nolint:gosec
t.Fatalf("err: %s", err)
}
Expand Down Expand Up @@ -352,7 +352,7 @@ func TestStepCleanVMX_noToolsCDROM(t *testing.T) {
step := new(StepCleanVMX)

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()
if err := os.WriteFile(vmxPath, []byte(testVMXISOPath), 0644); err != nil { //nolint:gosec
t.Fatalf("err: %s", err)
}
Expand Down Expand Up @@ -403,7 +403,7 @@ func TestStepCleanVMX_preserveUserCDROM(t *testing.T) {
step := new(StepCleanVMX)

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()
if err := os.WriteFile(vmxPath, []byte(testVMXMultipleCDROM), 0644); err != nil { //nolint:gosec
t.Fatalf("err: %s", err)
}
Expand Down
16 changes: 9 additions & 7 deletions builder/vmware/common/step_configure_vmx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func testVMXFile(t *testing.T) string {
if err != nil {
t.Fatalf("error writing .vmx file: %s", err)
}
tf.Close()
if err := tf.Close(); err != nil {
t.Fatalf("err: %s", err)
}

return tf.Name()
}
Expand All @@ -41,7 +43,7 @@ func TestStepConfigureVMX(t *testing.T) {
}

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()
state.Put("vmx_path", vmxPath)

// Test the run
Expand Down Expand Up @@ -92,7 +94,7 @@ func TestStepConfigureVMX_floppyPath(t *testing.T) {
step := new(StepConfigureVMX)

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()

state.Put("floppy_path", "foo")
state.Put("vmx_path", vmxPath)
Expand Down Expand Up @@ -140,7 +142,7 @@ func TestStepConfigureVMX_generatedAddresses(t *testing.T) {
step := new(StepConfigureVMX)

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()

additionalTestVmxData := []struct {
Key string
Expand Down Expand Up @@ -216,7 +218,7 @@ func TestStepConfigureVMX_displayNameMissing(t *testing.T) {

// testVMXFile adds displayName key/value pair to the VMX
vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()

// Bad: Delete displayName from the VMX/Create an empty VMX file
err := WriteVMX(vmxPath, map[string]string{})
Expand All @@ -242,7 +244,7 @@ func TestStepConfigureVMX_displayNameStore(t *testing.T) {

// testVMXFile adds displayName key/value pair to the VMX
vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()

state.Put("vmx_path", vmxPath)

Expand Down Expand Up @@ -388,7 +390,7 @@ func TestStepConfigureVMX_CDROMAdapter(t *testing.T) {
}

vmxPath := testVMXFile(t)
defer os.Remove(vmxPath)
defer func() { _ = os.Remove(vmxPath) }()
state.Put("vmx_path", vmxPath)
state.Put("temporaryDevices", []string{})

Expand Down
5 changes: 4 additions & 1 deletion builder/vmware/common/step_configure_vnc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ func (s *StepConfigureVNC) VNCAddress(ctx context.Context, vncBindAddress string
return "", 0, err
}

s.l.Listener.Close() // free port, but don't unlock lock file
// Free the port, but don't unlock the lock file.
if err := s.l.Listener.Close(); err != nil {
return "", 0, err
}
return s.l.Address, s.l.Port, nil
}

Expand Down
12 changes: 6 additions & 6 deletions builder/vmware/common/step_output_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func testOutputDir(t *testing.T) string {
if err != nil {
t.Fatalf("err: %s", err)
}
os.RemoveAll(td)
_ = os.RemoveAll(td)

return td
}
Expand All @@ -41,7 +41,7 @@ func TestStepOutputDir(t *testing.T) {
VMName: "testVM",
}
// Delete the test output directory when done
defer os.RemoveAll(td)
defer func() { _ = os.RemoveAll(td) }()

// Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
Expand Down Expand Up @@ -74,13 +74,13 @@ func TestStepOutputDir_existsNoForce(t *testing.T) {
VMName: "testVM",
}
// Delete the test output directory when done
defer os.RemoveAll(td)
defer func() { _ = os.RemoveAll(td) }()

// Make sure the dir exists
if err := os.MkdirAll(td, 0755); err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(td)
defer func() { _ = os.RemoveAll(td) }()

// Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
Expand Down Expand Up @@ -112,13 +112,13 @@ func TestStepOutputDir_existsForce(t *testing.T) {
step.Force = true

// Delete the test output directory when done
defer os.RemoveAll(td)
defer func() { _ = os.RemoveAll(td) }()

// Make sure the dir exists
if err := os.MkdirAll(td, 0755); err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(td)
defer func() { _ = os.RemoveAll(td) }()

// Test the run
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
Expand Down
18 changes: 12 additions & 6 deletions builder/vmware/common/step_prepare_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ func TestStepPrepareTools(t *testing.T) {
if err != nil {
t.Fatalf("err: %s", err)
}
tf.Close()
defer os.Remove(tf.Name())
if err := tf.Close(); err != nil {
t.Fatalf("err: %s", err)
}
defer func() { _ = os.Remove(tf.Name()) }()

state := testState(t)
step := &StepPrepareTools{
Expand Down Expand Up @@ -195,8 +197,10 @@ func TestStepPrepareTools_AttachMode_Flavor(t *testing.T) {
if err != nil {
t.Fatalf("err: %s", err)
}
tf.Close()
defer os.Remove(tf.Name())
if err := tf.Close(); err != nil {
t.Fatalf("err: %s", err)
}
defer func() { _ = os.Remove(tf.Name()) }()

state := testState(t)
step := &StepPrepareTools{
Expand Down Expand Up @@ -275,8 +279,10 @@ func TestStepPrepareTools_BackwardCompatibility(t *testing.T) {
if err != nil {
t.Fatalf("err: %s", err)
}
tf.Close()
defer os.Remove(tf.Name())
if err := tf.Close(); err != nil {
t.Fatalf("err: %s", err)
}
defer func() { _ = os.Remove(tf.Name()) }()

state := testState(t)
step := &StepPrepareTools{
Expand Down
4 changes: 2 additions & 2 deletions builder/vmware/common/step_shutdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func testLocalOutputDir(t *testing.T) *LocalOutputDir {
if err != nil {
t.Fatalf("err: %s", err)
}
os.RemoveAll(td)
_ = os.RemoveAll(td)

result := new(LocalOutputDir)
result.SetOutputDir(td)
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestStepShutdown_locks(t *testing.T) {
// Remove the lock file after a certain time
go func() {
time.Sleep(100 * time.Millisecond)
os.Remove(lockPath)
_ = os.Remove(lockPath)
}()

resultCh := make(chan multistep.StepAction, 1)
Expand Down
2 changes: 1 addition & 1 deletion builder/vmware/common/step_upload_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *StepUploadTools) Run(ctx context.Context, state multistep.StateBag) mul
state.Put("error", fmt.Errorf("error opening VMware Tools ISO: %s", err))
return multistep.ActionHalt
}
defer f.Close()
defer func() { _ = f.Close() }()

// Interpolate upload path template if using flavor
if c.ToolsUploadFlavor != "" {
Expand Down
Loading