-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Replace t.Fatal with require in Prometheus package #6690
Conversation
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, " ")) |
There was a problem hiding this comment.
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?
pkg/prometheus/operator_test.go
Outdated
if !test.expectErr { | ||
require.NoError(t, err) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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)
})
}
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 🤔
There was a problem hiding this 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!
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) |
There was a problem hiding this comment.
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 🤔
pkg/prometheus/operator_test.go
Outdated
if !test.expectErr { | ||
require.NoError(t, err) |
There was a problem hiding this comment.
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)
})
}
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) |
There was a problem hiding this comment.
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)
})
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing here :)
pkg/prometheus/server/rules_test.go
Outdated
require.Error(t, err) | ||
require.Equal(t, expectedError, err.Error(), "expected makeRulesConfigMaps to return error '%v' but got '%v'", expectedError, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
7e6342e
to
509013b
Compare
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, " ")) |
There was a problem hiding this comment.
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, " ")) |
There was a problem hiding this comment.
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.
There was a problem hiding this 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]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here 😬
valLabel, ok := sset.Spec.Template.ObjectMeta.Labels["testlabel"] | ||
require.True(t, (ok && valLabel == "testvalue"), "Pod labels are not properly propagated") |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
valAnnotation, ok := sset.Spec.Template.ObjectMeta.Annotations["testannotation"] | ||
require.True(t, (ok && valAnnotation == "testvalue"), "Pod annotations are not properly propagated") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
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) |
There was a problem hiding this comment.
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))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job, thanks!
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.)