Skip to content

Commit

Permalink
Ensuring that all instances of privatestate.Provider data that passed…
Browse files Browse the repository at this point in the history
… through to provider code have been initialised (#399)

Reference: #399
  • Loading branch information
bendbennett committed Aug 8, 2022
1 parent 4d2d121 commit f0166a6
Show file tree
Hide file tree
Showing 25 changed files with 980 additions and 147 deletions.
28 changes: 20 additions & 8 deletions internal/fromproto5/applyresourcechange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ func TestApplyResourceChangeRequest(t *testing.T) {
},
}

testProviderKeyValue := marshalToJson(map[string][]byte{
"providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`),
})

testProviderData, diags := privatestate.NewProviderData(context.Background(), testProviderKeyValue)
if diags.HasError() {
panic("error creating new provider data")
}

testEmptyProviderData, diags := privatestate.NewProviderData(context.Background(), nil)
if diags.HasError() {
panic("error creating new empty provider data")
}

testCases := map[string]struct {
input *tfprotov5.ApplyResourceChangeRequest
resourceSchema fwschema.Schema
Expand Down Expand Up @@ -151,27 +165,25 @@ func TestApplyResourceChangeRequest(t *testing.T) {
ResourceSchema: *testFwSchema,
PlannedPrivate: &privatestate.Data{
Framework: map[string][]byte{},
Provider: map[string][]byte{},
Provider: testEmptyProviderData,
},
},
},
"plannedprivate": {
input: &tfprotov5.ApplyResourceChangeRequest{
PlannedPrivate: marshalToJson(map[string][]byte{
".frameworkKey": []byte("framework value"),
"providerKey": []byte("provider value"),
".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`),
"providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`),
}),
},
resourceSchema: testFwSchema,
expected: &fwserver.ApplyResourceChangeRequest{
ResourceSchema: *testFwSchema,
PlannedPrivate: &privatestate.Data{
Framework: map[string][]byte{
".frameworkKey": []byte(`framework value`),
},
Provider: map[string][]byte{
"providerKey": []byte(`provider value`),
".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`),
},
Provider: testProviderData,
},
},
},
Expand Down Expand Up @@ -249,7 +261,7 @@ func TestApplyResourceChangeRequest(t *testing.T) {

got, diags := fromproto5.ApplyResourceChangeRequest(context.Background(), testCase.input, testCase.resourceType, testCase.resourceSchema, testCase.providerMetaSchema)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
if diff := cmp.Diff(got, testCase.expected, cmp.AllowUnexported(privatestate.ProviderData{})); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}

Expand Down
28 changes: 20 additions & 8 deletions internal/fromproto5/readresource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ func TestReadResourceRequest(t *testing.T) {
},
}

testProviderKeyValue := marshalToJson(map[string][]byte{
"providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`),
})

testProviderData, diags := privatestate.NewProviderData(context.Background(), testProviderKeyValue)
if diags.HasError() {
panic("error creating new provider data")
}

testEmptyProviderData, diags := privatestate.NewProviderData(context.Background(), nil)
if diags.HasError() {
panic("error creating new empty provider data")
}

testCases := map[string]struct {
input *tfprotov5.ReadResourceRequest
resourceSchema fwschema.Schema
Expand Down Expand Up @@ -112,26 +126,24 @@ func TestReadResourceRequest(t *testing.T) {
expected: &fwserver.ReadResourceRequest{
Private: &privatestate.Data{
Framework: map[string][]byte{},
Provider: map[string][]byte{},
Provider: testEmptyProviderData,
},
},
},
"private": {
input: &tfprotov5.ReadResourceRequest{
Private: marshalToJson(map[string][]byte{
".frameworkKey": []byte("framework value"),
"providerKey": []byte("provider value"),
".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`),
"providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`),
}),
},
resourceSchema: testFwSchema,
expected: &fwserver.ReadResourceRequest{
Private: &privatestate.Data{
Framework: map[string][]byte{
".frameworkKey": []byte(`framework value`),
},
Provider: map[string][]byte{
"providerKey": []byte(`provider value`),
".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`),
},
Provider: testProviderData,
},
},
},
Expand Down Expand Up @@ -175,7 +187,7 @@ func TestReadResourceRequest(t *testing.T) {

got, diags := fromproto5.ReadResourceRequest(context.Background(), testCase.input, testCase.resourceType, testCase.resourceSchema, testCase.providerMetaSchema)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
if diff := cmp.Diff(got, testCase.expected, cmp.AllowUnexported(privatestate.ProviderData{})); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}

Expand Down
28 changes: 20 additions & 8 deletions internal/fromproto6/applyresourcechange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ func TestApplyResourceChangeRequest(t *testing.T) {
},
}

