package main import ( "bytes" "context" "encoding/json" "os" "strings" "testing" "time" rtest "github.com/restic/restic/internal/test" ) func testRunFind(t testing.TB, wantJSON bool, gopts GlobalOptions, pattern string) []byte { buf := bytes.NewBuffer(nil) globalOptions.stdout = buf globalOptions.JSON = wantJSON defer func() { globalOptions.stdout = os.Stdout globalOptions.JSON = false }() opts := FindOptions{} rtest.OK(t, runFind(context.TODO(), opts, gopts, []string{pattern})) return buf.Bytes() } func TestFind(t *testing.T) { env, cleanup := withTestEnvironment(t) defer cleanup() datafile := testSetupBackupData(t, env) opts := BackupOptions{} testRunBackup(t, "", []string{env.testdata}, opts, env.gopts) testRunCheck(t, env.gopts) results := testRunFind(t, false, env.gopts, "unexistingfile") rtest.Assert(t, len(results) == 0, "unexisting file found in repo (%v)", datafile) results = testRunFind(t, false, env.gopts, "testfile") lines := strings.Split(string(results), "\n") rtest.Assert(t, len(lines) == 2, "expected one file found in repo (%v)", datafile) results = testRunFind(t, false, env.gopts, "testfile*") lines = strings.Split(string(results), "\n") rtest.Assert(t, len(lines) == 4, "expected three files found in repo (%v)", datafile) } type testMatch struct { Path string `json:"path,omitempty"` Permissions string `json:"permissions,omitempty"` Size uint64 `json:"size,omitempty"` Date time.Time `json:"date,omitempty"` UID uint32 `json:"uid,omitempty"` GID uint32 `json:"gid,omitempty"` } type testMatches struct { Hits int `json:"hits,omitempty"` SnapshotID string `json:"snapshot,omitempty"` Matches []testMatch `json:"matches,omitempty"` } func TestFindJSON(t *testing.T) { env, cleanup := withTestEnvironment(t) defer cleanup() datafile := testSetupBackupData(t, env) opts := BackupOptions{} testRunBackup(t, "", []string{env.testdata}, opts, env.gopts) testRunCheck(t, env.gopts) results := testRunFind(t, true, env.gopts, "unexistingfile") matches := []testMatches{} rtest.OK(t, json.Unmarshal(results, &matches)) rtest.Assert(t, len(matches) == 0, "expected no match in repo (%v)", datafile) results = testRunFind(t, true, env.gopts, "testfile") rtest.OK(t, json.Unmarshal(results, &matches)) rtest.Assert(t, len(matches) == 1, "expected a single snapshot in repo (%v)", datafile) rtest.Assert(t, len(matches[0].Matches) == 1, "expected a single file to match (%v)", datafile) rtest.Assert(t, matches[0].Hits == 1, "expected hits to show 1 match (%v)", datafile) results = testRunFind(t, true, env.gopts, "testfile*") rtest.OK(t, json.Unmarshal(results, &matches)) rtest.Assert(t, len(matches) == 1, "expected a single snapshot in repo (%v)", datafile) rtest.Assert(t, len(matches[0].Matches) == 3, "expected 3 files to match (%v)", datafile) rtest.Assert(t, matches[0].Hits == 3, "expected hits to show 3 matches (%v)", datafile) }