From 894ea964f658a4a1791f5667c7e4fd78940125bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Hedenstr=C3=B6m?= <erik@hedenstroem.com> Date: Sat, 18 May 2024 22:39:15 +0000 Subject: [PATCH] Added a proper mockcontainer for testing --- go.mod | 1 + go.sum | 10 ++++++++-- snok.go | 4 ++-- snok_test.go | 35 ++++++++++++++++++++++++----------- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 165a429..ff64a67 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.9.0 github.com/testcontainers/testcontainers-go v0.31.0 + github.com/testcontainers/testcontainers-go/modules/mockserver v0.31.0 ) require ( diff --git a/go.sum b/go.sum index a1f89f9..ac34cc9 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9 github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8= github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/BraspagDevelopers/mock-server-client v0.2.2 h1:Zro0OonNeaDwkkQGIxeJQfYweNKZ+m+8QIlDZAFRc/4= +github.com/BraspagDevelopers/mock-server-client v0.2.2/go.mod h1:LHulrZSfbCNeS/CoycaWdhE495FnyeI3iXm6+4Zjz5c= github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/Microsoft/hcsshim v0.11.4 h1:68vKo2VN8DE9AdN4tnkWnmdhqdbpUFM8OF3Airm7fz8= @@ -40,6 +42,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-resty/resty/v2 v2.3.0 h1:JOOeAvjSlapTT92p8xiS19Zxev1neGikoHsXJeOq8So= +github.com/go-resty/resty/v2 v2.3.0/go.mod h1:UpN9CgLZNsv4e9XG50UU8xdI0F43UQ4HmxLBDwaroHU= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= @@ -112,6 +116,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/testcontainers/testcontainers-go v0.31.0 h1:W0VwIhcEVhRflwL9as3dhY6jXjVCA27AkmbnZ+UTh3U= github.com/testcontainers/testcontainers-go v0.31.0/go.mod h1:D2lAoA0zUFiSY+eAflqK5mcUx/A5hrrORaEQrd0SefI= +github.com/testcontainers/testcontainers-go/modules/mockserver v0.31.0 h1:e5D1bRBs47TA0FeF9s3xUAXFOYNz8uTyVFAWrIcu6c0= +github.com/testcontainers/testcontainers-go/modules/mockserver v0.31.0/go.mod h1:BZCUIxWSnTx8riMhTwjjuyyW8IKZSD84ZjqiXeD7FHA= github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU= github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI= github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk= @@ -149,8 +155,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= -golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/snok.go b/snok.go index 8d885b7..ca923af 100644 --- a/snok.go +++ b/snok.go @@ -142,9 +142,9 @@ func (ct *CommandTest) executeTest(t *testing.T, test test, args []string) { if strings.HasPrefix(*test.Output, "/") && strings.HasSuffix(*test.Output, "/") { expr := (*test.Output)[1 : len(*test.Output)-1] re := regexp.MustCompile(expr) - require.True(t, re.MatchString(output), "Expected output to match %s, got %s", expr, output) + require.True(t, re.MatchString(output), "Expected output to match /%s/, got '%s'", expr, output) } else { - require.Equal(t, *test.Output, output, "Expected output %s, got %s", *test.Output, output) + require.Equal(t, *test.Output, output, "Expected output '%s', got '%s'", *test.Output, output) } } }) diff --git a/snok_test.go b/snok_test.go index 6ce7391..24ca046 100644 --- a/snok_test.go +++ b/snok_test.go @@ -1,6 +1,7 @@ package snok import ( + "context" "errors" "fmt" "io" @@ -12,6 +13,7 @@ import ( "github.com/spf13/cobra" "github.com/stretchr/testify/require" "github.com/testcontainers/testcontainers-go" + "github.com/testcontainers/testcontainers-go/modules/mockserver" ) var ct *CommandTest @@ -37,13 +39,24 @@ func TestCmds(t *testing.T) { } func mockContainer(ct *CommandTest, t *testing.T) (err error) { - if os.Getenv("TEST_MESSAGE") == "" { - log := testcontainers.Log{ - LogType: "test", - Content: []byte("testing"), + if os.Getenv("MOCKSERVER_URL") == "" { + ctx := context.Background() + opts := []testcontainers.ContainerCustomizer{ + testcontainers.WithImage("mockserver/mockserver:5.15.0"), + testcontainers.WithLogConsumers(ct), } - ct.Accept(log) - t.Setenv("TEST_MESSAGE", "testing") + mockserverContainer, err := mockserver.RunContainer(ctx, opts...) + if err != nil && strings.Contains(err.Error(), "Cannot connect to the Docker daemon") { + t.Skip("Docker not available") + } + require.NoError(t, err) + t.Cleanup(func() { + require.NoError(t, mockserverContainer.Terminate(ctx)) + }) + url, err := mockserverContainer.URL(ctx) + require.NoError(t, err) + t.Setenv("MOCKSERVER_URL", url) + echoCmd.Annotations["tests"] = strings.ReplaceAll(echoCmd.Annotations["tests"], "http://localhost", url) } return } @@ -77,9 +90,9 @@ var envCmd = &cobra.Command{ "order": "1", "tests": `[ { - "name": "Check Container Message", - "args": ["TEST_MESSAGE"], - "output": "testing" + "name": "Check Container URL", + "args": ["MOCKSERVER_URL"], + "output": "/^http:\\/\\/localhost:\\d+$/" } ]`, }, @@ -129,8 +142,8 @@ var echoCmd = &cobra.Command{ { "name": "Echo Input (http)", "args": ["test"], - "input": "https://www.example.com/", - "output": "/<html>/" + "input": "http://localhost/mockserver/dashboard", + "output": "/^test <!doctype html><html lang=\"en\">.*<\\/html>/" }, { "name": "Echo Input (ftp)", -- GitLab