80 lines
2.8 KiB
Go
80 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRun(t *testing.T) {
|
|
t.Setenv("ZAPRET_AUTOTUNE_DB", filepath.Join(t.TempDir(), "autotune.db"))
|
|
tests := []struct {
|
|
name string
|
|
args []string
|
|
code int
|
|
out string
|
|
err string
|
|
}{
|
|
{"status", []string{"status"}, 0, "apply: BLOCKED", ""},
|
|
{"discovery missing flags", []string{"discover", "youtube"}, 2, "", "requires --video-id"},
|
|
{"check refused", []string{"check", "youtube"}, 1, "", "YouTube probe URLs/video IDs have not been configured or agreed"},
|
|
{"unsupported check provider", []string{"check", "other"}, 2, "", usage},
|
|
{"missing check provider", []string{"check"}, 2, "", usage},
|
|
{"extra check argument", []string{"check", "youtube", "extra"}, 2, "", usage},
|
|
{"check flag rejected", []string{"check", "youtube", "--help"}, 2, "", usage},
|
|
{"missing command", nil, 2, "", usage},
|
|
{"unknown command", []string{"unknown"}, 2, "", usage},
|
|
{"unsupported provider", []string{"discover", "other"}, 2, "", usage},
|
|
{"missing provider", []string{"discover"}, 2, "", usage},
|
|
{"extra status argument", []string{"status", "extra"}, 2, "", usage},
|
|
{"extra discovery argument", []string{"discover", "youtube", "extra"}, 2, "", "requires --video-id"},
|
|
{"unsupported tune provider", []string{"tune", "other"}, 2, "", usage},
|
|
{"apply unavailable", []string{"apply"}, 2, "", usage},
|
|
{"disruption flag rejected", []string{"tune", "--allow-disruption"}, 2, "", usage},
|
|
{"help", []string{"--help"}, 0, usage, ""},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
var stdout, stderr bytes.Buffer
|
|
if got := run(tt.args, &stdout, &stderr); got != tt.code {
|
|
t.Fatalf("exit code = %d, want %d", got, tt.code)
|
|
}
|
|
for _, stream := range []struct{ got, want string }{
|
|
{stdout.String(), tt.out}, {stderr.String(), tt.err},
|
|
} {
|
|
if (stream.want == "" && stream.got != "") || !strings.Contains(stream.got, stream.want) {
|
|
t.Errorf("output = %q, want %q", stream.got, stream.want)
|
|
}
|
|
}
|
|
if tt.name == "status" && !strings.Contains(stdout.String(), "NOT CHECKED") {
|
|
t.Error("status must not claim production health")
|
|
}
|
|
if tt.name == "check refused" {
|
|
for _, want := range []string{"No network requests were made", "production health was not checked"} {
|
|
if !strings.Contains(stderr.String(), want) {
|
|
t.Errorf("check refusal must include %q", want)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type failingWriter struct{}
|
|
|
|
func (failingWriter) Write([]byte) (int, error) {
|
|
return 0, errors.New("write failed")
|
|
}
|
|
|
|
func TestOutputFailure(t *testing.T) {
|
|
t.Setenv("ZAPRET_AUTOTUNE_DB", filepath.Join(t.TempDir(), "autotune.db"))
|
|
for _, command := range []string{"status", "--help"} {
|
|
var stderr bytes.Buffer
|
|
if run([]string{command}, failingWriter{}, &stderr) != 1 {
|
|
t.Errorf("%s: output failure must return nonzero", command)
|
|
}
|
|
}
|
|
}
|