-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_test.go
More file actions
148 lines (133 loc) · 3.56 KB
/
node_test.go
File metadata and controls
148 lines (133 loc) · 3.56 KB
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
package pipes
import (
"bytes"
"errors"
"log"
"os/exec"
"strings"
"testing"
)
func EqualSlice(a, b []string) bool {
for in := range a {
if a[in] != b[in] {
return false
}
}
return true
}
func TestCreatingNewNode(t *testing.T) {
var testExec Executer
t.Run("Testing Factory method for NodeState", func(t *testing.T) {
_, err := NewNodeState(testExec)
if err != nil {
log.Fatalf("Error creating new node" + err.Error())
}
})
}
func TestNodeInput(t *testing.T) {
// Node Setup
node := &NodeState{}
buff := new(bytes.Buffer)
inpChan := make(chan *bytes.Buffer)
// TEST1:
t.Run("Checking by sending buffer address", func(t *testing.T) {
//sending the buffer address to the IP channel
go func(InpChan chan *bytes.Buffer, InpBuf *bytes.Buffer) {
InpChan <- InpBuf
}(inpChan, buff)
err := node.Input(inpChan)
if err != nil {
log.Fatalf("Some error in receiving the input buffer.")
}
if node.stdin != buff {
log.Fatalf("Err: difference in InpBuf and stdin got %s want %s", node.stdin, buff)
}
})
// TEST2:
t.Run("Checking by nil address", func(t *testing.T) {
//sending the nil address to the IP channel
go func(InpChan chan *bytes.Buffer, InpBuf *bytes.Buffer) {
InpChan <- InpBuf
}(inpChan, nil)
err := node.Input(inpChan)
if err == nil {
log.Fatalf("Error should be received as nil address is passed in inpChan")
}
if err.Error() != stdInBufNil {
log.Fatalf("string got %s want %s", err.Error(), buff)
}
})
}
func TestNodeOutput(t *testing.T) {
// SETUP
node := &NodeState{}
node.stdout = new(bytes.Buffer)
opChan := make(chan *bytes.Buffer)
// TEST1:
t.Run("Checking by sending buffer address", func(t *testing.T) {
//sending the buffer address to the IP channel
var OpBuff *bytes.Buffer
go func(OpChan chan *bytes.Buffer, OpBuf *bytes.Buffer) {
OpBuff = <-OpChan
}(opChan, OpBuff)
err := node.Output(opChan)
if err != nil {
log.Fatalf("Some error in sending the output buffer.")
}
})
// SETUP
node.stdout = nil
// TEST2
t.Run("Checking by sending nil address", func(t *testing.T) {
//sending the nil address to the IP channel
var OpBuff *bytes.Buffer
go func(OpChan chan *bytes.Buffer, OpBuf *bytes.Buffer) {
OpBuff = <-OpChan
}(opChan, OpBuff)
err := node.Output(opChan)
if err == nil {
log.Fatalf("Should have received error passing nill in stdout.")
}
if err.Error() != stdOutBufNil {
log.Fatalf("string got %s want %s", err.Error(), stdOutBufNil)
}
})
}
func TestProcessNode(t *testing.T) {
n := NodeState{}
command := []string{"echo", "hello"}
wrongCommand := []string{"lws"}
n.stdin = new(bytes.Buffer)
n.stdout = new(bytes.Buffer)
n.executer = &OsExec{command}
t.Run("Testing passing correct command", func(t *testing.T) {
err := n.executer.Execute(n.stdin, n.stdout)
if err != nil {
log.Fatalf("We shouldn't face any error running the correct command: %s", command)
}
})
n.executer = &OsExec{wrongCommand}
t.Run("Testing passing incorrect command", func(t *testing.T) {
err := n.executer.Execute(n.stdin, n.stdout)
if err == nil {
log.Fatalf("We should face any error running the incorrect command: %s", command)
}
if !strings.Contains(err.Error(), "not found") {
log.Fatalf("string got %s want %s", err.Error(), "Command not found")
}
})
}
//TODO: Create a mock of it.
type OsExec struct {
Cmds []string
}
func (o *OsExec) Execute(stdin, stdout *bytes.Buffer) error {
cmd := exec.Command(o.Cmds[0], o.Cmds[1:]...)
cmd.Stdin = stdin
cmd.Stdout = stdout
err := cmd.Run()
if err != nil {
return errors.New(err.Error())
}
return nil
}