From c5cd6f6fff93efba186270f63865aecc7f5c7e50 Mon Sep 17 00:00:00 2001 From: eduzgun Date: Fri, 14 Jun 2024 18:43:42 +0100 Subject: [PATCH 01/16] initwd Windows tests fixes Signed-off-by: eduzgun --- internal/initwd/from_module_test.go | 2 ++ internal/initwd/module_install_test.go | 14 ++++++++++---- internal/modsdir/manifest.go | 2 ++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/initwd/from_module_test.go b/internal/initwd/from_module_test.go index 388480590a..27755962e5 100644 --- a/internal/initwd/from_module_test.go +++ b/internal/initwd/from_module_test.go @@ -9,6 +9,7 @@ import ( "context" "os" "path/filepath" + "runtime" "strings" "testing" @@ -281,6 +282,7 @@ func TestDirFromModule_rel_submodules(t *testing.T) { } t.Cleanup(func() { os.Chdir(oldDir) + runtime.GC() }) hooks := &testInstallHooks{} diff --git a/internal/initwd/module_install_test.go b/internal/initwd/module_install_test.go index 2deecac4b6..7faaa3a799 100644 --- a/internal/initwd/module_install_test.go +++ b/internal/initwd/module_install_test.go @@ -12,6 +12,7 @@ import ( "fmt" "os" "path/filepath" + "runtime" "strings" "testing" @@ -61,7 +62,7 @@ func TestModuleInstaller(t *testing.T) { Name: "Install", ModuleAddr: "child_a.child_b", PackageAddr: "", - LocalPath: "child_a/child_b", + LocalPath: filepath.Join("child_a", "child_b"), }, } @@ -393,7 +394,7 @@ func TestModuleInstaller_symlink(t *testing.T) { Name: "Install", ModuleAddr: "child_a.child_b", PackageAddr: "", - LocalPath: "child_a/child_b", + LocalPath: filepath.Join("child_a", "child_b"), }, } @@ -739,10 +740,15 @@ func TestModuleInstaller_fromTests(t *testing.T) { _, diags := inst.InstallModules(context.Background(), ".", "tests", false, false, hooks) assertNoDiagnostics(t, diags) + // Use backslashes (\) for Windows paths in ModuleAddr + expectedModuleAddr := "test.tests.main.setup" + if runtime.GOOS == "windows" { + expectedModuleAddr = "test.tests\\main.setup" + } wantCalls := []testInstallHookCall{ { Name: "Install", - ModuleAddr: "test.tests.main.setup", + ModuleAddr: expectedModuleAddr, PackageAddr: "", LocalPath: "setup", }, @@ -764,7 +770,7 @@ func TestModuleInstaller_fromTests(t *testing.T) { config, loadDiags := loader.LoadConfigWithTests(".", "tests") assertNoDiagnostics(t, tfdiags.Diagnostics{}.Append(loadDiags)) - if config.Module.Tests["tests/main.tftest.hcl"].Runs[0].ConfigUnderTest == nil { + if config.Module.Tests[filepath.Join("tests", "main.tftest.hcl")].Runs[0].ConfigUnderTest == nil { t.Fatalf("should have loaded config into the relevant run block but did not") } } diff --git a/internal/modsdir/manifest.go b/internal/modsdir/manifest.go index 3360e4acc3..28f319f8c2 100644 --- a/internal/modsdir/manifest.go +++ b/internal/modsdir/manifest.go @@ -180,5 +180,7 @@ func (m Manifest) WriteSnapshotToDir(dir string) error { if err != nil { return err } + defer w.Close() + return m.WriteSnapshot(w) } From 3397ee59447d43a43dbb39ac125fcd276598685c Mon Sep 17 00:00:00 2001 From: eduzgun Date: Sun, 16 Jun 2024 20:13:49 +0100 Subject: [PATCH 02/16] Use filepath packge for cross-platform path handling Signed-off-by: eduzgun --- internal/configs/config_build.go | 7 +++---- internal/initwd/module_install_test.go | 8 +------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/internal/configs/config_build.go b/internal/configs/config_build.go index 27f7a8d6d9..37a0ac76f6 100644 --- a/internal/configs/config_build.go +++ b/internal/configs/config_build.go @@ -7,7 +7,7 @@ package configs import ( "fmt" - "path" + "path/filepath" "sort" "strings" @@ -64,8 +64,8 @@ func buildTestModules(root *Config, walker ModuleWalker) hcl.Diagnostics { // - file: main.tftest.hcl, run: setup - test.main.setup // - file: tests/main.tftest.hcl, run: setup - test.tests.main.setup - dir := path.Dir(name) - base := path.Base(name) + dir := filepath.Dir(name) + base := filepath.Base(name) path := addrs.Module{} path = append(path, "test") @@ -73,7 +73,6 @@ func buildTestModules(root *Config, walker ModuleWalker) hcl.Diagnostics { path = append(path, strings.Split(dir, "/")...) } path = append(path, strings.TrimSuffix(base, ".tftest.hcl"), run.Name) - req := ModuleRequest{ Name: run.Name, Path: path, diff --git a/internal/initwd/module_install_test.go b/internal/initwd/module_install_test.go index 7faaa3a799..5c2b6c5ee3 100644 --- a/internal/initwd/module_install_test.go +++ b/internal/initwd/module_install_test.go @@ -12,7 +12,6 @@ import ( "fmt" "os" "path/filepath" - "runtime" "strings" "testing" @@ -740,15 +739,10 @@ func TestModuleInstaller_fromTests(t *testing.T) { _, diags := inst.InstallModules(context.Background(), ".", "tests", false, false, hooks) assertNoDiagnostics(t, diags) - // Use backslashes (\) for Windows paths in ModuleAddr - expectedModuleAddr := "test.tests.main.setup" - if runtime.GOOS == "windows" { - expectedModuleAddr = "test.tests\\main.setup" - } wantCalls := []testInstallHookCall{ { Name: "Install", - ModuleAddr: expectedModuleAddr, + ModuleAddr: "test.tests.main.setup", PackageAddr: "", LocalPath: "setup", }, From 33b6e5c110913098a4c05a0cbaa95dfa22834d26 Mon Sep 17 00:00:00 2001 From: Emre Duzgun <100037959+eduzgun@users.noreply.github.com> Date: Sat, 15 Jun 2024 21:38:09 +0100 Subject: [PATCH 03/16] Update from_module_test.go Signed-off-by: Emre Duzgun <100037959+eduzgun@users.noreply.github.com> Signed-off-by: eduzgun --- internal/initwd/from_module_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/initwd/from_module_test.go b/internal/initwd/from_module_test.go index 27755962e5..f26818b4e3 100644 --- a/internal/initwd/from_module_test.go +++ b/internal/initwd/from_module_test.go @@ -282,6 +282,8 @@ func TestDirFromModule_rel_submodules(t *testing.T) { } t.Cleanup(func() { os.Chdir(oldDir) + // Trigger garbage collection to ensure that all open file handles are closed. + // This prevents "TempDir RemoveAll cleanup: The process cannot access the file because it is being used by another process" errors during TempDir cleanup. runtime.GC() }) From f8f7c0a9fbb4b7ed883da42a3cf64050ca93a333 Mon Sep 17 00:00:00 2001 From: eduzgun Date: Mon, 17 Jun 2024 21:14:48 +0100 Subject: [PATCH 04/16] Only manual garbage collect on Windows Signed-off-by: eduzgun --- internal/initwd/from_module_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/initwd/from_module_test.go b/internal/initwd/from_module_test.go index f26818b4e3..c0e76fd605 100644 --- a/internal/initwd/from_module_test.go +++ b/internal/initwd/from_module_test.go @@ -283,8 +283,11 @@ func TestDirFromModule_rel_submodules(t *testing.T) { t.Cleanup(func() { os.Chdir(oldDir) // Trigger garbage collection to ensure that all open file handles are closed. - // This prevents "TempDir RemoveAll cleanup: The process cannot access the file because it is being used by another process" errors during TempDir cleanup. - runtime.GC() + // This prevents "TempDir RemoveAll cleanup: The process cannot access the file + //because it is being used by another process" errors during TempDir cleanup on Windows. + if runtime.GOOS == "windows" { + runtime.GC() + } }) hooks := &testInstallHooks{} From 52890e74fc59ce887412e5489de59ccbab8ada5f Mon Sep 17 00:00:00 2001 From: eduzgun Date: Tue, 18 Jun 2024 11:52:57 +0100 Subject: [PATCH 05/16] backend/local tests fixed for Windows Signed-off-by: eduzgun --- internal/backend/local/testing.go | 8 ++++++++ internal/backend/testing.go | 33 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/internal/backend/local/testing.go b/internal/backend/local/testing.go index 417e340182..5b95e057d6 100644 --- a/internal/backend/local/testing.go +++ b/internal/backend/local/testing.go @@ -7,6 +7,7 @@ package local import ( "path/filepath" + "runtime" "testing" "github.com/zclconf/go-cty/cty" @@ -40,6 +41,13 @@ func TestLocal(t *testing.T) *Local { local.StateWorkspaceDir = filepath.Join(tempDir, "state.tfstate.d") local.ContextOpts = &tofu.ContextOpts{} + // Trigger garbage collection to ensure that all open file handles are closed. + // This prevents TempDir cleanup errors on Windows. + t.Cleanup(func() { + if runtime.GOOS == "windows" { + runtime.GC() + } + }) return local } diff --git a/internal/backend/testing.go b/internal/backend/testing.go index cf7059351b..224abd4135 100644 --- a/internal/backend/testing.go +++ b/internal/backend/testing.go @@ -7,6 +7,7 @@ package backend import ( "reflect" + "runtime" "sort" "testing" @@ -223,6 +224,18 @@ func TestBackendStates(t *testing.T, b Backend) { } } + // On Windows, lock and unlock the state to ensure no process is holding the file, + // which would prevent its deletion. This step avoids Tempdir cleanup errors. + if runtime.GOOS == "windows" { + fooInfo := statemgr.NewLockInfo() + fooInfo.Operation = "test" + var fooLock string + if fooLock, err = foo.Lock(fooInfo); err != nil { + t.Error("Failed to lock foo state in Windows test cleanup", err) + } + foo.Unlock(fooLock) + } + // Delete some workspaces if err := b.DeleteWorkspace("foo", true); err != nil { t.Fatalf("err: %s", err) @@ -246,6 +259,18 @@ func TestBackendStates(t *testing.T, b Backend) { if v := foo.State(); v.HasManagedResourceInstanceObjects() { t.Fatalf("should be empty: %s", v) } + + // On Windows, lock and unlock the state to ensure no process is holding the file, + // which would prevent its deletion. This step avoids Tempdir cleanup errors. + if runtime.GOOS == "windows" { + fooInfo := statemgr.NewLockInfo() + fooInfo.Operation = "test" + var fooLock string + if fooLock, err = foo.Lock(fooInfo); err != nil { + t.Error("Failed to lock foo state in Windows test cleanup", err) + } + foo.Unlock(fooLock) + } // and delete it again if err := b.DeleteWorkspace("foo", true); err != nil { t.Fatalf("err: %s", err) @@ -267,6 +292,14 @@ func TestBackendStates(t *testing.T, b Backend) { t.Fatalf("wrong workspaces list\ngot: %#v\nwant: %#v", workspaces, expected) } } + + // Trigger garbage collection to ensure that all open file handles are closed. + // This prevents TempDir cleanup errors on Windows. + t.Cleanup(func() { + if runtime.GOOS == "windows" { + runtime.GC() + } + }) } // TestBackendStateLocks will test the locking functionality of the remote From 250f7b2939898ea91addabe6a59cf52f48d343c1 Mon Sep 17 00:00:00 2001 From: eduzgun Date: Tue, 25 Jun 2024 07:48:24 +0100 Subject: [PATCH 06/16] Use go-acl pacakge to control file permissions for Windows Signed-off-by: eduzgun --- go.mod | 1 + go.sum | 3 +++ internal/backend/backend_test.go | 15 ++++++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index e5c55d647f..fd7f18e1e5 100644 --- a/go.mod +++ b/go.mod @@ -199,6 +199,7 @@ require ( github.com/hashicorp/serf v0.9.6 // indirect github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect + github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb // indirect github.com/henvic/httpretty v0.0.6 // indirect github.com/huandu/xstrings v1.3.3 // indirect github.com/imdario/mergo v0.3.13 // indirect diff --git a/go.sum b/go.sum index a9a96bbe1d..b1295ebfd2 100644 --- a/go.sum +++ b/go.sum @@ -733,6 +733,8 @@ github.com/hashicorp/vault/sdk v0.1.13/go.mod h1:B+hVj7TpuQY1Y/GPbCpffmgd+tSEwvh github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= +github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb h1:PGufWXXDq9yaev6xX1YQauaO1MV90e6Mpoq1I7Lz/VM= +github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb/go.mod h1:QiyDdbZLaJ/mZP4Zwc9g2QsfaEA4o7XvvgZegSci5/E= github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs= github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= @@ -1306,6 +1308,7 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190529164535-6a60838ec259/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/internal/backend/backend_test.go b/internal/backend/backend_test.go index 0c62f92fe4..39ca3ec95e 100644 --- a/internal/backend/backend_test.go +++ b/internal/backend/backend_test.go @@ -9,9 +9,11 @@ import ( "io" "os" "os/user" + "runtime" "strings" "testing" + "github.com/hectane/go-acl" "github.com/mitchellh/go-homedir" ) @@ -76,12 +78,19 @@ func TestRead_PathNoPermission(t *testing.T) { } f.Close() - if err := os.Chmod(f.Name(), 0); err != nil { - t.Fatalf("err: %s", err) + if runtime.GOOS == "windows" { + //Use go-acl pacakge to control file permissions for Windows + if err := acl.Chmod(f.Name(), 000); err != nil { + t.Fatalf("err: %s", err) + } + } else { + if err := os.Chmod(f.Name(), 000); err != nil { + t.Fatalf("err: %s", err) + } } contents, err := ReadPathOrContents(f.Name()) - + t.Log(contents) if err == nil { t.Fatal("Expected error, got none!") } From cf197b9e1502b6bee738536c4c88527ad82b28e9 Mon Sep 17 00:00:00 2001 From: eduzgun Date: Tue, 25 Jun 2024 13:24:02 +0100 Subject: [PATCH 07/16] Ensure consistent state initialisation across OS for tests Signed-off-by: eduzgun --- internal/backend/remote-state/http/server_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/backend/remote-state/http/server_test.go b/internal/backend/remote-state/http/server_test.go index 6e9ac6213f..a62283819f 100644 --- a/internal/backend/remote-state/http/server_test.go +++ b/internal/backend/remote-state/http/server_test.go @@ -361,6 +361,10 @@ func TestMTLSServer_WithCertPasses(t *testing.T) { if err = sm.RefreshState(); err != nil { t.Fatalf("unexpected error calling RefreshState: %v", err) } + + // Ensure state is not nil by building and writing a test state. + testState := states.BuildState(func(ss *states.SyncState) {}) + sm.WriteState(testState) state := sm.State() if nil == state { t.Fatal("nil state") From 862557a74464ead1a5a468bd3f1ae618f9adb943 Mon Sep 17 00:00:00 2001 From: eduzgun Date: Wed, 26 Jun 2024 10:21:38 +0100 Subject: [PATCH 08/16] Add Windows-specific env variables in s3 tests Signed-off-by: eduzgun --- .../remote-state/s3/backend_complete_test.go | 110 +++++++++++++++++- 1 file changed, 107 insertions(+), 3 deletions(-) diff --git a/internal/backend/remote-state/s3/backend_complete_test.go b/internal/backend/remote-state/s3/backend_complete_test.go index 01fdde1347..0373bd7d95 100644 --- a/internal/backend/remote-state/s3/backend_complete_test.go +++ b/internal/backend/remote-state/s3/backend_complete_test.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "regexp" + "runtime" "testing" "github.com/aws/aws-sdk-go-v2/aws" @@ -148,6 +149,9 @@ func (m noopMatcher) String() string { } func TestBackendConfig_Authentication(t *testing.T) { + testDirectory := t.TempDir() + sysRoot := os.Getenv("SYSTEMROOT") + testCases := map[string]struct { config map[string]any EnableEc2MetadataServer bool @@ -662,6 +666,20 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey t.Run(name, func(t *testing.T) { servicemocks.InitSessionTestEnv(t) + // Set Windows-specific environment variables + if runtime.GOOS == "windows" { + t.Setenv("TEMP", testDirectory) + t.Setenv("TMP", testDirectory) + t.Setenv("SYSTEMROOT", sysRoot) + + // Trigger garbage collection to ensure that all open file handles are closed. + // This prevents TempDir RemoveAll cleanup errors on Windows. + t.Cleanup(func() { + runtime.GC() + + }) + } + // Populate required fields tc.config["region"] = "us-east-1" tc.config["bucket"] = "bucket" @@ -782,6 +800,9 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey } } func TestBackendConfig_Authentication_AssumeRoleInline(t *testing.T) { + testDirectory := t.TempDir() + sysRoot := os.Getenv("SYSTEMROOT") + testCases := map[string]struct { config map[string]any EnableEc2MetadataServer bool @@ -1128,6 +1149,20 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey t.Run(name, func(t *testing.T) { servicemocks.InitSessionTestEnv(t) + // Set Windows-specific environment variables + if runtime.GOOS == "windows" { + t.Setenv("TEMP", testDirectory) + t.Setenv("TMP", testDirectory) + t.Setenv("SYSTEMROOT", sysRoot) + + // Trigger garbage collection to ensure that all open file handles are closed. + // This prevents TempDir RemoveAll cleanup errors on Windows. + t.Cleanup(func() { + runtime.GC() + + }) + } + ctx := context.TODO() // Populate required fields @@ -1222,6 +1257,9 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey } func TestBackendConfig_Authentication_AssumeRoleNested(t *testing.T) { + testDirectory := t.TempDir() + sysRoot := os.Getenv("SYSTEMROOT") + testCases := map[string]struct { config map[string]any EnableEc2MetadataServer bool @@ -1530,6 +1568,20 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey t.Run(name, func(t *testing.T) { servicemocks.InitSessionTestEnv(t) + // Set Windows-specific environment variables + if runtime.GOOS == "windows" { + t.Setenv("TEMP", testDirectory) + t.Setenv("TMP", testDirectory) + t.Setenv("SYSTEMROOT", sysRoot) + + // Trigger garbage collection to ensure that all open file handles are closed. + // This prevents TempDir RemoveAll cleanup errors on Windows. + t.Cleanup(func() { + runtime.GC() + + }) + } + ctx := context.TODO() // Populate required fields @@ -1626,6 +1678,9 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey } func TestBackendConfig_Authentication_AssumeRoleWithWebIdentity(t *testing.T) { + testDirectory := t.TempDir() + sysRoot := os.Getenv("SYSTEMROOT") + testCases := map[string]struct { config map[string]any SetConfig bool @@ -1803,6 +1858,20 @@ web_identity_token_file = no-such-file t.Run(name, func(t *testing.T) { servicemocks.InitSessionTestEnv(t) + // Set Windows-specific environment variables + if runtime.GOOS == "windows" { + t.Setenv("TEMP", testDirectory) + t.Setenv("TMP", testDirectory) + t.Setenv("SYSTEMROOT", sysRoot) + + // Trigger garbage collection to ensure that all open file handles are closed. + // This prevents TempDir RemoveAll cleanup errors on Windows. + t.Cleanup(func() { + runtime.GC() + + }) + } + ctx := context.TODO() // Populate required fields @@ -1823,8 +1892,6 @@ web_identity_token_file = no-such-file tc.config["sts_endpoint"] = ts.URL - t.Setenv("TMPDIR", t.TempDir()) - tokenFile, err := os.CreateTemp("", "aws-sdk-go-base-web-identity-token-file") if err != nil { t.Fatalf("unexpected error creating temporary web identity token file: %s", err) @@ -1841,12 +1908,15 @@ web_identity_token_file = no-such-file if tc.ExpandEnvVars { tmpdir := os.Getenv("TMPDIR") + if runtime.GOOS == "windows" { + tmpdir = os.Getenv("TEMP") + } rel, err := filepath.Rel(tmpdir, tokenFileName) if err != nil { t.Fatalf("error making path relative: %s", err) } t.Logf("relative: %s", rel) - tokenFileName = filepath.Join("$TMPDIR", rel) + tokenFileName = filepath.Join(tmpdir, rel) t.Logf("env tempfile: %s", tokenFileName) } @@ -1904,6 +1974,9 @@ web_identity_token_file = no-such-file } func TestBackendConfig_Region(t *testing.T) { + testDirectory := t.TempDir() + sysRoot := os.Getenv("SYSTEMROOT") + testCases := map[string]struct { config map[string]any EnvironmentVariables map[string]string @@ -2062,6 +2135,20 @@ region = us-west-2 t.Run(name, func(t *testing.T) { servicemocks.InitSessionTestEnv(t) + // Set Windows-specific environment variables + if runtime.GOOS == "windows" { + t.Setenv("TEMP", testDirectory) + t.Setenv("TMP", testDirectory) + t.Setenv("SYSTEMROOT", sysRoot) + + // Trigger garbage collection to ensure that all open file handles are closed. + // This prevents TempDir RemoveAll cleanup errors on Windows. + t.Cleanup(func() { + runtime.GC() + + }) + } + // Populate required fields tc.config["bucket"] = "bucket" tc.config["key"] = "key" @@ -2120,6 +2207,9 @@ region = us-west-2 } func TestBackendConfig_RetryMode(t *testing.T) { + testDirectory := t.TempDir() + sysRoot := os.Getenv("SYSTEMROOT") + testCases := map[string]struct { config map[string]any EnvironmentVariables map[string]string @@ -2171,6 +2261,20 @@ func TestBackendConfig_RetryMode(t *testing.T) { t.Run(name, func(t *testing.T) { servicemocks.InitSessionTestEnv(t) + // Set Windows-specific environment variables + if runtime.GOOS == "windows" { + t.Setenv("TEMP", testDirectory) + t.Setenv("TMP", testDirectory) + t.Setenv("SYSTEMROOT", sysRoot) + + // Trigger garbage collection to ensure that all open file handles are closed. + // This prevents TempDir RemoveAll cleanup errors on Windows. + t.Cleanup(func() { + runtime.GC() + + }) + } + // Populate required fields tc.config["bucket"] = "bucket" tc.config["key"] = "key" From 660db0f200075de349a10833e7858254861eb390 Mon Sep 17 00:00:00 2001 From: eduzgun Date: Wed, 26 Jun 2024 17:27:42 +0100 Subject: [PATCH 09/16] Fix linting Signed-off-by: eduzgun --- internal/backend/backend_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/backend/backend_test.go b/internal/backend/backend_test.go index 39ca3ec95e..a7a7afd91f 100644 --- a/internal/backend/backend_test.go +++ b/internal/backend/backend_test.go @@ -79,7 +79,7 @@ func TestRead_PathNoPermission(t *testing.T) { f.Close() if runtime.GOOS == "windows" { - //Use go-acl pacakge to control file permissions for Windows + // Use go-acl pacakge to control file permissions for Windows if err := acl.Chmod(f.Name(), 000); err != nil { t.Fatalf("err: %s", err) } From a64b3488dd1a1dc0c5e98ce827311c35a0fb500b Mon Sep 17 00:00:00 2001 From: eduzgun Date: Fri, 28 Jun 2024 16:17:39 +0100 Subject: [PATCH 10/16] Fix tempdir issue in s3 unit test Signed-off-by: eduzgun --- internal/backend/remote-state/s3/backend_complete_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/backend/remote-state/s3/backend_complete_test.go b/internal/backend/remote-state/s3/backend_complete_test.go index 1197a98a91..f5c1978bc9 100644 --- a/internal/backend/remote-state/s3/backend_complete_test.go +++ b/internal/backend/remote-state/s3/backend_complete_test.go @@ -1864,6 +1864,8 @@ web_identity_token_file = no-such-file tc.config["sts_endpoint"] = ts.URL + t.Setenv("TMPDIR", t.TempDir()) + tokenFile, err := os.CreateTemp("", "aws-sdk-go-base-web-identity-token-file") if err != nil { t.Fatalf("unexpected error creating temporary web identity token file: %s", err) @@ -1879,16 +1881,20 @@ web_identity_token_file = no-such-file } if tc.ExpandEnvVars { + var prefix string tmpdir := os.Getenv("TMPDIR") if runtime.GOOS == "windows" { tmpdir = os.Getenv("TEMP") + prefix = tmpdir + } else { + prefix = "$TMPDIR" } rel, err := filepath.Rel(tmpdir, tokenFileName) if err != nil { t.Fatalf("error making path relative: %s", err) } t.Logf("relative: %s", rel) - tokenFileName = filepath.Join(tmpdir, rel) + tokenFileName = filepath.Join(prefix, rel) t.Logf("env tempfile: %s", tokenFileName) } From 4858da7aa9f1ff41f3be7afd3bff7563a89daf13 Mon Sep 17 00:00:00 2001 From: eduzgun Date: Fri, 28 Jun 2024 17:39:32 +0100 Subject: [PATCH 11/16] Undo workaround for state tests Signed-off-by: eduzgun --- go.mod | 1 - go.sum | 3 --- internal/states/statemgr/filesystem.go | 10 +++++++++- internal/states/statemgr/filesystem_test.go | 11 ----------- 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index fd7f18e1e5..e5c55d647f 100644 --- a/go.mod +++ b/go.mod @@ -199,7 +199,6 @@ require ( github.com/hashicorp/serf v0.9.6 // indirect github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect - github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb // indirect github.com/henvic/httpretty v0.0.6 // indirect github.com/huandu/xstrings v1.3.3 // indirect github.com/imdario/mergo v0.3.13 // indirect diff --git a/go.sum b/go.sum index b1295ebfd2..a9a96bbe1d 100644 --- a/go.sum +++ b/go.sum @@ -733,8 +733,6 @@ github.com/hashicorp/vault/sdk v0.1.13/go.mod h1:B+hVj7TpuQY1Y/GPbCpffmgd+tSEwvh github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= -github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb h1:PGufWXXDq9yaev6xX1YQauaO1MV90e6Mpoq1I7Lz/VM= -github.com/hectane/go-acl v0.0.0-20230122075934-ca0b05cb1adb/go.mod h1:QiyDdbZLaJ/mZP4Zwc9g2QsfaEA4o7XvvgZegSci5/E= github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs= github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo= github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= @@ -1308,7 +1306,6 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190529164535-6a60838ec259/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/internal/states/statemgr/filesystem.go b/internal/states/statemgr/filesystem.go index 2b92d06985..60a8edfdbf 100644 --- a/internal/states/statemgr/filesystem.go +++ b/internal/states/statemgr/filesystem.go @@ -172,7 +172,15 @@ func (s *Filesystem) persistState(schemas *tofu.Schemas) error { return nil } } - defer s.stateFileOut.Sync() + + // Sync and close the file handle after all operations are complete + defer func() { + if s.stateFileOut != nil { + s.stateFileOut.Sync() + s.stateFileOut.Close() + s.stateFileOut = nil + } + }() if s.file == nil { s.file = NewStateFile() diff --git a/internal/states/statemgr/filesystem_test.go b/internal/states/statemgr/filesystem_test.go index 9f49c0444b..261172707e 100644 --- a/internal/states/statemgr/filesystem_test.go +++ b/internal/states/statemgr/filesystem_test.go @@ -189,8 +189,6 @@ func TestFilesystem_backup(t *testing.T) { // not the contents of the input file (which is left unchanged). func TestFilesystem_backupAndReadPath(t *testing.T) { defer testOverrideVersion(t, "1.2.3")() - info := NewLockInfo() - info.Operation = "test" workDir := t.TempDir() markerOutput := addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance) @@ -256,15 +254,6 @@ func TestFilesystem_backupAndReadPath(t *testing.T) { t.Fatalf("failed to write new state: %s", err) } - lockID, err := ls.Lock(info) - if err != nil { - t.Fatal(err) - } - - if err := ls.Unlock(lockID); err != nil { - t.Fatal(err) - } - // The backup functionality should've saved a copy of the original contents // of the _output_ file, even though the first snapshot was read from // the _input_ file. From bd001bb5cee7321c0efb387bee5060e0da39e42f Mon Sep 17 00:00:00 2001 From: eduzgun Date: Fri, 28 Jun 2024 17:59:35 +0100 Subject: [PATCH 12/16] Update internal/backend unit tests Signed-off-by: eduzgun --- internal/backend/backend_test.go | 14 +++++++------- internal/backend/testing.go | 23 ----------------------- internal/states/statemgr/filesystem.go | 8 +++----- 3 files changed, 10 insertions(+), 35 deletions(-) diff --git a/internal/backend/backend_test.go b/internal/backend/backend_test.go index a7a7afd91f..44572324ae 100644 --- a/internal/backend/backend_test.go +++ b/internal/backend/backend_test.go @@ -8,12 +8,12 @@ package backend import ( "io" "os" + "os/exec" "os/user" "runtime" "strings" "testing" - "github.com/hectane/go-acl" "github.com/mitchellh/go-homedir" ) @@ -79,18 +79,18 @@ func TestRead_PathNoPermission(t *testing.T) { f.Close() if runtime.GOOS == "windows" { - // Use go-acl pacakge to control file permissions for Windows - if err := acl.Chmod(f.Name(), 000); err != nil { - t.Fatalf("err: %s", err) + // Use cacls to remove all permissions for this file on Windows + cmd := exec.Command("cmd", "/c", "cacls", f.Name(), "/E", "/R", os.Getenv("USERNAME")) + if err := cmd.Run(); err != nil { + t.Fatalf("Failed to set file permissions with cacls: %s", err) } } else { - if err := os.Chmod(f.Name(), 000); err != nil { - t.Fatalf("err: %s", err) + if err := os.Chmod(f.Name(), 0); err != nil { + t.Fatalf("Failed to chmod file: %s", err) } } contents, err := ReadPathOrContents(f.Name()) - t.Log(contents) if err == nil { t.Fatal("Expected error, got none!") } diff --git a/internal/backend/testing.go b/internal/backend/testing.go index 224abd4135..459e75ec13 100644 --- a/internal/backend/testing.go +++ b/internal/backend/testing.go @@ -224,18 +224,6 @@ func TestBackendStates(t *testing.T, b Backend) { } } - // On Windows, lock and unlock the state to ensure no process is holding the file, - // which would prevent its deletion. This step avoids Tempdir cleanup errors. - if runtime.GOOS == "windows" { - fooInfo := statemgr.NewLockInfo() - fooInfo.Operation = "test" - var fooLock string - if fooLock, err = foo.Lock(fooInfo); err != nil { - t.Error("Failed to lock foo state in Windows test cleanup", err) - } - foo.Unlock(fooLock) - } - // Delete some workspaces if err := b.DeleteWorkspace("foo", true); err != nil { t.Fatalf("err: %s", err) @@ -260,17 +248,6 @@ func TestBackendStates(t *testing.T, b Backend) { t.Fatalf("should be empty: %s", v) } - // On Windows, lock and unlock the state to ensure no process is holding the file, - // which would prevent its deletion. This step avoids Tempdir cleanup errors. - if runtime.GOOS == "windows" { - fooInfo := statemgr.NewLockInfo() - fooInfo.Operation = "test" - var fooLock string - if fooLock, err = foo.Lock(fooInfo); err != nil { - t.Error("Failed to lock foo state in Windows test cleanup", err) - } - foo.Unlock(fooLock) - } // and delete it again if err := b.DeleteWorkspace("foo", true); err != nil { t.Fatalf("err: %s", err) diff --git a/internal/states/statemgr/filesystem.go b/internal/states/statemgr/filesystem.go index 60a8edfdbf..ed946b28c9 100644 --- a/internal/states/statemgr/filesystem.go +++ b/internal/states/statemgr/filesystem.go @@ -175,11 +175,9 @@ func (s *Filesystem) persistState(schemas *tofu.Schemas) error { // Sync and close the file handle after all operations are complete defer func() { - if s.stateFileOut != nil { - s.stateFileOut.Sync() - s.stateFileOut.Close() - s.stateFileOut = nil - } + s.stateFileOut.Sync() + s.stateFileOut.Close() + s.stateFileOut = nil }() if s.file == nil { From 3902cfd103e3e3af42b9ac1263652ab0de3e78ed Mon Sep 17 00:00:00 2001 From: eduzgun Date: Sat, 29 Jun 2024 12:41:35 +0100 Subject: [PATCH 13/16] More error checking in persist state Signed-off-by: eduzgun --- internal/states/statemgr/filesystem.go | 26 ++++++++++--------- .../states/statemgr/filesystem_lock_unix.go | 12 ++++++++- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/internal/states/statemgr/filesystem.go b/internal/states/statemgr/filesystem.go index ed946b28c9..91139e82d1 100644 --- a/internal/states/statemgr/filesystem.go +++ b/internal/states/statemgr/filesystem.go @@ -395,23 +395,25 @@ func (s *Filesystem) Unlock(id string) error { } else { log.Printf("[TRACE] statemgr.Filesystem: removed lock metadata file %s", lockInfoPath) } - fileName := s.stateFileOut.Name() - unlockErr := s.unlock() + if s.stateFileOut != nil { + fileName := s.stateFileOut.Name() - s.stateFileOut.Close() - s.stateFileOut = nil - s.lockID = "" + s.stateFileOut.Close() + s.stateFileOut = nil + s.lockID = "" - // clean up the state file if we created it an never wrote to it - stat, err := os.Stat(fileName) - if err == nil && stat.Size() == 0 && s.created { - err = os.Remove(fileName) - if err != nil { - log.Printf("[ERROR] stagemgr.Filesystem: error removing empty state file %q: %s", fileName, err) + // clean up the state file if we created it an never wrote to it + stat, err := os.Stat(fileName) + if err == nil && stat.Size() == 0 && s.created { + err = os.Remove(fileName) + if err != nil { + log.Printf("[ERROR] stagemgr.Filesystem: error removing empty state file %q: %s", fileName, err) + } } - } + } + s.lockID = "" return unlockErr } diff --git a/internal/states/statemgr/filesystem_lock_unix.go b/internal/states/statemgr/filesystem_lock_unix.go index 59652d6e32..ca445d3dd6 100644 --- a/internal/states/statemgr/filesystem_lock_unix.go +++ b/internal/states/statemgr/filesystem_lock_unix.go @@ -30,6 +30,17 @@ func (s *Filesystem) lock() error { } func (s *Filesystem) unlock() error { + + if s.stateFileOut == nil { + log.Print("TRACE] statemgr.Filesystem: statefileout is nil ") + return nil + } + + fd := s.stateFileOut.Fd() + if fd == ^uintptr(0) { + log.Print("TRACE] statemgr.Filesystem: fd is 0 ") + return nil + } log.Printf("[TRACE] statemgr.Filesystem: unlocking %s using fcntl flock", s.path) flock := &syscall.Flock_t{ Type: syscall.F_UNLCK, @@ -38,6 +49,5 @@ func (s *Filesystem) unlock() error { Len: 0, } - fd := s.stateFileOut.Fd() return syscall.FcntlFlock(fd, syscall.F_SETLK, flock) } From 36f78c1d6bc2c21b243a3ef6baedb752cd14a54f Mon Sep 17 00:00:00 2001 From: eduzgun Date: Sat, 29 Jun 2024 13:21:42 +0100 Subject: [PATCH 14/16] Code consistency changes Signed-off-by: eduzgun --- internal/backend/remote-state/http/server_test.go | 7 +++++-- internal/backend/remote-state/s3/backend_complete_test.go | 6 ------ internal/states/statemgr/filesystem.go | 8 ++++++-- internal/states/statemgr/filesystem_lock_unix.go | 8 +++++--- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/internal/backend/remote-state/http/server_test.go b/internal/backend/remote-state/http/server_test.go index a62283819f..61164a24e9 100644 --- a/internal/backend/remote-state/http/server_test.go +++ b/internal/backend/remote-state/http/server_test.go @@ -363,8 +363,11 @@ func TestMTLSServer_WithCertPasses(t *testing.T) { } // Ensure state is not nil by building and writing a test state. - testState := states.BuildState(func(ss *states.SyncState) {}) - sm.WriteState(testState) + testState := states.BuildState(func(_ *states.SyncState) {}) + err = sm.WriteState(testState) + if err != nil { + t.Errorf("error writing test state: %s", err) + } state := sm.State() if nil == state { t.Fatal("nil state") diff --git a/internal/backend/remote-state/s3/backend_complete_test.go b/internal/backend/remote-state/s3/backend_complete_test.go index f5c1978bc9..2a24954a71 100644 --- a/internal/backend/remote-state/s3/backend_complete_test.go +++ b/internal/backend/remote-state/s3/backend_complete_test.go @@ -648,7 +648,6 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey // This prevents TempDir RemoveAll cleanup errors on Windows. t.Cleanup(func() { runtime.GC() - }) } @@ -1131,7 +1130,6 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey // This prevents TempDir RemoveAll cleanup errors on Windows. t.Cleanup(func() { runtime.GC() - }) } @@ -1550,7 +1548,6 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey // This prevents TempDir RemoveAll cleanup errors on Windows. t.Cleanup(func() { runtime.GC() - }) } @@ -1840,7 +1837,6 @@ web_identity_token_file = no-such-file // This prevents TempDir RemoveAll cleanup errors on Windows. t.Cleanup(func() { runtime.GC() - }) } @@ -2123,7 +2119,6 @@ region = us-west-2 // This prevents TempDir RemoveAll cleanup errors on Windows. t.Cleanup(func() { runtime.GC() - }) } @@ -2249,7 +2244,6 @@ func TestBackendConfig_RetryMode(t *testing.T) { // This prevents TempDir RemoveAll cleanup errors on Windows. t.Cleanup(func() { runtime.GC() - }) } diff --git a/internal/states/statemgr/filesystem.go b/internal/states/statemgr/filesystem.go index 91139e82d1..8727057d63 100644 --- a/internal/states/statemgr/filesystem.go +++ b/internal/states/statemgr/filesystem.go @@ -175,7 +175,10 @@ func (s *Filesystem) persistState(schemas *tofu.Schemas) error { // Sync and close the file handle after all operations are complete defer func() { - s.stateFileOut.Sync() + if err := s.stateFileOut.Sync(); err != nil { + log.Printf("Error syncing statefile: %s", err) + } + s.stateFileOut.Close() s.stateFileOut = nil }() @@ -396,6 +399,7 @@ func (s *Filesystem) Unlock(id string) error { log.Printf("[TRACE] statemgr.Filesystem: removed lock metadata file %s", lockInfoPath) } unlockErr := s.unlock() + // Perform cleanup if stateFileOut is not nil. if s.stateFileOut != nil { fileName := s.stateFileOut.Name() @@ -403,7 +407,7 @@ func (s *Filesystem) Unlock(id string) error { s.stateFileOut = nil s.lockID = "" - // clean up the state file if we created it an never wrote to it + // Clean up the state file if we created it an never wrote to it stat, err := os.Stat(fileName) if err == nil && stat.Size() == 0 && s.created { err = os.Remove(fileName) diff --git a/internal/states/statemgr/filesystem_lock_unix.go b/internal/states/statemgr/filesystem_lock_unix.go index ca445d3dd6..79eeef887e 100644 --- a/internal/states/statemgr/filesystem_lock_unix.go +++ b/internal/states/statemgr/filesystem_lock_unix.go @@ -30,17 +30,19 @@ func (s *Filesystem) lock() error { } func (s *Filesystem) unlock() error { - + // Handle case where s.stateFileOut is nil, indicating no lock to release. if s.stateFileOut == nil { - log.Print("TRACE] statemgr.Filesystem: statefileout is nil ") + log.Print("[TRACE] statemgr.Filesystem: statefileout is nil, cannot unlock") return nil } + // Check if file descriptor is invalid fd := s.stateFileOut.Fd() if fd == ^uintptr(0) { - log.Print("TRACE] statemgr.Filesystem: fd is 0 ") + log.Print("[TRACE] statemgr.Filesystem: fd is invalid, cannot unlock") return nil } + log.Printf("[TRACE] statemgr.Filesystem: unlocking %s using fcntl flock", s.path) flock := &syscall.Flock_t{ Type: syscall.F_UNLCK, From 1a2368c75f6956ef6bc610d6307c335a07a76521 Mon Sep 17 00:00:00 2001 From: eduzgun Date: Mon, 1 Jul 2024 19:10:30 +0100 Subject: [PATCH 15/16] Fix more linting Signed-off-by: eduzgun --- internal/backend/backend_test.go | 2 +- .../remote-state/s3/backend_complete_test.go | 16 +++++++++------- internal/states/statemgr/filesystem.go | 3 +-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/internal/backend/backend_test.go b/internal/backend/backend_test.go index 44572324ae..0de24473b3 100644 --- a/internal/backend/backend_test.go +++ b/internal/backend/backend_test.go @@ -80,7 +80,7 @@ func TestRead_PathNoPermission(t *testing.T) { if runtime.GOOS == "windows" { // Use cacls to remove all permissions for this file on Windows - cmd := exec.Command("cmd", "/c", "cacls", f.Name(), "/E", "/R", os.Getenv("USERNAME")) + cmd := exec.Command("cmd", "/c", "cacls", f.Name(), "/E", "/R", os.Getenv("USERNAME")) // #nosec G204 if err := cmd.Run(); err != nil { t.Fatalf("Failed to set file permissions with cacls: %s", err) } diff --git a/internal/backend/remote-state/s3/backend_complete_test.go b/internal/backend/remote-state/s3/backend_complete_test.go index 2a24954a71..2459132ba5 100644 --- a/internal/backend/remote-state/s3/backend_complete_test.go +++ b/internal/backend/remote-state/s3/backend_complete_test.go @@ -148,6 +148,8 @@ func (m noopMatcher) String() string { return "" } +const osWindows = "windows" + func TestBackendConfig_Authentication(t *testing.T) { testDirectory := t.TempDir() sysRoot := os.Getenv("SYSTEMROOT") @@ -639,7 +641,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey servicemocks.InitSessionTestEnv(t) // Set Windows-specific environment variables - if runtime.GOOS == "windows" { + if runtime.GOOS == osWindows { t.Setenv("TEMP", testDirectory) t.Setenv("TMP", testDirectory) t.Setenv("SYSTEMROOT", sysRoot) @@ -1121,7 +1123,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey servicemocks.InitSessionTestEnv(t) // Set Windows-specific environment variables - if runtime.GOOS == "windows" { + if runtime.GOOS == osWindows { t.Setenv("TEMP", testDirectory) t.Setenv("TMP", testDirectory) t.Setenv("SYSTEMROOT", sysRoot) @@ -1539,7 +1541,7 @@ aws_secret_access_key = DefaultSharedCredentialsSecretKey servicemocks.InitSessionTestEnv(t) // Set Windows-specific environment variables - if runtime.GOOS == "windows" { + if runtime.GOOS == osWindows { t.Setenv("TEMP", testDirectory) t.Setenv("TMP", testDirectory) t.Setenv("SYSTEMROOT", sysRoot) @@ -1828,7 +1830,7 @@ web_identity_token_file = no-such-file servicemocks.InitSessionTestEnv(t) // Set Windows-specific environment variables - if runtime.GOOS == "windows" { + if runtime.GOOS == osWindows { t.Setenv("TEMP", testDirectory) t.Setenv("TMP", testDirectory) t.Setenv("SYSTEMROOT", sysRoot) @@ -1879,7 +1881,7 @@ web_identity_token_file = no-such-file if tc.ExpandEnvVars { var prefix string tmpdir := os.Getenv("TMPDIR") - if runtime.GOOS == "windows" { + if runtime.GOOS == osWindows { tmpdir = os.Getenv("TEMP") prefix = tmpdir } else { @@ -2110,7 +2112,7 @@ region = us-west-2 servicemocks.InitSessionTestEnv(t) // Set Windows-specific environment variables - if runtime.GOOS == "windows" { + if runtime.GOOS == osWindows { t.Setenv("TEMP", testDirectory) t.Setenv("TMP", testDirectory) t.Setenv("SYSTEMROOT", sysRoot) @@ -2235,7 +2237,7 @@ func TestBackendConfig_RetryMode(t *testing.T) { servicemocks.InitSessionTestEnv(t) // Set Windows-specific environment variables - if runtime.GOOS == "windows" { + if runtime.GOOS == osWindows { t.Setenv("TEMP", testDirectory) t.Setenv("TMP", testDirectory) t.Setenv("SYSTEMROOT", sysRoot) diff --git a/internal/states/statemgr/filesystem.go b/internal/states/statemgr/filesystem.go index 8727057d63..b6da1c9556 100644 --- a/internal/states/statemgr/filesystem.go +++ b/internal/states/statemgr/filesystem.go @@ -407,7 +407,7 @@ func (s *Filesystem) Unlock(id string) error { s.stateFileOut = nil s.lockID = "" - // Clean up the state file if we created it an never wrote to it + // Clean up the state file if we created it and never wrote to it stat, err := os.Stat(fileName) if err == nil && stat.Size() == 0 && s.created { err = os.Remove(fileName) @@ -415,7 +415,6 @@ func (s *Filesystem) Unlock(id string) error { log.Printf("[ERROR] stagemgr.Filesystem: error removing empty state file %q: %s", fileName, err) } } - } s.lockID = "" return unlockErr From 5f18c087113a14c234645bc646174f8f1ce2f1cd Mon Sep 17 00:00:00 2001 From: eduzgun Date: Fri, 12 Jul 2024 18:58:57 +0100 Subject: [PATCH 16/16] Undo build state change Signed-off-by: eduzgun --- internal/backend/remote-state/http/server_test.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/internal/backend/remote-state/http/server_test.go b/internal/backend/remote-state/http/server_test.go index 61164a24e9..6e9ac6213f 100644 --- a/internal/backend/remote-state/http/server_test.go +++ b/internal/backend/remote-state/http/server_test.go @@ -361,13 +361,6 @@ func TestMTLSServer_WithCertPasses(t *testing.T) { if err = sm.RefreshState(); err != nil { t.Fatalf("unexpected error calling RefreshState: %v", err) } - - // Ensure state is not nil by building and writing a test state. - testState := states.BuildState(func(_ *states.SyncState) {}) - err = sm.WriteState(testState) - if err != nil { - t.Errorf("error writing test state: %s", err) - } state := sm.State() if nil == state { t.Fatal("nil state")