-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec_os_test.go
More file actions
105 lines (101 loc) · 2.4 KB
/
exec_os_test.go
File metadata and controls
105 lines (101 loc) · 2.4 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
package main
import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"testing"
"time"
)
func TestOsExists(t *testing.T) {
t.Parallel()
runner := OsRunner{}
conf := SrvConfig{}
conf.Timeout = time.Duration(1000_000_000)
tmpd, err := os.MkdirTemp("", "")
if err != nil {
t.Error("tmpdir", err)
}
defer os.RemoveAll(tmpd)
conf.BaseDir = tmpd
ctx := context.Background()
_, _, err = runner.Exists(conf, "notexists", ctx)
if err == nil {
t.Error("not error", err)
}
if err = os.WriteFile(filepath.Join(tmpd, "exists"), []byte(""), 0755); err != nil {
t.Error("writefile", err)
}
res1, res2, err := runner.Exists(conf, "exists", ctx)
if err != nil {
t.Error("not error", err)
}
if res1 != "exists" {
t.Error("mismatch(script)", res1)
}
if res2 != "" {
t.Error("mismatch(pathinfo)", res2)
}
res1_2, res2_2, err := runner.Exists(conf, "exists/hello/world", ctx)
if err != nil {
t.Error("not error", err)
}
if res1_2 != "exists" {
t.Error("mismatch(script)", res1)
}
if res2_2 != "/hello/world" {
t.Error("mismatch(pathinfo)", res2)
}
}
func TestOsRun(t *testing.T) {
t.Parallel()
runner := OsRunner{}
conf := SrvConfig{}
conf.Timeout = time.Duration(1000_000_000)
tmpd, err := os.MkdirTemp("/var/tmp", "")
if err != nil {
t.Error("tmpdir", err)
}
conf.BaseDir = tmpd
ctx := context.Background()
if err = os.WriteFile(filepath.Join(tmpd, "cmd1"), []byte("#! /bin/sh\n"), 0755); err != nil {
t.Error("writefile", err)
}
env := map[string]string{}
stdin := io.NopCloser(&bytes.Buffer{})
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
if err = runner.Run(conf, "cmd1", env, stdin, stdout, stderr, ctx); err != nil {
t.Error("error", err)
}
if stdout.Len() != 0 {
t.Error("out", stdout.String())
}
if stderr.Len() != 0 {
t.Error("out", stderr.String())
}
}
func TestOsRunTimeout(t *testing.T) {
t.Parallel()
runner := OsRunner{}
conf := SrvConfig{}
tmpd, err := os.MkdirTemp("", "")
if err != nil {
t.Error("tmpdir", err)
}
defer os.RemoveAll(tmpd)
conf.BaseDir = tmpd
ctx := context.Background()
if err = os.WriteFile(filepath.Join(tmpd, "cmd1"), []byte("#! /bin/sh\nsleep 10"), 0755); err != nil {
t.Error("writefile", err)
}
env := map[string]string{}
stdin := io.NopCloser(&bytes.Buffer{})
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
err = runner.Run(conf, "cmd1", env, stdin, stdout, stderr, ctx)
if err == nil {
t.Error("no timeout ?")
}
}