mirror of
https://github.com/hanzokms/cli.git
synced 2026-10-30 20:30:00 +00:00
The gateway test started redis:8.4.0 through the testcontainers redis module and connected with go-redis. It now starts ghcr.io/hanzoai/kv and connects with github.com/hanzokv/go, so the e2e suite exercises the server we ship. The client is built from the container's host and mapped port rather than from its connection string. Our ParseURL accepts kv:// only, and a mapped port is host:port anyway — the URL was a detour. reset_redis.go is reset_kv.go; the PAM resource type is left as the generated client names it, because it names the external system being connected to, alongside kubernetes. Verified: go build ./... and go vet ./relay/... ./packages/... both clean.
74 lines
2.0 KiB
Go
74 lines
2.0 KiB
Go
package kms
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/docker/go-connections/nat"
|
|
"github.com/hanzokms/cli/e2e-tests/packages/client"
|
|
"github.com/testcontainers/testcontainers-go/modules/compose"
|
|
)
|
|
|
|
// composePortProvider implements client.ServicePortProvider for compose stacks
|
|
type composePortProvider struct {
|
|
stack compose.ComposeStack
|
|
}
|
|
|
|
// GetServicePort gets the mapped port for a service
|
|
func (p *composePortProvider) GetServicePort(ctx context.Context, serviceName string, internalPort string) (string, error) {
|
|
c, err := p.stack.ServiceContainer(ctx, serviceName)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to get %s c: %w", serviceName, err)
|
|
}
|
|
port, err := c.MappedPort(ctx, nat.Port(internalPort))
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to get %s port %s: %w", serviceName, internalPort, err)
|
|
}
|
|
return port.Port(), nil
|
|
}
|
|
|
|
// Reset resets the whole instance of KMS backend service to its original state after first time booting up
|
|
func Reset(ctx context.Context, stack compose.ComposeStack) error {
|
|
// Create port provider to get service ports
|
|
portProvider := &composePortProvider{stack: stack}
|
|
|
|
// Get PostgreSQL port
|
|
dbPort, err := portProvider.GetServicePort(ctx, "db", "5432")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get db port: %w", err)
|
|
}
|
|
|
|
// Get kv port
|
|
kvPort, err := portProvider.GetServicePort(ctx, "kv", "6379")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get kv port: %w", err)
|
|
}
|
|
|
|
// Reset PostgreSQL database
|
|
if err := client.ResetDB(ctx, client.WithDatabaseConfig(client.DatabaseConfig{
|
|
User: "kms",
|
|
Password: "kms",
|
|
Database: "kms",
|
|
Host: "localhost",
|
|
Port: dbPort,
|
|
})); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Reset the KV database
|
|
kvPortInt, err := strconv.Atoi(kvPort)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse kv port: %w", err)
|
|
}
|
|
if err := client.ResetKV(ctx, client.WithKVConfig(client.KVConfig{
|
|
Host: "localhost",
|
|
Port: kvPortInt,
|
|
Password: "",
|
|
})); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|