-
Notifications
You must be signed in to change notification settings - Fork 208
Fix running auth tests #1206
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
Fix running auth tests #1206
Conversation
This was done for auth due to a bug fixed in 3.7.7, but the change was not reverted (see mongodb-labs/drivers-evergreen-tools#38).
$tags = $server->getTags(); | ||
echo "dc: ", array_key_exists('dc', $tags) ? $tags['dc'] : 'not set', "\n"; | ||
echo "ordinal: ", array_key_exists('ordinal', $tags) ? $tags['ordinal'] : 'not set', "\n"; | ||
} |
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.
Is this still a potentially flaky test if the order of servers returned by getServers()
is non-deterministic? If so, perhaps this needs to be rewritten to instead have a function that asserts any server has certain tags. For example:
function assertSomeServerHasTags($servers, $expectedTags) {
foreach ($servers as $server) {
if (array_intersect_assoc($expectedTags, $server->getTags()) === $expectedTags) {
printf("Found server with tags: %s\n", json_encode($expectedTags));
return;
}
}
printf("No server has tags: %s\n", json_encode($expectedTags));
}
$servers = $manager->getServers();
assertSomeServerHasTags($servers, ['dc' => 'ny', 'ordinal' => 'one']);
assertSomeServerHasTags($servers, ['dc' => 'pa', 'ordinal' => 'two']);
assertSomeServerHasTags($servers, []);
My understanding of array_intersect_assoc()
is that its return value uses the same key ordering as the first argument, which makes it possible to do the ===
check above.
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 point. I've kept the loop but removed the use of array_intersect_assoc
, as the last check will match for any server:
php > var_dump(array_intersect_assoc([], ['dc' => 'pa', 'ordinal' => 'two']));
array(0) {
}
A loose equality check works around this, and since we're expecting the tags in the order that we create them I think that should be safe but still protect us against non-deterministic order of the server returned from getServers()
.
eba4821
to
c800146
Compare
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.
Suggestion to change to strict equality in the test, but LGTM either way.
echo "ordinal: ", array_key_exists('ordinal', $tags) ? $tags['ordinal'] : 'not set', "\n"; | ||
function assertSomeServerHasTags(array $servers, array $expectedTags) { | ||
foreach ($servers as $server) { | ||
if ($expectedTags == $server->getTags()) { |
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.
If getTags()
returns an array, is there any reason we can't do a strict equality check here?
Both are going to enforce the same order AFAIK, and the associative array itself just has string keys and values (no concern with mismatched types).
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 to a strict comparison, which then caused tests to fail again. With some debug output, I was able to capture the following diff:
003+ No server has tags: {"ordinal":"two","dc":"pa"}
004+ Server tags: {"localhost:27017":{"ordinal":"one","dc":"ny"},"localhost:27018":{"dc":"pa","ordinal":"two"},"localhost:27019":[]}
003- Found server with tags: {"ordinal":"two","dc":"pa"}
As you can see, the tags returned for the second server has a different order from the first, which causes a strict equality check to fail. I've added a comment to clarify why we're using ==
in the test for future reference.
0bba665
to
63b6a41
Compare
63b6a41
to
4abf4a7
Compare
While changing test setup to rely on drivers-evergreen-tools, I missed that the
AUTH
variable needs to be "auth" for drivers-evergreen-tools while "yes" was used for our own scripts. This PR updates the build config and fixes a test that can be flaky due to potential elections in the replica set.