This module provides a Testcontainers implementation for Mattermost. It allows you to run Mattermost in a container during integration tests.
package example_test
import (
"context"
"testing"
mmcontainer "github.com/mattermost/testcontainers-mattermost-go"
)
func TestMattermost(t *testing.T) {
ctx := context.Background()
// Start Mattermost container
container, err := mmcontainer.RunContainer(ctx)
if err != nil {
t.Fatal(err)
}
defer container.Terminate(ctx)
// Get admin client
client, err := container.GetAdminClient(ctx)
if err != nil {
t.Fatal(err)
}
// Use the client to interact with Mattermost
// ...
}
- Automatically starts a PostgreSQL container
- Creates initial admin user and team
- Provides methods to:
- Get admin and regular user clients
- Create users and teams
- Install plugins
- Update configuration
- Access logs
- And more...
The MattermostContainer
provides the following methods:
URL(ctx)
- Returns the URL of the Mattermost instanceTerminate(ctx)
- Terminates both Mattermost and PostgreSQL containersPostgresDSN(ctx)
- Returns the PostgreSQL connection stringPostgresConnection(ctx)
- Returns a direct SQL connection to PostgreSQL
GetAdminClient(ctx)
- Returns a Mattermost client logged in as adminGetClient(ctx, username, password)
- Returns a Mattermost client logged in as specified user
CreateAdmin(ctx, email, username, password)
- Creates an admin userCreateUser(ctx, email, username, password)
- Creates a regular user
CreateTeam(ctx, name, displayName)
- Creates a new teamAddUserToTeam(ctx, username, teamname)
- Adds a user to a team
SetConfig(ctx, configKey, configValue)
- Sets a single config valueUpdateConfig(ctx, cfg)
- Updates the entire Mattermost configuration
InstallPlugin(ctx, pluginPath, pluginID, pluginConfig)
- Installs and enables a plugin
GetLogs(ctx, lines)
- Returns the specified number of log lines
RunCtlCommand(ctx, command, args)
- Runs an mmctl command inside the container
The RunContainer
function accepts the following options to customize the Mattermost instance:
WithAdmin(email, username, password)
- Sets the admin user credentials- Default: [email protected]/admin/admin
WithTeam(name, displayName)
- Sets the initial team name and display name- Default: test/Test
WithPlugin(pluginPath, pluginID, pluginConfig)
- Installs and enables a plugin- pluginPath: Path to the plugin file (.tar.gz)
- pluginID: Unique identifier for the plugin
- pluginConfig: Optional plugin-specific configuration
WithConfig(cfg)
- Updates the entire Mattermost configurationWithConfigFile(path)
- Uses a config file from the host systemWithEnv(key, value)
- Sets an environment variableWithLicense(licenseBase64)
- Sets the Mattermost license
WithNetwork(network)
- Uses a custom Docker network- Adds container to the network with alias "mattermost"
- Enables communication between containers
WithLogConsumers(consumers...)
- Sets log consumers for container output
Example usage:
container, err := mmcontainer.RunContainer(ctx,
// Set custom admin credentials
mmcontainer.WithAdmin("[email protected]", "admin", "password"),
// Set custom team
mmcontainer.WithTeam("myteam", "My Team"),
// Install plugin
mmcontainer.WithPlugin("/path/to/plugin.tar.gz", "com.example.plugin", nil),
// Update config
mmcontainer.WithConfig(&model.Config{
// Custom config...
}),
// Set environment variables
mmcontainer.WithEnv("MM_SERVICESETTINGS_SITEURL", "http://localhost:8065"),
// Use custom network
mmcontainer.WithNetwork(network),
// Add log consumer
mmcontainer.WithLogConsumers(myLogConsumer),
)
- Docker
- Go 1.21 or later
This project is licensed under the MIT License - see the LICENSE file for details.