Skip to content

Commit

Permalink
Have tests run k6 scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Marcelo E. Magallon <[email protected]>
  • Loading branch information
mem committed Oct 5, 2023
1 parent 031d729 commit 2dd0a74
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
11 changes: 2 additions & 9 deletions internal/prober/multihttp/multihttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"time"

"github.com/grafana/synthetic-monitoring-agent/internal/k6runner"
"github.com/grafana/synthetic-monitoring-agent/internal/testhelper"
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

func TestNewProber(t *testing.T) {
ctx, cancel := testContext(t)
ctx, cancel := testhelper.Context(context.Background(), t)
t.Cleanup(cancel)

logger := zerolog.New(zerolog.NewTestWriter(t))
Expand Down Expand Up @@ -109,11 +110,3 @@ func (noopRunner) WithLogger(logger *zerolog.Logger) k6runner.Runner {
func (noopRunner) Run(ctx context.Context, script []byte) (*k6runner.RunResponse, error) {
return &k6runner.RunResponse{}, nil
}

func testContext(t *testing.T) (context.Context, func()) {
if deadline, ok := t.Deadline(); ok {
return context.WithDeadline(context.Background(), deadline)
}

return context.WithTimeout(context.Background(), 10*time.Second)
}
26 changes: 26 additions & 0 deletions internal/prober/multihttp/script_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package multihttp

import (
"bytes"
"context"
"net/http/httptest"
"strings"
"testing"

kitlog "github.com/go-kit/kit/log" //nolint:staticcheck // TODO(mem): replace in BBE
"github.com/grafana/synthetic-monitoring-agent/internal/k6runner"
"github.com/grafana/synthetic-monitoring-agent/internal/testhelper"
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
"github.com/mccutchen/go-httpbin/v2/httpbin"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -746,6 +753,25 @@ func TestSettingsToScript(t *testing.T) {
actual, err := settingsToScript(settings)
require.NoError(t, err)
require.NotEmpty(t, actual)

check := sm.Check{
Settings: sm.CheckSettings{
Multihttp: settings,
},
}

ctx, cancel := testhelper.Context(context.Background(), t)
t.Cleanup(cancel)
logger := zerolog.New(zerolog.NewTestWriter(t))
runner := k6runner.New("k6")

prober, err := NewProber(ctx, check, logger, runner)
require.NoError(t, err)

reg := prometheus.NewPedanticRegistry()
var buf bytes.Buffer
plogger := kitlog.NewLogfmtLogger(&buf)
prober.Probe(ctx, "foo", reg, plogger)
}

func TestReplaceVariablesInString(t *testing.T) {
Expand Down

0 comments on commit 2dd0a74

Please sign in to comment.