Documentation
¶
Overview ¶
Package testkit has the in-process fake of the Checkend API and the CLI harness for tests.
Index ¶
- Variables
- func Code(t testing.TB, r Result, want int)
- func Contains(t testing.TB, text, part string)
- func Equal(t testing.TB, got, want any)
- func Exists(path string) bool
- func IngestURL(dsn *url.URL, endpoint string) string
- func Matches(t testing.TB, text, pattern string)
- func NotContains(t testing.TB, text, part string)
- func NotMatches(t testing.TB, text, pattern string)
- func Remarshal(v any) any
- func Sum(data []byte) string
- func TarGz(files map[string]string) []byte
- type Call
- type FakeAPI
- func (f *FakeAPI) AddEvent(fields J) J
- func (f *FakeAPI) AddIssue(fields J) J
- func (f *FakeAPI) AddProject(name string, platform ...string) J
- func (f *FakeAPI) FindProject(ref string) J
- func (f *FakeAPI) Handle(method, pattern string, handle func(r *Request, match []string) *Reply)
- func (f *FakeAPI) Last() *Request
- func (f *FakeAPI) Paths() []string
- func (f *FakeAPI) Requests() []*Request
- func (f *FakeAPI) Reset()
- func (f *FakeAPI) Stop()
- type FakeCloudflare
- type FakeD1
- type FakeRelease
- type FakeReleases
- type FakeScript
- type Harness
- type J
- type Options
- type Prompt
- type Reply
- type Request
- type Result
Constants ¶
This section is empty.
Variables ¶
var Migrations = map[string]string{
"migrations/0001_init.sql": "CREATE TABLE settings (key TEXT PRIMARY KEY, value TEXT);",
"migrations/0002_projects.sql": "CREATE TABLE projects (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
}
Migrations are the migration files of a fake release.
Functions ¶
func IngestURL ¶
IngestURL is the URL where a Sentry SDK sends events for a DSN: the path of the DSN without its last segment (the base path of a hosted install), then /api/<project>/<endpoint>/.
func NotContains ¶
NotContains checks that text does not contain part.
func NotMatches ¶
NotMatches checks that text does not match a regular expression.
Types ¶
type FakeAPI ¶
type FakeAPI struct {
Token string
// SetupKey opens the /v1/setup routes. Tokens are more valid API tokens (agent tokens).
SetupKey string
Tokens []string
// APITokens are the tokens of POST /v1/tokens (with "token"; the list never shows it).
APITokens []J
// AgentApps is the app (public project ID) of each agent token.
AgentApps map[string]string
SetupMD string
TestEvents map[string]bool
Reports []string
MCPHeaders []http.Header
Uploads []*Request
// URL is the origin plus the base path. Origin is without the base path.
URL string
Origin string
// Outside are the requests outside the base path (the fake answers 404).
Outside []*Request
Health J
Ingest J
Projects []J
Issues []J
Events []J
Override func(r *Request) *Reply
// contains filtered or unexported fields
}
FakeAPI is a fake of the Checkend API (GET /health and /v1). It follows guides/api.md of the server. Tests change its state and can replace any response with Override.
func NewFakeAPI ¶
NewFakeAPI starts a fake on 127.0.0.1. It stops at the end of the test.
func NewFakeAPIAt ¶
NewFakeAPIAt starts a fake that serves under a base path ("/k7q2x9"), as a hosted install. Requests outside the base path get 404 and go to Outside.
func (*FakeAPI) AddProject ¶
AddProject adds a project with a 10-digit public ID and a DSN.
func (*FakeAPI) FindProject ¶
FindProject finds a project by public ID or slug.
func (*FakeAPI) Handle ¶
Handle adds a route (for endpoints that only some tests need). pattern matches the path.
type FakeCloudflare ¶
type FakeCloudflare struct {
URL string
Token string
Accounts []J
Subdomain string
Databases map[string]*FakeD1
Scripts map[string]*FakeScript
Buckets map[string]bool
// Calls are "METHOD /path" of each request.
Calls []string
// SetupOpen is the setup_open of /health on a workers.dev host.
SetupOpen bool
// Fail makes a request fail with HTTP 500 when it returns true ("METHOD /path").
Fail func(call string) bool
// contains filtered or unexported fields
}
FakeCloudflare is a fake of the parts of the Cloudflare API that `ce install cloudflare` uses: token verify, accounts, the workers.dev subdomain, D1 (create, query, Time Travel bookmarks), Worker uploads, secrets, schedules and R2 buckets.
func NewFakeCloudflare ¶
func NewFakeCloudflare(t testing.TB) *FakeCloudflare
NewFakeCloudflare starts the fake with one account and a workers.dev subdomain.
func (*FakeCloudflare) Account ¶
func (f *FakeCloudflare) Account() string
Account is the ID of the first account.
func (*FakeCloudflare) Client ¶
func (f *FakeCloudflare) Client() *http.Client
Client is an HTTP client that sends requests for *.workers.dev to the fake (the installed Worker answers /health there) and all other requests to their host.
func (*FakeCloudflare) DB ¶
func (f *FakeCloudflare) DB(name string) *FakeD1
DB finds a database by name.
type FakeD1 ¶
type FakeD1 struct {
ID string
Name string
Migrations []string
// SQL is each statement batch that ran, in order.
SQL []string
Tables []string
Bookmarks int
}
FakeD1 is a D1 database: its migrations (d1_migrations) and other tables.
type FakeRelease ¶
type FakeRelease struct {
Tag string
Commit string
Draft bool
Prerelease bool
// Manifest is the manifest as a value; Publish signs its JSON.
Manifest map[string]any
// Assets are the files, by name. ManifestBytes and Signature are in Assets too.
Assets map[string][]byte
}
FakeRelease is one GitHub release.
type FakeReleases ¶
type FakeReleases struct {
URL string
// API is the repo API, for example <URL>/repos/Checkend/checkend.
API string
KeyID string
Key ed25519.PrivateKey
// Releases, newest first.
Releases []*FakeRelease
// Downloads counts the asset downloads, by asset name.
Downloads map[string]int
// contains filtered or unexported fields
}
FakeReleases is a fake of the GitHub releases API of the server repo. Each release has a signed checkend-release.json (format 1), the Worker bundle, the migrations and SHA256SUMS.
func NewFakeReleases ¶
func NewFakeReleases(t testing.TB) *FakeReleases
NewFakeReleases starts the fake with no release.
func (*FakeReleases) PublicKeys ¶
func (f *FakeReleases) PublicKeys() map[string]string
PublicKeys are the trusted keys for the CLI: key ID to "ssh-ed25519 AAAA…".
func (*FakeReleases) Publish ¶
func (f *FakeReleases) Publish(version string, change ...func(m map[string]any)) *FakeRelease
Publish adds a release (newest first) with a valid manifest for this version. change can change the manifest before it is signed.
func (*FakeReleases) Sign ¶
func (f *FakeReleases) Sign(r *FakeRelease)
Sign writes checkend-release.json and its signature from r.Manifest.
type FakeScript ¶
type FakeScript struct {
Metadata map[string]any
Modules map[string]string
Types map[string]string
Secrets map[string]string
Schedules []string
WorkersDev bool
}
FakeScript is a Worker.
type Harness ¶
type Harness struct {
Home string
Repo string
Keyring *credstore.Memory
Prompts []Prompt
// Git has the output of git commands, by their arguments ("rev-parse HEAD").
Git map[string]string
// Releases is the fake of the server releases (nil: none is configured).
Releases *FakeReleases
// Exec fakes the programs that the CLI runs (ssh, docker). Calls records each call.
Exec func(stdin []byte, name string, args ...string) ([]byte, error)
Calls []Call
// DNS answers host lookups.
DNS map[string][]string
// HTTP is the HTTP client of the CLI (nil: the default).
HTTP *http.Client
// Executable is the path of ce for `ce update`; CLIReleaseAPI fakes its releases.
Executable string
CLIReleaseAPI string
// CEOnPath is the `ce` that PATH finds ("": none).
CEOnPath string
// Cloudflare is the fake of the Cloudflare API (nil: none).
Cloudflare *FakeCloudflare
// contains filtered or unexported fields
}
Harness runs the CLI in-process with a fake terminal, a temporary home, a temporary repo and a keychain in memory.
func NewHarness ¶
NewHarness makes a home and a repo (with .git) in a temporary directory.
type Options ¶
type Options struct {
Env map[string]string
TTY bool
// Answers for prompts, in order.
Answers []string
Stdin string
Cwd string
}
Options change one run.
type Request ¶
type Request struct {
Method string
Path string
Query url.Values
Authorization string
ContentType string
Body any
Raw []byte
Header http.Header
}
Request is one request that the fake received.