testProviderKeyValue := marshalToJson(map[string][]byte{
"providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`),
})

testProviderData, diags := privatestate.NewProviderData(context.Background(), testProviderKeyValue)
if diags.HasError() {
panic("error creating new provider data")
}

testEmptyProviderData, diags := privatestate.NewProviderData(context.Background(), nil)
if diags.HasError() {
panic("error creating new empty provider data")
}

testCases := map[string]struct {
input *tfprotov6.ApplyResourceChangeRequest
resourceSchema fwschema.Schema
Expand Down Expand Up @@ -151,27 +165,25 @@ func TestApplyResourceChangeRequest(t *testing.T) {
ResourceSchema: *testFwSchema,
PlannedPrivate: &privatestate.Data{
Framework: map[string][]byte{},
Provider: map[string][]byte{},
Provider: testEmptyProviderData,
},
},
},
"plannedprivate": {
input: &tfprotov6.ApplyResourceChangeRequest{
PlannedPrivate: marshalToJson(map[string][]byte{
".frameworkKey": []byte("framework value"),
"providerKey": []byte("provider value"),
".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`),
"providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`),
}),
},
resourceSchema: testFwSchema,
expected: &fwserver.ApplyResourceChangeRequest{
ResourceSchema: *testFwSchema,
PlannedPrivate: &privatestate.Data{
Framework: map[string][]byte{
".frameworkKey": []byte(`framework value`),
},
Provider: map[string][]byte{
"providerKey": []byte(`provider value`),
".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`),
},
Provider: testProviderData,
},
},
},
Expand Down Expand Up @@ -249,7 +261,7 @@ func TestApplyResourceChangeRequest(t *testing.T) {

got, diags := fromproto6.ApplyResourceChangeRequest(context.Background(), testCase.input, testCase.resourceType, testCase.resourceSchema, testCase.providerMetaSchema)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
if diff := cmp.Diff(got, testCase.expected, cmp.AllowUnexported(privatestate.ProviderData{})); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}

Expand Down
28 changes: 20 additions & 8 deletions internal/fromproto6/readresource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ func TestReadResourceRequest(t *testing.T) {
},
}

testProviderKeyValue := marshalToJson(map[string][]byte{
"providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`),
})

testProviderData, diags := privatestate.NewProviderData(context.Background(), testProviderKeyValue)
if diags.HasError() {
panic("error creating new provider data")
}

testEmptyProviderData, diags := privatestate.NewProviderData(context.Background(), nil)
if diags.HasError() {
panic("error creating new empty provider data")
}

testCases := map[string]struct {
input *tfprotov6.ReadResourceRequest
resourceSchema fwschema.Schema
Expand Down Expand Up @@ -112,26 +126,24 @@ func TestReadResourceRequest(t *testing.T) {
expected: &fwserver.ReadResourceRequest{
Private: &privatestate.Data{
Framework: map[string][]byte{},
Provider: map[string][]byte{},
Provider: testEmptyProviderData,
},
},
},
"private": {
input: &tfprotov6.ReadResourceRequest{
Private: marshalToJson(map[string][]byte{
".frameworkKey": []byte("framework value"),
"providerKey": []byte("provider value"),
".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`),
"providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`),
}),
},
resourceSchema: testFwSchema,
expected: &fwserver.ReadResourceRequest{
Private: &privatestate.Data{
Framework: map[string][]byte{
".frameworkKey": []byte(`framework value`),
},
Provider: map[string][]byte{
"providerKey": []byte(`provider value`),
".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`),
},
Provider: testProviderData,
},
},
},
Expand Down Expand Up @@ -175,7 +187,7 @@ func TestReadResourceRequest(t *testing.T) {

got, diags := fromproto6.ReadResourceRequest(context.Background(), testCase.input, testCase.resourceType, testCase.resourceSchema, testCase.providerMetaSchema)

if diff := cmp.Diff(got, testCase.expected); diff != "" {
if diff := cmp.Diff(got, testCase.expected, cmp.AllowUnexported(privatestate.ProviderData{})); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}

Expand Down
Loading

0 comments on commit f0166a6

Please sign in to comment.