|
| 1 | +/* Copyright (C) 2024 NooBaa */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const path = require('path'); |
| 5 | +const mocha = require('mocha'); |
| 6 | +const assert = require('assert'); |
| 7 | +const fs_utils = require('../../util/fs_utils'); |
| 8 | +const { TYPES, ACTIONS } = require('../../manage_nsfs/manage_nsfs_constants'); |
| 9 | +const { TMP_PATH, set_path_permissions_and_owner, invalid_nsfs_root_permissions, generate_s3_client, get_coretest_path, |
| 10 | + validate_expiration_header, update_file_mtime, exec_manage_cli } = require('../system_tests/test_utils'); |
| 11 | +const _ = require('lodash'); |
| 12 | + |
| 13 | +const coretest_path = get_coretest_path(); |
| 14 | +const coretest = require(coretest_path); |
| 15 | +const { rpc_client, EMAIL, get_admin_mock_account_details } = coretest; |
| 16 | +coretest.setup({}); |
| 17 | + |
| 18 | +let s3_admin; |
| 19 | + |
| 20 | +const tmp_fs_root = path.join(TMP_PATH, 'test_nc_bucket_lifecycle/'); |
| 21 | + |
| 22 | +/** |
| 23 | + * is_nc_coretest returns true when the test runs on NC env |
| 24 | + */ |
| 25 | +const is_nc_coretest = process.env.NC_CORETEST === 'true'; |
| 26 | + |
| 27 | +mocha.describe('nc lifecycle - check expiration header', async function () { |
| 28 | + const bucket_path = path.join(tmp_fs_root, 'test-bucket/'); |
| 29 | + const bucket_name = 'test-bucket'; |
| 30 | + |
| 31 | + mocha.before(async function() { |
| 32 | + this.timeout(0); // eslint-disable-line no-invalid-this |
| 33 | + if (invalid_nsfs_root_permissions()) this.skip(); // eslint-disable-line no-invalid-this |
| 34 | + // create paths |
| 35 | + await fs_utils.create_fresh_path(tmp_fs_root, 0o777); |
| 36 | + await fs_utils.create_fresh_path(bucket_path, 0o770); |
| 37 | + await fs_utils.file_must_exist(bucket_path); |
| 38 | + |
| 39 | + // set permissions |
| 40 | + if (is_nc_coretest) { |
| 41 | + const { uid, gid } = get_admin_mock_account_details(); |
| 42 | + await set_path_permissions_and_owner(bucket_path, { uid, gid }, 0o700); |
| 43 | + } |
| 44 | + |
| 45 | + // create s3_admin client |
| 46 | + const admin = (await rpc_client.account.read_account({ email: EMAIL, })); |
| 47 | + const admin_keys = admin.access_keys; |
| 48 | + s3_admin = generate_s3_client(admin_keys[0].access_key.unwrap(), |
| 49 | + admin_keys[0].secret_key.unwrap(), |
| 50 | + coretest.get_http_address()); |
| 51 | + |
| 52 | + // create test bucket |
| 53 | + const cli_bucket_options = { |
| 54 | + name: bucket_name, |
| 55 | + owner: admin.name, |
| 56 | + path: bucket_path, |
| 57 | + }; |
| 58 | + await exec_manage_cli(TYPES.BUCKET, ACTIONS.ADD, cli_bucket_options); |
| 59 | + }); |
| 60 | + |
| 61 | + mocha.after(async function() { |
| 62 | + this.timeout(0); // eslint-disable-line no-invalid-this |
| 63 | + fs_utils.folder_delete(tmp_fs_root); |
| 64 | + }); |
| 65 | + |
| 66 | + const run_expiration_test = async ({ rules, expected_id, expected_days, key, tagging = undefined, size = 1000}) => { |
| 67 | + const putLifecycleParams = { |
| 68 | + Bucket: bucket_name, |
| 69 | + LifecycleConfiguration: { Rules: rules } |
| 70 | + }; |
| 71 | + await s3_admin.putBucketLifecycleConfiguration(putLifecycleParams); |
| 72 | + |
| 73 | + const putObjectParams = { |
| 74 | + Bucket: bucket_name, |
| 75 | + Key: key, |
| 76 | + Body: 'x'.repeat(size) // default 1KB if size not specified |
| 77 | + }; |
| 78 | + if (tagging) { |
| 79 | + putObjectParams.Tagging = tagging; |
| 80 | + } |
| 81 | + const start_time = new Date(); |
| 82 | + let res = await s3_admin.putObject(putObjectParams); |
| 83 | + assert.ok(res.Expiration, 'expiration header missing in putObject response'); |
| 84 | + |
| 85 | + // update file mtime to simulate a 5-days old object |
| 86 | + await update_file_mtime(path.join(bucket_path, key)); |
| 87 | + |
| 88 | + res = await s3_admin.headObject({ Bucket: bucket_name, Key: key }); |
| 89 | + assert.ok(res.Expiration, 'expiration header missing in headObject response'); |
| 90 | + |
| 91 | + const valid = validate_expiration_header(res.Expiration, start_time, expected_id, expected_days - 5); |
| 92 | + assert.ok(valid, `expected rule ${expected_id} to match`); |
| 93 | + }; |
| 94 | + |
| 95 | + function generate_rule(expiration_days, id, prefix, tags, size_gt, size_lt) { |
| 96 | + const filters = {}; |
| 97 | + if (prefix) filters.Prefix = prefix; |
| 98 | + if (Array.isArray(tags) && tags.length) filters.Tags = tags; |
| 99 | + if (size_gt !== undefined) filters.ObjectSizeGreaterThan = size_gt; |
| 100 | + if (size_lt !== undefined) filters.ObjectSizeLessThan = size_lt; |
| 101 | + |
| 102 | + const filter = Object.keys(filters).length > 1 ? { And: filters } : filters; |
| 103 | + |
| 104 | + return { |
| 105 | + ID: id, |
| 106 | + Status: 'Enabled', |
| 107 | + Filter: filter, |
| 108 | + Expiration: { Days: expiration_days }, |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + mocha.it('should select rule with longest prefix', async () => { |
| 113 | + const rules = [ |
| 114 | + generate_rule(10, 'short-prefix', 'lifecycle-test1/', [], undefined, undefined), |
| 115 | + generate_rule(17, 'long-prefix', 'lifecycle-test1/logs/', [], undefined, undefined), |
| 116 | + ]; |
| 117 | + await run_expiration_test({ |
| 118 | + rules, |
| 119 | + key: 'lifecycle-test1/logs//file.txt', |
| 120 | + expected_id: 'long-prefix', |
| 121 | + expected_days: 17 |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + mocha.it('should select rule with more tags when prefix is same', async () => { |
| 126 | + const rules = [ |
| 127 | + generate_rule(5, 'one-tag', 'lifecycle-test2/', [{ Key: 'env', Value: 'prod' }], undefined, undefined), |
| 128 | + generate_rule(9, 'two-tags', 'lifecycle-test2/', [ |
| 129 | + { Key: 'env', Value: 'prod' }, |
| 130 | + { Key: 'team', Value: 'backend' } |
| 131 | + ], undefined, undefined), |
| 132 | + ]; |
| 133 | + await run_expiration_test({ |
| 134 | + rules, |
| 135 | + key: 'lifecycle-test2/file2.txt', |
| 136 | + tagging: 'env=prod&team=backend', |
| 137 | + expected_id: 'two-tags', |
| 138 | + expected_days: 9 |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + mocha.it('should select rule with narrower size span when prefix and tags are matching', async () => { |
| 143 | + const rules = [ |
| 144 | + generate_rule(4, 'wide-range', 'lifecycle-test3/', [], 100, 10000), |
| 145 | + generate_rule(6, 'narrow-range', 'lifecycle-test3/', [], 1000, 5000), |
| 146 | + ]; |
| 147 | + await run_expiration_test({ |
| 148 | + rules, |
| 149 | + key: 'lifecycle-test3/file3.txt', |
| 150 | + size: 1500, |
| 151 | + expected_id: 'narrow-range', |
| 152 | + expected_days: 6 |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + mocha.it('should fallback to first matching rule if all filters are equal', async () => { |
| 157 | + const rules = [ |
| 158 | + generate_rule(7, 'rule-a', 'lifecycle/test4/', [], 0, 10000), |
| 159 | + generate_rule(11, 'rule-b', 'lifecycle/test4/', [], 0, 10000), |
| 160 | + ]; |
| 161 | + await run_expiration_test({ |
| 162 | + rules, |
| 163 | + key: 'lifecycle/test4/file4.txt', |
| 164 | + expected_id: 'rule-a', |
| 165 | + expected_days: 7 |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments