Files
kopia/tests/testenv/cli_test_env.go
Jarek Kowalski e64d5b8eab Fixed few subtle threading bugs uncovered by stress test and rewrote the test to be model-based (#1157)
* testing: refactored logs directory management

* content: fixed index mutex to be shared across all write sessions

added mutex protection during writecontent/refresh race

* testing: upload log artifacts

* content: bump revision number after index has been added

This fixes a bug where manifest manager in another session for
the same open repository may not see a content added, because they
will prematurely cache the incomplete set of contents.

This took 2 weeks to find.

* manifest: improved log output, fixed unnecessary mutex release

* testing: rewrote stress test to be model-based and more precise
2021-07-01 21:37:27 -07:00

217 lines
5.7 KiB
Go

// Package testenv contains Environment for use in testing.
package testenv
import (
"bufio"
"io"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/kopia/kopia/internal/clock"
"github.com/kopia/kopia/internal/testutil"
)
const (
// TestRepoPassword is a password for repositories created in tests.
TestRepoPassword = "qWQPJ2hiiLgWRRCr"
)
// CLIRunner encapsulates running kopia subcommands for testing purposes.
// It supports implementations that use subprocesses or in-process invocations.
type CLIRunner interface {
Start(t *testing.T, args []string) (stdout, stderr io.Reader, wait func() error, kill func())
}
// CLITest encapsulates state for a CLI-based test.
type CLITest struct {
startTime time.Time
RepoDir string
ConfigDir string
Runner CLIRunner
fixedArgs []string
DefaultRepositoryCreateFlags []string
}
// NewCLITest creates a new instance of *CLITest.
func NewCLITest(t *testing.T, runner CLIRunner) *CLITest {
t.Helper()
configDir := testutil.TempDirectory(t)
fixedArgs := []string{
// use per-test config file, to avoid clobbering current user's setup.
"--config-file", filepath.Join(configDir, ".kopia.config"),
}
// disable the use of keyring
switch runtime.GOOS {
case "darwin":
fixedArgs = append(fixedArgs, "--no-use-keychain")
case "windows":
fixedArgs = append(fixedArgs, "--no-use-credential-manager")
case "linux":
fixedArgs = append(fixedArgs, "--no-use-keyring")
}
var formatFlags []string
if testutil.ShouldReduceTestComplexity() {
formatFlags = []string{
"--encryption", "CHACHA20-POLY1305-HMAC-SHA256",
"--block-hash", "BLAKE2S-256",
}
}
return &CLITest{
startTime: clock.Now(),
RepoDir: testutil.TempDirectory(t),
ConfigDir: configDir,
fixedArgs: fixedArgs,
DefaultRepositoryCreateFlags: formatFlags,
Runner: runner,
}
}
// RunAndExpectSuccess runs the given command, expects it to succeed and returns its output lines.
func (e *CLITest) RunAndExpectSuccess(t *testing.T, args ...string) []string {
t.Helper()
stdout, _, err := e.Run(t, false, args...)
if err != nil {
t.Fatalf("'kopia %v' failed with %v", strings.Join(args, " "), err)
}
return stdout
}
// RunAndProcessStderr runs the given command, and streams its output line-by-line to a given function until it returns false.
func (e *CLITest) RunAndProcessStderr(t *testing.T, callback func(line string) bool, args ...string) (kill func()) {
t.Helper()
stdout, stderr, _, kill := e.Runner.Start(t, e.cmdArgs(args))
go io.Copy(io.Discard, stdout)
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
if !callback(scanner.Text()) {
break
}
}
// complete the scan in background without processing lines.
go func() {
for scanner.Scan() {
// ignore
}
}()
return kill
}
// RunAndExpectSuccessWithErrOut runs the given command, expects it to succeed and returns its stdout and stderr lines.
func (e *CLITest) RunAndExpectSuccessWithErrOut(t *testing.T, args ...string) (stdout, stderr []string) {
t.Helper()
stdout, stderr, err := e.Run(t, false, args...)
if err != nil {
t.Fatalf("'kopia %v' failed with %v", strings.Join(args, " "), err)
}
return stdout, stderr
}
// RunAndExpectFailure runs the given command, expects it to fail and returns its output lines.
func (e *CLITest) RunAndExpectFailure(t *testing.T, args ...string) []string {
t.Helper()
stdout, _, err := e.Run(t, true, args...)
if err == nil {
t.Fatalf("'kopia %v' succeeded, but expected failure", strings.Join(args, " "))
}
return stdout
}
// RunAndVerifyOutputLineCount runs the given command and asserts it returns the given number of output lines, then returns them.
func (e *CLITest) RunAndVerifyOutputLineCount(t *testing.T, wantLines int, args ...string) []string {
t.Helper()
lines := e.RunAndExpectSuccess(t, args...)
if len(lines) != wantLines {
t.Fatalf("unexpected list of results of 'kopia %v': %v lines (%v) wanted %v", strings.Join(args, " "), len(lines), lines, wantLines)
}
return lines
}
func (e *CLITest) cmdArgs(args []string) []string {
var suffix []string
// detect repository creation and override DefaultRepositoryCreateFlags for best
// performance on the current platform.
if len(args) >= 2 && (args[0] == "repo" && args[1] == "create") {
suffix = e.DefaultRepositoryCreateFlags
}
return append(append(append([]string(nil), e.fixedArgs...), args...), suffix...)
}
// Run executes kopia with given arguments and returns the output lines.
func (e *CLITest) Run(t *testing.T, expectedError bool, args ...string) (stdout, stderr []string, err error) {
t.Helper()
t.Logf("running 'kopia %v'", strings.Join(args, " "))
args = e.cmdArgs(args)
t0 := clock.Now()
stdoutReader, stderrReader, wait, _ := e.Runner.Start(t, args)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
scanner := bufio.NewScanner(stdoutReader)
for scanner.Scan() {
stdout = append(stdout, scanner.Text())
}
}()
wg.Add(1)
go func() {
defer wg.Done()
scanner := bufio.NewScanner(stderrReader)
for scanner.Scan() {
stderr = append(stderr, scanner.Text())
}
}()
wg.Wait()
gotErr := wait()
if expectedError {
require.Error(t, gotErr, "unexpected success when running 'kopia %v' (stdout:\n%v\nstderr:\n%v", strings.Join(args, " "), strings.Join(stdout, "\n"), strings.Join(stderr, "\n"))
} else {
require.NoError(t, gotErr, "unexpected error when running 'kopia %v' (stdout:\n%v\nstderr:\n%v", strings.Join(args, " "), strings.Join(stdout, "\n"), strings.Join(stderr, "\n"))
}
t.Logf("finished in %v: 'kopia %v'", clock.Since(t0).Milliseconds(), strings.Join(args, " "))
return stdout, stderr, gotErr
}