-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_test.go
95 lines (72 loc) · 2.11 KB
/
main_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
package main
import (
"io/ioutil"
"log"
"os"
"path"
"syscall"
"testing"
. "github.com/franela/goblin"
"github.com/franela/vault/vault"
"github.com/franela/vault/vault/testutils"
)
func TestMain(t *testing.T) {
g := Goblin(t)
g.Describe("Main", func() {
g.Describe("#isGPGInstalled", func() {
g.It("Should return false if gpg is not in the PATH", func() {
prevPath := os.Getenv("PATH")
defer func() {
os.Setenv("PATH", prevPath)
}()
os.Setenv("PATH", "")
g.Assert(isGPGInstalled()).IsFalse()
})
g.It("Should return true if gpg is in the PATH", func() {
g.Assert(isGPGInstalled()).IsTrue()
})
})
g.Describe("#Run", func() {
g.It("Should log to dev/null by default if no verbose flag is set ", func() {
dir := testutils.GetTemporaryHomeDir()
filePath := path.Join(dir, "stderr")
logFile, _ := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0644)
syscall.Dup2(int(logFile.Fd()), 2)
initializeCli([]string{})
log.Println("Test")
defer func() {
logFile.Close()
}()
logFile, _ = os.Open(filePath)
fileContent, _ := ioutil.ReadAll(logFile)
g.Assert(len(fileContent)).Equal(0)
})
g.It("Should log to stderr if verbose flag is set ", func() {
dir := testutils.GetTemporaryHomeDir()
filePath := path.Join(dir, "stderr")
logFile, _ := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0644)
syscall.Dup2(int(logFile.Fd()), 2)
initializeCli([]string{"-verbose"})
log.Println("Test")
logFile.Close()
defer func() {
logFile.Close()
}()
logFile, _ = os.Open(filePath)
fileContent, _ := ioutil.ReadAll(logFile)
g.Assert(len(fileContent)).Equal(25)
})
g.It("Should run validator if command returns with an error", func() {
testutils.SetTestGPGHome("nokey")
vault.SetHomeDir(testutils.GetTemporaryHomeDir())
called := false
mockValidator := &testutils.MockValidator{
ValidateMock: func() { called = true },
}
commandValidator = mockValidator
runCommand(initializeCli([]string{"set"}))
g.Assert(called).IsTrue()
})
})
})
}