From bdf8b7531b5657061eaab11886144e854389185c Mon Sep 17 00:00:00 2001 From: Giordano Ricci Date: Fri, 3 Mar 2023 14:06:59 +0000 Subject: [PATCH] [9.4.x] Correlations: Fix flaky integration tests (#64004) (#64139) Correlations: Fix flaky integration tests (#64004) * Correlations: Fix flaky integration tests * set explore-squad as correlations tests code owners (cherry picked from commit 4a7fbea7a4075743e1c021d7d4ff75b3ffe1e641) --- .github/CODEOWNERS | 1 + pkg/tests/api/correlations/common_test.go | 15 ++++++--- .../correlations/correlations_create_test.go | 31 +++++++------------ .../correlations/correlations_delete_test.go | 31 +++++++------------ .../correlations/correlations_read_test.go | 27 ++++++---------- .../correlations/correlations_update_test.go | 31 +++++++------------ 6 files changed, 57 insertions(+), 79 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 10cdff7719b..213450e010c 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -133,6 +133,7 @@ WORKFLOW.md @torkelo /pkg/services/validations/ @grafana/backend-platform /pkg/setting/ @grafana/backend-platform /pkg/tests/ @grafana/backend-platform +/pkg/tests/api/correlations/ @grafana/explore-squad /pkg/tsdb/grafanads/ @grafana/backend-platform /pkg/tsdb/intervalv2/ @grafana/backend-platform /pkg/tsdb/legacydata/ @grafana/backend-platform diff --git a/pkg/tests/api/correlations/common_test.go b/pkg/tests/api/correlations/common_test.go index aabdb761929..aa20136d22b 100644 --- a/pkg/tests/api/correlations/common_test.go +++ b/pkg/tests/api/correlations/common_test.go @@ -43,7 +43,7 @@ func NewTestEnv(t *testing.T) TestContext { } type User struct { - username string + User user.User password string } @@ -121,8 +121,8 @@ func (c TestContext) getURL(url string, user User) string { c.t.Helper() baseUrl := fmt.Sprintf("http://%s", c.env.Server.HTTPServer.Listener.Addr()) - if user.username != "" && user.password != "" { - baseUrl = fmt.Sprintf("http://%s:%s@%s", user.username, user.password, c.env.Server.HTTPServer.Listener.Addr()) + if user.User.Login != "" && user.password != "" { + baseUrl = fmt.Sprintf("http://%s:%s@%s", user.User.Login, user.password, c.env.Server.HTTPServer.Listener.Addr()) } return fmt.Sprintf( @@ -132,7 +132,7 @@ func (c TestContext) getURL(url string, user User) string { ) } -func (c TestContext) createUser(cmd user.CreateUserCommand) { +func (c TestContext) createUser(cmd user.CreateUserCommand) User { c.t.Helper() store := c.env.SQLStore store.Cfg.AutoAssignOrg = true @@ -144,8 +144,13 @@ func (c TestContext) createUser(cmd user.CreateUserCommand) { usrSvc, err := userimpl.ProvideService(store, orgService, store.Cfg, nil, nil, quotaService) require.NoError(c.t, err) - _, err = usrSvc.CreateUserForTests(context.Background(), &cmd) + user, err := usrSvc.CreateUserForTests(context.Background(), &cmd) require.NoError(c.t, err) + + return User{ + User: *user, + password: cmd.Password, + } } func (c TestContext) createDs(cmd *datasources.AddDataSourceCommand) { diff --git a/pkg/tests/api/correlations/correlations_create_test.go b/pkg/tests/api/correlations/correlations_create_test.go index 3921767c6fa..64a344e402e 100644 --- a/pkg/tests/api/correlations/correlations_create_test.go +++ b/pkg/tests/api/correlations/correlations_create_test.go @@ -21,31 +21,24 @@ func TestIntegrationCreateCorrelation(t *testing.T) { } ctx := NewTestEnv(t) - adminUser := User{ - username: "admin", - password: "admin", - } - editorUser := User{ - username: "editor", - password: "editor", - } - - ctx.createUser(user.CreateUserCommand{ - DefaultOrgRole: string(org.RoleEditor), - Password: editorUser.password, - Login: editorUser.username, - }) - ctx.createUser(user.CreateUserCommand{ + adminUser := ctx.createUser(user.CreateUserCommand{ DefaultOrgRole: string(org.RoleAdmin), - Password: adminUser.password, - Login: adminUser.username, + Password: "admin", + Login: "admin", + }) + + editorUser := ctx.createUser(user.CreateUserCommand{ + DefaultOrgRole: string(org.RoleEditor), + Password: "editor", + Login: "editor", + OrgID: adminUser.User.OrgID, }) createDsCommand := &datasources.AddDataSourceCommand{ Name: "read-only", Type: "loki", ReadOnly: true, - OrgId: 1, + OrgId: adminUser.User.OrgID, } ctx.createDs(createDsCommand) readOnlyDS := createDsCommand.Result.Uid @@ -53,7 +46,7 @@ func TestIntegrationCreateCorrelation(t *testing.T) { createDsCommand = &datasources.AddDataSourceCommand{ Name: "writable", Type: "loki", - OrgId: 1, + OrgId: adminUser.User.OrgID, } ctx.createDs(createDsCommand) writableDs := createDsCommand.Result.Uid diff --git a/pkg/tests/api/correlations/correlations_delete_test.go b/pkg/tests/api/correlations/correlations_delete_test.go index 9002a5582d7..624f56df50b 100644 --- a/pkg/tests/api/correlations/correlations_delete_test.go +++ b/pkg/tests/api/correlations/correlations_delete_test.go @@ -21,31 +21,24 @@ func TestIntegrationDeleteCorrelation(t *testing.T) { } ctx := NewTestEnv(t) - adminUser := User{ - username: "admin", - password: "admin", - } - editorUser := User{ - username: "editor", - password: "editor", - } - - ctx.createUser(user.CreateUserCommand{ - DefaultOrgRole: string(org.RoleEditor), - Password: editorUser.password, - Login: editorUser.username, - }) - ctx.createUser(user.CreateUserCommand{ + adminUser := ctx.createUser(user.CreateUserCommand{ DefaultOrgRole: string(org.RoleAdmin), - Password: adminUser.password, - Login: adminUser.username, + Password: "admin", + Login: "admin", + }) + + editorUser := ctx.createUser(user.CreateUserCommand{ + DefaultOrgRole: string(org.RoleEditor), + Password: "editor", + Login: "editor", + OrgID: adminUser.User.OrgID, }) createDsCommand := &datasources.AddDataSourceCommand{ Name: "read-only", Type: "loki", ReadOnly: true, - OrgId: 1, + OrgId: adminUser.User.OrgID, } ctx.createDs(createDsCommand) readOnlyDS := createDsCommand.Result.Uid @@ -53,7 +46,7 @@ func TestIntegrationDeleteCorrelation(t *testing.T) { createDsCommand = &datasources.AddDataSourceCommand{ Name: "writable", Type: "loki", - OrgId: 1, + OrgId: adminUser.User.OrgID, } ctx.createDs(createDsCommand) writableDs := createDsCommand.Result.Uid diff --git a/pkg/tests/api/correlations/correlations_read_test.go b/pkg/tests/api/correlations/correlations_read_test.go index e11763a0286..b92c96d746b 100644 --- a/pkg/tests/api/correlations/correlations_read_test.go +++ b/pkg/tests/api/correlations/correlations_read_test.go @@ -23,24 +23,17 @@ func TestIntegrationReadCorrelation(t *testing.T) { } ctx := NewTestEnv(t) - adminUser := User{ - username: "admin", - password: "admin", - } - viewerUser := User{ - username: "viewer", - password: "viewer", - } - - ctx.createUser(user.CreateUserCommand{ - DefaultOrgRole: string(org.RoleViewer), - Password: viewerUser.password, - Login: viewerUser.username, - }) - ctx.createUser(user.CreateUserCommand{ + adminUser := ctx.createUser(user.CreateUserCommand{ DefaultOrgRole: string(org.RoleAdmin), - Password: adminUser.password, - Login: adminUser.username, + Password: "admin", + Login: "admin", + }) + + viewerUser := ctx.createUser(user.CreateUserCommand{ + DefaultOrgRole: string(org.RoleViewer), + Password: "viewer", + Login: "viewer", + OrgID: adminUser.User.OrgID, }) t.Run("Get all correlations", func(t *testing.T) { diff --git a/pkg/tests/api/correlations/correlations_update_test.go b/pkg/tests/api/correlations/correlations_update_test.go index a085abac46f..7d65221093d 100644 --- a/pkg/tests/api/correlations/correlations_update_test.go +++ b/pkg/tests/api/correlations/correlations_update_test.go @@ -21,31 +21,24 @@ func TestIntegrationUpdateCorrelation(t *testing.T) { } ctx := NewTestEnv(t) - adminUser := User{ - username: "admin", - password: "admin", - } - editorUser := User{ - username: "editor", - password: "editor", - } - - ctx.createUser(user.CreateUserCommand{ - DefaultOrgRole: string(org.RoleEditor), - Password: editorUser.password, - Login: editorUser.username, - }) - ctx.createUser(user.CreateUserCommand{ + adminUser := ctx.createUser(user.CreateUserCommand{ DefaultOrgRole: string(org.RoleAdmin), - Password: adminUser.password, - Login: adminUser.username, + Password: "admin", + Login: "admin", + }) + + editorUser := ctx.createUser(user.CreateUserCommand{ + DefaultOrgRole: string(org.RoleEditor), + Password: "editor", + Login: "editor", + OrgID: adminUser.User.OrgID, }) createDsCommand := &datasources.AddDataSourceCommand{ Name: "read-only", Type: "loki", ReadOnly: true, - OrgId: 1, + OrgId: adminUser.User.OrgID, } ctx.createDs(createDsCommand) readOnlyDS := createDsCommand.Result.Uid @@ -53,7 +46,7 @@ func TestIntegrationUpdateCorrelation(t *testing.T) { createDsCommand = &datasources.AddDataSourceCommand{ Name: "writable", Type: "loki", - OrgId: 1, + OrgId: adminUser.User.OrgID, } ctx.createDs(createDsCommand) writableDs := createDsCommand.Result.Uid