-
Notifications
You must be signed in to change notification settings - Fork 208
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
Add tests to check for V1CheckpointManager fixing dbGuid if mis-match. #1064
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0aa1aa7
Add tests to check for V1CheckpointManager fixing dbGuid if mis-match.
calebmshafer fbff17b
Merge branch 'master' into add-checkpoint-dbguid-test
mergify[bot] 619f1e1
Merge branch 'master' into add-checkpoint-dbguid-test
mergify[bot] 38c056b
Merge branch 'master' into add-checkpoint-dbguid-test
mergify[bot] 0f457b4
fix lint errors
calebmshafer b3f456e
Merge branch 'master' into add-checkpoint-dbguid-test
mergify[bot] 0a9e98d
Merge branch 'master' into add-checkpoint-dbguid-test
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
common/changes/@bentley/imodeljs-backend/add-checkpoint-dbguid-test_2021-03-30-04-00.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@bentley/imodeljs-backend", | ||
"comment": "", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@bentley/imodeljs-backend", | ||
"email": "31107829+calebmshafer@users.noreply.github.com" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
core/backend/src/test/standalone/CheckpointManager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { assert } from "chai"; | ||
import * as path from "path"; | ||
import * as sinon from "sinon"; | ||
import { ClientRequestContext } from "@bentley/bentleyjs-core"; | ||
import { AccessToken, AuthorizedClientRequestContext } from "@bentley/itwin-client"; | ||
import { CheckpointManager, V1CheckpointManager } from "../../CheckpointManager"; | ||
import { IModelHost } from "../../imodeljs-backend"; | ||
import { SnapshotDb } from "../../IModelDb"; | ||
import { IModelJsFs } from "../../IModelJsFs"; | ||
import { Guid } from "@bentley/bentleyjs-core"; | ||
import { IModelTestUtils } from "../IModelTestUtils"; | ||
|
||
describe("V1 Checkpoint Manager", () => { | ||
it("empty props", async () => { | ||
const props = { | ||
contextId: "", | ||
iModelId: "", | ||
changeSetId: "", | ||
requestContext: new AuthorizedClientRequestContext(new AccessToken()), | ||
}; | ||
assert.equal(V1CheckpointManager.getFileName(props), path.join(IModelHost.cacheDir, "imodels", "checkpoints", "first.bim")); | ||
}); | ||
|
||
it("changeset only props", async () => { | ||
const props = { | ||
contextId: "", | ||
iModelId: "", | ||
changeSetId: "1234", | ||
requestContext: new AuthorizedClientRequestContext(new AccessToken()), | ||
}; | ||
assert.equal(V1CheckpointManager.getFileName(props), path.join(IModelHost.cacheDir, "imodels", "checkpoints", "1234.bim")); | ||
}); | ||
|
||
it("changeset+context props", async () => { | ||
const props = { | ||
contextId: "5678", | ||
iModelId: "", | ||
changeSetId: "1234", | ||
requestContext: new AuthorizedClientRequestContext(new AccessToken()), | ||
}; | ||
assert.equal(V1CheckpointManager.getFileName(props), path.join(IModelHost.cacheDir, "imodels", "checkpoints", "1234.bim")); | ||
}); | ||
|
||
it("changeset+context+imodel props", async () => { | ||
const props = { | ||
contextId: "5678", | ||
iModelId: "910", | ||
changeSetId: "1234", | ||
requestContext: new AuthorizedClientRequestContext(new AccessToken()), | ||
}; | ||
assert.equal(V1CheckpointManager.getFileName(props), path.join(IModelHost.cacheDir, "imodels", "910", "checkpoints", "1234.bim")); | ||
}); | ||
|
||
it("getFolder", async () => { | ||
assert.equal(V1CheckpointManager.getFolder("1234"), path.join(IModelHost.cacheDir, "imodels", "1234", "checkpoints")); | ||
assert.equal(V1CheckpointManager.getFolder(""), path.join(IModelHost.cacheDir, "imodels", "checkpoints")); | ||
}); | ||
|
||
it("should fix invalid dbGuid during download", async () => { | ||
const dbPath = IModelTestUtils.prepareOutputFile("IModel", "TestCheckpoint.bim"); | ||
const snapshot = SnapshotDb.createEmpty(dbPath, { rootSubject: { name: "test" } }); | ||
const iModelId = Guid.createValue(); // This is wrong - it should be `snapshot.getGuid()`! | ||
const contextId = Guid.createValue(); | ||
const changeSetId = IModelTestUtils.generateChangeSetId(); | ||
snapshot.nativeDb.saveProjectGuid(Guid.normalize(contextId)); | ||
snapshot.nativeDb.saveLocalValue("ParentChangeSetId", changeSetId); | ||
snapshot.saveChanges(); | ||
|
||
assert.notEqual(iModelId, snapshot.nativeDb.getDbGuid()); // Ensure the Snapshot dbGuid and iModelId are different | ||
|
||
snapshot.close(); | ||
|
||
const mockCheckpoint = { | ||
wsgId: "INVALID", | ||
ecId: "INVALID", | ||
changeSetId, | ||
downloadUrl: `INVALID`, | ||
mergedChangeSetId: changeSetId, | ||
}; | ||
|
||
const checkpointsHandler = IModelHost.iModelClient.checkpoints; | ||
sinon.stub(checkpointsHandler, "get").callsFake(async () => [mockCheckpoint]); | ||
sinon.stub(IModelHost.iModelClient, "checkpoints").get(() => checkpointsHandler); | ||
|
||
const fileHandler = IModelHost.iModelClient.fileHandler!; | ||
sinon.stub(fileHandler, "downloadFile").callsFake(async (_requestContext: AuthorizedClientRequestContext, _downloadUrl: string, path: string) => { | ||
IModelJsFs.copySync(dbPath, path); | ||
}); | ||
sinon.stub(IModelHost.iModelClient, "fileHandler").get(() => fileHandler); | ||
|
||
const ctx = ClientRequestContext.current as AuthorizedClientRequestContext; | ||
const downloadedDbPath = IModelTestUtils.prepareOutputFile("IModel", "TestCheckpoint2.bim"); | ||
await V1CheckpointManager.downloadCheckpoint({ localFile: downloadedDbPath, checkpoint: { requestContext: ctx, contextId, iModelId, changeSetId } }) | ||
const db = SnapshotDb.openCheckpointV1(downloadedDbPath, { requestContext: ctx, contextId, iModelId, changeSetId }); | ||
assert.equal(iModelId, db.nativeDb.getDbGuid(), "expected the V1 Checkpoint download to fix the improperly set dbGuid."); | ||
}); | ||
}); | ||
|
||
describe("Checkpoint Manager", () => { | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it("open missing local file should return undefined", async () => { | ||
const checkpoint = { | ||
contextId: "5678", | ||
iModelId: "910", | ||
changeSetId: "1234", | ||
requestContext: new AuthorizedClientRequestContext(new AccessToken()), // Why is this on CheckpointProps rather than DownloadRequest | ||
}; | ||
const request = { | ||
localFile: V1CheckpointManager.getFileName(checkpoint), | ||
checkpoint, | ||
}; | ||
const db = CheckpointManager.tryOpenLocalFile(request); | ||
assert.isUndefined(db); | ||
}); | ||
|
||
it("open a bad bim file should return undefined", async () => { | ||
const checkpoint = { | ||
contextId: "5678", | ||
iModelId: "910", | ||
changeSetId: "1234", | ||
requestContext: new AuthorizedClientRequestContext(new AccessToken()), // Why is this on CheckpointProps rather than DownloadRequest | ||
}; | ||
|
||
// Setup a local file | ||
const folder = V1CheckpointManager.getFolder(checkpoint.iModelId); | ||
if (!IModelJsFs.existsSync(folder)) | ||
IModelJsFs.recursiveMkDirSync(folder); | ||
|
||
const outputFile = V1CheckpointManager.getFileName(checkpoint); | ||
if (IModelJsFs.existsSync(outputFile)) | ||
IModelJsFs.unlinkSync(outputFile); | ||
|
||
IModelJsFs.writeFileSync(outputFile, "Testing"); | ||
|
||
// Attempt to open the file | ||
const request = { | ||
localFile: V1CheckpointManager.getFileName(checkpoint), | ||
checkpoint, | ||
}; | ||
const db = CheckpointManager.tryOpenLocalFile(request); | ||
assert.isUndefined(db); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@calebmshafer I thought I would make this api public for downloading V2 checkpoints. But, the public way to do that is
BriefcaseManager.downloadBriefcase
that takes aRequestNewBriefcaseArg
, so everything aboutCheckpointManager
should be internal.