mirror of
https://github.com/restic/restic.git
synced 2025-03-30 00:00:14 +01:00
add a test to cmd_ls_integration_test.go: test rest ls --json
(#5255)
* cmd_ls: one more test: ls --json to check the JSON lines validate that the individual JSON lines are valid JSON statements. Check for snap ID and the path names in the backup.
This commit is contained in:
parent
c405e9e748
commit
cc4712f8e9
1 changed files with 60 additions and 0 deletions
|
@ -1,12 +1,14 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/internal/restic"
|
||||
rtest "github.com/restic/restic/internal/test"
|
||||
)
|
||||
|
||||
|
@ -101,3 +103,61 @@ func TestRunLsSort(t *testing.T) {
|
|||
rtest.Equals(t, test.expected, fileList, fmt.Sprintf("mismatch for mode %v", test.mode))
|
||||
}
|
||||
}
|
||||
|
||||
// JSON lines test
|
||||
func TestRunLsJson(t *testing.T) {
|
||||
pathList := []string{
|
||||
"/0",
|
||||
"/0/for_cmd_ls",
|
||||
"/0/for_cmd_ls/file1.txt",
|
||||
"/0/for_cmd_ls/file2.txt",
|
||||
"/0/for_cmd_ls/python.py",
|
||||
}
|
||||
|
||||
env, cleanup := withTestEnvironment(t)
|
||||
defer cleanup()
|
||||
|
||||
testSetupBackupData(t, env)
|
||||
opts := BackupOptions{}
|
||||
testRunBackup(t, env.testdata, []string{"0/for_cmd_ls"}, opts, env.gopts)
|
||||
snapshotIDs := testListSnapshots(t, env.gopts, 1)
|
||||
|
||||
env.gopts.Quiet = true
|
||||
env.gopts.JSON = true
|
||||
buf := testRunLsWithOpts(t, env.gopts, LsOptions{}, []string{"latest"})
|
||||
byteLines := bytes.Split(buf, []byte{'\n'})
|
||||
|
||||
// partial copy of snapshot structure from cmd_ls
|
||||
type lsSnapshot struct {
|
||||
*restic.Snapshot
|
||||
ID *restic.ID `json:"id"`
|
||||
ShortID string `json:"short_id"` // deprecated
|
||||
MessageType string `json:"message_type"` // "snapshot"
|
||||
StructType string `json:"struct_type"` // "snapshot", deprecated
|
||||
}
|
||||
|
||||
var snappy lsSnapshot
|
||||
rtest.OK(t, json.Unmarshal(byteLines[0], &snappy))
|
||||
rtest.Equals(t, snappy.ShortID, snapshotIDs[0].Str(), "expected snap IDs to be identical")
|
||||
|
||||
// partial copy of node structure from cmd_ls
|
||||
type lsNode struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Path string `json:"path"`
|
||||
Permissions string `json:"permissions,omitempty"`
|
||||
Inode uint64 `json:"inode,omitempty"`
|
||||
MessageType string `json:"message_type"` // "node"
|
||||
StructType string `json:"struct_type"` // "node", deprecated
|
||||
}
|
||||
|
||||
var testNode lsNode
|
||||
for i, nodeLine := range byteLines[1:] {
|
||||
if len(nodeLine) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
rtest.OK(t, json.Unmarshal(nodeLine, &testNode))
|
||||
rtest.Equals(t, pathList[i], testNode.Path)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue