-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.go
97 lines (80 loc) · 2.22 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package cmd
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
"github.com/capeprivacy/cli/capetest"
czip "github.com/capeprivacy/cli/zip"
)
var testCmd = &cobra.Command{
Use: "test directory [input]",
Short: "Test your function with Cape",
Long: "Test your function with Cape\n" +
"Test will also read input data from stdin, example: \"echo '1234' | cape test dir\".\n" +
"Results are output to stdout so you can easily pipe them elsewhere",
RunE: func(cmd *cobra.Command, args []string) error {
err := Test(cmd, args)
if _, ok := err.(UserError); !ok {
cmd.SilenceUsage = true
}
return err
},
}
func init() {
rootCmd.AddCommand(testCmd)
testCmd.PersistentFlags().StringP("file", "f", "", "input data file")
}
func Test(cmd *cobra.Command, args []string) error {
// Set test to be in verbose mode by default.
log.SetLevel(log.DebugLevel)
u := C.EnclaveHost
insecure := C.Insecure
if len(args) < 1 || len(args) > 2 {
return UserError{Msg: "you must provide input data", Err: fmt.Errorf("invalid number of input arguments")}
}
fnZip, err := czip.Create(args[0])
if err != nil {
return UserError{Msg: "unable to zip specified directory", Err: err}
}
var input []byte
file, err := cmd.Flags().GetString("file")
if err != nil {
return UserError{Msg: "you must specify a file correctly", Err: err}
}
switch {
case file != "":
// input file was provided
input, err = ioutil.ReadFile(file)
if err != nil {
return UserError{Msg: "unable to read data file", Err: err}
}
case len(args) == 2:
// read input from command line string
input = []byte(args[1])
default:
// read input from stdin
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, cmd.InOrStdin()); err != nil {
return UserError{Msg: "unable to read data from stdin", Err: err}
}
input = buf.Bytes()
}
token, err := authToken()
if err != nil {
return err
}
res, err := test(capetest.TestRequest{Function: fnZip, Input: input, AuthToken: token}, u+"/v1/test", insecure)
if err != nil {
return err
}
if _, err := cmd.OutOrStdout().Write(res.Message); err != nil {
return err
}
log.Info()
return nil
}
var authToken = getAuthToken
var test = capetest.CapeTest