Skip to content

Commit

Permalink
misc. test updates
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed Feb 25, 2023
1 parent dcfc91f commit 235c861
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 59 deletions.
30 changes: 12 additions & 18 deletions blobfs/blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
)

func setupTestS3Bucket(t *testing.T) *url.URL {
t.Helper()

backend := s3mem.New()
faker := gofakes3.New(backend)

Expand Down Expand Up @@ -49,6 +51,8 @@ func fakeGCSObject(name, contentType, content string) fakestorage.Object {
}

func setupTestGCSBucket(t *testing.T) *fakestorage.Server {
t.Helper()

objs := []fakestorage.Object{
fakeGCSObject("file1", "text/plain", "hello"),
fakeGCSObject("file2", "application/json", `{"value": "goodbye world"}`),
Expand Down Expand Up @@ -98,8 +102,7 @@ func TestBlobFS_S3(t *testing.T) {

defer cancel()

os.Setenv("AWS_ANON", "true")
defer os.Unsetenv("AWS_ANON")
t.Setenv("AWS_ANON", "true")

fsys, err := New(tests.MustURL("s3://mybucket/?region=us-east-1&disableSSL=true&s3ForcePathStyle=true&endpoint=" + srvURL.Host))
assert.NoError(t, err)
Expand All @@ -113,17 +116,10 @@ func TestBlobFS_S3(t *testing.T) {

os.Unsetenv("AWS_ANON")

os.Setenv("AWS_ACCESS_KEY_ID", "fake")
defer os.Unsetenv("AWS_ACCESS_KEY_ID")

os.Setenv("AWS_SECRET_ACCESS_KEY", "fake")
defer os.Unsetenv("AWS_SECRET_ACCESS_KEY")

os.Setenv("AWS_S3_ENDPOINT", srvURL.Host)
defer os.Unsetenv("AWS_S3_ENDPOINT")

os.Setenv("AWS_REGION", "eu-west-1")
defer os.Unsetenv("AWS_REGION")
t.Setenv("AWS_ACCESS_KEY_ID", "fake")
t.Setenv("AWS_SECRET_ACCESS_KEY", "fake")
t.Setenv("AWS_S3_ENDPOINT", srvURL.Host)
t.Setenv("AWS_REGION", "eu-west-1")

fsys, err = New(tests.MustURL("s3://mybucket/dir2/?disableSSL=true&s3ForcePathStyle=true"))
assert.NoError(t, err)
Expand All @@ -140,8 +136,7 @@ func TestBlobFS_GCS(t *testing.T) {

srv := setupTestGCSBucket(t)

os.Setenv("GOOGLE_ANON", "true")
defer os.Unsetenv("GOOGLE_ANON")
t.Setenv("GOOGLE_ANON", "true")

fsys, err := New(tests.MustURL("gs://mybucket"))
assert.NoError(t, err)
Expand Down Expand Up @@ -176,7 +171,7 @@ func TestBlobFS_Azure(t *testing.T) {

defer cancel()

os.Setenv("AZURE_STORAGE_ACCOUNT", "azureopendatastorage")
t.Setenv("AZURE_STORAGE_ACCOUNT", "azureopendatastorage")

fsys, err := New(tests.MustURL("azblob://citydatacontainer/Crime/Processed/2020/1/20/"))
assert.NoError(t, err)
Expand Down Expand Up @@ -208,8 +203,7 @@ func TestBlobFS_Azure(t *testing.T) {
func TestBlobFS_ReadDir(t *testing.T) {
srvURL := setupTestS3Bucket(t)

os.Setenv("AWS_ANON", "true")
defer os.Unsetenv("AWS_ANON")
t.Setenv("AWS_ANON", "true")

fsys, err := New(tests.MustURL("s3://mybucket/?region=us-east-1&disableSSL=true&s3ForcePathStyle=true&endpoint=" + srvURL.Host))
assert.NoError(t, err)
Expand Down
22 changes: 7 additions & 15 deletions gitfs/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func TestAutoAuthenticator(t *testing.T) {
// ssh public key auth with env var
key := base64.StdEncoding.EncodeToString(testdata.PEMBytes["rsa"])

os.Setenv("GIT_SSH_KEY", key)
defer os.Unsetenv("GIT_SSH_KEY")
t.Setenv("GIT_SSH_KEY", key)

am, err = a.Authenticate(tests.MustURL("ssh://git@example.com"))
assert.NoError(t, err)
Expand Down Expand Up @@ -131,17 +130,15 @@ func TestBasicAuthenticator(t *testing.T) {
&githttp.BasicAuth{Username: "user", Password: "swordfish"}, am)

// credentials from env used when none are provided
os.Setenv("GIT_HTTP_PASSWORD", "swordfish")
defer os.Unsetenv("GIT_HTTP_PASSWORD")
t.Setenv("GIT_HTTP_PASSWORD", "swordfish")

am, err = a.Authenticate(tests.MustURL("https://example.com/foo"))
assert.NoError(t, err)
assert.EqualValues(t,
&githttp.BasicAuth{Username: "", Password: "swordfish"}, am)

// provided credentials override env used when none are provided
os.Setenv("GIT_HTTP_PASSWORD", "pufferfish")
defer os.Unsetenv("GIT_HTTP_PASSWORD")
t.Setenv("GIT_HTTP_PASSWORD", "pufferfish")

a.username = "user"
a.password = "swordfish"
Expand Down Expand Up @@ -209,8 +206,7 @@ func TestTokenAuthenticator(t *testing.T) {
assert.Error(t, err)

// token from env
os.Setenv("GIT_HTTP_TOKEN", "foo")
defer os.Unsetenv("GIT_HTTP_TOKEN")
t.Setenv("GIT_HTTP_TOKEN", "foo")

am, err := a.Authenticate(tests.MustURL("http://example.com/foo"))
assert.NoError(t, err)
Expand All @@ -223,7 +219,6 @@ func TestTokenAuthenticator(t *testing.T) {
assert.EqualValues(t, &githttp.TokenAuth{Token: "bar"}, am)
}

//nolint:funlen
func TestPublicKeyAuthenticator(t *testing.T) {
envfsys := fstest.MapFS{}
a := &publicKeyAuthenticator{envfsys: envfsys}
Expand All @@ -246,8 +241,7 @@ func TestPublicKeyAuthenticator(t *testing.T) {

enckey := base64.StdEncoding.EncodeToString([]byte(key))

os.Setenv("GIT_SSH_KEY", enckey)
defer os.Unsetenv("GIT_SSH_KEY")
t.Setenv("GIT_SSH_KEY", enckey)

am, err := a.Authenticate(tests.MustURL("ssh://example.com/foo"))
assert.NoError(t, err)
Expand All @@ -256,8 +250,7 @@ func TestPublicKeyAuthenticator(t *testing.T) {
// key from file referenced by env (non-base64)
os.Unsetenv("GIT_SSH_KEY")

os.Setenv("GIT_SSH_KEY_FILE", "/testdata/key.pem")
defer os.Unsetenv("GIT_SSH_KEY_FILE")
t.Setenv("GIT_SSH_KEY_FILE", "/testdata/key.pem")

envfsys["testdata/key.pem"] = &fstest.MapFile{Data: []byte(key)}

Expand All @@ -268,8 +261,7 @@ func TestPublicKeyAuthenticator(t *testing.T) {
// provided key overrides env
os.Unsetenv("GIT_SSH_KEY_FILE")

os.Setenv("GIT_SSH_KEY", "unparseable key, but will be ignored")
defer os.Unsetenv("GIT_SSH_KEY")
t.Setenv("GIT_SSH_KEY", "unparseable key, but will be ignored")

a.username = "user"
a.privKey = testdata.PEMBytes["ed25519"]
Expand Down
7 changes: 3 additions & 4 deletions internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ func TestGetenvFS(t *testing.T) {
assert.Equal(t, os.Getenv("USER"), GetenvFS(fsys, "USER"))
assert.Equal(t, "default value", GetenvFS(fsys, "BLAHBLAHBLAH", "default value"))

defer os.Unsetenv("FOO_FILE")
os.Setenv("FOO_FILE", "/tmp/foo")
t.Setenv("FOO_FILE", "/tmp/foo")
assert.Equal(t, "foo", GetenvFS(fsys, "FOO", "bar"))

os.Setenv("FOO_FILE", "/tmp/missing")
t.Setenv("FOO_FILE", "/tmp/missing")
assert.Equal(t, "bar", GetenvFS(fsys, "FOO", "bar"))

fsys["tmp/unreadable"] = &fstest.MapFile{Mode: 0o100}

os.Setenv("FOO_FILE", "/tmp/unreadable")
t.Setenv("FOO_FILE", "/tmp/unreadable")
assert.Equal(t, "bar", GetenvFS(fsys, "FOO", "bar"))
}
19 changes: 6 additions & 13 deletions internal/tests/integration/vaultfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ func TestVaultFS_TokenAuth(t *testing.T) {
assert.Equal(t, `{"value":"bar"}`, string(b))

// token in env var
os.Setenv("VAULT_TOKEN", tok)
defer os.Unsetenv("VAULT_TOKEN")
t.Setenv("VAULT_TOKEN", tok)

fsys, err = vaultfs.New(tests.MustURL("vault+http://" + addr))
assert.NoError(t, err)
Expand All @@ -207,8 +206,7 @@ func TestVaultFS_TokenAuth(t *testing.T) {
assert.Equal(t, `{"value":"bar"}`, string(b))

// address and token in env var
os.Setenv("VAULT_ADDR", "http://"+addr)
defer os.Unsetenv("VAULT_ADDR")
t.Setenv("VAULT_ADDR", "http://"+addr)

fsys, err = vaultfs.New(tests.MustURL("vault:///"))
assert.NoError(t, err)
Expand Down Expand Up @@ -288,13 +286,9 @@ func TestVaultFS_UserPassAuth(t *testing.T) {
assert.Equal(t, `{"value":"bar"}`, string(b))

// with a bunch of env vars
os.Setenv("VAULT_ADDR", "http://"+addr)
os.Setenv("VAULT_AUTH_USERNAME", "dave")
os.Setenv("VAULT_AUTH_PASSWORD", "foo")

defer os.Unsetenv("VAULT_ADDR")
defer os.Unsetenv("VAULT_AUTH_USERNAME")
defer os.Unsetenv("VAULT_AUTH_PASSWORD")
t.Setenv("VAULT_ADDR", "http://"+addr)
t.Setenv("VAULT_AUTH_USERNAME", "dave")
t.Setenv("VAULT_AUTH_PASSWORD", "foo")

fsys, err = vaultfs.New(tests.MustURL("vault:///secret/"))
assert.NoError(t, err)
Expand Down Expand Up @@ -520,8 +514,7 @@ func TestVaultFS_AppRoleAuth_ReusedToken(t *testing.T) {
func TestVaultFS_AppIDAuth(t *testing.T) {
// temporarily allow the deprecated pending-removal appID auth method
// when this starts failing completely, we should remove support
_ = os.Setenv("VAULT_ALLOW_PENDING_REMOVAL_MOUNTS", "true")
defer os.Unsetenv("VAULT_ALLOW_PENDING_REMOVAL_MOUNTS")
t.Setenv("VAULT_ALLOW_PENDING_REMOVAL_MOUNTS", "true")

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down
9 changes: 3 additions & 6 deletions vaultfs/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func TestEnvAuthLogin(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

os.Setenv("VAULT_TOKEN", "foo")
defer os.Unsetenv("VAULT_TOKEN")
t.Setenv("VAULT_TOKEN", "foo")

m := EnvAuthMethod()
err := m.Login(ctx, v)
Expand All @@ -42,8 +41,7 @@ func TestTokenLogin(t *testing.T) {
client := &api.Client{}

// use env var if none provided
os.Setenv("VAULT_TOKEN", "foo")
defer os.Unsetenv("VAULT_TOKEN")
t.Setenv("VAULT_TOKEN", "foo")

m := TokenAuthMethod("")
err := m.Login(ctx, client)
Expand All @@ -59,8 +57,7 @@ func TestTokenLogin(t *testing.T) {
// support VAULT_TOKEN_FILE
os.Unsetenv("VAULT_TOKEN")

os.Setenv("VAULT_TOKEN_FILE", "/tmp/file")
defer os.Unsetenv("VAULT_TOKEN_FILE")
t.Setenv("VAULT_TOKEN_FILE", "/tmp/file")

fsys := fstest.MapFS{}
fsys["tmp/file"] = &fstest.MapFile{Data: []byte("tempfiletoken")}
Expand Down
4 changes: 1 addition & 3 deletions vaultfs/vaultauth/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"net/http"
"os"
"testing"

"github.com/hashicorp/vault/api"
Expand All @@ -18,8 +17,7 @@ func TestEnvAuthLogin(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

os.Setenv("VAULT_TOKEN", "foo")
defer os.Unsetenv("VAULT_TOKEN")
t.Setenv("VAULT_TOKEN", "foo")

m := EnvAuthMethod()
s, err := m.Login(ctx, v)
Expand Down

0 comments on commit 235c861

Please sign in to comment.