Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use require to prevent a panic when dereferencing a nil pointer #458

Merged
merged 1 commit into from Jul 14, 2022

Conversation

sebasslash
Copy link
Contributor

@sebasslash sebasslash commented Jul 6, 2022

Description

This PR stemmed from a panic from occurring during the execution of TestPolicySetsRead/with_policy_set_version.

Failed
=== RUN   TestPolicySetsRead/with_policy_set_version
    --- FAIL: TestPolicySetsRead/with_policy_set_version (0.40s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x73cf25]

goroutine 2388 [running]:
testing.tRunner.func1.2({0x807ca0, 0xb8e330})
	/usr/local/go/src/testing/testing.go:1209 +0x24e
testing.tRunner.func1()
	/usr/local/go/src/testing/testing.go:1212 +0x218
panic({0x807ca0, 0xb8e330})
	/usr/local/go/src/runtime/panic.go:1038 +0x215
github.com/hashicorp/go-tfe.TestPolicySetsRead.func3(0x91c5b0)
	/home/circleci/project/policy_set_integration_test.go:298 +0x445
testing.tRunner(0xc0005069c0, 0xc00059f9e0)
	/usr/local/go/src/testing/testing.go:1259 +0x102
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:1306 +0x35a
exit status 2

What's happening here is the test is expecting two relationships to be included in the response, CurrentVersion and NewestVersion, but was subject to the overall flakiness of our CI environment. This PR does not fix nor prevent the flakiness.

Note: Why require and not assert? require makes the same calls as assert but it will terminate the execution of a test upon failure. Using assert would still continue the test's execution, dereference a nil pointer, and boom panic!

Therefore this PR attempts to go through each integration test and swap assert for require in instances where:

  • We check for the existence of nested fields (we require the parent object to be present)
  • We use the include query param in a test
  • When "assertion" order matters; pretty much ties into the first point

@sebasslash sebasslash force-pushed the patch-panic branch 3 times, most recently from 7a4133f to c4da100 Compare Jul 11, 2022

assert.NotEmpty(t, rl.Items)
assert.NotNil(t, rl.Items[0].Workspace)
require.NotNil(t, rl.Items[0].Workspace)
Copy link
Contributor

@Uk1288 Uk1288 Jul 13, 2022

Choose a reason for hiding this comment

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

should the require be added to lines#76 require.NotEmpty instead?

Copy link
Contributor Author

@sebasslash sebasslash Jul 13, 2022

Choose a reason for hiding this comment

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

Yes! It should be thanks 👍

Uk1288
Uk1288 previously approved these changes Jul 13, 2022
Copy link
Contributor

@Uk1288 Uk1288 left a comment

Looks good to me 🎉

This is a workaround to simply prevent a panic from occurring. What's happening here
is the test is expecting two relationships to be included in the response, CurrentVersion and NewestVersion, but was
subject to the overall flakiness of our CI environment. This commit does not fix nor prevent the flakiness.

Why `require` and not `assert`? `require` makes the same calls as `assert` except it will terminate the execution
of a test upon failure. Using `assert` would still continue the test's execution, dereference a nil pointer, and boom panic!
Uk1288
Uk1288 approved these changes Jul 14, 2022
@sebasslash sebasslash merged commit b62af28 into main Jul 14, 2022
6 checks passed
@sebasslash sebasslash deleted the patch-panic branch Jul 14, 2022
@github-actions
Copy link

github-actions bot commented Jul 14, 2022

Reminder to the contributor that merged this PR: if your changes have added important functionality or fixed a relevant bug, open a follow-up PR to update CHANGELOG.md with a note on your changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants