-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
archive_test.go
207 lines (173 loc) · 6.01 KB
/
archive_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
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
package cmd
import (
"archive/tar"
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"go.k6.io/k6/errext/exitcodes"
)
func TestArchiveThresholds(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
noThresholds bool
testFilename string
wantErr bool
}{
{
name: "archive should fail with exit status 104 on a malformed threshold expression",
noThresholds: false,
testFilename: "testdata/thresholds/malformed_expression.js",
wantErr: true,
},
{
name: "archive should on a malformed threshold expression but --no-thresholds flag set",
noThresholds: true,
testFilename: "testdata/thresholds/malformed_expression.js",
wantErr: false,
},
{
name: "run should fail with exit status 104 on a threshold applied to a non existing metric",
noThresholds: false,
testFilename: "testdata/thresholds/non_existing_metric.js",
wantErr: true,
},
{
name: "run should succeed on a threshold applied to a non existing metric with the --no-thresholds flag set",
noThresholds: true,
testFilename: "testdata/thresholds/non_existing_metric.js",
wantErr: false,
},
{
name: "run should succeed on a threshold applied to a non existing submetric with the --no-thresholds flag set",
noThresholds: true,
testFilename: "testdata/thresholds/non_existing_metric.js",
wantErr: false,
},
{
name: "run should fail with exit status 104 on a threshold applying an unsupported aggregation method to a metric",
noThresholds: false,
testFilename: "testdata/thresholds/unsupported_aggregation_method.js",
wantErr: true,
},
{
name: "run should succeed on a threshold applying an unsupported aggregation method to a metric with the --no-thresholds flag set",
noThresholds: true,
testFilename: "testdata/thresholds/unsupported_aggregation_method.js",
wantErr: false,
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
testScript, err := ioutil.ReadFile(testCase.testFilename)
require.NoError(t, err)
testState := newGlobalTestState(t)
require.NoError(t, afero.WriteFile(testState.fs, filepath.Join(testState.cwd, testCase.testFilename), testScript, 0o644))
testState.args = []string{"k6", "archive", testCase.testFilename}
if testCase.noThresholds {
testState.args = append(testState.args, "--no-thresholds")
}
if testCase.wantErr {
testState.expectedExitCode = int(exitcodes.InvalidConfig)
}
newRootCommand(testState.globalState).execute()
})
}
}
func TestArchiveContainsEnv(t *testing.T) {
t.Parallel()
// given some script that will be archived
fileName := "script.js"
testScript := []byte(`export default function () {}`)
testState := newGlobalTestState(t)
require.NoError(t, afero.WriteFile(testState.fs, filepath.Join(testState.cwd, fileName), testScript, 0o644))
// when we do archiving and passing the `--env` flags
testState.args = []string{"k6", "--env", "ENV1=lorem", "--env", "ENV2=ipsum", "archive", fileName}
newRootCommand(testState.globalState).execute()
require.NoError(t, untar(t, testState.fs, "archive.tar", "tmp/"))
data, err := afero.ReadFile(testState.fs, "tmp/metadata.json")
require.NoError(t, err)
metadata := struct {
Env map[string]string
}{}
// then unpacked metadata should contain the environment variables with the proper values
require.NoError(t, json.Unmarshal(data, &metadata))
require.Len(t, metadata.Env, 2)
require.Contains(t, metadata.Env, "ENV1")
require.Contains(t, metadata.Env, "ENV2")
require.Equal(t, "lorem", metadata.Env["ENV1"])
require.Equal(t, "ipsum", metadata.Env["ENV2"])
}
func TestArchiveNotContainsEnv(t *testing.T) {
t.Parallel()
// given some script that will be archived
fileName := "script.js"
testScript := []byte(`export default function () {}`)
testState := newGlobalTestState(t)
require.NoError(t, afero.WriteFile(testState.fs, filepath.Join(testState.cwd, fileName), testScript, 0o644))
// when we do archiving and passing the `--env` flags altogether with `--exclude-env-vars` flag
testState.args = []string{"k6", "--env", "ENV1=lorem", "--env", "ENV2=ipsum", "archive", "--exclude-env-vars", fileName}
newRootCommand(testState.globalState).execute()
require.NoError(t, untar(t, testState.fs, "archive.tar", "tmp/"))
data, err := afero.ReadFile(testState.fs, "tmp/metadata.json")
require.NoError(t, err)
metadata := struct {
Env map[string]string
}{}
// then unpacked metadata should not contain any environment variables passed at the moment of archive creation
require.NoError(t, json.Unmarshal(data, &metadata))
require.Len(t, metadata.Env, 0)
}
// untar untars a `fileName` file to a `destination` path
func untar(t *testing.T, fs afero.Fs, fileName string, destination string) error {
t.Helper()
archiveFile, err := afero.ReadFile(fs, fileName)
if err != nil {
return err
}
reader := bytes.NewBuffer(archiveFile)
tr := tar.NewReader(reader)
for {
header, err := tr.Next()
switch {
case errors.Is(err, io.EOF):
return nil
case err != nil:
return err
case header == nil:
continue
}
// as long as this code in a test helper, we can safely
// omit G305: File traversal when extracting zip/tar archive
target := filepath.Join(destination, header.Name) //nolint:gosec
switch header.Typeflag {
case tar.TypeDir:
if _, err := fs.Stat(target); err != nil && !os.IsNotExist(err) {
return err
}
if err := fs.MkdirAll(target, 0o755); err != nil {
return err
}
case tar.TypeReg:
f, err := fs.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
return err
}
defer func() { _ = f.Close() }()
// as long as this code in a test helper, we can safely
// omit G110: Potential DoS vulnerability via decompression bomb
if _, err := io.Copy(f, tr); err != nil { //nolint:gosec
return err
}
}
}
}