Merge pull request #13872 from ptabor/20220402-osx-unit-test-pass

Fix TestauthTokenBundleOnOverwrite on OsX:
This commit is contained in:
Piotr Tabor 2022-04-02 20:03:38 +02:00 committed by GitHub
commit f85cd0296f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 15 deletions

View File

@ -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)) })
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -15,6 +15,7 @@
package clientv3_test
import (
"log"
"os"
"testing"
"time"
@ -44,6 +45,16 @@ func forUnitTestsRunInMockedContext(mocking func(), example func()) {
// TestMain sets up an etcd cluster if running the examples.
func TestMain(m *testing.M) {
testutil.ExitInShortMode("Skipping: the tests require real cluster")
tempDir := os.TempDir()
defer os.RemoveAll(tempDir)
err := os.Chdir(tempDir)
if err != nil {
log.Printf("Failed to change working dir to: %s: %v", tempDir, err)
os.Exit(1)
}
v := m.Run()
lazyCluster.Terminate()