54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
/*
|
|
Copyright 2026.
|
|
|
|
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 postgresql
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
api "git.ddupan.top/panxiao81/postgresql-tenant-operator/api/v1alpha1"
|
|
"git.ddupan.top/panxiao81/postgresql-tenant-operator/internal/instance"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
)
|
|
|
|
const testCanary = "canary-secret"
|
|
|
|
func TestDatabaseFailuresDoNotLeakDriverMessages(t *testing.T) {
|
|
tests := []struct {
|
|
err error
|
|
reason string
|
|
}{
|
|
{context.DeadlineExceeded, api.ReasonDependencyUnavailable},
|
|
{&pgconn.PgError{Code: "28P01", Message: testCanary}, api.ReasonAuthenticationFailed},
|
|
{&pgconn.PgError{Code: "42501", Message: testCanary}, api.ReasonInsufficientPrivileges},
|
|
{&pgconn.PgError{Code: "53300", Message: testCanary}, api.ReasonDependencyUnavailable},
|
|
}
|
|
for _, tt := range tests {
|
|
err := classify("database operation", fmt.Errorf("driver canary-secret: %w", tt.err))
|
|
var failure instance.Failure
|
|
if !errors.As(err, &failure) || failure.Reason != tt.reason {
|
|
t.Fatal("wrong error category")
|
|
}
|
|
if strings.Contains(fmt.Sprintf("%+v %#v", err, err), "canary") {
|
|
t.Fatal("driver message escaped")
|
|
}
|
|
}
|
|
}
|