-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add integration test for dbmode strategy (#6445)
- Loading branch information
Showing
9 changed files
with
324 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package sendconfig | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-logr/logr" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
k8stypes "k8s.io/apimachinery/pkg/types" | ||
|
||
"github.com/kong/kubernetes-ingress-controller/v3/internal/dataplane/failures" | ||
"github.com/kong/kubernetes-ingress-controller/v3/internal/logging" | ||
) | ||
|
||
// resourceErrorsToResourceFailures translates a slice of ResourceError to a slice of failures.ResourceFailure. | ||
func resourceErrorsToResourceFailures(resourceErrors []ResourceError, logger logr.Logger) []failures.ResourceFailure { | ||
var out []failures.ResourceFailure | ||
for _, ee := range resourceErrors { | ||
obj := metav1.PartialObjectMetadata{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: ee.Kind, | ||
APIVersion: ee.APIVersion, | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: ee.Namespace, | ||
Name: ee.Name, | ||
UID: k8stypes.UID(ee.UID), | ||
}, | ||
} | ||
for problemSource, problem := range ee.Problems { | ||
logger.V(logging.DebugLevel).Info("Adding failure", "resource_name", ee.Name, "source", problemSource, "problem", problem) | ||
resourceFailure, failureCreateErr := failures.NewResourceFailure( | ||
fmt.Sprintf("invalid %s: %s", problemSource, problem), | ||
&obj, | ||
) | ||
if failureCreateErr != nil { | ||
logger.Error(failureCreateErr, "Could not create resource failure event") | ||
} else { | ||
out = append(out, resourceFailure) | ||
} | ||
} | ||
} | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package containers | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/modules/postgres" | ||
"github.com/testcontainers/testcontainers-go/network" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
) | ||
|
||
const ( | ||
// postgresImage is the default image used for the Postgres container. | ||
postgresImage = "postgres:16-alpine" | ||
|
||
// postgresUser is the default user for the Postgres container. | ||
postgresUser = "postgres" | ||
|
||
// postgresPassword is the default password for the Postgres container. | ||
postgresPassword = "pass" | ||
|
||
// postgresDatabase is the default database for the Postgres container. | ||
postgresDatabase = "kong" | ||
|
||
// postgres is the default port for the Postgres container. | ||
postgresPort = 5432 | ||
|
||
// postgresContainerNetworkAlias is the hostname alias for the Postgres container. | ||
postgresContainerNetworkAlias = "db" | ||
) | ||
|
||
type Postgres struct { | ||
container testcontainers.Container | ||
} | ||
|
||
func NewPostgres(ctx context.Context, t *testing.T, net *testcontainers.DockerNetwork) *Postgres { | ||
postgresC, err := postgres.Run(ctx, | ||
postgresImage, | ||
network.WithNetwork([]string{postgresContainerNetworkAlias}, net), | ||
testcontainers.WithEnv(map[string]string{ | ||
"POSTGRES_USER": postgresUser, | ||
"POSTGRES_PASSWORD": postgresPassword, | ||
"POSTGRES_DB": postgresDatabase, | ||
}), | ||
testcontainers.WithWaitStrategy( | ||
wait.ForLog("database system is ready to accept connections"), | ||
), | ||
) | ||
require.NoError(t, err) | ||
t.Logf("Postgres container ID: %s", postgresC.GetContainerID()) | ||
|
||
t.Cleanup(func() { //nolint:contextcheck | ||
// If the container is already terminated, we don't need to terminate it again. | ||
if postgresC.IsRunning() { | ||
assert.NoError(t, postgresC.Terminate(context.Background())) | ||
} | ||
}) | ||
|
||
runKongDBMigrations(ctx, t, net.Name) | ||
|
||
return &Postgres{ | ||
container: postgresC, | ||
} | ||
} | ||
|
||
// runKongDBMigrations runs the Kong migrations bootstrap command in a container against a Postgres database container. | ||
func runKongDBMigrations(ctx context.Context, t *testing.T, networkName string) { | ||
// Run Kong migrations bootstrap command in a container. | ||
kongMigrationsC, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ | ||
ContainerRequest: testcontainers.ContainerRequest{ | ||
Image: kongImageUnderTest(), | ||
Env: map[string]string{ | ||
"KONG_DATABASE": "postgres", | ||
"KONG_PG_HOST": postgresContainerNetworkAlias, | ||
"KONG_PG_PORT": strconv.Itoa(postgresPort), | ||
"KONG_PG_USER": postgresUser, | ||
"KONG_PG_PASSWORD": postgresPassword, | ||
"KONG_PG_DATABASE": postgresDatabase, | ||
}, | ||
Cmd: []string{ | ||
"kong", "migrations", "bootstrap", | ||
"--yes", "--force", | ||
"--db-timeout", "30", | ||
}, | ||
Networks: []string{networkName}, | ||
}, | ||
Started: true, | ||
}) | ||
require.NoError(t, err) | ||
t.Logf("Kong migrations container ID: %s", kongMigrationsC.GetContainerID()) | ||
|
||
// Wait for migrations to finish successfully (status == "exited" and exit code == 0). | ||
const ( | ||
timeout = 30 * time.Second | ||
period = 1 * time.Second | ||
) | ||
timer := time.After(timeout) | ||
ticker := time.NewTicker(period) | ||
defer ticker.Stop() | ||
for range ticker.C { | ||
select { | ||
case <-timer: | ||
assert.Fail(t, "Kong migrations bootstrap timed out") | ||
return | ||
default: | ||
} | ||
|
||
state, err := kongMigrationsC.State(ctx) | ||
require.NoError(t, err) | ||
if state.Status == "exited" { | ||
if !assert.Equal(t, 0, state.ExitCode, "Kong migrations bootstrap failed") { | ||
logs, err := kongMigrationsC.Logs(ctx) | ||
require.NoError(t, err) | ||
|
||
logsB, err := io.ReadAll(logs) | ||
require.NoError(t, err) | ||
|
||
t.Logf("Kong migrations bootstrap logs: %s", string(logsB)) | ||
} | ||
return | ||
} | ||
|
||
t.Logf("Waiting for Kong migrations to finish, current state: %s", state.Status) | ||
} | ||
} |
Oops, something went wrong.