improve coverage in tests

(cherry picked from commit bd2b9ac225)
This commit is contained in:
Seebs 2022-11-18 11:11:08 -06:00 committed by Fletcher Haynes
parent f2d1c3f459
commit f06b187bbd
4 changed files with 68 additions and 1 deletions

30
ctl/backup_test.go Normal file
View file

@ -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)
}
}

View file

@ -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

View file

@ -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("")))

24
ctl/restore_test.go Normal file
View file

@ -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)
}
}