From 0dfa4d75cfd2450e1a71b87feff58119fa99edfa Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Thu, 28 Jan 2021 19:27:42 +0000 Subject: [PATCH 1/3] tests: test in parallel --- internal/cli/alerts/acknowledge_test.go | 1 + internal/cli/alerts/describe_test.go | 1 + internal/cli/atlas/dbusers/create_test.go | 1 + internal/cli/atlas/logs/download_test.go | 1 + internal/cli/global_opts_test.go | 1 + internal/cli/output_opts_test.go | 1 + internal/convert/cluster_config_test.go | 1 + internal/convert/rs_config_test.go | 1 + internal/validate/validate_test.go | 4 ++++ 9 files changed, 12 insertions(+) diff --git a/internal/cli/alerts/acknowledge_test.go b/internal/cli/alerts/acknowledge_test.go index c76e955f63..ac69fb5feb 100644 --- a/internal/cli/alerts/acknowledge_test.go +++ b/internal/cli/alerts/acknowledge_test.go @@ -87,6 +87,7 @@ func TestAcknowledgeOpts_Run(t *testing.T) { opts := tt.opts wantErr := tt.wantErr t.Run(tt.name, func(t *testing.T) { + t.Parallel() ackReq := opts.newAcknowledgeRequest() if wantErr { mockStore. diff --git a/internal/cli/alerts/describe_test.go b/internal/cli/alerts/describe_test.go index 23d35cf4e3..23f661cf05 100644 --- a/internal/cli/alerts/describe_test.go +++ b/internal/cli/alerts/describe_test.go @@ -71,6 +71,7 @@ func TestDescribeOpts_Run(t *testing.T) { cmd := tt.cmd wantErr := tt.wantErr t.Run(tt.name, func(t *testing.T) { + t.Parallel() if wantErr { mockStore. EXPECT(). diff --git a/internal/cli/atlas/dbusers/create_test.go b/internal/cli/atlas/dbusers/create_test.go index 77704f821e..6796418def 100644 --- a/internal/cli/atlas/dbusers/create_test.go +++ b/internal/cli/atlas/dbusers/create_test.go @@ -184,6 +184,7 @@ func TestCreateOpts_validate(t *testing.T) { fields := tt.fields wantErr := tt.wantErr t.Run(tt.name, func(t *testing.T) { + t.Parallel() opts := &CreateOpts{ x509Type: fields.x509Type, awsIamType: fields.awsIamType, diff --git a/internal/cli/atlas/logs/download_test.go b/internal/cli/atlas/logs/download_test.go index 4c0c20d098..25c92d58b2 100644 --- a/internal/cli/atlas/logs/download_test.go +++ b/internal/cli/atlas/logs/download_test.go @@ -91,6 +91,7 @@ func TestDownloadOpts_initDefaultOut(t *testing.T) { out := tt.fields.out want := tt.want t.Run(tt.name, func(t *testing.T) { + t.Parallel() opts := &DownloadOpts{ name: logName, } diff --git a/internal/cli/global_opts_test.go b/internal/cli/global_opts_test.go index 33e666b0b8..d166c67413 100644 --- a/internal/cli/global_opts_test.go +++ b/internal/cli/global_opts_test.go @@ -119,6 +119,7 @@ func TestGenerateAliases(t *testing.T) { want := tt.want args := tt.args t.Run(tt.name, func(t *testing.T) { + t.Parallel() got := GenerateAliases(args.use, args.extra...) assert.Equal(t, got, want) }) diff --git a/internal/cli/output_opts_test.go b/internal/cli/output_opts_test.go index 76a28a6b31..a4b4615a57 100644 --- a/internal/cli/output_opts_test.go +++ b/internal/cli/output_opts_test.go @@ -56,6 +56,7 @@ func TestOutputOpts_outputTypeAndValue(t *testing.T) { } want := tt.want t.Run(tt.name, func(t *testing.T) { + t.Parallel() _, got := opts.outputTypeAndValue() if got != want { t.Errorf("parseTemplate() got = %v, want %v", got, want) diff --git a/internal/convert/cluster_config_test.go b/internal/convert/cluster_config_test.go index f17f503343..bbddd467b3 100644 --- a/internal/convert/cluster_config_test.go +++ b/internal/convert/cluster_config_test.go @@ -958,6 +958,7 @@ func TestClusterConfig_PatchAutomationConfig(t *testing.T) { current := tc.current expected := tc.expected t.Run(name, func(t *testing.T) { + t.Parallel() if err := changes.PatchAutomationConfig(current); err != nil { t.Fatalf("PatchAutomationConfig() unexpected error: %v\n", err) } diff --git a/internal/convert/rs_config_test.go b/internal/convert/rs_config_test.go index dd9d85ac43..8efaef8c16 100644 --- a/internal/convert/rs_config_test.go +++ b/internal/convert/rs_config_test.go @@ -56,6 +56,7 @@ func TestRSConfig_protocolVer(t *testing.T) { expected := tc.wantedProtocolVersion wantErr := tc.wantErr t.Run(name, func(t *testing.T) { + t.Parallel() ver, err := m.protocolVer() if (err != nil) != wantErr { t.Fatalf("protocolVer() unexpected error: %v\n", err) diff --git a/internal/validate/validate_test.go b/internal/validate/validate_test.go index 926d4d5b7a..be673ab990 100644 --- a/internal/validate/validate_test.go +++ b/internal/validate/validate_test.go @@ -120,6 +120,7 @@ func TestCredentials(t *testing.T) { } func TestFlagInSlice(t *testing.T) { + t.Parallel() type args struct { value string flag string @@ -155,6 +156,7 @@ func TestFlagInSlice(t *testing.T) { validValues := tt.args.validValues wantErr := tt.wantErr t.Run(tt.name, func(t *testing.T) { + t.Parallel() if err := FlagInSlice(value, flag, validValues); (err != nil) != wantErr { t.Errorf("FlagInSlice() error = %v, wantErr %v", err, wantErr) } @@ -163,6 +165,7 @@ func TestFlagInSlice(t *testing.T) { } func TestOptionalURL(t *testing.T) { + t.Parallel() tests := []struct { name string val interface{} @@ -193,6 +196,7 @@ func TestOptionalURL(t *testing.T) { val := tt.val wantErr := tt.wantErr t.Run(tt.name, func(t *testing.T) { + t.Parallel() if err := OptionalURL(val); (err != nil) != wantErr { t.Errorf("OptionalURL() error = %v, wantErr %v", err, wantErr) } From 56370dbf7eccb7ae2b5d4c0639583c1cf22d4e9a Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Tue, 2 Feb 2021 17:33:27 +0000 Subject: [PATCH 2/3] more parall --- internal/convert/automation_config_test.go | 2 + internal/convert/custom_db_role_test.go | 136 +++++++++++++-------- 2 files changed, 84 insertions(+), 54 deletions(-) diff --git a/internal/convert/automation_config_test.go b/internal/convert/automation_config_test.go index 71b17d9102..a0edecd05f 100644 --- a/internal/convert/automation_config_test.go +++ b/internal/convert/automation_config_test.go @@ -27,6 +27,7 @@ import ( func TestFromAutomationConfig(t *testing.T) { name := "cluster_1" t.Run("replica set", func(t *testing.T) { + t.Parallel() config := fixture.AutomationConfigWithOneReplicaSet(name, false) expected := []*ClusterConfig{ { @@ -82,6 +83,7 @@ func TestFromAutomationConfig(t *testing.T) { } }) t.Run("sharded cluster", func(t *testing.T) { + t.Parallel() config := fixture.AutomationConfigWithOneShardedCluster(name, false) expected := []*ClusterConfig{ { diff --git a/internal/convert/custom_db_role_test.go b/internal/convert/custom_db_role_test.go index 89aca79d27..3eda77ebd7 100644 --- a/internal/convert/custom_db_role_test.go +++ b/internal/convert/custom_db_role_test.go @@ -30,35 +30,49 @@ func TestBuildAtlasInheritedRoles(t *testing.T) { } tests := []test{ - {input: []string{"admin"}, want: []mongodbatlas.InheritedRole{ - { - Role: "admin", - Db: "admin", - }}, + { + input: []string{"admin"}, + want: []mongodbatlas.InheritedRole{ + { + Role: "admin", + Db: "admin", + }, + }, }, - {input: []string{"admin@test"}, want: []mongodbatlas.InheritedRole{ - { - Role: "admin", - Db: "test", - }}, + { + input: []string{"admin@test"}, + want: []mongodbatlas.InheritedRole{ + { + Role: "admin", + Db: "test", + }, + }, }, - {input: []string{"admin@test", "something"}, want: []mongodbatlas.InheritedRole{ - { - Role: "admin", - Db: "test", + { + input: []string{"admin@test", "something"}, + want: []mongodbatlas.InheritedRole{ + { + Role: "admin", + Db: "test", + }, + { + Role: "something", + Db: "admin", + }, }, - { - Role: "something", - Db: "admin", - }}, }, } for _, tc := range tests { - got := BuildAtlasInheritedRoles(tc.input) - if err := deep.Equal(tc.want, got); err != nil { - t.Fatalf("expected: %v, got: %v", tc.want, got) - } + input := tc.input + want := tc.want + t.Run("", func(t *testing.T) { + t.Parallel() + got := BuildAtlasInheritedRoles(input) + if err := deep.Equal(want, got); err != nil { + t.Fatalf("expected: %v, got: %v", want, got) + } + }) } } @@ -71,51 +85,65 @@ func TestBuildAtlasActions(t *testing.T) { cluster := true tests := []test{ - {input: []string{"clusterName"}, want: []mongodbatlas.Action{ - { - Action: "clusterName", - Resources: []mongodbatlas.Resource{ - { - Cluster: &cluster, + { + input: []string{"clusterName"}, + want: []mongodbatlas.Action{ + { + Action: "clusterName", + Resources: []mongodbatlas.Resource{ + { + Cluster: &cluster, + }, }, }, }, - }}, - {input: []string{"clusterName@testdb.collection"}, want: []mongodbatlas.Action{ - { - Action: "clusterName", - Resources: []mongodbatlas.Resource{ - { - Db: "testdb", - Collection: "collection", + }, + { + input: []string{"clusterName@testdb.collection"}, + want: []mongodbatlas.Action{ + { + Action: "clusterName", + Resources: []mongodbatlas.Resource{ + { + Db: "testdb", + Collection: "collection", + }, }, }, }, - }}, - {input: []string{"clusterName", "name@DATA_LAKE"}, want: []mongodbatlas.Action{ - { - Action: "clusterName", - Resources: []mongodbatlas.Resource{ - { - Cluster: &cluster, + }, + { + input: []string{"clusterName", "name@DATA_LAKE"}, + want: []mongodbatlas.Action{ + { + Action: "clusterName", + Resources: []mongodbatlas.Resource{ + { + Cluster: &cluster, + }, }, }, - }, - { - Action: "name", - Resources: []mongodbatlas.Resource{ - { - Db: "DATA_LAKE", + { + Action: "name", + Resources: []mongodbatlas.Resource{ + { + Db: "DATA_LAKE", + }, }, }, }, - }}, + }, } for _, tc := range tests { - got := BuildAtlasActions(tc.input) - if err := deep.Equal(tc.want, got); err != nil { - t.Fatalf("expected: %v, got: %v", tc.want, got) - } + input := tc.input + want := tc.want + t.Run("", func(t *testing.T) { + t.Parallel() + got := BuildAtlasActions(input) + if err := deep.Equal(want, got); err != nil { + t.Fatalf("expected: %v, got: %v", want, got) + } + }) } } From 5b685249b708a6326a3b2a0c1a760bc3ec0eecef Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Tue, 2 Feb 2021 18:15:15 +0000 Subject: [PATCH 3/3] Update describe_test.go --- internal/cli/alerts/describe_test.go | 1 - internal/validate/validate_test.go | 174 +++++++++++++++++---------- 2 files changed, 108 insertions(+), 67 deletions(-) diff --git a/internal/cli/alerts/describe_test.go b/internal/cli/alerts/describe_test.go index 23f661cf05..23d35cf4e3 100644 --- a/internal/cli/alerts/describe_test.go +++ b/internal/cli/alerts/describe_test.go @@ -71,7 +71,6 @@ func TestDescribeOpts_Run(t *testing.T) { cmd := tt.cmd wantErr := tt.wantErr t.Run(tt.name, func(t *testing.T) { - t.Parallel() if wantErr { mockStore. EXPECT(). diff --git a/internal/validate/validate_test.go b/internal/validate/validate_test.go index be673ab990..2894bbec8d 100644 --- a/internal/validate/validate_test.go +++ b/internal/validate/validate_test.go @@ -24,78 +24,120 @@ import ( ) func TestURL(t *testing.T) { - t.Run("Valid URL", func(t *testing.T) { - err := URL("http://test.com/") - if err != nil { - t.Fatalf("URL() unexpected error %v\n", err) - } - }) - t.Run("invalid value", func(t *testing.T) { - err := URL(1) - if err == nil { - t.Fatalf("URL() unexpected error %v\n", err) - } - }) - t.Run("missing trailing slash", func(t *testing.T) { - err := URL("http://test.com") - if err == nil { - t.Fatalf("URL() unexpected error %v\n", err) - } - }) + tests := []struct { + name string + val interface{} + wantErr bool + }{ + { + name: "Valid URL", + val: "http://test.com/", + wantErr: false, + }, + { + name: "invalid value", + val: 1, + wantErr: true, + }, + { + name: "missing trailing slash", + val: "http://test.com", + wantErr: true, + }, + } + for _, tt := range tests { + val := tt.val + wantErr := tt.wantErr + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + if err := URL(val); (err != nil) != wantErr { + t.Errorf("URL() error = %v, wantErr %v", err, wantErr) + } + }) + } } func TestOptionalObjectID(t *testing.T) { - t.Run("Empty value", func(t *testing.T) { - err := OptionalObjectID("") - if err != nil { - t.Fatalf("OptionalObjectID() unexpected error %v\n", err) - } - }) - t.Run("nil value", func(t *testing.T) { - err := OptionalObjectID(nil) - if err != nil { - t.Fatalf("OptionalObjectID() unexpected error %v\n", err) - } - }) - t.Run("Valid ObjectID", func(t *testing.T) { - err := OptionalObjectID("5e9f088b4797476aa0a5d56a") - if err != nil { - t.Fatalf("OptionalObjectID() unexpected error %v\n", err) - } - }) - t.Run("Short ObjectID", func(t *testing.T) { - err := OptionalObjectID("5e9f088b4797476aa0a5d56") - if err == nil { - t.Fatal("OptionalObjectID() expected an error\n") - } - }) - t.Run("Invalid ObjectID", func(t *testing.T) { - err := OptionalObjectID("5e9f088b4797476aa0a5d56z") - if err == nil { - t.Fatal("OptionalObjectID() expected an error\n") - } - }) + tests := []struct { + name string + val interface{} + wantErr bool + }{ + { + name: "Empty value", + val: "", + wantErr: false, + }, + { + name: "nil value", + val: nil, + wantErr: false, + }, + { + name: "Valid ObjectID", + val: "5e9f088b4797476aa0a5d56a", + wantErr: false, + }, + { + name: "Short ObjectID", + val: "5e9f088b4797476aa0a5d56", + wantErr: true, + }, + { + name: "Invalid ObjectID", + val: "5e9f088b4797476aa0a5d56z", + wantErr: true, + }, + } + for _, tt := range tests { + val := tt.val + wantErr := tt.wantErr + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + if err := OptionalObjectID(val); (err != nil) != wantErr { + t.Errorf("OptionalObjectID() error = %v, wantErr %v", err, wantErr) + } + }) + } } func TestObjectID(t *testing.T) { - t.Run("Valid ObjectID", func(t *testing.T) { - err := ObjectID("5e9f088b4797476aa0a5d56a") - if err != nil { - t.Fatalf("ObjectID() unexpected error %v\n", err) - } - }) - t.Run("Short ObjectID", func(t *testing.T) { - err := ObjectID("5e9f088b4797476aa0a5d56") - if err == nil { - t.Fatal("ObjectID() expected an error\n") - } - }) - t.Run("Invalid ObjectID", func(t *testing.T) { - err := ObjectID("5e9f088b4797476aa0a5d56z") - if err == nil { - t.Fatal("ObjectID() expected an error\n") - } - }) + tests := []struct { + name string + val string + wantErr bool + }{ + { + name: "Empty value", + val: "", + wantErr: true, + }, + { + name: "Valid ObjectID", + val: "5e9f088b4797476aa0a5d56a", + wantErr: false, + }, + { + name: "Short ObjectID", + val: "5e9f088b4797476aa0a5d56", + wantErr: true, + }, + { + name: "Invalid ObjectID", + val: "5e9f088b4797476aa0a5d56z", + wantErr: true, + }, + } + for _, tt := range tests { + val := tt.val + wantErr := tt.wantErr + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + if err := ObjectID(val); (err != nil) != wantErr { + t.Errorf("OptionalObjectID() error = %v, wantErr %v", err, wantErr) + } + }) + } } func TestCredentials(t *testing.T) {