Skip to content

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

Merged
merged 4 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .evergreen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ tasks:
- func: "bootstrap mongo-orchestration"
vars:
TOPOLOGY: "server"
AUTH: "yes"
AUTH: "auth"
- func: "run tests"

- name: "test-standalone-ssl"
Expand Down Expand Up @@ -554,7 +554,7 @@ tasks:
- func: "bootstrap mongo-orchestration"
vars:
TOPOLOGY: "replica_set"
AUTH: "yes"
AUTH: "auth"
- func: "run tests"

- name: "test-replicaset-old"
Expand Down
51 changes: 51 additions & 0 deletions .evergreen/orchestration/configs/replica_sets/basic-ssl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"id": "repl0",
"members": [
{
"procParams": {
"ipv6": true,
"bind_ip": "127.0.0.1,::1",
"journal": true,
"oplogSize": 500,
"port": 27017
},
"rsParams": {
"tags": {
"ordinal": "one",
"dc": "ny"
}
}
},
{
"procParams": {
"ipv6": true,
"bind_ip": "127.0.0.1,::1",
"journal": true,
"oplogSize": 500,
"port": 27018
},
"rsParams": {
"tags": {
"ordinal": "two",
"dc": "pa"
}
}
},
{
"procParams": {
"ipv6": true,
"bind_ip": "127.0.0.1,::1",
"journal": true,
"port": 27019
},
"rsParams": {
}
}
],
"sslParams": {
"sslOnNormalPorts": true,
"sslPEMKeyFile": "ABSOLUTE_PATH_REPLACEMENT_TOKEN/.evergreen/x509gen/server.pem",
"sslCAFile": "ABSOLUTE_PATH_REPLACEMENT_TOKEN/.evergreen/x509gen/ca.pem",
"sslWeakCertificateValidation" : true
}
}
45 changes: 45 additions & 0 deletions .evergreen/orchestration/configs/replica_sets/basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"id": "repl0",
"members": [
{
"procParams": {
"ipv6": true,
"bind_ip": "127.0.0.1,::1",
"journal": true,
"oplogSize": 500,
"port": 27017
},
"rsParams": {
"tags": {
"ordinal": "one",
"dc": "ny"
}
}
},
{
"procParams": {
"ipv6": true,
"bind_ip": "127.0.0.1,::1",
"journal": true,
"oplogSize": 500,
"port": 27018
},
"rsParams": {
"tags": {
"ordinal": "two",
"dc": "pa"
}
}
},
{
"procParams": {
"ipv6": true,
"bind_ip": "127.0.0.1,::1",
"journal": true,
"port": 27019
},
"rsParams": {
}
}
]
}
3 changes: 2 additions & 1 deletion tests/server/server-executeQuery-008.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ $manager = new MongoDB\Driver\Manager(URI);

$primaryRp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY);
$primary = $manager->selectServer($primaryRp);
$serverCount = count($manager->getServers());

$bulk = new \MongoDB\Driver\BulkWrite;
$bulk->insert(['_id' => 1, 'x' => 1]);
$primary->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY));
$primary->executeBulkWrite(NS, $bulk, new MongoDB\Driver\WriteConcern($serverCount));

$secondaryRp = new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY);
$secondary = $manager->selectServer($secondaryRp);
Expand Down
33 changes: 22 additions & 11 deletions tests/server/server-getTags-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,32 @@ MongoDB\Driver\Server::getTags() with replica set
require_once __DIR__ . "/../utils/basic.inc";

$manager = new MongoDB\Driver\Manager(URI);
$command = new MongoDB\Driver\Command(['ping' => 1]);
$manager->executeCommand(DATABASE_NAME, $command);

$tags = $manager->selectServer(new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_PRIMARY))->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";
function assertSomeServerHasTags(array $servers, array $expectedTags) {
foreach ($servers as $server) {
/* Using a non-strict comparison guards against tags being returned in
* a different order than expected. */
if ($expectedTags == $server->getTags()) {
Copy link
Member

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).

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 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.

printf("Found server with tags: %s\n", json_encode($expectedTags));
return;
}
}

$tags = $manager->selectServer(new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY))->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";
printf("No server has tags: %s\n", json_encode($expectedTags));
}
Copy link
Member

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.

Copy link
Member Author

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().


$servers = $manager->getServers();
assertSomeServerHasTags($servers, ['dc' => 'ny', 'ordinal' => 'one']);
assertSomeServerHasTags($servers, ['dc' => 'pa', 'ordinal' => 'two']);
assertSomeServerHasTags($servers, []);

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
dc: ny
ordinal: one
dc: pa
ordinal: two
--EXPECT--
Found server with tags: {"dc":"ny","ordinal":"one"}
Found server with tags: {"dc":"pa","ordinal":"two"}
Found server with tags: []
===DONE===