Skip to content

Commit d099bc5

Browse files
committed
chore: prober/multihttp: test with all k6 binaries found in dist
1 parent 6d25add commit d099bc5

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

internal/prober/multihttp/script_test.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"net/http"
77
"net/http/httptest"
8+
"path/filepath"
89
"strings"
910
"testing"
1011

@@ -846,31 +847,34 @@ func TestSettingsToScript(t *testing.T) {
846847
},
847848
}
848849

849-
ctx, cancel := testhelper.Context(context.Background(), t)
850-
t.Cleanup(cancel)
850+
for _, k6path := range testhelper.K6Paths(t) {
851+
t.Run(filepath.Base(k6path), func(t *testing.T) {
852+
ctx, cancel := testhelper.Context(context.Background(), t)
853+
t.Cleanup(cancel)
851854

852-
k6path := testhelper.K6Path(t)
853-
store := testhelper.NoopSecretStore{}
854-
runner, err := k6runner.New(k6runner.RunnerOpts{Uri: k6path})
855-
require.NoError(t, err)
855+
store := testhelper.NoopSecretStore{}
856+
runner, err := k6runner.New(k6runner.RunnerOpts{Uri: k6path})
857+
require.NoError(t, err)
856858

857-
logger := testhelper.Logger(t)
858-
prober, err := NewProber(ctx, check, logger, runner, http.Header{}, &store)
859-
require.NoError(t, err)
859+
logger := testhelper.Logger(t)
860+
prober, err := NewProber(ctx, check, logger, runner, http.Header{}, &store)
861+
require.NoError(t, err)
860862

861-
reg := prometheus.NewPedanticRegistry()
862-
require.NotNil(t, reg)
863+
reg := prometheus.NewPedanticRegistry()
864+
require.NotNil(t, reg)
863865

864-
var buf bytes.Buffer
865-
userLogger := level.NewFilter(kitlog.NewLogfmtLogger(&buf), level.AllowInfo(), level.SquelchNoLevel(false))
866-
require.NotNil(t, userLogger)
866+
var buf bytes.Buffer
867+
userLogger := level.NewFilter(kitlog.NewLogfmtLogger(&buf), level.AllowInfo(), level.SquelchNoLevel(false))
868+
require.NotNil(t, userLogger)
867869

868-
success, duration := prober.Probe(ctx, check.Target, reg, userLogger)
870+
success, duration := prober.Probe(ctx, check.Target, reg, userLogger)
869871

870-
t.Log("Log entries:\n" + buf.String())
872+
t.Log("Log entries:\n" + buf.String())
871873

872-
require.True(t, success)
873-
require.NotEqual(t, 0, duration)
874+
require.True(t, success)
875+
require.NotEqual(t, 0, duration)
876+
})
877+
}
874878
}
875879

876880
func TestReplaceVariablesInString(t *testing.T) {

internal/testhelper/testhelper.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"runtime"
10+
"strings"
1011
"testing"
1112
"time"
1213

@@ -76,13 +77,28 @@ func ModuleDir(t *testing.T) string {
7677
return dir
7778
}
7879

79-
func K6Path(t *testing.T) string {
80+
func K6Paths(t *testing.T) []string {
8081
t.Helper()
8182

82-
k6path := filepath.Join(ModuleDir(t), "dist", runtime.GOOS+"-"+runtime.GOARCH, "sm-k6")
83-
require.FileExistsf(t, k6path, "k6 program must exist at %s", k6path)
83+
dir := filepath.Join(ModuleDir(t), "dist", runtime.GOOS+"-"+runtime.GOARCH)
84+
entries, err := os.ReadDir(dir)
85+
require.NoErrorf(t, err, "reading k6 binaries directory %s", dir)
8486

85-
return k6path
87+
var paths []string
88+
for _, entry := range entries {
89+
if entry.IsDir() || !strings.Contains(entry.Name(), "k6") {
90+
continue
91+
}
92+
info, err := entry.Info()
93+
if err != nil || info.Mode().Perm()&0o111 == 0 {
94+
continue
95+
}
96+
paths = append(paths, filepath.Join(dir, entry.Name()))
97+
}
98+
99+
require.NotEmptyf(t, paths, "no k6 binaries found in %s", dir)
100+
101+
return paths
86102
}
87103

88104
// NoopSecretStore is a test implementation of the SecretProvider interface

0 commit comments

Comments
 (0)