-
Notifications
You must be signed in to change notification settings - Fork 20
/
shell_test.go
154 lines (148 loc) · 4.88 KB
/
shell_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"strconv"
"sync"
"testing"
)
type MockedShellServiceNotInteractive struct {
ShellBinary string
Logger *Logger
CommandsReactions map[string]interface{}
CommandsRun []string
Environment []string
Mutex *sync.Mutex
}
func NewMockedShellServiceNotInteractive(logger *Logger) *MockedShellServiceNotInteractive {
return &MockedShellServiceNotInteractive{
Logger: logger,
Mutex: &sync.Mutex{},
CommandsRun: make([]string, 0),
}
}
func (bs MockedShellServiceNotInteractive) SetEnvironment(variables []string) {
bs.Environment = make([]string, 0)
for _, value := range variables {
bs.Environment = append(bs.Environment, value)
}
}
func NewMockedShellServiceNotInteractive2(logger *Logger, commandsReactions map[string]interface{}) *MockedShellServiceNotInteractive {
return &MockedShellServiceNotInteractive{
Logger: logger,
CommandsReactions: commandsReactions,
Mutex: &sync.Mutex{},
CommandsRun: make([]string, 0),
}
}
func (ss *MockedShellServiceNotInteractive) AppendCommandRun(command string) {
ss.Mutex.Lock()
ss.CommandsRun = append(ss.CommandsRun, command)
ss.Mutex.Unlock()
}
// AppendCommandRun needs to be invoked on a pointer to object, because it changes the object state.
// Since this method invokes AppendCommandRun() function, it also needs to be invoked on a pointer to object.
func (bs *MockedShellServiceNotInteractive) RunInteractive(cmdString string, separePGroup bool) (int, bool) {
cmd := fmt.Sprintf("Pretending to run: %s", cmdString)
bs.Logger.Log("debug", cmd)
bs.AppendCommandRun(cmd)
return 0, false
}
func (bs *MockedShellServiceNotInteractive) RunGetOutput(cmdString string, separePGroup bool) (string, string, int, bool) {
cmd := fmt.Sprintf("Pretending to run: %s", cmdString)
bs.Logger.Log("debug", cmd)
bs.AppendCommandRun(cmd)
if bs.CommandsReactions != nil {
if val, ok := bs.CommandsReactions[cmdString]; ok {
valArr := val.([]string)
stdo := valArr[0]
stde := valArr[1]
es, err := strconv.Atoi(valArr[2])
if err != nil {
panic(err)
}
cmdInfo := cmdInfoToString(cmdString, stdo, stde, es)
bs.Logger.Log("debug", fmt.Sprintf("Pretending to return: %s", cmdInfo))
return stdo, stde, es, false
}
}
return "", "", 0, false
}
func (bs MockedShellServiceNotInteractive) CheckIfInteractive() bool {
return false
}
type MockedShellServiceInteractive struct {
ShellBinary string
Logger *Logger
Environment []string
CommandsRun []string
Mutex *sync.Mutex
}
func (bs MockedShellServiceInteractive) SetEnvironment(variables []string) {
bs.Environment = make([]string, 0)
for _, value := range variables {
bs.Environment = append(bs.Environment, value)
}
}
func NewMockedShellServiceInteractive(logger *Logger) *MockedShellServiceInteractive {
return &MockedShellServiceInteractive{
Logger: logger,
Mutex: &sync.Mutex{},
CommandsRun: make([]string, 0),
}
}
func (ss *MockedShellServiceInteractive) AppendCommandRun(command string) {
ss.Mutex.Lock()
ss.CommandsRun = append(ss.CommandsRun, command)
ss.Mutex.Unlock()
}
func (bs *MockedShellServiceInteractive) RunInteractive(cmdString string, separePGroup bool) (int, bool) {
cmd := fmt.Sprintf("Pretending to run: %s", cmdString)
bs.Logger.Log("debug", cmd)
bs.AppendCommandRun(cmd)
return 0, false
}
func (bs *MockedShellServiceInteractive) RunGetOutput(cmdString string, separePGroup bool) (string, string, int, bool) {
cmd := fmt.Sprintf("Pretending to run: %s", cmdString)
bs.Logger.Log("debug", cmd)
bs.AppendCommandRun(cmd)
return "", "", 0, false
}
func (bs MockedShellServiceInteractive) CheckIfInteractive() bool {
return true
}
func TestMockedShellService_CheckIfInteractive(t *testing.T){
logger := NewLogger("debug")
shell := NewMockedShellServiceNotInteractive(logger)
interactive := shell.CheckIfInteractive()
assert.False(t, interactive)
}
func TestBashShellService_CheckIfInteractive(t *testing.T) {
logger := NewLogger("debug")
shell := NewBashShellService(logger)
interactive := shell.CheckIfInteractive()
assert.False(t, interactive)
}
func TestBashShellService_RunInteractive(t *testing.T) {
logger := NewLogger("debug")
shell := NewBashShellService(logger)
exitstatus, signaled := shell.RunInteractive("echo hello", false)
assert.Equal(t, 0, exitstatus)
assert.Equal(t, false, signaled)
}
func TestBashShellService_RunGetOutput(t *testing.T) {
logger := NewLogger("debug")
shell := NewBashShellService(logger)
stdout, sterr, exitstatus, signaled := shell.RunGetOutput("echo hello",false)
assert.Equal(t, "hello\n", stdout)
assert.Equal(t, "", sterr)
assert.Equal(t, 0, exitstatus)
assert.Equal(t, false, signaled)
}
func TestBashShellService_SetEnv(t *testing.T) {
logger := NewLogger("debug")
shell := NewBashShellService(logger)
shell.SetEnvironment([]string{"ABC=123", "DEF=444", "ZZZ=999", "YYY=666"})
assert.Equal(t, 4, len(shell.Environment))
assert.Equal(t, "ABC=123", shell.Environment[0])
}