Skip to content

Commit

Permalink
Merge pull request #224 from bcgov/syncTags
Browse files Browse the repository at this point in the history
Change SyncTags to only backfill coms-id tag for latest version
  • Loading branch information
wilwong89 authored Oct 26, 2023
2 parents 0da83be + a3d90f4 commit 9199c6a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
16 changes: 12 additions & 4 deletions app/src/services/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,17 +328,25 @@ const service = {
const comsTags = comsTagsForVersion[0]?.tagset ?? [];
// S3 Tags
const s3Tags = toLowerKeys(s3TagsForVersion?.TagSet ?? []);

// Ensure `coms-id` tag exists on this version in S3
if (s3Tags.length < 10 && !s3Tags.find(s3T => s3T.key === 'coms-id')) {
/**
* Add coms-id tag to latest version in S3 if not already present
* NOTE: For a sync job the _deriveObjectId() function will have already added
* the coms-id to latest version.
* TODO: check if this version is still also the latest on corresponding version in S3
*/
if (comsVersion.isLatest && s3Tags.length < 10 && !s3Tags.find(s3T => s3T.key === 'coms-id')) {
/**
* NOTE: adding tags to a specified version (passing a `VersionId` parameter) will affect `Last Modified`
* attribute of multiple versions on some s3 storage providors including Dell ECS
*/
await storageService.putObjectTagging({
filePath: path,
tags: (s3TagsForVersion?.TagSet ?? []).concat([{
Key: 'coms-id',
Value: comsVersion.objectId
}]),
s3VersionId: comsVersion.s3VersionId,
bucketId: bucketId,
// s3VersionId: comsVersion.s3VersionId,
});
// add to our arrays for comaprison
s3Tags.push({ key: 'coms-id', value: comsVersion.objectId });
Expand Down
72 changes: 70 additions & 2 deletions app/tests/unit/services/sync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,8 @@ describe('syncTags', () => {
const comsVersion = {
id: validUuidv4,
objectId: validUuidv4,
s3VersionId: validUuidv4
s3VersionId: validUuidv4,
isLatest: true
};

beforeEach(() => {
Expand Down Expand Up @@ -898,7 +899,6 @@ describe('syncTags', () => {
Key: 'coms-id',
Value: comsVersion.objectId
}]),
s3VersionId: comsVersion.s3VersionId,
bucketId: bucketId,
}));
expect(versionTrx.commit).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -940,7 +940,75 @@ describe('syncTags', () => {
Key: 'coms-id',
Value: comsVersion.objectId
}]),
bucketId: bucketId,
}));
expect(versionTrx.commit).toHaveBeenCalledTimes(1);
});

it('should not write coms-id tag when coms version is not latest', async () => {
fetchTagsForVersionSpy.mockResolvedValue([{}]);
getObjectTaggingSpy.mockResolvedValue({});
putObjectTaggingSpy.mockResolvedValue({});

comsVersion.isLatest = false;
const result = await service.syncTags(comsVersion, path, bucketId);
// reset for other tests
comsVersion.isLatest = true;

expect(result).toBeTruthy();
expect(Array.isArray(result)).toBeTruthy();
expect(result).toHaveLength(0);

expect(Version.startTransaction).toHaveBeenCalledTimes(1);
expect(associateTagsSpy).toHaveBeenCalledTimes(0);
expect(dissociateTagsSpy).toHaveBeenCalledTimes(0);
expect(fetchTagsForVersionSpy).toHaveBeenCalledTimes(1);
expect(fetchTagsForVersionSpy).toHaveBeenCalledWith(expect.objectContaining({
versionIds: comsVersion.id
}), expect.any(Object));
expect(getObjectTaggingSpy).toHaveBeenCalledTimes(1);
expect(getObjectTaggingSpy).toHaveBeenCalledWith(expect.objectContaining({
filePath: path,
s3VersionId: comsVersion.s3VersionId,
bucketId: bucketId
}));
expect(getSpy).toHaveBeenCalledTimes(0);
expect(putObjectTaggingSpy).toHaveBeenCalledTimes(0);
expect(versionTrx.commit).toHaveBeenCalledTimes(1);
});

it('should write coms-id tag when coms version is latest', async () => {
fetchTagsForVersionSpy.mockResolvedValue([{}]);
getObjectTaggingSpy.mockResolvedValue({});
putObjectTaggingSpy.mockResolvedValue({});

const result = await service.syncTags(comsVersion, path, bucketId);

expect(result).toBeTruthy();
expect(Array.isArray(result)).toBeTruthy();
expect(result).toHaveLength(1);

expect(Version.startTransaction).toHaveBeenCalledTimes(1);
expect(associateTagsSpy).toHaveBeenCalledTimes(1);
expect(dissociateTagsSpy).toHaveBeenCalledTimes(0);
expect(fetchTagsForVersionSpy).toHaveBeenCalledTimes(1);
expect(fetchTagsForVersionSpy).toHaveBeenCalledWith(expect.objectContaining({
versionIds: comsVersion.id
}), expect.any(Object));
expect(getObjectTaggingSpy).toHaveBeenCalledTimes(1);
expect(getObjectTaggingSpy).toHaveBeenCalledWith(expect.objectContaining({
filePath: path,
s3VersionId: comsVersion.s3VersionId,
bucketId: bucketId
}));
expect(getSpy).toHaveBeenCalledTimes(0);
expect(putObjectTaggingSpy).toHaveBeenCalledTimes(1);
expect(putObjectTaggingSpy).toHaveBeenCalledWith(expect.objectContaining({
filePath: path,
tags: expect.arrayContaining([{
Key: 'coms-id',
Value: comsVersion.objectId
}]),
bucketId: bucketId,
}));
expect(versionTrx.commit).toHaveBeenCalledTimes(1);
Expand Down

0 comments on commit 9199c6a

Please sign in to comment.