-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
69 lines (53 loc) · 2.07 KB
/
command_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package rcom
import (
"bytes"
"context"
"encoding/gob"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/ungerik/go-fs"
)
func cpCommand() (command *Command, expectedFile fs.MemFile) {
inputFile := fs.NewMemFile("input.txt", []byte("rcom test file"))
expectedFile = fs.NewMemFile("output.txt", []byte("rcom test file"))
command = &Command{
Name: copyCmd(),
Args: []string{"input.txt", "output.txt"},
Files: map[string][]byte{inputFile.FileName: inputFile.FileData},
ResultFilePatterns: []string{"*.txt"}, // Not necessary for the command to work, but to test the filter
}
return command, expectedFile
}
func Test_GobCommand(t *testing.T) {
buf := bytes.NewBuffer(nil)
// gob.Register(&fs.MemFile{}) // Using []fs.FileReader for Command.File does not work
encoded, _ := cpCommand()
err := gob.NewEncoder(buf).Encode(encoded)
assert.NoError(t, err)
var decoded *Command
err = gob.NewDecoder(buf).Decode(&decoded)
assert.NoError(t, err)
assert.Equal(t, encoded, decoded)
}
func Test_Command_ExecuteLocally(t *testing.T) {
command, expectedFile := cpCommand()
result, callID, err := ExecuteLocally(context.Background(), command)
assert.NoError(t, err)
assert.Equal(t, result.CallID, callID, "congruent callID")
resultFileData, ok := result.Files[expectedFile.Name()]
assert.True(t, ok, "expected result file exists")
assert.Equal(t, expectedFile.FileData, resultFileData, "result file has expected content")
assert.False(t, fs.TempDir().Join(callID.String()).Exists(), "temp dir of call was removed")
}
func Test_Command_ExecuteRemotely(t *testing.T) {
svc := &service{map[string]bool{copyCmd(): true}}
server := httptest.NewServer(svc)
defer server.Close()
command, expectedFile := cpCommand()
result, err := ExecuteRemotely(context.Background(), server.URL, command)
assert.NoError(t, err)
resultFileData, ok := result.Files[expectedFile.Name()]
assert.True(t, ok, "expected result file exists")
assert.Equal(t, expectedFile.FileData, resultFileData, "result file has expected content")
}