Skip to content

Replace t.Fatal with require in Prometheus package #6690

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

Merged
merged 3 commits into from
Jun 29, 2024

Conversation

AshwinSriram11
Copy link
Member

Description

Continuation to #6659, #6680 and #6683.

Type of change

What type of changes does your code introduce to the Prometheus operator? Put an x in the box that apply.

  • CHANGE (fix or feature that would cause existing functionality to not work as expected)
  • FEATURE (non-breaking change which adds functionality)
  • BUGFIX (non-breaking change which fixes an issue)
  • ENHANCEMENT (non-breaking change which improves existing functionality)
  • NONE (if none of the other choices apply. Example, tooling, build system, CI, docs, etc.)

@AshwinSriram11 AshwinSriram11 requested a review from a team as a code owner June 18, 2024 11:39
if !found {
t.Fatalf("--tsdb.path argument should be given to the Thanos sidecar, got %q", strings.Join(sset.Spec.Template.Spec.Containers[3].Args, " "))
}
require.True(t, found, "--tsdb.path argument should be given to the Thanos sidecar, got %q", strings.Join(sset.Spec.Template.Spec.Containers[3].Args, " "))
Copy link
Member Author

Choose a reason for hiding this comment

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

I checked why the tests are failing and I got this error

--- FAIL: TestThanosObjectStorageFile (0.00s)
panic: runtime error: index out of range [3] with length 3 [recovered]
        panic: runtime error: index out of range [3] with length 3

When I changed it to sset.Spec.Template.Spec.Containers[2].Args, it was working fine. Wdyt about this?

