forked from wingedpig/loom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loom_test.go
111 lines (94 loc) · 2.31 KB
/
loom_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
package loom
import (
"io/ioutil"
"os"
"strings"
"testing"
)
func Test_Run(t *testing.T) {
// These tests assume you can ssh log into localhost. Adjust config.User and such accordingly.
var config Config
config.Host = "127.0.0.1:22"
localout, err := config.Local("/bin/ls -l /var/tmp")
if err != nil {
t.Errorf("Local error, %s", err)
}
remoteout, err := config.Run("/bin/ls -l /var/tmp")
if err != nil {
t.Errorf("Run error, %s", err)
}
remoteout = strings.Replace(remoteout, "\r", "", -1)
if localout != remoteout {
t.Errorf("Local and remote differ")
}
dir, err := ioutil.TempDir("/var/tmp", "loom")
if err != nil {
t.Errorf("Cannot create temp directory, %s", err)
}
defer os.RemoveAll(dir)
file1 := dir + "/a"
file2 := dir + "/b"
testText := `Lorem ipsum Minim voluptate aliquip commodo.`
err = config.PutString(testText, file1)
if err != nil {
t.Errorf("PutString error, %s", err)
}
err = config.Get(file1, file2)
if err != nil {
t.Errorf("Get error, %s", err)
}
// read file2, verify that it matches testText
verifyText, err := ioutil.ReadFile(file2)
if err != nil {
t.Errorf("Get failed, can't read %s, %s", file2, err)
}
if string(verifyText) != testText {
t.Errorf("Text mismatch, expected '%s', got '%s'", testText, verifyText)
}
}
func TestInjectSudoPasswordIfNecessary(t *testing.T) {
config := Config{
User: "user",
Password: "user",
Host: "127.0.0.1:22",
DisplayOutput: true,
AbortOnError: false,
}
val, err := config.Sudo("ls -l")
if err != nil {
t.Fatal(err)
}
t.Log(val)
}
func TestEnsureSudoMatcherShouldMatch(t *testing.T) {
totalPayload := `Command 1
[sudo] password for user:
Command2`
sm := newSudoMatcher("user")
match := sm.Match([]byte(totalPayload))
if !match {
t.Errorf("Expected a match")
}
}
func TestEnsureSudoMatcherShouldNotMatch(t *testing.T) {
totalPayload := `some othercommand
foo
bar`
sm := newSudoMatcher("user")
match := sm.Match([]byte(totalPayload))
if match {
t.Errorf("No match was expected")
}
}
func TestEnsureSudoMatcherShouldMatchAfterMultipleMatchTries(t *testing.T) {
sm := newSudoMatcher("user")
match := sm.Match([]byte(`Command 1
[sudo] password`))
if match {
t.Errorf("Expected no match")
}
match = sm.Match([]byte(` for user:`))
if !match {
t.Errorf("Expected a match")
}
}