|
| 1 | +package forgejo |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/testcontainers/testcontainers-go" |
| 11 | + "github.com/testcontainers/testcontainers-go/exec" |
| 12 | + "github.com/testcontainers/testcontainers-go/wait" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + defaultHTTPPort = "3000/tcp" |
| 17 | + defaultSSHPort = "22/tcp" |
| 18 | + defaultUser = "forgejo-admin" |
| 19 | + defaultPassword = "forgejo-admin" |
| 20 | + defaultEmail = "admin@forgejo.local" |
| 21 | +) |
| 22 | + |
| 23 | +// Container represents the Forgejo container type used in the module |
| 24 | +type Container struct { |
| 25 | + testcontainers.Container |
| 26 | + adminUsername string |
| 27 | + adminPassword string |
| 28 | +} |
| 29 | + |
| 30 | +// AdminUsername returns the admin username for the Forgejo instance. |
| 31 | +func (c *Container) AdminUsername() string { |
| 32 | + return c.adminUsername |
| 33 | +} |
| 34 | + |
| 35 | +// AdminPassword returns the admin password for the Forgejo instance. |
| 36 | +func (c *Container) AdminPassword() string { |
| 37 | + return c.adminPassword |
| 38 | +} |
| 39 | + |
| 40 | +// extractAdminCredentials parses FORGEJO_ADMIN_* env vars from the container |
| 41 | +// environment, falling back to the default values for any that are not set. |
| 42 | +func extractAdminCredentials(env []string) (username, password, email string) { |
| 43 | + username, password, email = defaultUser, defaultPassword, defaultEmail |
| 44 | + for _, e := range env { |
| 45 | + if v, ok := strings.CutPrefix(e, "FORGEJO_ADMIN_USERNAME="); ok { |
| 46 | + username = v |
| 47 | + } |
| 48 | + if v, ok := strings.CutPrefix(e, "FORGEJO_ADMIN_PASSWORD="); ok { |
| 49 | + password = v |
| 50 | + } |
| 51 | + if v, ok := strings.CutPrefix(e, "FORGEJO_ADMIN_EMAIL="); ok { |
| 52 | + email = v |
| 53 | + } |
| 54 | + } |
| 55 | + return |
| 56 | +} |
| 57 | + |
| 58 | +// Run creates an instance of the Forgejo container type |
| 59 | +func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) { |
| 60 | + // Closure variables populated by the PostReadies hook so we can avoid |
| 61 | + // a second container.Inspect call after Run returns. |
| 62 | + var adminUser, adminPass string |
| 63 | + |
| 64 | + moduleOpts := make([]testcontainers.ContainerCustomizer, 0, 4+len(opts)) |
| 65 | + moduleOpts = append(moduleOpts, |
| 66 | + testcontainers.WithExposedPorts(defaultHTTPPort, defaultSSHPort), |
| 67 | + testcontainers.WithWaitStrategy( |
| 68 | + wait.ForHTTP("/api/healthz").WithPort(defaultHTTPPort), |
| 69 | + ), |
| 70 | + // Use SQLite for simplicity in tests (no external DB needed). |
| 71 | + // INSTALL_LOCK skips the install wizard so the instance is ready to use. |
| 72 | + testcontainers.WithEnv(map[string]string{ |
| 73 | + "FORGEJO__database__DB_TYPE": "sqlite3", |
| 74 | + "FORGEJO__security__INSTALL_LOCK": "true", |
| 75 | + "FORGEJO_ADMIN_USERNAME": defaultUser, |
| 76 | + "FORGEJO_ADMIN_PASSWORD": defaultPassword, |
| 77 | + "FORGEJO_ADMIN_EMAIL": defaultEmail, |
| 78 | + }), |
| 79 | + ) |
| 80 | + |
| 81 | + moduleOpts = append(moduleOpts, opts...) |
| 82 | + |
| 83 | + // Add lifecycle hook to create admin user after container is ready. |
| 84 | + // The hook reads credentials from container env vars so that user-provided |
| 85 | + // options (which override the defaults above) are respected. |
| 86 | + // The command runs as the "git" user because Forgejo refuses to run CLI |
| 87 | + // commands as root. |
| 88 | + adminHook := testcontainers.ContainerLifecycleHooks{ |
| 89 | + PostReadies: []testcontainers.ContainerHook{ |
| 90 | + func(ctx context.Context, container testcontainers.Container) error { |
| 91 | + inspect, err := container.Inspect(ctx) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("inspect forgejo: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + username, password, email := extractAdminCredentials(inspect.Config.Env) |
| 97 | + |
| 98 | + // Store credentials in closure for Run to use later. |
| 99 | + adminUser = username |
| 100 | + adminPass = password |
| 101 | + |
| 102 | + code, output, err := container.Exec(ctx, []string{ |
| 103 | + "forgejo", "admin", "user", "create", |
| 104 | + "--username", username, |
| 105 | + "--password", password, |
| 106 | + "--email", email, |
| 107 | + "--admin", |
| 108 | + "--must-change-password=false", |
| 109 | + }, exec.WithUser("git")) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("create admin user: %w", err) |
| 112 | + } |
| 113 | + if code != 0 { |
| 114 | + data, _ := io.ReadAll(output) |
| 115 | + return fmt.Errorf("create admin user: exit code %d: %s", code, string(data)) |
| 116 | + } |
| 117 | + return nil |
| 118 | + }, |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + moduleOpts = append(moduleOpts, testcontainers.WithAdditionalLifecycleHooks(adminHook)) |
| 123 | + |
| 124 | + ctr, err := testcontainers.Run(ctx, img, moduleOpts...) |
| 125 | + var c *Container |
| 126 | + if ctr != nil { |
| 127 | + c = &Container{Container: ctr} |
| 128 | + } |
| 129 | + |
| 130 | + if err != nil { |
| 131 | + return c, fmt.Errorf("run forgejo: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + // Credentials were populated by the PostReadies hook above. |
| 135 | + c.adminUsername = adminUser |
| 136 | + c.adminPassword = adminPass |
| 137 | + |
| 138 | + return c, nil |
| 139 | +} |
| 140 | + |
| 141 | +// ConnectionString returns the HTTP URL for the Forgejo instance |
| 142 | +func (c *Container) ConnectionString(ctx context.Context) (string, error) { |
| 143 | + return c.PortEndpoint(ctx, defaultHTTPPort, "http") |
| 144 | +} |
| 145 | + |
| 146 | +// SSHConnectionString returns the SSH endpoint for Git operations |
| 147 | +func (c *Container) SSHConnectionString(ctx context.Context) (string, error) { |
| 148 | + return c.PortEndpoint(ctx, defaultSSHPort, "") |
| 149 | +} |
| 150 | + |
| 151 | +// WithAdminCredentials sets the admin username, password, and email for the Forgejo instance. |
| 152 | +// These credentials are used to create an admin user after the container is ready. |
| 153 | +func WithAdminCredentials(username, password, email string) testcontainers.CustomizeRequestOption { |
| 154 | + return func(req *testcontainers.GenericContainerRequest) error { |
| 155 | + if username == "" || password == "" || email == "" { |
| 156 | + return errors.New("WithAdminCredentials: username, password, and email must not be empty") |
| 157 | + } |
| 158 | + if req.Env == nil { |
| 159 | + req.Env = make(map[string]string) |
| 160 | + } |
| 161 | + req.Env["FORGEJO_ADMIN_USERNAME"] = username |
| 162 | + req.Env["FORGEJO_ADMIN_PASSWORD"] = password |
| 163 | + req.Env["FORGEJO_ADMIN_EMAIL"] = email |
| 164 | + return nil |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// WithConfig sets a Forgejo configuration value using the FORGEJO__section__key |
| 169 | +// environment variable format. |
| 170 | +// See https://forgejo.org/docs/latest/admin/config-cheat-sheet/ for available options. |
| 171 | +func WithConfig(section, key, value string) testcontainers.CustomizeRequestOption { |
| 172 | + return func(req *testcontainers.GenericContainerRequest) error { |
| 173 | + if section == "" || key == "" { |
| 174 | + return fmt.Errorf("WithConfig: section and key must not be empty (got section=%q, key=%q)", section, key) |
| 175 | + } |
| 176 | + if strings.Contains(section, "__") || strings.Contains(key, "__") { |
| 177 | + return fmt.Errorf("WithConfig: section and key must not contain \"__\" (got section=%q, key=%q)", section, key) |
| 178 | + } |
| 179 | + if req.Env == nil { |
| 180 | + req.Env = make(map[string]string) |
| 181 | + } |
| 182 | + envKey := fmt.Sprintf("FORGEJO__%s__%s", section, key) |
| 183 | + req.Env[envKey] = value |
| 184 | + return nil |
| 185 | + } |
| 186 | +} |
0 commit comments