2023-05-14 10:02:34 +00:00
|
|
|
package restore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2023-05-14 10:02:34 +00:00
|
|
|
"github.com/restic/restic/internal/test"
|
2024-07-27 23:06:26 +00:00
|
|
|
"github.com/restic/restic/internal/ui"
|
2023-05-14 10:02:34 +00:00
|
|
|
)
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
func createJSONProgress() (*ui.MockTerminal, ProgressPrinter) {
|
|
|
|
term := &ui.MockTerminal{}
|
2024-05-31 18:38:51 +00:00
|
|
|
printer := NewJSONProgress(term, 3)
|
|
|
|
return term, printer
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONPrintUpdate(t *testing.T) {
|
|
|
|
term, printer := createJSONProgress()
|
2024-05-31 12:12:06 +00:00
|
|
|
printer.Update(State{3, 11, 0, 29, 47, 0}, 5*time.Second)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{"{\"message_type\":\"status\",\"seconds_elapsed\":5,\"percent_done\":0.6170212765957447,\"total_files\":11,\"files_restored\":3,\"total_bytes\":47,\"bytes_restored\":29}\n"}, term.Output)
|
2023-05-14 10:02:34 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 12:12:06 +00:00
|
|
|
func TestJSONPrintUpdateWithSkipped(t *testing.T) {
|
2024-05-31 18:38:51 +00:00
|
|
|
term, printer := createJSONProgress()
|
2024-05-31 12:12:06 +00:00
|
|
|
printer.Update(State{3, 11, 2, 29, 47, 59}, 5*time.Second)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{"{\"message_type\":\"status\",\"seconds_elapsed\":5,\"percent_done\":0.6170212765957447,\"total_files\":11,\"files_restored\":3,\"files_skipped\":2,\"total_bytes\":47,\"bytes_restored\":29,\"bytes_skipped\":59}\n"}, term.Output)
|
2024-05-31 12:12:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-14 10:02:34 +00:00
|
|
|
func TestJSONPrintSummaryOnSuccess(t *testing.T) {
|
2024-05-31 18:38:51 +00:00
|
|
|
term, printer := createJSONProgress()
|
2024-05-31 12:12:06 +00:00
|
|
|
printer.Finish(State{11, 11, 0, 47, 47, 0}, 5*time.Second)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{"{\"message_type\":\"summary\",\"seconds_elapsed\":5,\"total_files\":11,\"files_restored\":11,\"total_bytes\":47,\"bytes_restored\":47}\n"}, term.Output)
|
2023-05-14 10:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONPrintSummaryOnErrors(t *testing.T) {
|
2024-05-31 18:38:51 +00:00
|
|
|
term, printer := createJSONProgress()
|
2024-05-31 12:12:06 +00:00
|
|
|
printer.Finish(State{3, 11, 0, 29, 47, 0}, 5*time.Second)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{"{\"message_type\":\"summary\",\"seconds_elapsed\":5,\"total_files\":11,\"files_restored\":3,\"total_bytes\":47,\"bytes_restored\":29}\n"}, term.Output)
|
2023-05-14 10:02:34 +00:00
|
|
|
}
|
2024-05-31 12:12:06 +00:00
|
|
|
|
|
|
|
func TestJSONPrintSummaryOnSuccessWithSkipped(t *testing.T) {
|
2024-05-31 18:38:51 +00:00
|
|
|
term, printer := createJSONProgress()
|
2024-05-31 12:12:06 +00:00
|
|
|
printer.Finish(State{11, 11, 2, 47, 47, 59}, 5*time.Second)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{"{\"message_type\":\"summary\",\"seconds_elapsed\":5,\"total_files\":11,\"files_restored\":11,\"files_skipped\":2,\"total_bytes\":47,\"bytes_restored\":47,\"bytes_skipped\":59}\n"}, term.Output)
|
2024-05-31 12:12:06 +00:00
|
|
|
}
|
2024-05-31 18:38:51 +00:00
|
|
|
|
|
|
|
func TestJSONPrintCompleteItem(t *testing.T) {
|
|
|
|
for _, data := range []struct {
|
|
|
|
action ItemAction
|
|
|
|
size uint64
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{ActionDirRestored, 0, "{\"message_type\":\"verbose_status\",\"action\":\"restored\",\"item\":\"test\",\"size\":0}\n"},
|
|
|
|
{ActionFileRestored, 123, "{\"message_type\":\"verbose_status\",\"action\":\"restored\",\"item\":\"test\",\"size\":123}\n"},
|
|
|
|
{ActionFileUpdated, 123, "{\"message_type\":\"verbose_status\",\"action\":\"updated\",\"item\":\"test\",\"size\":123}\n"},
|
|
|
|
{ActionFileUnchanged, 123, "{\"message_type\":\"verbose_status\",\"action\":\"unchanged\",\"item\":\"test\",\"size\":123}\n"},
|
2024-06-29 19:29:42 +00:00
|
|
|
{ActionDeleted, 0, "{\"message_type\":\"verbose_status\",\"action\":\"deleted\",\"item\":\"test\",\"size\":0}\n"},
|
2024-05-31 18:38:51 +00:00
|
|
|
} {
|
|
|
|
term, printer := createJSONProgress()
|
|
|
|
printer.CompleteItem(data.action, "test", data.size)
|
2024-07-27 23:06:26 +00:00
|
|
|
test.Equals(t, []string{data.expected}, term.Output)
|
2024-05-31 18:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-27 23:06:26 +00:00
|
|
|
|
|
|
|
func TestJSONError(t *testing.T) {
|
|
|
|
term, printer := createJSONProgress()
|
|
|
|
test.Equals(t, printer.Error("/path", errors.New("error \"message\"")), nil)
|
2024-08-03 19:29:10 +00:00
|
|
|
test.Equals(t, []string{"{\"message_type\":\"error\",\"error\":{\"message\":\"error \\\"message\\\"\"},\"during\":\"restore\",\"item\":\"/path\"}\n"}, term.Errors)
|
2024-07-27 23:06:26 +00:00
|
|
|
}
|