Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix or skip broken unit tests, run them in CI #14

Open
wants to merge 6 commits into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/push-pr-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ jobs:
# No point in running tests for foreign architectures we cannot run.
if: steps.runner-info.outputs.native == 'true'
run: |-
# TODO: Run also unit tests. They are currently broken and need fixing.
go test ./integration/...
go test ./...

- name: Upload artifact to release
if: github.event_name == 'release'
Expand Down
83 changes: 61 additions & 22 deletions output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package sm

import (
"bytes"
"io"
"strings"
"testing"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"go.k6.io/k6/metrics"
"go.k6.io/k6/output"
)

func TestOutputNew(t *testing.T) {
t.Parallel()

testcases := map[string]struct {
input output.Params
expectError bool
Expand Down Expand Up @@ -45,14 +49,18 @@ func TestOutputNew(t *testing.T) {
}

func TestOutputDescription(t *testing.T) {
t.Parallel()

var out Output
require.NotEmpty(t, out.Description())
}

func TestOutputStart(t *testing.T) {
func TestOutputStartStop(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()

out, err := New(output.Params{ConfigArgument: "test.out", FS: fs})
out, err := New(output.Params{ConfigArgument: "test.out", FS: fs, Logger: nopLogger()})
require.NoError(t, err)

err = out.Start()
Expand All @@ -61,30 +69,13 @@ func TestOutputStart(t *testing.T) {
err = out.Stop()
require.NoError(t, err)

// At this point we should have an empty file.
fi, err := fs.Stat("test.out")
require.NoError(t, err)
require.Equal(t, int64(0), fi.Size())
}

// TestOutputStop tests that the metrics are correctly collected and written to the file.
func TestOutputStop(t *testing.T) {
fs := afero.NewMemMapFs()

out, err := New(output.Params{ConfigArgument: "test.out", FS: fs})
require.NoError(t, err)

err = out.Start()
fileOut, err := fs.Open("test.out")
require.NoError(t, err)

// TODO(mem): add samples

err = out.Stop()
output, err := io.ReadAll(fileOut)
require.NoError(t, err)

fi, err := fs.Stat("test.out")
require.NoError(t, err)
require.Equal(t, int64(0), fi.Size())
require.Contains(t, string(output), "probe_script_duration_seconds")
}

func makeSample(name string, value float64) metrics.Sample {
Expand All @@ -99,6 +90,7 @@ func makeSample(name string, value float64) metrics.Sample {
}

func TestDeriveMetricNameAndValue(t *testing.T) {
t.Parallel()

testcases := map[string]struct {
input metrics.Sample
Expand Down Expand Up @@ -138,7 +130,11 @@ func TestDeriveMetricNameAndValue(t *testing.T) {
}

for name, tc := range testcases {
tc := tc

t.Run(name, func(t *testing.T) {
t.Parallel()

actualName, actualValue := deriveMetricNameAndValue(tc.input)
require.Equal(t, tc.expectedName, actualName)
require.Equal(t, tc.expectedValue, actualValue)
Expand All @@ -147,6 +143,8 @@ func TestDeriveMetricNameAndValue(t *testing.T) {
}

func TestGetStats(t *testing.T) {
t.Parallel()

testcases := map[string]struct {
input []float64
expected stats
Expand Down Expand Up @@ -174,7 +172,11 @@ func TestGetStats(t *testing.T) {
}

for name, tc := range testcases {
tc := tc

t.Run(name, func(t *testing.T) {
t.Parallel()

actual := getStats(tc.input)
if tc.expected != actual {
t.Log("expected:", tc.expected, "actual:", actual)
Expand All @@ -185,6 +187,8 @@ func TestGetStats(t *testing.T) {
}

func TestIsValidMetricName(t *testing.T) {
t.Parallel()

testcases := map[string]struct {
input string
expected bool
Expand All @@ -208,7 +212,11 @@ func TestIsValidMetricName(t *testing.T) {
}

for name, tc := range testcases {
tc := tc

t.Run(name, func(t *testing.T) {
t.Parallel()

actual := isValidMetricNameRe(tc.input)
if actual != tc.expected {
t.Log("expected:", tc.expected, "actual:", actual, "input:", tc.input)
Expand All @@ -225,6 +233,8 @@ func TestIsValidMetricName(t *testing.T) {
}

func TestSanitizeLabelName(t *testing.T) {
t.Parallel()

testcases := map[string]struct {
input string
expected string
Expand Down Expand Up @@ -258,6 +268,8 @@ func TestSanitizeLabelName(t *testing.T) {
}

func TestBufferedMetricTextOutputValue(t *testing.T) {
t.Parallel()

type metricData struct {
name string
kvs []string
Expand Down Expand Up @@ -338,7 +350,11 @@ func TestBufferedMetricTextOutputValue(t *testing.T) {
}

for name, tc := range testcases {
tc := tc

t.Run(name, func(t *testing.T) {
t.Parallel()

var buf bytes.Buffer
to := newBufferedMetricTextOutput(&buf, tc.kvs...)
for _, d := range tc.data {
Expand All @@ -358,6 +374,8 @@ func joinNewline(s ...string) string {
}

func TestBufferedMetricTextOutputStats(t *testing.T) {
t.Parallel()

type metricData struct {
name string
kvs []string
Expand Down Expand Up @@ -495,6 +513,8 @@ func TestBufferedMetricTextOutputStats(t *testing.T) {
}

func TestTargetMetricsCollectionWriteOne(t *testing.T) {
t.Parallel()

c := newTargetMetricsCollection()

require.Len(t, c, 0)
Expand Down Expand Up @@ -528,6 +548,8 @@ func TestTargetMetricsCollectionWriteOne(t *testing.T) {
c.Write(&buf)

expected := joinNewline(
`probe_http_got_expected_response{url="http://example.com",method="GET",scenario="s",group="g"} 1`,
`probe_http_error_code{url="http://example.com",method="GET",scenario="s",group="g"} 0`,
`probe_http_info{tls_version="1.3",proto="1.1",k="v",url="http://example.com",method="GET",scenario="s",group="g"} 1`,
`probe_http_requests_total{url="http://example.com",method="GET",scenario="s",group="g"} 1`,
`probe_http_requests_failed_total{url="http://example.com",method="GET",scenario="s",group="g"} 0`,
Expand All @@ -539,12 +561,15 @@ func TestTargetMetricsCollectionWriteOne(t *testing.T) {
`probe_http_duration_seconds{phase="tls",url="http://example.com",method="GET",scenario="s",group="g"} 0.001`,
`probe_http_duration_seconds{phase="processing",url="http://example.com",method="GET",scenario="s",group="g"} 0.001`,
`probe_http_duration_seconds{phase="transfer",url="http://example.com",method="GET",scenario="s",group="g"} 0.001`,
`probe_http_total_duration_seconds{url="http://example.com",method="GET",scenario="s",group="g"} 0.001`,
)

require.Equal(t, expected, buf.String())
}

func TestTargetMetricsCollectionWriteMany(t *testing.T) {
t.Parallel()

c := newTargetMetricsCollection()

require.Len(t, c, 0)
Expand Down Expand Up @@ -578,6 +603,8 @@ func TestTargetMetricsCollectionWriteMany(t *testing.T) {
c.Write(&buf)

expected := joinNewline(
`probe_http_got_expected_response{url="http://example.com",method="GET",scenario="s",group="g"} 1`,
`probe_http_error_code{url="http://example.com",method="GET",scenario="s",group="g"} 0`,
`probe_http_info{tls_version="1.3",proto="1.1",k="v",url="http://example.com",method="GET",scenario="s",group="g"} 1`,
`probe_http_requests_total{url="http://example.com",method="GET",scenario="s",group="g"} 2`,
`probe_http_requests_failed_total{url="http://example.com",method="GET",scenario="s",group="g"} 1`,
Expand Down Expand Up @@ -609,7 +636,19 @@ func TestTargetMetricsCollectionWriteMany(t *testing.T) {
`probe_http_duration_seconds{phase="transfer",url="http://example.com",method="GET",scenario="s",group="g"} 0.001`,
`probe_http_duration_seconds_count{phase="transfer",url="http://example.com",method="GET",scenario="s",group="g"} 2`,
`probe_http_duration_seconds_sum{phase="transfer",url="http://example.com",method="GET",scenario="s",group="g"} 0.002`,
`probe_http_total_duration_seconds_min{url="http://example.com",method="GET",scenario="s",group="g"} 0.001`,
`probe_http_total_duration_seconds_max{url="http://example.com",method="GET",scenario="s",group="g"} 0.001`,
`probe_http_total_duration_seconds{url="http://example.com",method="GET",scenario="s",group="g"} 0.001`,
`probe_http_total_duration_seconds_count{url="http://example.com",method="GET",scenario="s",group="g"} 2`,
`probe_http_total_duration_seconds_sum{url="http://example.com",method="GET",scenario="s",group="g"} 0.002`,
)

require.Equal(t, expected, buf.String())
}

func nopLogger() *logrus.Logger {
l := logrus.New()
l.SetOutput(io.Discard)

return l
}