-
Notifications
You must be signed in to change notification settings - Fork 453
/
common.go
153 lines (137 loc) · 4.37 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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package integration
import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"testing"
"time"
"github.com/m3db/m3/src/m3em/agent"
"github.com/stretchr/testify/require"
)
const (
rngSeed = int64(123456789)
defaultUniqueBytes = int64(1024 * 8)
)
type testProgram []byte
var (
// nolint: varcheck
shortLivedTestProgram = testProgram([]byte(`#!/usr/bin/env bash
if [ "$#" -ne 2 ]; then
echo "Args: $@" >&2
echo "Illegal number of arguments" >&2
exit 1
fi
echo -ne "testing random output"`))
// nolint: varcheck
longRunningTestProgram = testProgram([]byte(`#!/usr/bin/env bash
echo -ne "testing random output"
while true; do sleep 1; done
echo -ne "should never get this"`))
)
func newTempFile(t *testing.T, dir string, content []byte) *os.File {
tmpfile, err := ioutil.TempFile(dir, "example")
require.NoError(t, err)
n, err := tmpfile.Write(content)
require.NoError(t, err)
require.Equal(t, len(content), n)
require.NoError(t, tmpfile.Close())
return tmpfile
}
func newTestScript(t *testing.T, dir string, scriptNum int, script testProgram) string {
file, err := ioutil.TempFile(dir, fmt.Sprintf("testscript%d.sh", scriptNum))
require.NoError(t, err)
name := file.Name()
require.NoError(t, file.Chmod(0755))
scriptContents := []byte(script)
numWritten, err := file.Write(scriptContents)
require.NoError(t, err)
require.Equal(t, len(scriptContents), numWritten)
require.NoError(t, file.Close())
return name
}
func newLargeTempFile(t *testing.T, dir string, numBytes int64) *os.File {
if numBytes%defaultUniqueBytes != 0 {
require.FailNow(t, "numBytes must be divisble by %d", defaultUniqueBytes)
}
byteStream := newRandByteStream(t, defaultUniqueBytes)
numIters := numBytes / defaultUniqueBytes
tmpfile, err := ioutil.TempFile(dir, "example-large-file")
require.NoError(t, err)
for i := int64(0); i < numIters; i++ {
n, err := tmpfile.Write(byteStream)
require.NoError(t, err)
require.Equal(t, len(byteStream), n)
}
require.NoError(t, tmpfile.Close())
return tmpfile
}
func newTempDir(t *testing.T) string {
path, err := ioutil.TempDir("", "integration-test")
require.NoError(t, err)
return path
}
func newSubDir(t *testing.T, dir, subdir string) string {
newPath := path.Join(dir, subdir)
err := os.Mkdir(newPath, os.FileMode(0755))
require.NoError(t, err)
return newPath
}
func newRandByteStream(t *testing.T, numBytes int64) []byte {
if mod8 := numBytes % int64(8); mod8 != 0 {
require.FailNow(t, "numBytes must be divisible by 8")
}
var (
buff = bytes.NewBuffer(nil)
num = numBytes / 8
r = rand.New(rand.NewSource(rngSeed))
)
for i := int64(0); i < num; i++ {
n := r.Int63()
err := binary.Write(buff, binary.LittleEndian, n)
require.NoError(t, err)
}
return buff.Bytes()
}
// returns true if agent finished, false otherwise
func waitUntilAgentFinished(a agent.Agent, timeout time.Duration) bool {
start := time.Now()
seenRunning := false
stopped := false
for !stopped {
now := time.Now()
if now.Sub(start) > timeout {
return stopped
}
running := a.Running()
seenRunning = seenRunning || running
stopped = seenRunning && !running
time.Sleep(10 * time.Millisecond)
}
return stopped
}
func testExecGenFn(binary string, config string) (string, []string) {
return binary, []string{"-f", config}
}