Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Recover from interface assertion panic in convert func#948

Merged
iyabchen merged 6 commits into
GoogleCloudPlatform:mainfrom
iyabchen:interface-panic
Sep 14, 2022
Merged

Recover from interface assertion panic in convert func#948
iyabchen merged 6 commits into
GoogleCloudPlatform:mainfrom
iyabchen:interface-panic

Conversation

@iyabchen

@iyabchen iyabchen commented Sep 2, 2022

Copy link
Copy Markdown
Contributor

b/239699320

@iyabchen iyabchen requested review from a team and melinath and removed request for a team September 2, 2022 14:30
@melinath

melinath commented Sep 2, 2022

Copy link
Copy Markdown
Member

I think you said you were concerned about not being able to test this - but it looks like your tests are working fine? Could you elaborate?

@iyabchen

iyabchen commented Sep 2, 2022

Copy link
Copy Markdown
Contributor Author

I think you said you were concerned about not being able to test this - but it looks like your tests are working fine? Could you elaborate?

I am not able to test by deleting a field in .tfplan.json, and trigger the interface assertion. for example, for a google_compute_instance tfplan, I converted it to .tfplan.json first, then in the file, I delete "description" and its value in .tfplan.json. In compute_instance.go, the code was doing d.Get("description").(string), so I thought it should get nil and trigger the error, but it did not.

The way I can trigger the error is, change d.Get("description").(string) -> d.Get("abc").(string), that might match what a terraform provider schema change.

The unit test I did was, it uses a fake converter with a fake convert function, so it is totally not related to any individual resource converter's logic.

@melinath

Copy link
Copy Markdown
Member

The way I can trigger the error is, change d.Get("description").(string) -> d.Get("abc").(string), that might match what a terraform provider schema change.

Ah - yeah, I guess that makes sense. If TFV knows the schema type of the field, then it converts nil to the "zero value" for that schema type. (This is the same behavior that TF uses.)

if r.Value == nil && s != nil {

So the only way to trigger the panic is for the converter to try to access a field that isn't in the schema TFV has - which is what you've done with d.Get("abc").

@melinath

Copy link
Copy Markdown
Member

That seems odd, though - if that's the only trigger, users should never really run into this, since we keep the TPG / TFV versions in sync. Possibly they're on an older version?

@melinath

Copy link
Copy Markdown
Member

After thinking about this a little more, I'm not entirely sure that I believe that the TPG and TFV versions being out of sync is the source of this error. It might make sense to just catch panics and display them to users including the resource address so that we can get better debug information from users in the future & more accurately determine a root cause.

@iyabchen

Copy link
Copy Markdown
Contributor Author

After thinking about this a little more, I'm not entirely sure that I believe that the TPG and TFV versions being out of sync is the source of this error. It might make sense to just catch panics and display them to users including the resource address so that we can get better debug information from users in the future & more accurately determine a root cause.

Updated the code to include stack trace when it recovers from panic, and added the rd.Kind() into the error message.

@melinath melinath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple minor requests, otherwise LGTM

Comment thread converters/google/convert.go Outdated
Comment thread converters/google/convert.go Outdated
@iyabchen iyabchen merged commit 9cb4f44 into GoogleCloudPlatform:main Sep 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants