From e4c06d7a13bc11ba30af48fe126376a77aed37a3 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Thu, 1 Feb 2024 21:30:04 -0500 Subject: [PATCH] Add test for invalid auth Builds upon the test contexts added here: https://github.com/grafana/grizzly/pull/323 Closes https://github.com/grafana/grizzly/issues/104 This PR adds an integration test that an appropriate message is shown when invalid auth is provided. I needed to update the client because the errors were not showing up correctly in the current version --- go.mod | 23 +++++----- go.sum | 46 ++++++++++--------- integration/context_test.go | 6 +-- integration/dashboard_test.go | 32 ++++++++++++- .../testdata/contexts/get-contexts.txt | 1 + .../{ReciqtgGk.json => ReciqtgGk.yml} | 0 integration/utils_test.go | 46 +++++++++++++------ 7 files changed, 103 insertions(+), 51 deletions(-) rename integration/testdata/dashboards/{ReciqtgGk.json => ReciqtgGk.yml} (100%) diff --git a/go.mod b/go.mod index 6b15ef22..10bd6405 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,10 @@ require ( github.com/fatih/color v1.15.0 github.com/gdamore/tcell v1.4.0 github.com/go-clix/cli v0.2.0 - github.com/go-openapi/runtime v0.26.2 + github.com/go-openapi/runtime v0.27.1 github.com/gobwas/glob v0.2.3 github.com/google/go-jsonnet v0.20.0 - github.com/grafana/grafana-openapi-client-go v0.0.0-20231219151618-11c46332acad + github.com/grafana/grafana-openapi-client-go v0.0.0-20240131162504-9263d72bd697 github.com/grafana/synthetic-monitoring-agent v0.16.5 github.com/grafana/synthetic-monitoring-api-go-client v0.7.0 github.com/grafana/tanka v0.25.0 @@ -35,18 +35,18 @@ require ( github.com/gdamore/encoding v1.0.0 // indirect github.com/go-logr/logr v1.3.0 // indirect github.com/go-logr/stdr v1.2.2 // indirect - github.com/go-openapi/analysis v0.21.5 // indirect + github.com/go-openapi/analysis v0.22.0 // indirect github.com/go-openapi/errors v0.21.0 // indirect - github.com/go-openapi/jsonpointer v0.20.1 // indirect - github.com/go-openapi/jsonreference v0.20.3 // indirect - github.com/go-openapi/loads v0.21.3 // indirect - github.com/go-openapi/spec v0.20.12 // indirect - github.com/go-openapi/strfmt v0.21.10 // indirect - github.com/go-openapi/swag v0.22.5 // indirect - github.com/go-openapi/validate v0.22.4 // indirect + github.com/go-openapi/jsonpointer v0.20.2 // indirect + github.com/go-openapi/jsonreference v0.20.4 // indirect + github.com/go-openapi/loads v0.21.5 // indirect + github.com/go-openapi/spec v0.20.13 // indirect + github.com/go-openapi/strfmt v0.22.0 // indirect + github.com/go-openapi/swag v0.22.9 // indirect + github.com/go-openapi/validate v0.22.6 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.3 // indirect - github.com/google/uuid v1.4.0 // indirect + github.com/google/uuid v1.5.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/hcl v1.0.0 // indirect @@ -86,6 +86,7 @@ require ( go.uber.org/multierr v1.9.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/net v0.17.0 // indirect + golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/term v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/go.sum b/go.sum index 4455ab3b..2574e6d6 100644 --- a/go.sum +++ b/go.sum @@ -89,26 +89,26 @@ github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= 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-openapi/analysis v0.21.5 h1:3tHfEBh6Ia8eKc4M7khOGjPOAlWKJ10d877Cr9teujI= -github.com/go-openapi/analysis v0.21.5/go.mod h1:25YcZosX9Lwz2wBsrFrrsL8bmjjXdlyP6zsr2AMy29M= +github.com/go-openapi/analysis v0.22.0 h1:wQ/d07nf78HNj4u+KiSY0sT234IAyePPbMgpUjUJQR0= +github.com/go-openapi/analysis v0.22.0/go.mod h1:acDnkkCI2QxIo8sSIPgmp1wUlRohV7vfGtAIVae73b0= github.com/go-openapi/errors v0.21.0 h1:FhChC/duCnfoLj1gZ0BgaBmzhJC2SL/sJr8a2vAobSY= github.com/go-openapi/errors v0.21.0/go.mod h1:jxNTMUxRCKj65yb/okJGEtahVd7uvWnuWfj53bse4ho= -github.com/go-openapi/jsonpointer v0.20.1 h1:MkK4VEIEZMj4wT9PmjaUmGflVBr9nvud4Q4UVFbDoBE= -github.com/go-openapi/jsonpointer v0.20.1/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs= -github.com/go-openapi/jsonreference v0.20.3 h1:EjGcjTW8pD1mRis6+w/gmoBdqv5+RbE9B85D1NgDOVQ= -github.com/go-openapi/jsonreference v0.20.3/go.mod h1:FviDZ46i9ivh810gqzFLl5NttD5q3tSlMLqLr6okedM= -github.com/go-openapi/loads v0.21.3 h1:8sSH2FIm/SnbDUGv572md4YqVMFne/a9Eubvcd3anew= -github.com/go-openapi/loads v0.21.3/go.mod h1:Y3aMR24iHbKHppOj91nQ/SHc0cuPbAr4ndY4a02xydc= -github.com/go-openapi/runtime v0.26.2 h1:elWyB9MacRzvIVgAZCBJmqTi7hBzU0hlKD4IvfX0Zl0= -github.com/go-openapi/runtime v0.26.2/go.mod h1:O034jyRZ557uJKzngbMDJXkcKJVzXJiymdSfgejrcRw= -github.com/go-openapi/spec v0.20.12 h1:cgSLbrsmziAP2iais+Vz7kSazwZ8rsUZd6TUzdDgkVI= -github.com/go-openapi/spec v0.20.12/go.mod h1:iSCgnBcwbMW9SfzJb8iYynXvcY6C/QFrI7otzF7xGM4= -github.com/go-openapi/strfmt v0.21.10 h1:JIsly3KXZB/Qf4UzvzJpg4OELH/0ASDQsyk//TTBDDk= -github.com/go-openapi/strfmt v0.21.10/go.mod h1:vNDMwbilnl7xKiO/Ve/8H8Bb2JIInBnH+lqiw6QWgis= -github.com/go-openapi/swag v0.22.5 h1:fVS63IE3M0lsuWRzuom3RLwUMVI2peDH01s6M70ugys= -github.com/go-openapi/swag v0.22.5/go.mod h1:Gl91UqO+btAM0plGGxHqJcQZ1ZTy6jbmridBTsDy8A0= -github.com/go-openapi/validate v0.22.4 h1:5v3jmMyIPKTR8Lv9syBAIRxG6lY0RqeBPB1LKEijzk8= -github.com/go-openapi/validate v0.22.4/go.mod h1:qm6O8ZIcPVdSY5219468Jv7kBdGvkiZLPOmqnqTUZ2A= +github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q= +github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs= +github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdXSSgNeAhojU= +github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4= +github.com/go-openapi/loads v0.21.5 h1:jDzF4dSoHw6ZFADCGltDb2lE4F6De7aWSpe+IcsRzT0= +github.com/go-openapi/loads v0.21.5/go.mod h1:PxTsnFBoBe+z89riT+wYt3prmSBP6GDAQh2l9H1Flz8= +github.com/go-openapi/runtime v0.27.1 h1:ae53yaOoh+fx/X5Eaq8cRmavHgDma65XPZuvBqvJYto= +github.com/go-openapi/runtime v0.27.1/go.mod h1:fijeJEiEclyS8BRurYE1DE5TLb9/KZl6eAdbzjsrlLU= +github.com/go-openapi/spec v0.20.13 h1:XJDIN+dLH6vqXgafnl5SUIMnzaChQ6QTo0/UPMbkIaE= +github.com/go-openapi/spec v0.20.13/go.mod h1:8EOhTpBoFiask8rrgwbLC3zmJfz4zsCUueRuPM6GNkw= +github.com/go-openapi/strfmt v0.22.0 h1:Ew9PnEYc246TwrEspvBdDHS4BVKXy/AOVsfqGDgAcaI= +github.com/go-openapi/strfmt v0.22.0/go.mod h1:HzJ9kokGIju3/K6ap8jL+OlGAbjpSv27135Yr9OivU4= +github.com/go-openapi/swag v0.22.9 h1:XX2DssF+mQKM2DHsbgZK74y/zj4mo9I99+89xUmuZCE= +github.com/go-openapi/swag v0.22.9/go.mod h1:3/OXnFfnMAwBD099SwYRk7GD3xOrr1iL7d/XNLXVVwE= +github.com/go-openapi/validate v0.22.6 h1:+NhuwcEYpWdO5Nm4bmvhGLW0rt1Fcc532Mu3wpypXfo= +github.com/go-openapi/validate v0.22.6/go.mod h1:eaddXSqKeTg5XpSmj1dYyFTK/95n/XHwcOY+BMxKMyM= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= @@ -175,13 +175,13 @@ github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLe github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= -github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= -github.com/grafana/grafana-openapi-client-go v0.0.0-20231219151618-11c46332acad h1:OuTuIuc+D5JwvNOQGbReML1UyxcMP34qWYGpA44IqLY= -github.com/grafana/grafana-openapi-client-go v0.0.0-20231219151618-11c46332acad/go.mod h1:Rvw2Gi7zbybXpAOKRdRhN28cSxUQTSgHvwHkBb1SyLo= +github.com/grafana/grafana-openapi-client-go v0.0.0-20240131162504-9263d72bd697 h1:pqQoKCVWXVtPwgCjFcCtuWiElkSNv1a97uUjiYEUl0k= +github.com/grafana/grafana-openapi-client-go v0.0.0-20240131162504-9263d72bd697/go.mod h1:EapKj5Z1OKDssvRofVwD8XEuJtZUaysLWPFFM2QoK0Q= github.com/grafana/synthetic-monitoring-agent v0.16.5 h1:aKspOWUQyQAH1jfzTae8+Buks+TJR3IrhTLFhWRTmG8= github.com/grafana/synthetic-monitoring-agent v0.16.5/go.mod h1:nvUY4lgjsX9MQAbsYvIhWSECcpL6i7Ml/9PXkJj3KWw= github.com/grafana/synthetic-monitoring-api-go-client v0.7.0 h1:3ZfQzmXDBPcQTTgMAIIiTw5Dwxm/B4lzf34Sto0d0YY= @@ -446,6 +446,8 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/integration/context_test.go b/integration/context_test.go index 73bf407a..a4fa6267 100644 --- a/integration/context_test.go +++ b/integration/context_test.go @@ -12,9 +12,9 @@ func TestContexts(t *testing.T) { TestDir: "testdata/contexts", Commands: []Command{ { - Command: "config get-contexts", - ExpectedCode: 0, - ExpectedOutput: "get-contexts.txt", + Command: "config get-contexts", + ExpectedCode: 0, + ExpectedOutputFile: "get-contexts.txt", }, }, }) diff --git a/integration/dashboard_test.go b/integration/dashboard_test.go index ec307a1d..f894b8ed 100644 --- a/integration/dashboard_test.go +++ b/integration/dashboard_test.go @@ -14,9 +14,37 @@ func TestDashboard(t *testing.T) { RunOnContexts: allContexts, Commands: []Command{ { - Command: "get Dashboard.ReciqtgGk", + Command: "get Dashboard.ReciqtgGk", + ExpectedCode: 0, + ExpectedOutputFile: "ReciqtgGk.yml", + }, + }, + }) + }) + + t.Run("Diff dashboard - success", func(t *testing.T) { + runTest(t, GrizzlyTest{ + TestDir: dir, + RunOnContexts: allContexts, + Commands: []Command{ + { + Command: "diff ReciqtgGk.yml", ExpectedCode: 0, - ExpectedOutput: "ReciqtgGk.json", + ExpectedOutput: "Dashboard.ReciqtgGk no differences\n", + }, + }, + }) + }) + + t.Run("Diff dashboard - invalid auth", func(t *testing.T) { + runTest(t, GrizzlyTest{ + TestDir: dir, + RunOnContexts: []string{"invalid_auth"}, + Commands: []Command{ + { + Command: "diff ReciqtgGk.yml", + ExpectedCode: 1, + ExpectedLogsContain: "Invalid username or password", }, }, }) diff --git a/integration/testdata/contexts/get-contexts.txt b/integration/testdata/contexts/get-contexts.txt index 6c92e795..9a2f2d8c 100644 --- a/integration/testdata/contexts/get-contexts.txt +++ b/integration/testdata/contexts/get-contexts.txt @@ -1,3 +1,4 @@ basic_auth * default + invalid_auth subpath diff --git a/integration/testdata/dashboards/ReciqtgGk.json b/integration/testdata/dashboards/ReciqtgGk.yml similarity index 100% rename from integration/testdata/dashboards/ReciqtgGk.json rename to integration/testdata/dashboards/ReciqtgGk.yml diff --git a/integration/utils_test.go b/integration/utils_test.go index 4631d303..68584df1 100644 --- a/integration/utils_test.go +++ b/integration/utils_test.go @@ -14,10 +14,11 @@ import ( var allContexts = []string{"default", "subpath", "basic_auth"} type Command struct { - Command string - ExpectedCode int - ExpectedError error - ExpectedOutput string + Command string + ExpectedCode int + ExpectedLogsContain string + ExpectedOutput string + ExpectedOutputFile string } type GrizzlyTest struct { TestDir string @@ -40,15 +41,26 @@ func runTest(t *testing.T, test GrizzlyTest) { } commands = append(commands, test.Commands...) for _, command := range commands { - stdout, stderr, exitCode, err := runLocalGrizzly(t, test.TestDir, command.Command) - if command.ExpectedError != nil { - require.Error(t, err, command.ExpectedError) + stdout, stderr, err := runLocalGrizzly(t, test.TestDir, command.Command) + exitCode := 0 + if err != nil { + exitCode = err.(*exec.ExitError).ExitCode() } require.Equal(t, command.ExpectedCode, exitCode, "Exited with %d (%d expected).\nOutput: %s\nstderr: %s", exitCode, command.ExpectedCode, stdout, stderr) - if command.ExpectedOutput != "" { - data, err := os.ReadFile(filepath.Join(test.TestDir, command.ExpectedOutput)) + + // Check stderr + if command.ExpectedLogsContain != "" { + require.Contains(t, stderr, command.ExpectedLogsContain) + } + + // Check stdout + if command.ExpectedOutputFile != "" { + bytes, err := os.ReadFile(filepath.Join(test.TestDir, command.ExpectedOutputFile)) require.NoError(t, err) - require.Equal(t, string(data), stdout) + command.ExpectedOutput = string(bytes) + } + if command.ExpectedOutput != "" { + require.Equal(t, command.ExpectedOutput, stdout) } } if test.Validate != nil { @@ -58,7 +70,7 @@ func runTest(t *testing.T, test GrizzlyTest) { } } -func runLocalGrizzly(t *testing.T, dir string, command string) (stdout, stderr string, exitCode int, err error) { +func runLocalGrizzly(t *testing.T, dir string, command string) (stdout, stderr string, err error) { t.Helper() args := []string{} @@ -69,7 +81,7 @@ func runLocalGrizzly(t *testing.T, dir string, command string) (stdout, stderr s cmd.Stderr = stdErrBuf cmd.Dir = dir output, err := cmd.Output() - return string(output), stdErrBuf.String(), cmd.ProcessState.ExitCode(), err + return string(output), stdErrBuf.String(), err } // Setting up a settings.yaml file. We don't want to commit this file to avoid git diff. @@ -82,16 +94,24 @@ func setupContexts(t *testing.T, dir string) { "config create-context default", "config use-context default", "config set grafana.url http://localhost:3001", + "config create-context subpath", "config use-context subpath", "config set grafana.url http://localhost:3003/grafana", + "config create-context basic_auth", "config use-context basic_auth", "config set grafana.url http://localhost:3004", "config set grafana.user admin", "config set grafana.token admin", + + "config create-context invalid_auth", + "config use-context invalid_auth", + "config set grafana.url http://localhost:3004", + "config set grafana.user admin", + "config set grafana.token invalid", } { - _, _, _, err = runLocalGrizzly(t, dir, command) + _, _, err = runLocalGrizzly(t, dir, command) require.NoError(t, err) }