-
Notifications
You must be signed in to change notification settings - Fork 5
/
func_test.go
77 lines (63 loc) · 2.01 KB
/
func_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
package eveadm
import (
// "io/ioutil"
"fmt"
// "strings"
"testing"
"time"
"github.com/itmo-eve/eveadm/cmd"
"github.com/spf13/cobra"
)
// testCmd represents the test command
var testCmd = &cobra.Command{
Use: "test",
Short: "Run shell command with arguments on 'test' mode",
Long: `
Run shell command with arguments on 'test' mode. For example:
eveadm test ps x`,
Run: func(c *cobra.Command, args []string) {
cmd.Envs = c.Flag("env").Value.String()
if cmd.Verbose {
fmt.Println("test called with envs:", cmd.Envs)
}
cmd.Test = true
cmd.Run(c, cmd.Timeout, args, cmd.Envs)
},
}
func init() {
cmd.RootCmd.AddCommand(testCmd)
testCmd.PersistentFlags().StringP("env", "e", "", "Setting environment variables")
}
func TestFuncExecute(t *testing.T) {
var eout string
t.Run("correct execution", func(t *testing.T) {
eout, _ = executeCommand(cmd.RootCmd, "test", "ls")
checkStringContains(t, eout, "README.md")
})
t.Run("wrong execution", func(t *testing.T) {
eout, _ = executeCommand(cmd.RootCmd, "test", "ls", "qwe")
checkStringContains(t, eout, "No such file or directory")
})
t.Run("environment variables setting", func(t *testing.T) {
eout, _ = executeCommand(cmd.RootCmd, "test", "locale")
checkStringOmits(t, eout, "LANG=ru_RU.UTF-8")
//checkStringContains(t, eout, "LANG=C")
eout, _ = executeCommand(cmd.RootCmd, "test", "locale", "-e",
"LANG=ru_RU.UTF-8")
checkStringContains(t, eout, "LANG=ru_RU.UTF-8")
})
t.Run("timewait", func(t *testing.T) {
start := time.Now()
eout, _ = executeCommand(cmd.RootCmd, "test", "sleep", "100")
elapsed := time.Since(start)
if elapsed < 100*time.Second {
t.Errorf("Expected time of execution for 'sleep 100': \n %v\nGot:\n %v\n", 100*time.Second, elapsed)
}
start = time.Now()
eout, _ = executeCommand(cmd.RootCmd, "test", "sleep", "100", "-t", "1")
elapsed = time.Since(start)
if elapsed > 100*time.Second {
t.Errorf("Expected time of execution for 'sleep 100 -t 1': \n %v\nGot:\n %v\n", 1*time.Minute, elapsed)
}
})
}