2021-05-06 17:56:57 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package ultest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2022-10-11 12:39:08 +01:00
|
|
|
"io"
|
2022-05-10 19:31:20 +01:00
|
|
|
"sort"
|
2021-05-06 17:56:57 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zeebo/clingy"
|
|
|
|
|
2022-01-06 19:55:46 +00:00
|
|
|
"storj.io/storj/cmd/uplink/ulext"
|
|
|
|
"storj.io/storj/cmd/uplink/ulfs"
|
|
|
|
"storj.io/storj/cmd/uplink/ulloc"
|
2021-05-06 17:56:57 +01:00
|
|
|
)
|
|
|
|
|
2021-05-26 21:19:29 +01:00
|
|
|
// Commands is an alias to refer to a function that builds clingy commands.
|
|
|
|
type Commands = func(clingy.Commands, ulext.External)
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// Setup returns some State that can be run multiple times with different command
|
|
|
|
// line arguments.
|
2021-05-26 21:19:29 +01:00
|
|
|
func Setup(cmds Commands, opts ...ExecuteOption) State {
|
2021-05-06 17:56:57 +01:00
|
|
|
return State{
|
|
|
|
cmds: cmds,
|
|
|
|
opts: opts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// State represents some state and environment for a command to execute in.
|
|
|
|
type State struct {
|
2021-05-26 21:19:29 +01:00
|
|
|
cmds Commands
|
2021-05-06 17:56:57 +01:00
|
|
|
opts []ExecuteOption
|
|
|
|
}
|
|
|
|
|
|
|
|
// With appends the provided options and returns a new State.
|
|
|
|
func (st State) With(opts ...ExecuteOption) State {
|
|
|
|
st.opts = append([]ExecuteOption(nil), st.opts...)
|
|
|
|
st.opts = append(st.opts, opts...)
|
|
|
|
return st
|
|
|
|
}
|
|
|
|
|
|
|
|
// Succeed is the same as Run followed by result.RequireSuccess.
|
|
|
|
func (st State) Succeed(t *testing.T, args ...string) Result {
|
|
|
|
result := st.Run(t, args...)
|
|
|
|
result.RequireSuccess(t)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail is the same as Run followed by result.RequireFailure.
|
|
|
|
func (st State) Fail(t *testing.T, args ...string) Result {
|
|
|
|
result := st.Run(t, args...)
|
|
|
|
result.RequireFailure(t)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the command specified by the args and returns a Result.
|
|
|
|
func (st State) Run(t *testing.T, args ...string) Result {
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
var stdin bytes.Buffer
|
|
|
|
var ran bool
|
|
|
|
|
2022-05-10 19:31:20 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
lfs := ulfs.NewLocal(ulfs.NewLocalBackendMem())
|
|
|
|
rfs := newRemoteFilesystem()
|
|
|
|
fs := ulfs.NewMixed(lfs, rfs)
|
|
|
|
|
|
|
|
cs := &callbackState{
|
|
|
|
fs: fs,
|
|
|
|
rfs: rfs,
|
|
|
|
}
|
2021-05-12 17:16:56 +01:00
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
ok, err := clingy.Environment{
|
|
|
|
Name: "uplink-test",
|
|
|
|
Args: args,
|
|
|
|
|
|
|
|
Stdin: &stdin,
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
|
|
|
|
2022-08-30 10:51:31 +01:00
|
|
|
Wrap: func(ctx context.Context, cmd clingy.Command) error {
|
2021-05-06 17:56:57 +01:00
|
|
|
for _, opt := range st.opts {
|
2022-05-10 19:31:20 +01:00
|
|
|
opt.fn(t, ctx, cs)
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 19:31:20 +01:00
|
|
|
if len(cs.stdin) > 0 {
|
|
|
|
_, _ = stdin.WriteString(cs.stdin)
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ran = true
|
2021-05-12 17:16:56 +01:00
|
|
|
return cmd.Execute(ctx)
|
2021-05-06 17:56:57 +01:00
|
|
|
},
|
2022-05-10 19:31:20 +01:00
|
|
|
}.Run(ctx, func(cmds clingy.Commands) {
|
|
|
|
st.cmds(cmds, newExternal(fs, nil))
|
2021-05-26 21:19:29 +01:00
|
|
|
})
|
2021-05-06 17:56:57 +01:00
|
|
|
|
|
|
|
if ok && err == nil {
|
|
|
|
require.True(t, ran, "no command was executed: %q", args)
|
|
|
|
}
|
2021-05-12 17:16:56 +01:00
|
|
|
|
2022-05-10 19:31:20 +01:00
|
|
|
files := rfs.Files()
|
|
|
|
files = gatherLocalFiles(ctx, t, lfs, files)
|
|
|
|
sort.Slice(files, func(i, j int) bool { return files[i].less(files[j]) })
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
return Result{
|
2021-10-02 00:47:53 +01:00
|
|
|
Stdout: stdout.String(),
|
|
|
|
Stderr: stderr.String(),
|
|
|
|
Ok: ok,
|
|
|
|
Err: err,
|
2022-05-10 19:31:20 +01:00
|
|
|
Files: files,
|
|
|
|
Pending: rfs.Pending(),
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:31:20 +01:00
|
|
|
func gatherLocalFiles(ctx context.Context, t *testing.T, fs ulfs.FilesystemLocal, files []File) []File {
|
|
|
|
{
|
|
|
|
iter, err := fs.List(ctx, "", &ulfs.ListOptions{Recursive: true})
|
|
|
|
require.NoError(t, err)
|
|
|
|
files = collectIterator(ctx, t, fs, iter, files)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
iter, err := fs.List(ctx, "/", &ulfs.ListOptions{Recursive: true})
|
|
|
|
require.NoError(t, err)
|
|
|
|
files = collectIterator(ctx, t, fs, iter, files)
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
|
|
|
func collectIterator(ctx context.Context, t *testing.T, fs ulfs.FilesystemLocal, iter ulfs.ObjectIterator, files []File) []File {
|
|
|
|
for iter.Next() {
|
|
|
|
func() {
|
|
|
|
loc := iter.Item().Loc.Loc()
|
|
|
|
|
|
|
|
mrh, err := fs.Open(ctx, loc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { _ = mrh.Close() }()
|
|
|
|
|
|
|
|
rh, err := mrh.NextPart(ctx, -1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { _ = rh.Close() }()
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
data, err := io.ReadAll(rh)
|
2022-05-10 19:31:20 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
files = append(files, File{
|
|
|
|
Loc: loc,
|
|
|
|
Contents: string(data),
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
require.NoError(t, iter.Err())
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
|
|
|
type callbackState struct {
|
|
|
|
stdin string
|
|
|
|
fs ulfs.Filesystem
|
|
|
|
rfs *remoteFilesystem
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
// ExecuteOption allows one to control the environment that a command executes in.
|
|
|
|
type ExecuteOption struct {
|
2022-08-30 10:51:31 +01:00
|
|
|
fn func(t *testing.T, ctx context.Context, cs *callbackState)
|
2021-05-12 17:16:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithFilesystem lets one do arbitrary setup on the filesystem in a callback.
|
2022-08-30 10:51:31 +01:00
|
|
|
func WithFilesystem(cb func(t *testing.T, ctx context.Context, fs ulfs.Filesystem)) ExecuteOption {
|
|
|
|
return ExecuteOption{func(t *testing.T, ctx context.Context, cs *callbackState) {
|
2022-05-10 19:31:20 +01:00
|
|
|
cb(t, ctx, cs.fs)
|
2021-05-12 17:16:56 +01:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBucket ensures the bucket exists.
|
|
|
|
func WithBucket(name string) ExecuteOption {
|
2022-08-30 10:51:31 +01:00
|
|
|
return ExecuteOption{func(_ *testing.T, _ context.Context, cs *callbackState) {
|
2022-05-10 19:31:20 +01:00
|
|
|
cs.rfs.ensureBucket(name)
|
2021-05-12 17:16:56 +01:00
|
|
|
}}
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithStdin sets the command to execute with the provided string as standard input.
|
|
|
|
func WithStdin(stdin string) ExecuteOption {
|
2022-08-30 10:51:31 +01:00
|
|
|
return ExecuteOption{func(_ *testing.T, _ context.Context, cs *callbackState) {
|
2022-05-10 19:31:20 +01:00
|
|
|
cs.stdin = stdin
|
2021-05-06 17:56:57 +01:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFile sets the command to execute with a file created at the given location.
|
2021-05-12 17:16:56 +01:00
|
|
|
func WithFile(location string, contents ...string) ExecuteOption {
|
|
|
|
contents = append([]string(nil), contents...)
|
2022-08-30 10:51:31 +01:00
|
|
|
return ExecuteOption{func(t *testing.T, ctx context.Context, cs *callbackState) {
|
2021-05-06 17:56:57 +01:00
|
|
|
loc, err := ulloc.Parse(location)
|
2021-05-12 17:16:56 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
if bucket, _, ok := loc.RemoteParts(); ok {
|
2022-05-10 19:31:20 +01:00
|
|
|
cs.rfs.ensureBucket(bucket)
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
2021-05-12 17:16:56 +01:00
|
|
|
|
2023-04-06 18:03:02 +01:00
|
|
|
wh, err := cs.fs.Create(ctx, loc, nil)
|
2021-05-12 17:16:56 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { _ = wh.Abort() }()
|
|
|
|
|
|
|
|
for _, content := range contents {
|
|
|
|
_, err := wh.Write([]byte(content))
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
if len(contents) == 0 {
|
|
|
|
_, err := wh.Write([]byte(location))
|
|
|
|
require.NoError(t, err)
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
2021-05-12 17:16:56 +01:00
|
|
|
|
|
|
|
require.NoError(t, wh.Commit())
|
2021-05-06 17:56:57 +01:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPendingFile sets the command to execute with a pending upload happening to
|
|
|
|
// the provided location.
|
|
|
|
func WithPendingFile(location string) ExecuteOption {
|
2022-08-30 10:51:31 +01:00
|
|
|
return ExecuteOption{func(t *testing.T, ctx context.Context, cs *callbackState) {
|
2021-05-06 17:56:57 +01:00
|
|
|
loc, err := ulloc.Parse(location)
|
2021-05-12 17:16:56 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
if bucket, _, ok := loc.RemoteParts(); ok {
|
2022-05-10 19:31:20 +01:00
|
|
|
cs.rfs.ensureBucket(bucket)
|
2021-10-02 00:47:53 +01:00
|
|
|
} else {
|
|
|
|
t.Fatalf("Invalid pending local file: %s", loc)
|
2021-05-06 17:56:57 +01:00
|
|
|
}
|
2021-05-12 17:16:56 +01:00
|
|
|
|
2022-05-10 19:31:20 +01:00
|
|
|
_, err = cs.fs.Create(ctx, loc, nil)
|
2021-05-12 17:16:56 +01:00
|
|
|
require.NoError(t, err)
|
2021-05-06 17:56:57 +01:00
|
|
|
}}
|
|
|
|
}
|