feat(runner): add the own code runner contract, client and nsjail service
This commit is contained in:
1 parent
5e1db37746
commit
cdf6bcbb91
109 files changed
+24084
-28
No files matched your search
@@ -0,0 +1,116 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const testToken = "test-token-0123456789abcdefghijklmnop"
|
||||
|
||||
func env(values map[string]string) lookupFunc {
|
||||
return func(name string) (string, bool) {
|
||||
value, ok := values[name]
|
||||
return value, ok
|
||||
}
|
||||
}
|
||||
|
||||
func noFiles(string) ([]byte, error) {
|
||||
return nil, fs.ErrNotExist
|
||||
}
|
||||
|
||||
func TestParseDefaultsWithTLS(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cfg, err := parse(env(map[string]string{
|
||||
"RUNNER_TLS_CERT_FILE": "/run/secrets/tls.crt",
|
||||
"RUNNER_TLS_KEY_FILE": "/run/secrets/tls.key",
|
||||
"RUNNER_API_TOKEN": testToken,
|
||||
}), noFiles)
|
||||
if err != nil {
|
||||
t.Fatalf("parse: %v", err)
|
||||
}
|
||||
if cfg.MaxConcurrentRuns != DefaultMaxConcurrentRuns || cfg.ResultTTL != DefaultResultTTL || cfg.AllowPlaintextHTTP {
|
||||
t.Fatalf("unexpected defaults: %+v", cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRequiresTLSUnlessTheDevSwitchIsSet(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := parse(env(map[string]string{"RUNNER_API_TOKEN": testToken}), noFiles)
|
||||
if err == nil || !strings.Contains(err.Error(), "TLS is required") {
|
||||
t.Fatalf("parse without TLS = %v, want a TLS error", err)
|
||||
}
|
||||
cfg, err := parse(env(map[string]string{
|
||||
"RUNNER_API_TOKEN": testToken,
|
||||
"RUNNER_DEV_ALLOW_PLAINTEXT_HTTP": "true",
|
||||
}), noFiles)
|
||||
if err != nil || !cfg.AllowPlaintextHTTP {
|
||||
t.Fatalf("parse with the dev switch = %+v, %v", cfg, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseReadsTheTokenFileAndHidesTheValue(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
files := func(path string) ([]byte, error) {
|
||||
if path == "/run/secrets/token" {
|
||||
return []byte(testToken + "\n"), nil
|
||||
}
|
||||
return nil, fs.ErrNotExist
|
||||
}
|
||||
cfg, err := parse(env(map[string]string{
|
||||
"RUNNER_API_TOKEN_FILE": "/run/secrets/token",
|
||||
"RUNNER_DEV_ALLOW_PLAINTEXT_HTTP": "1",
|
||||
}), files)
|
||||
if err != nil || cfg.APIToken != testToken {
|
||||
t.Fatalf("parse token file = %q, %v", cfg.APIToken, err)
|
||||
}
|
||||
|
||||
short := "short-secret"
|
||||
_, err = parse(env(map[string]string{
|
||||
"RUNNER_API_TOKEN": short,
|
||||
"RUNNER_DEV_ALLOW_PLAINTEXT_HTTP": "1",
|
||||
}), noFiles)
|
||||
if err == nil || strings.Contains(err.Error(), short) {
|
||||
t.Fatalf("parse short token = %v, want an error without the value", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseReportsEveryInvalidValue(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := parse(env(map[string]string{
|
||||
"RUNNER_API_TOKEN": testToken,
|
||||
"RUNNER_DEV_ALLOW_PLAINTEXT_HTTP": "true",
|
||||
"RUNNER_MAX_CONCURRENT_RUNS": "many",
|
||||
"RUNNER_RESULT_TTL": "soon",
|
||||
"RUNNER_SANDBOX_UID": "0",
|
||||
"RUNNER_WORK_DIR": "relative/work",
|
||||
}), noFiles)
|
||||
if err == nil {
|
||||
t.Fatal("parse accepted invalid values")
|
||||
}
|
||||
for _, name := range []string{"RUNNER_MAX_CONCURRENT_RUNS", "RUNNER_RESULT_TTL", "RUNNER_SANDBOX_UID", "RUNNER_WORK_DIR"} {
|
||||
if !strings.Contains(err.Error(), name) {
|
||||
t.Errorf("error %q does not name %s", err, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRejectsConflictingTLSAndPlaintext(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, err := parse(env(map[string]string{
|
||||
"RUNNER_API_TOKEN": testToken,
|
||||
"RUNNER_DEV_ALLOW_PLAINTEXT_HTTP": "true",
|
||||
"RUNNER_TLS_CERT_FILE": "/tls.crt",
|
||||
"RUNNER_TLS_KEY_FILE": "/tls.key",
|
||||
"RUNNER_SHUTDOWN_TIMEOUT": time.Second.String(),
|
||||
}), noFiles)
|
||||
if err == nil || !strings.Contains(err.Error(), "conflicts") {
|
||||
t.Fatalf("parse = %v, want a conflict error", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user