diff --git a/client/pkg/testutil/before.go b/client/pkg/testutil/before.go new file mode 100644 index 000000000..801aa042d --- /dev/null +++ b/client/pkg/testutil/before.go @@ -0,0 +1,40 @@ +// Copyright 2022 The etcd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package testutil + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +// These are constants from "go.etcd.io/etcd/server/v3/verify", +// but we don't want to take dependency. +const ENV_VERIFY = "ETCD_VERIFY" +const ENV_VERIFY_ALL_VALUE = "all" + +func BeforeTest(t testing.TB) { + RegisterLeakDetection(t) + os.Setenv(ENV_VERIFY, ENV_VERIFY_ALL_VALUE) + + path, err := os.Getwd() + assert.NoError(t, err) + tempDir := t.TempDir() + assert.NoError(t, os.Chdir(tempDir)) + t.Logf("Changing working directory to: %s", tempDir) + + t.Cleanup(func() { assert.NoError(t, os.Chdir(path)) }) +} diff --git a/client/v3/client_test.go b/client/v3/client_test.go index b8c62a95b..4884fe646 100644 --- a/client/v3/client_test.go +++ b/client/v3/client_test.go @@ -18,7 +18,6 @@ import ( "context" "fmt" "net" - "path/filepath" "testing" "time" @@ -204,8 +203,13 @@ func TestZapWithLogger(t *testing.T) { } func TestAuthTokenBundleNoOverwrite(t *testing.T) { + // This call in particular changes working directory to the tmp dir of + // the test. The `etcd-auth-test:0` can be created in local directory, + // not exceeding the longest allowed path on OsX. + testutil.BeforeTest(t) + // Create a mock AuthServer to handle Authenticate RPCs. - lis, err := net.Listen("unix", filepath.Join(t.TempDir(), "etcd-auth-test:0")) + lis, err := net.Listen("unix", "etcd-auth-test:0") if err != nil { t.Fatal(err) } diff --git a/tests/framework/e2e/testing.go b/tests/framework/e2e/testing.go index e1447146c..7d7de27fd 100644 --- a/tests/framework/e2e/testing.go +++ b/tests/framework/e2e/testing.go @@ -15,24 +15,12 @@ package e2e import ( - "os" "testing" - "github.com/stretchr/testify/assert" "go.etcd.io/etcd/client/pkg/v3/testutil" - "go.etcd.io/etcd/server/v3/verify" ) func BeforeTest(t testing.TB) { SkipInShortMode(t) - testutil.RegisterLeakDetection(t) - os.Setenv(verify.ENV_VERIFY, verify.ENV_VERIFY_ALL_VALUE) - - path, err := os.Getwd() - assert.NoError(t, err) - tempDir := t.TempDir() - assert.NoError(t, os.Chdir(tempDir)) - t.Logf("Changing working directory to: %s", tempDir) - - t.Cleanup(func() { assert.NoError(t, os.Chdir(path)) }) + testutil.BeforeTest(t) }