Skip to content

Commit 653b5e7

Browse files
committed
Add corner case testing
1 parent 83ade9a commit 653b5e7

File tree

7 files changed

+115
-106
lines changed

7 files changed

+115
-106
lines changed

access/resource_sql_permissions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,5 +431,5 @@ func TestResourceSqlPermissions_Delete(t *testing.T) {
431431
}
432432

433433
func TestResourceSqlPermissions_CornerCases(t *testing.T) {
434-
qa.ResourceCornerCases(t, ResourceSqlPermissions(), "database/foo")
434+
qa.ResourceCornerCases(t, ResourceSqlPermissions(), qa.CornerCaseID("database/foo"))
435435
}

common/reflect_resource.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ func SchemaPath(s map[string]*schema.Schema, path ...string) (*schema.Schema, er
6666
return nil, fmt.Errorf("%v does not compute", path)
6767
}
6868

69+
func MustSchemaPath(s map[string]*schema.Schema, path ...string) *schema.Schema {
70+
sch, err := SchemaPath(s, path...)
71+
if err != nil {
72+
panic(err)
73+
}
74+
return sch
75+
}
76+
6977
// StructToSchema makes schema from a struct type & applies customizations from callback given
7078
func StructToSchema(v interface{}, customize func(map[string]*schema.Schema) map[string]*schema.Schema) map[string]*schema.Schema {
7179
rv := reflect.ValueOf(v)

compute/resource_job_test.go

Lines changed: 74 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
"github.com/databrickslabs/terraform-provider-databricks/common"
10-
1110
"github.com/databrickslabs/terraform-provider-databricks/qa"
1211
"github.com/stretchr/testify/assert"
1312
"github.com/stretchr/testify/require"
@@ -457,42 +456,6 @@ func TestResourceJobCreateSingleNode_Fail(t *testing.T) {
457456
require.Equal(t, true, strings.Contains(err.Error(), "NumWorkers could be 0 only for SingleNode clusters"))
458457
}
459458

460-
func TestResourceJobCreate_Error(t *testing.T) {
461-
d, err := qa.ResourceFixture{
462-
Fixtures: []qa.HTTPFixture{
463-
{
464-
Method: "POST",
465-
Resource: "/api/2.0/jobs/create",
466-
Response: common.APIErrorBody{
467-
ErrorCode: "INVALID_REQUEST",
468-
Message: "Internal error happened",
469-
},
470-
Status: 400,
471-
},
472-
},
473-
Resource: ResourceJob(),
474-
HCL: `existing_cluster_id = "abc"
475-
max_concurrent_runs = 1
476-
max_retries = 3
477-
min_retry_interval_millis = 5000
478-
name = "Featurizer"
479-
retry_on_timeout = true
480-
481-
spark_jar_task {
482-
main_class_name = "com.labs.BarMain"
483-
}
484-
library {
485-
jar = "dbfs://aa/bb/cc.jar"
486-
}
487-
library {
488-
jar = "dbfs://ff/gg/hh.jar"
489-
}`,
490-
Create: true,
491-
}.Apply(t)
492-
qa.AssertErrorStartsWith(t, err, "Internal error happened")
493-
assert.Equal(t, "", d.Id(), "Id should be empty for error creates")
494-
}
495-
496459
func TestResourceJobRead(t *testing.T) {
497460
d, err := qa.ResourceFixture{
498461
Fixtures: []qa.HTTPFixture{
@@ -676,6 +639,62 @@ func TestResourceJobUpdate(t *testing.T) {
676639
assert.Equal(t, "Featurizer New", d.Get("name"))
677640
}
678641

642+
func TestResourceJobUpdate_Tasks(t *testing.T) {
643+
qa.ResourceFixture{
644+
Fixtures: []qa.HTTPFixture{
645+
{
646+
Method: "POST",
647+
Resource: "/api/2.1/jobs/reset",
648+
ExpectedRequest: UpdateJobRequest{
649+
JobID: 789,
650+
NewSettings: &JobSettings{
651+
Name: "Featurizer New",
652+
Tasks: []JobTaskSettings{
653+
{
654+
ExistingClusterID: "abc",
655+
SparkJarTask: &SparkJarTask{
656+
MainClassName: "com.labs.BarMain",
657+
},
658+
},
659+
},
660+
MaxConcurrentRuns: 1,
661+
},
662+
},
663+
Response: Job{
664+
JobID: 789,
665+
},
666+
},
667+
{
668+
Method: "GET",
669+
Resource: "/api/2.1/jobs/get?job_id=789",
670+
Response: Job{
671+
Settings: &JobSettings{
672+
Tasks: []JobTaskSettings{
673+
{
674+
ExistingClusterID: "abc",
675+
SparkJarTask: &SparkJarTask{
676+
MainClassName: "com.labs.BarMain",
677+
},
678+
},
679+
},
680+
},
681+
},
682+
},
683+
},
684+
ID: "789",
685+
Update: true,
686+
Resource: ResourceJob(),
687+
HCL: `
688+
name = "Featurizer New"
689+
task {
690+
existing_cluster_id = "abc"
691+
spark_jar_task {
692+
main_class_name = "com.labs.BarMain"
693+
}
694+
}`,
695+
}.ApplyNoError(t)
696+
}
697+
679698
func TestResourceJobUpdate_Restart(t *testing.T) {
680699
d, err := qa.ResourceFixture{
681700
Fixtures: []qa.HTTPFixture{
@@ -905,44 +924,6 @@ func TestJobRestarts(t *testing.T) {
905924
})
906925
}
907926

908-
func TestResourceJobUpdate_Error(t *testing.T) {
909-
d, err := qa.ResourceFixture{
910-
Fixtures: []qa.HTTPFixture{
911-
{
912-
Method: "POST",
913-
Resource: "/api/2.0/jobs/reset",
914-
Response: common.APIErrorBody{
915-
ErrorCode: "INVALID_REQUEST",
916-
Message: "Internal error happened",
917-
},
918-
Status: 400,
919-
},
920-
},
921-
ID: "789",
922-
Update: true,
923-
Resource: ResourceJob(),
924-
HCL: `existing_cluster_id = "abc"
925-
max_concurrent_runs = 1
926-
max_retries = 3
927-
min_retry_interval_millis = 5000
928-
name = "Featurizer New"
929-
retry_on_timeout = true
930-
931-
spark_jar_task {
932-
main_class_name = "com.labs.BarMain"
933-
parameters = ["--cleanup", "full"]
934-
}
935-
library {
936-
jar = "dbfs://aa/bb/cc.jar"
937-
}
938-
library {
939-
jar = "dbfs://ff/gg/hh.jar"
940-
}`,
941-
}.Apply(t)
942-
qa.AssertErrorStartsWith(t, err, "Internal error happened")
943-
assert.Equal(t, "789", d.Id())
944-
}
945-
946927
func TestResourceJobDelete(t *testing.T) {
947928
d, err := qa.ResourceFixture{
948929
Fixtures: []qa.HTTPFixture{
@@ -987,27 +968,6 @@ func TestResourceJobUpdate_FailNumWorkersZero(t *testing.T) {
987968
require.Equal(t, true, strings.Contains(err.Error(), "NumWorkers could be 0 only for SingleNode clusters"))
988969
}
989970

990-
func TestResourceJobDelete_Error(t *testing.T) {
991-
d, err := qa.ResourceFixture{
992-
Fixtures: []qa.HTTPFixture{
993-
{
994-
Method: "POST",
995-
Resource: "/api/2.0/jobs/delete",
996-
Response: common.APIErrorBody{
997-
ErrorCode: "INVALID_REQUEST",
998-
Message: "Internal error happened",
999-
},
1000-
Status: 400,
1001-
},
1002-
},
1003-
ID: "789",
1004-
Delete: true,
1005-
Resource: ResourceJob(),
1006-
}.Apply(t)
1007-
qa.AssertErrorStartsWith(t, err, "Internal error happened")
1008-
assert.Equal(t, "789", d.Id())
1009-
}
1010-
1011971
func TestJobsAPIList(t *testing.T) {
1012972
qa.HTTPFixturesApply(t, []qa.HTTPFixture{
1013973
{
@@ -1057,3 +1017,21 @@ func TestJobsAPIRunsList(t *testing.T) {
10571017
assert.Len(t, l.Runs, 1)
10581018
})
10591019
}
1020+
1021+
func TestJobResourceCornerCases_HTTP(t *testing.T) {
1022+
qa.ResourceCornerCases(t, ResourceJob(), qa.CornerCaseID("10"))
1023+
}
1024+
1025+
func TestJobResourceCornerCases_WrongID(t *testing.T) {
1026+
qa.ResourceCornerCases(t, ResourceJob(),
1027+
qa.CornerCaseID("x"),
1028+
qa.CornerCaseSkipCRUD("create"),
1029+
qa.CornerCaseExpectError(`strconv.ParseInt: parsing "x": invalid syntax`))
1030+
}
1031+
1032+
func TestJobResource_SparkConfDiffSuppress(t *testing.T) {
1033+
jr := ResourceJob()
1034+
scs := common.MustSchemaPath(jr.Schema, "new_cluster", "spark_conf")
1035+
assert.True(t, scs.DiffSuppressFunc("new_cluster.0.spark_conf.%", "1", "0", nil))
1036+
assert.False(t, scs.DiffSuppressFunc("new_cluster.0.spark_conf.%", "1", "1", nil))
1037+
}

qa/testing.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,41 @@ func (f ResourceFixture) ExpectError(t *testing.T, msg string) {
265265
assert.EqualError(t, err, msg)
266266
}
267267

268+
type CornerCase struct {
269+
part string
270+
value string
271+
}
272+
273+
func CornerCaseID(id string) CornerCase {
274+
return CornerCase{"id", id}
275+
}
276+
277+
func CornerCaseExpectError(msg string) CornerCase {
278+
return CornerCase{"expect_error", msg}
279+
}
280+
281+
func CornerCaseSkipCRUD(method string) CornerCase {
282+
return CornerCase{"skip_crud", method}
283+
}
284+
268285
// ResourceCornerCases checks for corner cases of error handling. Optional field name used to create error
269-
func ResourceCornerCases(t *testing.T, resource *schema.Resource, id ...string) {
286+
func ResourceCornerCases(t *testing.T, resource *schema.Resource, cc ...CornerCase) {
287+
config := map[string]string{
288+
"id": "x",
289+
"expect_error": "I'm a teapot",
290+
}
270291
teapot := "I'm a teapot"
271292
m := map[string]func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics{
272293
"create": resource.CreateContext,
273294
"read": resource.ReadContext,
274295
"update": resource.UpdateContext,
275296
"delete": resource.DeleteContext,
276297
}
277-
fakeID := "x"
278-
if len(id) > 0 {
279-
fakeID = id[0]
298+
for _, corner := range cc {
299+
if corner.part == "skip_crud" {
300+
delete(m, corner.value)
301+
}
302+
config[corner.part] = corner.value
280303
}
281304
HTTPFixturesApply(t, []HTTPFixture{
282305
{
@@ -291,14 +314,14 @@ func ResourceCornerCases(t *testing.T, resource *schema.Resource, id ...string)
291314
},
292315
}, func(ctx context.Context, client *common.DatabricksClient) {
293316
validData := resource.TestResourceData()
294-
validData.SetId(fakeID)
317+
validData.SetId(config["id"])
295318
for n, v := range m {
296319
if v == nil {
297320
continue
298321
}
299322
diags := v(ctx, validData, client)
300323
if assert.Len(t, diags, 1) {
301-
assert.Equalf(t, diags[0].Summary, teapot,
324+
assert.Equalf(t, diags[0].Summary, config["expect_error"],
302325
"%s didn't handle correct error on valid data", n)
303326
}
304327
}

qa/testing_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,5 @@ func TestResourceCornerCases(t *testing.T) {
308308
Required: true,
309309
},
310310
},
311-
}.ToResource(), "x")
311+
}.ToResource(), CornerCaseID("x"))
312312
}

sqlanalytics/resource_visualization_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,5 +274,5 @@ func TestVisualizationDelete(t *testing.T) {
274274
}
275275

276276
func TestResourceVisualizationCornerCases(t *testing.T) {
277-
qa.ResourceCornerCases(t, ResourceVisualization(), "foo/bar")
277+
qa.ResourceCornerCases(t, ResourceVisualization(), qa.CornerCaseID("foo/bar"))
278278
}

sqlanalytics/resource_widget_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,5 +652,5 @@ func TestWidgetDelete(t *testing.T) {
652652
}
653653

654654
func TestResourceWidgetCornerCases(t *testing.T) {
655-
qa.ResourceCornerCases(t, ResourceWidget(), "foo/bar")
655+
qa.ResourceCornerCases(t, ResourceWidget(), qa.CornerCaseID("foo/bar"))
656656
}

0 commit comments

Comments
 (0)