File: exec_test.go

package info (click to toggle)
golang-k8s-utils 0.0~git20250321.1f6e0b7-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,012 kB
  • sloc: sh: 245; makefile: 27
file content (252 lines) | stat: -rw-r--r-- 5,987 bytes parent folder | download | duplicates (2)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package exec

import (
	"context"
	"io"
	"io/ioutil"
	"os"
	osexec "os/exec"
	"testing"
	"time"
)

func TestExecutorNoArgs(t *testing.T) {
	ex := New()

	cmd := ex.Command("true")
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Errorf("expected success, got %v", err)
	}
	if len(out) != 0 {
		t.Errorf("expected no output, got %q", string(out))
	}

	cmd = ex.Command("false")
	out, err = cmd.CombinedOutput()
	if err == nil {
		t.Errorf("expected failure, got nil error")
	}
	if len(out) != 0 {
		t.Errorf("expected no output, got %q", string(out))
	}
	ee, ok := err.(ExitError)
	if !ok {
		t.Errorf("expected an ExitError, got %+v", err)
	}
	if ee.Exited() {
		if code := ee.ExitStatus(); code != 1 {
			t.Errorf("expected exit status 1, got %d", code)
		}
	}

	cmd = ex.Command("/does/not/exist")
	_, err = cmd.CombinedOutput()
	if err == nil {
		t.Errorf("expected failure, got nil error")
	}
	if ee, ok := err.(ExitError); ok {
		t.Errorf("expected non-ExitError, got %+v", ee)
	}
}

func TestExecutorWithArgs(t *testing.T) {
	ex := New()

	cmd := ex.Command("echo", "stdout")
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Errorf("expected success, got %+v", err)
	}
	if string(out) != "stdout\n" {
		t.Errorf("unexpected output: %q", string(out))
	}

	cmd = ex.Command("/bin/sh", "-c", "echo stderr > /dev/stderr")
	out, err = cmd.CombinedOutput()
	if err != nil {
		t.Errorf("expected success, got %+v", err)
	}
	if string(out) != "stderr\n" {
		t.Errorf("unexpected output: %q", string(out))
	}
}

func TestLookPath(t *testing.T) {
	ex := New()

	shExpected, _ := osexec.LookPath("sh")
	sh, _ := ex.LookPath("sh")
	if sh != shExpected {
		t.Errorf("unexpected result for LookPath: got %s, expected %s", sh, shExpected)
	}
}

func TestExecutableNotFound(t *testing.T) {
	exec := New()

	cmd := exec.Command("fake_executable_name")
	_, err := cmd.CombinedOutput()
	if err != ErrExecutableNotFound {
		t.Errorf("cmd.CombinedOutput(): Expected error ErrExecutableNotFound but got %v", err)
	}

	cmd = exec.Command("fake_executable_name")
	_, err = cmd.Output()
	if err != ErrExecutableNotFound {
		t.Errorf("cmd.Output(): Expected error ErrExecutableNotFound but got %v", err)
	}

	cmd = exec.Command("fake_executable_name")
	err = cmd.Run()
	if err != ErrExecutableNotFound {
		t.Errorf("cmd.Run(): Expected error ErrExecutableNotFound but got %v", err)
	}
}

func TestStopBeforeStart(t *testing.T) {
	cmd := New().Command("echo", "hello")

	// no panic calling Stop before calling Run
	cmd.Stop()

	cmd.Run()

	// no panic calling Stop after command is done
	cmd.Stop()
}

func TestTimeout(t *testing.T) {
	exec := New()
	ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
	defer cancel()

	err := exec.CommandContext(ctx, "sleep", "2").Run()
	if err != context.DeadlineExceeded {
		t.Errorf("expected %v but got %v", context.DeadlineExceeded, err)
	}
}

func TestSetEnv(t *testing.T) {
	ex := New()

	out, err := ex.Command("/bin/sh", "-c", "echo $FOOBAR").CombinedOutput()
	if err != nil {
		t.Errorf("expected success, got %+v", err)
	}
	if string(out) != "\n" {
		t.Errorf("unexpected output: %q", string(out))
	}

	cmd := ex.Command("/bin/sh", "-c", "echo $FOOBAR")
	cmd.SetEnv([]string{"FOOBAR=baz"})
	out, err = cmd.CombinedOutput()
	if err != nil {
		t.Errorf("expected success, got %+v", err)
	}
	if string(out) != "baz\n" {
		t.Errorf("unexpected output: %q", string(out))
	}
}

func TestStdIOPipes(t *testing.T) {
	cmd := New().Command("/bin/sh", "-c", "echo 'OUT'>&1; echo 'ERR'>&2")

	stdoutPipe, err := cmd.StdoutPipe()
	if err != nil {
		t.Fatalf("expected StdoutPipe() not to error, got: %v", err)
	}
	stderrPipe, err := cmd.StderrPipe()
	if err != nil {
		t.Fatalf("expected StderrPipe() not to error, got: %v", err)
	}

	stdout := make(chan string)
	stderr := make(chan string)

	go func() {
		stdout <- readAll(t, stdoutPipe, "StdOut")
	}()
	go func() {
		stderr <- readAll(t, stderrPipe, "StdErr")
	}()

	if err := cmd.Start(); err != nil {
		t.Errorf("expected Start() not to error, got: %v", err)
	}

	if e, a := "OUT\n", <-stdout; e != a {
		t.Errorf("expected StdOut to be '%s', got: '%v'", e, a)
	}

	if e, a := "ERR\n", <-stderr; e != a {
		t.Errorf("expected StdErr to be '%s', got: '%v'", e, a)
	}

	if err := cmd.Wait(); err != nil {
		t.Errorf("expected Wait() not to error, got: %v", err)
	}
}

func readAll(t *testing.T, r io.Reader, n string) string {
	t.Helper()

	b, err := ioutil.ReadAll(r)
	if err != nil {
		t.Fatalf("unexpected error when reading from %s: %v", n, err)
	}

	return string(b)
}

func TestExecutorGo119LookPath(t *testing.T) {
	orig := os.Getenv("PATH")
	defer func() { os.Setenv("PATH", orig) }()

	os.Setenv("PATH", "./testdata")

	ex := New()
	path, err := ex.LookPath("hello")
	if err != nil {
		t.Errorf("expected success, got %v", err)
	}
	if path != "testdata/hello" {
		t.Errorf("expected relative path to hello script, got %v", path)
	}

	cmd := ex.Command("hello")
	out, err := cmd.CombinedOutput()
	if err != nil {
		t.Errorf("expected success, got %v", err)
	} else {
		if len(out) != 5 {
			t.Errorf("expected 'hello' output, got %q", string(out))
		}
	}

	cmd = ex.CommandContext(context.Background(), "hello")
	out, err = cmd.CombinedOutput()
	if err != nil {
		t.Errorf("expected success, got %v", err)
	} else {
		if len(out) != 5 {
			t.Errorf("expected 'hello' output, got %q", string(out))
		}
	}
}