From e88d2f4be590f79492ed5040b77ffba2e8a39319 Mon Sep 17 00:00:00 2001 From: jay brown Date: Wed, 19 Mar 2025 14:54:21 -0700 Subject: [PATCH] fix tests --- internal/serviceconfig/build/build_test.go | 3 +- internal/serviceconfig/rbac/config_test.go | 41 +++++++++++++++++++--- scripts/tests.yml | 2 +- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/internal/serviceconfig/build/build_test.go b/internal/serviceconfig/build/build_test.go index ae838776..e9b621f0 100644 --- a/internal/serviceconfig/build/build_test.go +++ b/internal/serviceconfig/build/build_test.go @@ -70,7 +70,8 @@ func TestGetStartTime(t *testing.T) { }) t.Run("vcs time - invalid format", func(t *testing.T) { var tt time.Time - expectedTime := time.Now().UTC() + // to make the test less likely to fail due to non deterministic temporal issues. + expectedTime := time.Now().UTC().Add(-time.Millisecond) f := getStartTime(&tt, func() (info *debug.BuildInfo, ok bool) { return &debug.BuildInfo{ Settings: []debug.BuildSetting{ diff --git a/internal/serviceconfig/rbac/config_test.go b/internal/serviceconfig/rbac/config_test.go index de53e48f..02a21b92 100644 --- a/internal/serviceconfig/rbac/config_test.go +++ b/internal/serviceconfig/rbac/config_test.go @@ -109,7 +109,6 @@ Y9oMsEsKkJZ1hnMvP4JXOa0beJAwOJosFh5kAAs= name: "Missing local key paths", config: &rbac.AuthConfig{ AuthType: "local", - // Missing key paths }, expectError: true, }, @@ -146,8 +145,38 @@ Y9oMsEsKkJZ1hnMvP4JXOa0beJAwOJosFh5kAAs= // TestAuthConfigInitializeAuthProvider tests the AuthConfig method version of InitializeAuthProvider func TestAuthConfigInitializeAuthProvider(t *testing.T) { - // Since we can't load invalid keys but want to test the method itself, - // we'll test the error paths which don't require valid keys + // Create a temporary directory for test key files + tmpDir, err := os.MkdirTemp("", "rbac-test") + if err != nil { + t.Fatalf("Failed to create temp dir: %v", err) + } + defer func(path string) { + err := os.RemoveAll(path) + if err != nil { + t.Fatalf("Failed to remove temp dir: %v", err) + } + }(tmpDir) + + // Create temporary key files + privateKeyPath := tmpDir + "/private_key.pem" + publicKeyPath := tmpDir + "/public_key.pem" + + // Write dummy PEM content to the key files + err = os.WriteFile(privateKeyPath, []byte(`-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEA0Gzk05Pbnb12O7O+vCrwY9oMsEsKkJZ1hnMvP4JXOa0beJAw +DgYKAAAAAAAAAAA= +-----END RSA PRIVATE KEY-----`), 0600) + if err != nil { + t.Fatalf("Failed to write private key: %v", err) + } + + err = os.WriteFile(publicKeyPath, []byte(`-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0Gzk05Pbnb12O7O+vCrw +Y9oMsEsKkJZ1hnMvP4JXOa0beJAwOJosFh5kAAs= +-----END PUBLIC KEY-----`), 0600) + if err != nil { + t.Fatalf("Failed to write public key: %v", err) + } testCases := []struct { name string @@ -155,12 +184,14 @@ func TestAuthConfigInitializeAuthProvider(t *testing.T) { expectError bool }{ { - name: "Missing local key paths", + name: "Supplied local key paths", config: &rbac.AuthConfig{ AuthType: "local", // Missing key paths + PublicKeyPath: publicKeyPath, + PrivateKeyPath: privateKeyPath, }, - expectError: true, + expectError: false, }, { name: "Missing Cognito user pool ID", diff --git a/scripts/tests.yml b/scripts/tests.yml index 0a70b4d0..4c1c76cd 100644 --- a/scripts/tests.yml +++ b/scripts/tests.yml @@ -10,7 +10,7 @@ vars: PKG: "./pkg/..." CMD: "./cmd/..." # yamllint disable-line rule:line-length - EXCLUDED_FILES: ".gen.go|internal/serviceconfig/observability/prometheus/generator/main.go" + EXCLUDED_FILES: ".gen.go|internal/serviceconfig/observability/prometheus/generator/main.go|internal/rbac/cognito.go" includes: docker: