Skip to content

Built-in checks

Import github.com/davideimola/worky/checks for pre-built check functions.

File system

FileExists(path string) func(context.Context) error

Passes if the file at path exists.

checks.FileExists("done.txt")
checks.FileExists("/etc/hosts")

DirExists(path string) func(context.Context) error

Passes if the directory at path exists.

checks.DirExists("output/")
checks.DirExists("src/components")

FileContains(path, text string) func(context.Context) error

Passes if the file at path contains the substring text.

checks.FileContains("hello.txt", "Hello, World!")
checks.FileContains("config.yaml", "replicas: 3")

FileMatchesRegex(path, pattern string) func(context.Context) error

Passes if the file at path matches the regular expression pattern. If pattern is not a valid regular expression, the check returns an error instead of panicking.

checks.FileMatchesRegex("output.json", `"status":\s*"ok"`)

Environment variables

EnvVarSet(name string) func(context.Context) error

Passes if the environment variable name is set and non-empty.

checks.EnvVarSet("KUBECONFIG")
checks.EnvVarSet("AWS_REGION")

EnvVarEquals(name, value string) func(context.Context) error

Passes if the environment variable name equals value.

checks.EnvVarEquals("STAGE", "production")

Commands

CommandSucceeds(name string, args ...string) func(context.Context) error

Passes if the command exits with code 0. Respects context cancellation and timeout.

checks.CommandSucceeds("docker", "info")
checks.CommandSucceeds("kubectl", "cluster-info")
checks.CommandSucceeds("node", "--version")

CommandOutputContains(text, name string, args ...string) func(context.Context) error

Passes if the command exits 0 and its combined output contains text. Respects context cancellation and timeout.

checks.CommandOutputContains("Running", "kubectl", "get", "pods", "-n", "default")
checks.CommandOutputContains("v1.", "kubectl", "version", "--client")

Network

PortOpen(host string, port int) func(context.Context) error

Passes if a TCP connection to host:port succeeds within 3 seconds. Respects context cancellation.

checks.PortOpen("localhost", 5432)  // PostgreSQL
checks.PortOpen("localhost", 6379)  // Redis

HTTPStatus(url string, expectedStatus int) func(context.Context) error

Passes if a GET request to url returns expectedStatus. Respects context cancellation and timeout.

checks.HTTPStatus("http://localhost:8080/health", 200)
checks.HTTPStatus("http://localhost:3000", 200)

HTTPBodyContains(url, text string) func(context.Context) error

Passes if the body of a GET request to url contains text. Respects context cancellation and timeout.

checks.HTTPBodyContains("http://localhost:8080/api/status", `"ok"`)