![Go Reference](https://camo.githubusercontent.com/8fc9741559b2eb07f3ed06dc48c1130f6a4a13b0d12d80d747e40855d1c4c275/68747470733a2f2f706b672e676f2e6465762f62616467652f6769746875622e636f6d2f7374696c6c79612f74657374636f6e7461696e6572732d6b6579636c6f616b2e737667)
- Native integration with Testcontainers.
- Customization via
realm.json
to create custom realms, users, clients, etc.
- Provides
AdminClient
to interact with Keycloak API.
- Customization via jar's providers.
- TLS support.
go get github.com/stillya/testcontainers-keycloak
package main
import (
"context"
"fmt"
keycloak "github.com/stillya/testcontainers-keycloak"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"os"
"testing"
)
var keycloakContainer *keycloak.KeycloakContainer
func Test_Example(t *testing.T) {
ctx := context.Background()
authServerURL, err := keycloakContainer.GetAuthServerURL(ctx)
if err != nil {
t.Errorf("GetAuthServerURL() error = %v", err)
return
}
fmt.Println(authServerURL)
// Output:
// http://localhost:32768/auth
}
func TestMain(m *testing.M) {
defer func() {
if r := recover(); r != nil {
shutDown()
fmt.Println("Panic")
}
}()
setup()
code := m.Run()
shutDown()
os.Exit(code)
}
func setup() {
var err error
ctx := context.Background()
keycloakContainer, err = RunContainer(ctx)
if err != nil {
panic(err)
}
}
func shutDown() {
ctx := context.Background()
err := keycloakContainer.Terminate(ctx)
if err != nil {
panic(err)
}
}
func RunContainer(ctx context.Context) (*keycloak.KeycloakContainer, error) {
return keycloak.RunContainer(ctx,
keycloak.WithContextPath("/auth"),
keycloak.WithRealmImportFile("../testdata/realm-export.json"),
keycloak.WithAdminUsername("admin"),
keycloak.WithAdminPassword("admin"),
)
}