https://github.com/stillya/testcontainers-keycloak
A Testcontainers implementation for Keycloak SSO.
https://github.com/stillya/testcontainers-keycloak
golang keycloak testcontainers
Last synced: 10 days ago
JSON representation
A Testcontainers implementation for Keycloak SSO.
- Host: GitHub
- URL: https://github.com/stillya/testcontainers-keycloak
- Owner: stillya
- License: mit
- Created: 2023-08-31T19:28:59.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2026-01-26T13:51:23.000Z (18 days ago)
- Last Synced: 2026-01-27T02:58:55.597Z (18 days ago)
- Topics: golang, keycloak, testcontainers
- Language: Go
- Homepage:
- Size: 53.7 KB
- Stars: 10
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Keycloak Testcontainer - [testcontainers](https://www.testcontainers.org/) implementation for [Keycloak](https://www.keycloak.org/) SSO.
[](https://github.com/stillya/testcontainers-keycloak/actions/workflows/go.yml)
[](https://coveralls.io/github/stillya/testcontainers-keycloak?branch=master)
[](https://pkg.go.dev/github.com/stillya/testcontainers-keycloak)
* Native integration with [Testcontainers](https://www.testcontainers.org/).
* 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.
## Installation
```bash
go get github.com/stillya/testcontainers-keycloak
```
## Usage
```go
package main
import (
"context"
"fmt"
keycloak "github.com/stillya/testcontainers-keycloak"
"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.Run(ctx,
"keycloak/keycloak:24.0",
keycloak.WithContextPath("/auth"),
keycloak.WithRealmImportFile("../testdata/realm-export.json"),
keycloak.WithAdminUsername("admin"),
keycloak.WithAdminPassword("admin"),
)
}
```