chore: publish IntegraCI build modules 732ec6c83925

This commit is contained in:
dhimaz0445 committed 2026-10-28 08:09:16 +00:00
1 parent 7a84930dea
commit 5f8a24d014
133 files changed
+18990 -2

No files matched your search

+240
View File
@@ -0,0 +1,240 @@
// Verified 2026-07-16: dagger v0.21.7 develop + go build + dagger functions
// pass. Generated code (dagger.gen.go, internal/) is gitignored; run
// 'dagger develop' to regenerate.
//
// Package main implements the IntegraCI e2e-test Dagger module.
//
// Runs web end-to-end tests (Playwright or Cypress) against a target
// baseURL, uploads the report artifact, and POSTs results to the
// IntegraCI API.
//
// Usage:
//
// dagger call run --source . --base-url https://staging.example.com --tool playwright
// dagger call run --source . --base-url https://staging.example.com --tool cypress --api-url http://localhost:8000 --token $TOKEN --service-id $SVC_ID
// dagger call run --source . --base-url https://staging.example.com --tool selenium --command "pytest -m e2e"
// dagger call run --source . --base-url https://staging.example.com --tool none
package main
import (
"context"
"dagger/e-2-e-test/internal/dagger"
"fmt"
"strings"
)
// E2eTest is the IntegraCI web end-to-end testing module.
type E2eTest struct{}
// E2EResult contains the output of an end-to-end test run.
type E2EResult struct {
Tool string `json:"tool"`
BaseURL string `json:"base_url"`
ExitCode int `json:"exit_code"`
Output string `json:"output"`
ReportPath string `json:"report_path,omitempty"`
ReportSize int `json:"report_size"`
Posted bool `json:"posted"`
Success bool `json:"success"`
}
// Run executes a web end-to-end test suite against baseURL using either
// Playwright (default) or Cypress, uploads the report artifact, and
// optionally POSTs the results to the IntegraCI API.
func (m *E2eTest) Run(
ctx context.Context,
// Source directory containing the e2e test suite
source *dagger.Directory,
// Base URL the tests run against
baseURL string,
// Test runner to use: "playwright" | "cypress" | "selenium" | "none"
// +optional
// +default="playwright"
tool string,
// Command to run for the selenium tool (engine-agnostic; no official action)
// +optional
// +default="pytest -m e2e"
command string,
// Path inside the source the report artifact is written to
// +optional
// +default="playwright-report"
reportPath string,
// IntegraCI API URL for posting results
// +optional
apiURL string,
// JWT token for API authentication
// +optional
token *dagger.Secret,
// Service ID to link test results
// +optional
serviceID string,
) (*E2EResult, error) {
result := &E2EResult{
Tool: tool,
BaseURL: baseURL,
ReportPath: reportPath,
}
var (
output string
report string
err error
)
switch strings.ToLower(strings.TrimSpace(tool)) {
case "none":
// No-op skip, mirroring the adapter's _comment_skip intent.
result.Tool = "none"
result.Output = "Web E2E disabled (tool=none)"
result.Success = true
return result, nil
case "cypress":
output, report, err = m.runCypress(ctx, source, baseURL, reportPath)
case "selenium":
output, report, err = m.runSelenium(ctx, source, baseURL, command)
case "", "playwright":
result.Tool = "playwright"
output, report, err = m.runPlaywright(ctx, source, baseURL, reportPath)
default:
return nil, fmt.Errorf("unsupported tool %q: use \"playwright\", \"cypress\", \"selenium\" or \"none\"", tool)
}
if err != nil {
return nil, fmt.Errorf("%s run failed: %w", result.Tool, err)
}
result.Output = output
result.ReportSize = len(report)
result.Success = true
// POST results to the IntegraCI API when API params are supplied.
if apiURL != "" && token != nil && serviceID != "" {
if err := m.postResults(ctx, apiURL, token, serviceID, result); err != nil {
return result, fmt.Errorf("post results: %w", err)
}
result.Posted = true
}
return result, nil
}
// runPlaywright installs Playwright browsers and runs the suite against
// baseURL, then reads the report artifact back out of the container.
func (m *E2eTest) runPlaywright(
ctx context.Context,
source *dagger.Directory,
baseURL string,
reportPath string,
) (string, string, error) {
runner := dag.Container().
From("mcr.microsoft.com/playwright:v1.44.0-jammy").
WithMountedDirectory("/src", source).
WithWorkdir("/src").
WithEnvVariable("BASE_URL", baseURL).
WithExec([]string{"npm", "ci"}).
WithExec([]string{"npx", "playwright", "install", "--with-deps"}).
WithExec([]string{"npx", "playwright", "test"})
output, err := runner.Stdout(ctx)
if err != nil {
return output, "", fmt.Errorf("playwright test: %w", err)
}
report, rerr := runner.File("/src/" + reportPath + "/index.html").Contents(ctx)
if rerr != nil {
// Report artifact is best-effort; absence is not fatal.
report = ""
}
return output, report, nil
}
// runCypress installs dependencies and runs the Cypress suite against
// baseURL, then reads the report artifact back out of the container.
func (m *E2eTest) runCypress(
ctx context.Context,
source *dagger.Directory,
baseURL string,
reportPath string,
) (string, string, error) {
runner := dag.Container().
From("cypress/included:13.11.0").
WithMountedDirectory("/src", source).
WithWorkdir("/src").
WithEnvVariable("CYPRESS_BASE_URL", baseURL).
WithExec([]string{"npm", "ci"}).
WithExec([]string{"cypress", "run", "--config", "baseUrl=" + baseURL})
output, err := runner.Stdout(ctx)
if err != nil {
return output, "", fmt.Errorf("cypress run: %w", err)
}
report, rerr := runner.File("/src/" + reportPath).Contents(ctx)
if rerr != nil {
report = ""
}
return output, report, nil
}
// runSelenium runs a configurable test command (default "pytest -m e2e")
// against baseURL. Selenium has no official CI action, so the canonical
// recipe is to run the project's own command inside a Python image with a
// Selenium runtime. The baseURL is exposed via BASE_URL for the suite.
func (m *E2eTest) runSelenium(
ctx context.Context,
source *dagger.Directory,
baseURL string,
command string,
) (string, string, error) {
cmd := strings.TrimSpace(command)
if cmd == "" {
cmd = "pytest -m e2e"
}
output, err := dag.Container().
From("python:3.12-slim").
WithMountedDirectory("/src", source).
WithWorkdir("/src").
WithEnvVariable("BASE_URL", baseURL).
WithExec([]string{"sh", "-c", cmd}).
Stdout(ctx)
if err != nil {
return output, "", fmt.Errorf("selenium command: %w", err)
}
return output, "", nil
}
// postResults POSTs the E2E result summary to the IntegraCI API. Mirrors
// the security-scan post-to-API pattern: a small curl invocation inside a
// throwaway container so no host network access is required.
func (m *E2eTest) postResults(
ctx context.Context,
apiURL string,
token *dagger.Secret,
serviceID string,
result *E2EResult,
) error {
payload := fmt.Sprintf(
`{"service_id":%q,"tool":%q,"base_url":%q,"success":%t,"report_size":%d}`,
serviceID, result.Tool, result.BaseURL, result.Success, result.ReportSize,
)
_, err := dag.Container().
From("curlimages/curl:8.10.1").
WithSecretVariable("API_TOKEN", token).
WithExec([]string{
"sh", "-c",
"curl -sS -X POST " +
"-H \"Authorization: Bearer $API_TOKEN\" " +
"-H \"Content-Type: application/json\" " +
"-d '" + payload + "' " +
strings.TrimRight(apiURL, "/") + "/api/v1/e2e-results",
}).
Stdout(ctx)
if err != nil {
return fmt.Errorf("curl post: %w", err)
}
return nil
}