Comment on lines 199 to 200
if !test.expectErr {
require.NoError(t, err)
Copy link
Member Author

Choose a reason for hiding this comment

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

This might be better. Wdyt?

require.False(t,(err!=nil && !test.expectErr),"unexpected error occurred: %v", err)

Copy link
Member

Choose a reason for hiding this comment

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

I think it looks good the way it is now, we just don't need the second if because, if we get there, it's always true.

What do you think about the suggestion below?

	for _, c := range cases {
		test := c
		t.Run(test.name, func(t *testing.T) {
			err := ValidateRemoteWriteSpec(test.spec)
			if test.expectErr {
				require.Error(t, err)
				return
			}
			require.NoError(t, err)
		})
	}

Comment on lines 184 to 174
if test.shouldContain {
t.Fatalf("expected Prometheus args to contain %v, but got %v", test.expectedArg, promArgs)
} else {
t.Fatalf("expected Prometheus args to NOT contain %v, but got %v", test.expectedArg, promArgs)
}
require.False(t, test.shouldContain, "expected Prometheus args to contain %v, but got %v", test.expectedArg, promArgs)
require.True(t, test.shouldContain, "expected Prometheus args to NOT contain %v, but got %v", test.expectedArg, promArgs)
Copy link
Member Author

Choose a reason for hiding this comment

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

I feel this is not a good method. There are many other places with same code. Wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

What if we just use require.Equal(t, test.shouldContain, found)?

I think this could replace the whole if block 🤔

Copy link
Member

@ArthurSens ArthurSens left a comment

Choose a reason for hiding this comment

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

There was a big refactoring of the tests in #6688, which already replaced the t.Fatal appearances. Could you rebase your branch and forget about the file pkg/prometheus/agent/statefulset_test.go? :)

I've done an incomplete review, I'll try to go over it again at another time!

Comment on lines 184 to 174
if test.shouldContain {
t.Fatalf("expected Prometheus args to contain %v, but got %v", test.expectedArg, promArgs)
} else {
t.Fatalf("expected Prometheus args to NOT contain %v, but got %v", test.expectedArg, promArgs)
}
require.False(t, test.shouldContain, "expected Prometheus args to contain %v, but got %v", test.expectedArg, promArgs)
require.True(t, test.shouldContain, "expected Prometheus args to NOT contain %v, but got %v", test.expectedArg, promArgs)
Copy link
Member

Choose a reason for hiding this comment

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

What if we just use require.Equal(t, test.shouldContain, found)?

I think this could replace the whole if block 🤔

Comment on lines 199 to 200
if !test.expectErr {
require.NoError(t, err)
Copy link
Member

Choose a reason for hiding this comment

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

I think it looks good the way it is now, we just don't need the second if because, if we get there, it's always true.

What do you think about the suggestion below?

	for _, c := range cases {
		test := c
		t.Run(test.name, func(t *testing.T) {
			err := ValidateRemoteWriteSpec(test.spec)
			if test.expectErr {
				require.Error(t, err)
				return
			}
			require.NoError(t, err)
		})
	}

Comment on lines 423 to 424
if err != nil && !tc.expectedErr {
t.Fatalf("expected no error, got: %v", err)
if !tc.expectedErr {
require.NoError(t, err)
}
if err == nil && tc.expectedErr {
t.Fatalf("expected an error, got nil")
if tc.expectedErr {
require.Error(t, err)
Copy link
Member

Choose a reason for hiding this comment

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

It's a similar situation here, we can use early returns to not need so many ifs

		t.Run(tc.scenario, func(t *testing.T) {
			err := validateRelabelConfig(&tc.prometheus, tc.relabelConfig)
			if tc.expectedErr {
				require.Error(t, err)
				return
			}
			require.NoError(t, err)
		})

Comment on lines 767 to 768
t.Logf("err %v", err)
if err != nil && !tc.expectedErr {
t.Fatalf("expected no error, got: %v", err)
if !tc.expectedErr {
require.NoError(t, err)
}
if err == nil && tc.expectedErr {
t.Fatalf("expected an error, got nil")
if tc.expectedErr {
require.Error(t, err)
Copy link
Member

Choose a reason for hiding this comment

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

Same thing here :)

require.Error(t, err)
require.Equal(t, expectedError, err.Error(), "expected makeRulesConfigMaps to return error '%v' but got '%v'", expectedError, err)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
require.Error(t, err)
require.Equal(t, expectedError, err.Error(), "expected makeRulesConfigMaps to return error '%v' but got '%v'", expectedError, err)
require.Equal(t, expectedError, err.Error(), "expected makeRulesConfigMaps to return error '%v' but got '%v'", expectedError, err)

If err is nil, the require.Equal(...) will catch it already, right?

if !found {
t.Fatalf("--tsdb.path argument should be given to the Thanos sidecar, got %q", strings.Join(sset.Spec.Template.Spec.Containers[3].Args, " "))
}
require.True(t, found, "--tsdb.path argument should be given to the Thanos sidecar, got %q", strings.Join(sset.Spec.Template.Spec.Containers[2].Args, " "))
Copy link
Member Author

Choose a reason for hiding this comment

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

I changed the index from 3 to 2 because I received the following error locally on running the test

--- FAIL: TestThanosObjectStorageFile (0.00s)
panic: runtime error: index out of range [3] with length 3 [recovered]
        panic: runtime error: index out of range [3] with length 3

After changing, it was running fine.

if !found {
t.Fatalf("--tsdb.path argument should be given to the Thanos sidecar, got %q", strings.Join(sset.Spec.Template.Spec.Containers[3].Args, " "))
}
require.True(t, found, "--tsdb.path argument should be given to the Thanos sidecar, got %q", strings.Join(sset.Spec.Template.Spec.Containers[2].Args, " "))
Copy link
Member Author

Choose a reason for hiding this comment

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

Same here too

I changed the index from 3 to 2 because I received the following error locally on running the test

--- FAIL: TestThanosObjectStorageFile (0.00s)
panic: runtime error: index out of range [3] with length 3 [recovered]
        panic: runtime error: index out of range [3] with length 3

After changing, it was running fine.

ArthurSens
ArthurSens previously approved these changes Jun 28, 2024
Copy link
Member

@ArthurSens ArthurSens left a comment

Choose a reason for hiding this comment

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

I've added just a few nitpicks, but generally LGTM :)

t.Fatalf("Expected input %sto be matching a prometheus agent key, but did not", notExp)
}
require.Equal(t, c.expectedKey, key, "Expected prometheus agent key %q got %q", c.expectedKey, key)
require.Equal(t, c.expectedMatch, match, "Expected input %sto be matching a prometheus agent key, but did not", map[bool]string{true: "", false: "not "}[c.expectedMatch])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
require.Equal(t, c.expectedMatch, match, "Expected input %sto be matching a prometheus agent key, but did not", map[bool]string{true: "", false: "not "}[c.expectedMatch])
require.Equal(t, c.expectedMatch, match)

I think this complicated logic doesn't help much 😅

t.Fatalf("Expected input %sto be matching a prometheus key, but did not", notExp)
}
require.Equal(t, c.expectedKey, key, "Expected prometheus key %q got %q", c.expectedKey, key)
require.Equal(t, c.expectedMatch, match, "Expected input %sto be matching a prometheus key, but did not", map[bool]string{true: "", false: "not "}[c.expectedMatch])
Copy link
Member

Choose a reason for hiding this comment

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

Same here 😬

Comment on lines 154 to 155
valLabel, ok := sset.Spec.Template.ObjectMeta.Labels["testlabel"]
require.True(t, (ok && valLabel == "testvalue"), "Pod labels are not properly propagated")
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to split this in 2 requires?

require.True(t, ok)
require.Equal(t, valLabel, "testvalue")

We could even just use require.Equal(t, valLabel, "testvalue") and forget about asserting ok I think. Because if valLabel == "testvalue" it's impossible for ok to be false

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, didn't think of it. Nice catch.

Comment on lines 157 to 158
valAnnotation, ok := sset.Spec.Template.ObjectMeta.Annotations["testannotation"]
require.True(t, (ok && valAnnotation == "testvalue"), "Pod annotations are not properly propagated")
Copy link
Member

Choose a reason for hiding this comment

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

Same here

Comment on lines 1408 to 1409
require.False(t, (foundRetention != test.shouldContainRetention || foundRetentionFlag != test.shouldContainRetention), "test %d, expected Prometheus args to %scontain %v, but got %v", i, map[bool]string{true: "", false: "NOT "}[test.shouldContainRetention], test.expectedRetentionArg, promArgs)
require.False(t, (foundRetentionSize != test.shouldContainRetentionSize || foundRetentionSizeFlag != test.shouldContainRetentionSize), "test %d, expected Prometheus args to %scontain %v, but got %v", i, map[bool]string{true: "", false: "NOT "}[test.shouldContainRetentionSize], test.expectedRetentionSizeArg, promArgs)
Copy link
Member

Choose a reason for hiding this comment

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

We don't necessarily need to keep the previous message with this refactoring. It would be even better to not add them at all I believe.

We added messages with t.Fatal because t.Fatal has no context whatsoever. The require package already adds a lot of context to the test, and emits nice human readable comparisons when the test fails.

We could simplify here with just:

if test.shouldContainRetention {
      require.True(t, (foundRetention && foundRetentionFlag))
}

if test.shouldContainRetentionSize {
      require.True(t, (foundRetentionSize && foundRetentionSizeFlag))
}

Copy link
Member

@ArthurSens ArthurSens left a comment

Choose a reason for hiding this comment

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

Good job, thanks!

@ArthurSens ArthurSens merged commit 709f4bf into prometheus-operator:main Jun 29, 2024
18 checks passed
@AshwinSriram11 AshwinSriram11 deleted the replace-Fatal-3 branch July 10, 2024 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants