/
common.go
166 lines (147 loc) · 4.12 KB
/
common.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
155
156
157
158
159
160
161
162
163
164
165
166
/*
Copyright 2017 Mirantis
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 framework
import (
"bytes"
"fmt"
"io"
"strings"
"time"
)
const (
NginxImage = "docker.io/nginx:1.14.2"
BusyboxImage = "docker.io/busybox:1.30.0"
)
// ErrTimeout is the timeout error returned from functions wrapped by WithTimeout
var ErrTimeout = fmt.Errorf("timeout")
// CommandError holds an exit code for commands finished without any Executor error
type CommandError struct {
ExitCode int
}
func (e CommandError) Error() string {
return fmt.Sprintf("command finished with %d exit code", e.ExitCode)
}
var _ error = CommandError{}
// Command is the interface to control the command started with an Executor
type Command interface {
Kill() error
Wait() error
}
// Executor is the interface to run shell commands in arbitrary places
type Executor interface {
io.Closer
Run(stdin io.Reader, stdout, stderr io.Writer, command ...string) error
Start(stdin io.Reader, stdout, stderr io.Writer, command ...string) (Command, error)
Logs() (string, error)
}
// Run executes command with the given executor, returns stdout/stderr as strings
// and exit code in CommandError
func Run(executor Executor, input string, command ...string) (string, string, error) {
outBuf := &bytes.Buffer{}
errBuf := &bytes.Buffer{}
inBuf := bytes.NewBufferString(input)
err := executor.Run(inBuf, outBuf, errBuf, command...)
return outBuf.String(), errBuf.String(), err
}
// RunSimple is a simplified version of Run that verifies exit code/stderr internally and returns stdout only
func RunSimple(executor Executor, command ...string) (string, error) {
stdout, stderr, err := Run(executor, "", command...)
if err != nil {
if ce, ok := err.(CommandError); ok {
if ce.ExitCode != 0 {
return "", fmt.Errorf("command exited with code %d, stderr: %s", ce.ExitCode, strings.TrimSpace(stderr)+strings.TrimSpace(stdout))
}
return strings.TrimSpace(stdout), nil
}
return "", err
}
return strings.TrimSpace(stdout), nil
}
func trimBlock(s string) string {
lines := strings.Split(s, "\n")
for i, line := range lines {
lines[i] = strings.TrimSpace(line)
}
return strings.Join(lines, "\n")
}
func waitFor(f func() error, wait, poll time.Duration, waitFailure bool) error {
if poll <= 0 || wait <= 0 {
wait = time.Duration(time.Hour)
poll = 0
}
timeout := time.After(wait)
err := f()
if err == nil && !waitFailure || err != nil && waitFailure {
return err
}
result := err
for {
select {
case <-time.After(poll):
err := f()
if err == nil && !waitFailure || err != nil && waitFailure {
return err
}
result = err
if poll == 0 {
return result
}
case <-timeout:
return result
}
}
}
func waitForConsistentState(f func() error, timing ...time.Duration) error {
if len(timing) == 0 {
panic("timing is not provided")
}
var pollPeriod time.Duration
if len(timing) == 1 || timing[1] <= 0 {
pollPeriod = time.Duration(timing[0].Nanoseconds() / 10)
} else {
pollPeriod = timing[1]
}
var err error
for timing[0] > 0 {
now := time.Now()
if err = waitFor(f, timing[0], pollPeriod, false); err != nil {
timing[0] -= time.Now().Sub(now)
continue
}
if len(timing) >= 2 {
now := time.Now()
if err = waitFor(f, timing[2], pollPeriod, true); err != nil {
timing[0] -= time.Now().Sub(now)
continue
}
}
break
}
return err
}
// WithTimeout adds timeout to synchronous function
func WithTimeout(timeout time.Duration, fn func() error) func() error {
return func() error {
res := make(chan error, 1)
go func() {
res <- fn()
}()
timer := time.After(timeout)
select {
case e := <-res:
return e
case <-timer:
return ErrTimeout
}
}
}