Actions: Introduce A11y test (#106806)
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
|
||||
"dagger.io/dagger"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer cancel()
|
||||
|
||||
if err := NewApp().Run(ctx, os.Args); err != nil {
|
||||
cancel()
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func NewApp() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "a11y",
|
||||
Usage: "Run Grafana accessibility tests",
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "grafana-dir",
|
||||
Usage: "Path to the grafana/grafana clone directory",
|
||||
Value: ".",
|
||||
Validator: mustBeDir("grafana-dir"),
|
||||
TakesFile: true,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "package",
|
||||
Usage: "Path to the grafana tar.gz package",
|
||||
Value: "grafana.tar.gz",
|
||||
Validator: mustBeFile("package", false),
|
||||
TakesFile: true,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "license",
|
||||
Usage: "Path to the Grafana Enterprise license file (optional)",
|
||||
Validator: mustBeFile("license", true),
|
||||
TakesFile: true,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "flags",
|
||||
Usage: "Flags to pass through to the e2e runner",
|
||||
},
|
||||
},
|
||||
Action: run,
|
||||
}
|
||||
}
|
||||
|
||||
func run(ctx context.Context, cmd *cli.Command) error {
|
||||
grafanaDir := cmd.String("grafana-dir")
|
||||
targzPath := cmd.String("package")
|
||||
licensePath := cmd.String("license")
|
||||
runnerFlags := cmd.String("flags")
|
||||
|
||||
d, err := dagger.Connect(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect to Dagger: %w", err)
|
||||
}
|
||||
|
||||
yarnCache := d.CacheVolume("yarn")
|
||||
|
||||
//nolint:gosec
|
||||
nvmrcContents, err := os.ReadFile(filepath.Join(grafanaDir, ".nvmrc"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read .nvmrc file: %w", err)
|
||||
}
|
||||
nodeVersion := string(nvmrcContents)
|
||||
|
||||
grafana := d.Host().Directory(grafanaDir, dagger.HostDirectoryOpts{
|
||||
Exclude: []string{"node_modules", "*.tar.gz"},
|
||||
})
|
||||
targz := d.Host().File(targzPath)
|
||||
|
||||
var license *dagger.File
|
||||
if licensePath != "" {
|
||||
license = d.Host().File(licensePath)
|
||||
}
|
||||
|
||||
svc, err := GrafanaService(ctx, d, GrafanaServiceOpts{
|
||||
GrafanaDir: grafana,
|
||||
GrafanaTarGz: targz,
|
||||
License: license,
|
||||
YarnCache: yarnCache,
|
||||
NodeVersion: nodeVersion,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create Grafana service: %w", err)
|
||||
}
|
||||
|
||||
c := RunTest(d, svc, grafana, yarnCache, nodeVersion, runnerFlags)
|
||||
c, err = c.Sync(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to run a11y test suite: %w", err)
|
||||
}
|
||||
|
||||
code, err := c.ExitCode(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get exit code of a11y test suite: %w", err)
|
||||
}
|
||||
if code != 0 {
|
||||
return fmt.Errorf("a11y tests failed with exit code %d", code)
|
||||
}
|
||||
|
||||
log.Println("a11y tests completed successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func mustBeFile(arg string, emptyOk bool) func(string) error {
|
||||
return func(s string) error {
|
||||
if s == "" {
|
||||
if emptyOk {
|
||||
return nil
|
||||
}
|
||||
return cli.Exit(arg+" cannot be empty", 1)
|
||||
}
|
||||
stat, err := os.Stat(s)
|
||||
if err != nil {
|
||||
return cli.Exit(arg+" does not exist or cannot be read: "+s, 1)
|
||||
}
|
||||
if stat.IsDir() {
|
||||
return cli.Exit(arg+" must be a file, not a directory: "+s, 1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func mustBeDir(arg string) func(string) error {
|
||||
return func(s string) error {
|
||||
if s == "" {
|
||||
return cli.Exit(arg+" cannot be empty", 1)
|
||||
}
|
||||
stat, err := os.Stat(s)
|
||||
if err != nil {
|
||||
return cli.Exit(arg+" does not exist or cannot be read: "+s, 1)
|
||||
}
|
||||
if !stat.IsDir() {
|
||||
return cli.Exit(arg+" must be a directory: "+s, 1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
func RunTest(
|
||||
d *dagger.Client,
|
||||
svc *dagger.Service,
|
||||
src *dagger.Directory, cache *dagger.CacheVolume,
|
||||
nodeVersion, runnerFlags string) *dagger.Container {
|
||||
command := fmt.Sprintf(
|
||||
"./e2e-runner a11y --start-grafana=false"+
|
||||
" --grafana-host grafana --grafana-port 3001 %s", runnerFlags)
|
||||
|
||||
return GrafanaFrontend(d, cache, nodeVersion, src).
|
||||
WithExec([]string{"/bin/sh", "-c", "apt-get update && apt-get install -y git curl"}).
|
||||
WithExec([]string{"curl", "-LO", "https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb"}).
|
||||
WithExec([]string{"apt-get", "install", "-y", "./google-chrome-stable_current_amd64.deb"}).
|
||||
WithWorkdir("/src").
|
||||
WithServiceBinding("grafana", svc).
|
||||
WithExec([]string{"/bin/bash", "-c", command}, dagger.ContainerWithExecOpts{Expect: dagger.ReturnTypeAny})
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"dagger.io/dagger"
|
||||
)
|
||||
|
||||
func NodeImage(version string) string {
|
||||
return fmt.Sprintf("node:%s-slim", strings.TrimPrefix(strings.TrimSpace(version), "v"))
|
||||
}
|
||||
|
||||
type GrafanaServiceOpts struct {
|
||||
GrafanaDir *dagger.Directory
|
||||
GrafanaTarGz *dagger.File
|
||||
License *dagger.File
|
||||
YarnCache *dagger.CacheVolume
|
||||
NodeVersion string
|
||||
}
|
||||
|
||||
func Frontend(src *dagger.Directory) *dagger.Directory {
|
||||
return src.
|
||||
WithoutFile("go.mod").
|
||||
WithoutFile("go.sum").
|
||||
WithoutFile("go.work").
|
||||
WithoutFile("go.work.sum").
|
||||
WithoutDirectory(".github").
|
||||
WithoutDirectory("docs").
|
||||
WithoutDirectory("pkg").
|
||||
WithoutDirectory("apps").
|
||||
WithoutDirectory("videos")
|
||||
}
|
||||
|
||||
func WithGrafanaFrontend(c *dagger.Container, src *dagger.Directory) *dagger.Container {
|
||||
return c.WithDirectory("/src", Frontend(src), dagger.ContainerWithDirectoryOpts{
|
||||
Exclude: []string{
|
||||
"*drone*",
|
||||
"*.go",
|
||||
"*.md",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func WithYarnCache(c *dagger.Container, cache *dagger.CacheVolume) *dagger.Container {
|
||||
return c.
|
||||
WithWorkdir("/src").
|
||||
WithMountedCache("/yarn/cache", cache)
|
||||
}
|
||||
|
||||
func GrafanaFrontend(d *dagger.Client, yarnCache *dagger.CacheVolume, nodeVersion string, grafanaDir *dagger.Directory) *dagger.Container {
|
||||
container := d.Container().From(NodeImage(nodeVersion))
|
||||
container = WithGrafanaFrontend(container, grafanaDir)
|
||||
return WithYarnCache(container, yarnCache).
|
||||
WithEnvVariable("YARN_CACHE_FOLDER", "/yarn/cache").
|
||||
WithExec([]string{"yarn", "install", "--immutable"})
|
||||
}
|
||||
|
||||
func GrafanaService(ctx context.Context, d *dagger.Client, opts GrafanaServiceOpts) (*dagger.Service, error) {
|
||||
src := GrafanaFrontend(d, opts.YarnCache, opts.NodeVersion, opts.GrafanaDir)
|
||||
|
||||
container := d.Container().From("alpine:3").
|
||||
WithExec([]string{"apk", "add", "--no-cache", "bash", "tar", "netcat-openbsd"}).
|
||||
WithMountedFile("/src/grafana.tar.gz", opts.GrafanaTarGz).
|
||||
WithExec([]string{"mkdir", "-p", "/src/grafana"}).
|
||||
WithExec([]string{"tar", "--strip-components=1", "-xzf", "/src/grafana.tar.gz", "-C", "/src/grafana"}).
|
||||
WithDirectory("/src/grafana/devenv", src.Directory("/src/devenv")).
|
||||
WithDirectory("/src/grafana/e2e", src.Directory("/src/e2e")).
|
||||
WithDirectory("/src/grafana/scripts", src.Directory("/src/scripts")).
|
||||
WithDirectory("/src/grafana/tools", src.Directory("/src/tools")).
|
||||
WithWorkdir("/src/grafana").
|
||||
WithEnvVariable("GF_APP_MODE", "development").
|
||||
WithEnvVariable("GF_SERVER_HTTP_PORT", "3001").
|
||||
WithEnvVariable("GF_SERVER_ROUTER_LOGGING", "1").
|
||||
WithExposedPort(3001)
|
||||
|
||||
var licenseArg string
|
||||
if opts.License != nil {
|
||||
container = container.WithMountedFile("/src/license.jwt", opts.License)
|
||||
licenseArg = "/src/license.jwt"
|
||||
}
|
||||
|
||||
// We add all GF_ environment variables to allow for overriding Grafana configuration.
|
||||
// It is unlikely the runner has any such otherwise.
|
||||
for _, env := range os.Environ() {
|
||||
if strings.HasPrefix(env, "GF_") {
|
||||
parts := strings.SplitN(env, "=", 2)
|
||||
container = container.WithEnvVariable(parts[0], parts[1])
|
||||
}
|
||||
}
|
||||
|
||||
svc := container.AsService(dagger.ContainerAsServiceOpts{Args: []string{"bash", "-x", "scripts/grafana-server/start-server", licenseArg}})
|
||||
|
||||
return svc, nil
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
func RunSuite(d *dagger.Client, svc *dagger.Service, src *dagger.Directory, cache *dagger.CacheVolume, suite, runnerFlags string) *dagger.Container {
|
||||
command := fmt.Sprintf(
|
||||
"./e2e-runner --start-grafana=false --cypress-video"+
|
||||
"./e2e-runner cypress --start-grafana=false --cypress-video"+
|
||||
" --grafana-base-url http://grafana:3001 --suite %s %s", suite, runnerFlags)
|
||||
|
||||
return WithYarnCache(WithGrafanaFrontend(d.Container().From("cypress/included:13.1.0"), src), cache).
|
||||
|
||||
Reference in New Issue
Block a user