From bd2b9ac225ab6606935b3fa473197082bcbe28f6 Mon Sep 17 00:00:00 2001 From: Seebs Date: Fri, 18 Nov 2022 11:11:08 -0600 Subject: [PATCH] improve coverage in tests --- ctl/backup_test.go | 30 ++++++++++++++++++++++++++++++ ctl/restore_tar.go | 2 +- ctl/restore_tar_test.go | 13 +++++++++++++ ctl/restore_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 ctl/backup_test.go create mode 100644 ctl/restore_test.go diff --git a/ctl/backup_test.go b/ctl/backup_test.go new file mode 100644 index 000000000..de5718912 --- /dev/null +++ b/ctl/backup_test.go @@ -0,0 +1,30 @@ +// Copyright 2021 Molecula Corp. All rights reserved. +package ctl + +import ( + "context" + "errors" + "os" + "testing" +) + +func TestBackupCommand_Run(t *testing.T) { + cm := NewBackupCommand(os.Stdin, os.Stdout, os.Stderr) + cm.OutputDir = "" + err := cm.Run(context.Background()) + if !errors.Is(err, UsageError) { + t.Fatalf("expected usage error, got %v", err) + } + cm.OutputDir = "foo" + cm.Concurrency = 0 + err = cm.Run(context.Background()) + if !errors.Is(err, UsageError) { + t.Fatalf("expected usage error, got %v", err) + } + cm.Concurrency = 1 + cm.HeaderTimeoutStr = "until the cat wakes up" + err = cm.Run(context.Background()) + if !errors.Is(err, UsageError) { + t.Fatalf("expected usage error, got %v", err) + } +} diff --git a/ctl/restore_tar.go b/ctl/restore_tar.go index 86901ba3a..8515a3f9b 100644 --- a/ctl/restore_tar.go +++ b/ctl/restore_tar.go @@ -80,7 +80,7 @@ func (cmd *RestoreTarCommand) Run(ctx context.Context) (err error) { } else { file, err := os.Open(cmd.Path) if err != nil { - return (err) + return err } defer file.Close() f = file diff --git a/ctl/restore_tar_test.go b/ctl/restore_tar_test.go index ebe4868ae..efb8577b1 100644 --- a/ctl/restore_tar_test.go +++ b/ctl/restore_tar_test.go @@ -3,7 +3,9 @@ package ctl import ( "bytes" "context" + "errors" "net/http" + "os" "strings" "testing" @@ -20,6 +22,17 @@ func TestRestoreTarCommand_Run(t *testing.T) { cm := NewRestoreTarCommand(stdin, stdout, stderr) hostport := cmd.API.Node().URI.HostPort() cm.Host = hostport + cm.Path = "" + err := cm.Run(context.Background()) + if !errors.Is(err, UsageError) { + t.Fatalf("expected usage error with empty path, got %v", err) + } + cm.Path = "nonexistent-file" + err = cm.Run(context.Background()) + if _, ok := err.(*os.PathError); !ok { + t.Fatalf("expected path error with nonexistent path, got %v", err) + } + // use stdin cm.Path = "-" resp, err := http.DefaultClient.Do(test.MustNewHTTPRequest("POST", "http://"+hostport+"/index/i", strings.NewReader(""))) diff --git a/ctl/restore_test.go b/ctl/restore_test.go new file mode 100644 index 000000000..84cafdae9 --- /dev/null +++ b/ctl/restore_test.go @@ -0,0 +1,24 @@ +// Copyright 2021 Molecula Corp. All rights reserved. +package ctl + +import ( + "context" + "errors" + "os" + "testing" +) + +func TestRestoreCommand_Run(t *testing.T) { + cm := NewRestoreCommand(os.Stdin, os.Stdout, os.Stderr) + cm.Path = "" + err := cm.Run(context.Background()) + if !errors.Is(err, UsageError) { + t.Fatalf("expected usage error, got %v", err) + } + cm.Path = "foo" + cm.Concurrency = 0 + err = cm.Run(context.Background()) + if !errors.Is(err, UsageError) { + t.Fatalf("expected usage error, got %v", err) + } +}