Skip to content
Merged
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
95 changes: 55 additions & 40 deletions internal/redact/redact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,64 @@ import (
func TestVars(t *testing.T) {
t.Parallel()

redactConfig := []string{
"*_PASSWORD",
"*_TOKEN",
}
environment := []env.Pair{
{Name: "BUILDKITE_PIPELINE", Value: "unit-test"},
// These are example values, and are not leaked credentials
{Name: "DATABASE_USERNAME", Value: "AzureDiamond"},
{Name: "DATABASE_PASSWORD", Value: "hunter2"},
}

got, short, err := Vars(redactConfig, environment)
if err != nil {
t.Errorf("Vars(%q, %q) error = %v", redactConfig, environment, err)
}
if len(short) > 0 {
t.Errorf("Vars(%q, %q) short = %q", redactConfig, environment, short)
tests := []struct {
name string
redactConfig []string
environment []env.Pair
wantMatched []env.Pair
wantShort []string
}{
{
name: "hunter2",
redactConfig: []string{"*_PASSWORD", "*_TOKEN"},
environment: []env.Pair{
{Name: "BUILDKITE_PIPELINE", Value: "unit-test"},
// These are example values, and are not leaked credentials
{Name: "DATABASE_USERNAME", Value: "AzureDiamond"},
{Name: "DATABASE_PASSWORD", Value: "hunter2"},
},
wantMatched: []env.Pair{{Name: "DATABASE_PASSWORD", Value: "hunter2"}},
wantShort: nil,
},
{
name: "short",
redactConfig: []string{"*_PASSWORD", "*_TOKEN"},
environment: []env.Pair{
{Name: "BUILDKITE_PIPELINE", Value: "unit-test"},
// These are example values, and are not leaked credentials
{Name: "DATABASE_USERNAME", Value: "AzureDiamond"},
{Name: "DATABASE_PASSWORD", Value: "hunt"},
},
wantMatched: nil,
wantShort: []string{"DATABASE_PASSWORD"},
},
{
name: "empty",
redactConfig: nil,
environment: []env.Pair{
{Name: "FOO", Value: "BAR"},
{Name: "BUILDKITE_PIPELINE", Value: "unit-test"},
},
wantMatched: nil,
wantShort: nil,
},
}
want := []env.Pair{{Name: "DATABASE_PASSWORD", Value: "hunter2"}}

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Vars(%q, %q) diff (-got +want)\n%s", redactConfig, environment, diff)
}
}

func TestValuesToRedactEmpty(t *testing.T) {
t.Parallel()

redactConfig := []string{}
environment := []env.Pair{
{Name: "FOO", Value: "BAR"},
{Name: "BUILDKITE_PIPELINE", Value: "unit-test"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

got, short, err := Vars(redactConfig, environment)
if err != nil {
t.Errorf("Vars(%q, %q) error = %v", redactConfig, environment, err)
}
if len(short) > 0 {
t.Errorf("Vars(%q, %q) short = %q", redactConfig, environment, short)
}
if len(got) != 0 {
t.Errorf("Vars(%q, %q) = %q, want empty slice", redactConfig, environment, got)
matched, short, err := Vars(test.redactConfig, test.environment)
if err != nil {
t.Fatalf("Vars(%q, %q) error = %v", test.redactConfig, test.environment, err)
}
if diff := cmp.Diff(matched, test.wantMatched); diff != "" {
t.Errorf("Vars(%q, %q) matched diff (-got +want)\n%s", test.redactConfig, test.environment, diff)
}
if diff := cmp.Diff(short, test.wantShort); diff != "" {
t.Errorf("Vars(%q, %q) short diff (-got +want)\n%s", test.redactConfig, test.environment, diff)
}
})
}
}

Expand Down