|
| 1 | +import { When, Then } from '@cucumber/cucumber'; |
| 2 | +import Zenko from '../world/Zenko'; |
| 3 | +import { createAndRunPod, getMongoDBConfig, getZenkoVersion } from 'steps/utils/kubernetes'; |
| 4 | +import assert from 'assert'; |
| 5 | +import { GetObjectCommand } from '@aws-sdk/client-s3'; |
| 6 | +import { Utils } from 'cli-testing'; |
| 7 | +import { getObject, headObject, getReplicationLocationConfig } from 'steps/utils/utils'; |
| 8 | +import { safeJsonParse } from 'common/utils'; |
| 9 | + |
| 10 | +When('I run the job to replicate existing objects with status {string}', |
| 11 | + { timeout: 600000 }, |
| 12 | + async function ( |
| 13 | + this: Zenko, |
| 14 | + sourceObjectStatus: string, |
| 15 | + ) { |
| 16 | + const sourceBucket = this.getSaved<string>('bucketName'); |
| 17 | + const replicationLocation = this.getSaved<string>('replicationLocation'); |
| 18 | + const { replicaSetHosts } = await getMongoDBConfig(this); |
| 19 | + const { locationType } = await getReplicationLocationConfig(this, replicationLocation); |
| 20 | + const zenkoVersion = await getZenkoVersion(this); |
| 21 | + const s3utilsVersion = zenkoVersion.spec.versions.s3utils; |
| 22 | + const podManifest = { |
| 23 | + apiVersion: 'v1', |
| 24 | + kind: 'Pod', |
| 25 | + metadata: { |
| 26 | + name: `s3utils-crr-existing-${Utils.randomString().toLowerCase()}`, |
| 27 | + namespace: 'default', |
| 28 | + labels: { |
| 29 | + app: 's3utils', |
| 30 | + script: 'crrExistingObjects.js' |
| 31 | + } |
| 32 | + }, |
| 33 | + spec: { |
| 34 | + restartPolicy: 'Never', |
| 35 | + containers: [ |
| 36 | + { |
| 37 | + name: 's3utils', |
| 38 | + image: `${s3utilsVersion.image}:${s3utilsVersion.tag}`, |
| 39 | + command: ['node'], |
| 40 | + args: ['crrExistingObjects.js', sourceBucket], |
| 41 | + env: [ |
| 42 | + { |
| 43 | + name: 'MONGODB_REPLICASET', |
| 44 | + value: replicaSetHosts.join(',') |
| 45 | + }, |
| 46 | + { |
| 47 | + name: 'MONGODB_AUTH_USERNAME', |
| 48 | + valueFrom: { |
| 49 | + secretKeyRef: { |
| 50 | + name: 'mongodb-db-creds', |
| 51 | + key: 'mongodb-username' |
| 52 | + } |
| 53 | + } |
| 54 | + }, |
| 55 | + { |
| 56 | + name: 'MONGODB_AUTH_PASSWORD', |
| 57 | + valueFrom: { |
| 58 | + secretKeyRef: { |
| 59 | + name: 'mongodb-db-creds', |
| 60 | + key: 'mongodb-password' |
| 61 | + } |
| 62 | + } |
| 63 | + }, |
| 64 | + { |
| 65 | + name: 'MONGODB_DATABASE', |
| 66 | + valueFrom: { |
| 67 | + secretKeyRef: { |
| 68 | + name: 'mongodb-db-creds', |
| 69 | + key: 'mongodb-database' |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | + { name: 'MONGODB_SHARD_COLLECTIONS', value: 'true' }, |
| 74 | + { name: 'STORAGE_TYPE', value: locationType }, |
| 75 | + { name: 'TARGET_REPLICATION_STATUS', value: sourceObjectStatus }, |
| 76 | + { name: 'SITE_NAME', value: replicationLocation }, |
| 77 | + ] |
| 78 | + } |
| 79 | + ] |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + await createAndRunPod(this, podManifest); |
| 84 | + }); |
| 85 | + |
| 86 | +Then('the object should eventually be replicated', |
| 87 | + async function (this: Zenko) { |
| 88 | + const objectName = this.getSaved<string>('objectName'); |
| 89 | + const bucketSource = this.getSaved<string>('bucketName'); |
| 90 | + const startTime = Date.now(); |
| 91 | + const replicationTimeoutMs = 90_000; |
| 92 | + while (Date.now() - startTime < replicationTimeoutMs) { |
| 93 | + await new Promise(resolve => setTimeout(resolve, 3000)); |
| 94 | + |
| 95 | + const response = await headObject(this, objectName, bucketSource); |
| 96 | + assert(response.stdout); |
| 97 | + assert.strictEqual(response.statusCode, 200, `failed to headobject, ${response.statusCode}`); |
| 98 | + const parsed = safeJsonParse<{ |
| 99 | + ReplicationStatus?: string; |
| 100 | + LastModified?: string; |
| 101 | + ETag?: string; |
| 102 | + ContentLength?: number; |
| 103 | + VersionId?: string; |
| 104 | + Metadata?: Record<string, string>; |
| 105 | + }>(response.stdout || '{}'); |
| 106 | + assert(parsed.ok); |
| 107 | + const replicationStatus = parsed.result?.ReplicationStatus; |
| 108 | + assert.notStrictEqual(replicationStatus, 'FAILED', `replication failed for object ${objectName}`); |
| 109 | + if (replicationStatus === 'COMPLETED') { |
| 110 | + return; |
| 111 | + } |
| 112 | + if (replicationStatus === 'PENDING' || replicationStatus === 'PROCESSING') { |
| 113 | + continue; |
| 114 | + } |
| 115 | + } |
| 116 | + assert.fail(`Timeout: Object '${objectName}' was not replicated successfully until timeout`); |
| 117 | + }); |
| 118 | + |
| 119 | +Then( |
| 120 | + 'the replicated object should be the same as the source object', |
| 121 | + async function ( |
| 122 | + this: Zenko, |
| 123 | + ) { |
| 124 | + const objectName = this.getSaved<string>('objectName'); |
| 125 | + const bucketSource = this.getSaved<string>('bucketName'); |
| 126 | + const replicationLocation = this.getSaved<string>('replicationLocation'); |
| 127 | + const { destinationBucket, bucketMatch, awsS3Client } = |
| 128 | + await getReplicationLocationConfig(this, replicationLocation); |
| 129 | + |
| 130 | + // When bucketMatch is disabled on the destination bucket, |
| 131 | + // replicated objects are named sourceBucket/objectName |
| 132 | + let key = `${bucketSource}/${objectName}`; |
| 133 | + if (bucketMatch) { |
| 134 | + key = objectName; |
| 135 | + } |
| 136 | + |
| 137 | + const command = new GetObjectCommand({ |
| 138 | + Bucket: destinationBucket, |
| 139 | + Key: key, |
| 140 | + }); |
| 141 | + const replicaObj = await awsS3Client.send(command); |
| 142 | + const sourceResponse = await getObject(this, objectName, bucketSource); |
| 143 | + assert.strictEqual(sourceResponse.statusCode, 200, `failed to getObject, ${sourceResponse.statusCode}`); |
| 144 | + const sourceObj = safeJsonParse<{ |
| 145 | + ReplicationStatus?: string; |
| 146 | + LastModified?: string; |
| 147 | + ETag?: string; |
| 148 | + ContentLength?: number; |
| 149 | + VersionId?: string; |
| 150 | + Metadata?: Record<string, string>; |
| 151 | + }>(sourceResponse.stdout || '{}'); |
| 152 | + assert(sourceObj.ok); |
| 153 | + |
| 154 | + assert.strictEqual(sourceObj.result?.ReplicationStatus, 'COMPLETED'); |
| 155 | + assert.strictEqual( |
| 156 | + sourceObj.result?.ContentLength, |
| 157 | + replicaObj.ContentLength |
| 158 | + ); |
| 159 | + assert.strictEqual( |
| 160 | + sourceObj.result?.Metadata?.[`${replicationLocation}-version-id`], |
| 161 | + replicaObj.VersionId |
| 162 | + ); |
| 163 | + assert.strictEqual( |
| 164 | + sourceObj.result?.Metadata?.[`${replicationLocation}-replication-status`], |
| 165 | + 'COMPLETED' |
| 166 | + ); |
| 167 | + assert.strictEqual( |
| 168 | + sourceObj.result?.VersionId, |
| 169 | + replicaObj.Metadata?.['scal-version-id'] |
| 170 | + ); |
| 171 | + assert.strictEqual( |
| 172 | + replicaObj.Metadata?.['scal-replication-status'], |
| 173 | + 'REPLICA' |
| 174 | + ); |
| 175 | + }); |
0 commit comments