mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-04 07:07:11 +00:00
use fake filesystem to test key file flag for ark restic init-repo
Signed-off-by: Steve Kriss <steve@heptio.com>
This commit is contained in:
@@ -18,11 +18,11 @@ package restic
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/heptio/ark/pkg/client"
|
||||
"github.com/heptio/ark/pkg/cmd"
|
||||
"github.com/heptio/ark/pkg/restic"
|
||||
"github.com/heptio/ark/pkg/util/filesystem"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
@@ -55,13 +55,15 @@ type InitRepositoryOptions struct {
|
||||
KeyData string
|
||||
KeySize int
|
||||
|
||||
fileSystem filesystem.Interface
|
||||
kubeClient kclientset.Interface
|
||||
keyBytes []byte
|
||||
}
|
||||
|
||||
func NewInitRepositoryOptions() *InitRepositoryOptions {
|
||||
return &InitRepositoryOptions{
|
||||
KeySize: 1024,
|
||||
KeySize: 1024,
|
||||
fileSystem: filesystem.NewFileSystem(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +91,7 @@ func (o *InitRepositoryOptions) Complete(f client.Factory) error {
|
||||
|
||||
switch {
|
||||
case o.KeyFile != "":
|
||||
data, err := ioutil.ReadFile(o.KeyFile)
|
||||
data, err := o.fileSystem.ReadFile(o.KeyFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"github.com/heptio/ark/pkg/client"
|
||||
clientset "github.com/heptio/ark/pkg/generated/clientset/versioned"
|
||||
arktest "github.com/heptio/ark/pkg/util/test"
|
||||
)
|
||||
|
||||
type fakeFactory struct{}
|
||||
@@ -46,10 +47,14 @@ func TestComplete(t *testing.T) {
|
||||
err = o.Complete(&fakeFactory{})
|
||||
assert.EqualError(t, err, errKeyFileAndKeyDataProvided.Error())
|
||||
|
||||
// TODO test that if KeyFile is provided, the data is used
|
||||
// (should move the pkg/restore/FileSystem interface to
|
||||
// a common location and use it here so ioutil.ReadFile
|
||||
// can be easily mocked)
|
||||
// if KeyFile is provided, its contents are used
|
||||
fileContents := []byte("bar")
|
||||
o = &InitRepositoryOptions{
|
||||
KeyFile: "/foo",
|
||||
fileSystem: arktest.NewFakeFileSystem().WithFile("/foo", fileContents),
|
||||
}
|
||||
assert.NoError(t, o.Complete(&fakeFactory{}))
|
||||
assert.Equal(t, fileContents, o.keyBytes)
|
||||
|
||||
// if KeyData is provided, it's used
|
||||
o = &InitRepositoryOptions{
|
||||
|
||||
Reference in New Issue
Block a user