|
| 1 | +import { test, expect } from 'test-utils'; |
| 2 | +import { |
| 3 | + RepositoriesApi, |
| 4 | + SnapshotsApi, |
| 5 | + ListRepositoriesRequest, |
| 6 | + ListSnapshotsForRepoRequest, |
| 7 | + ApiSnapshotResponse, |
| 8 | +} from 'test-utils/client'; |
| 9 | + |
| 10 | +test.describe('Snapshot Expiry Check', () => { |
| 11 | + const isProd = !!process.env.PROD; |
| 12 | + const retentionDays = isProd ? 366 : 91; |
| 13 | + const retentionDate = new Date(); |
| 14 | + retentionDate.setDate(retentionDate.getDate() - retentionDays); |
| 15 | + const envName = isProd ? 'PROD' : 'STAGE'; |
| 16 | + |
| 17 | + const user = isProd ? 'read-only' : 'stable_sam'; |
| 18 | + const token = isProd ? process.env.READONLY_TOKEN : process.env.STABLE_SAM_TOKEN; |
| 19 | + |
| 20 | + test.use({ |
| 21 | + storageState: `.auth/${user}.json`, |
| 22 | + extraHTTPHeaders: token ? { Authorization: token } : {}, |
| 23 | + }); |
| 24 | + |
| 25 | + test(`Check snapshot expiry for Red Hat and EPEL repositories (Env: ${envName}, Limit: ${retentionDays} days)`, async ({ |
| 26 | + client, |
| 27 | + }) => { |
| 28 | + const reposApi = new RepositoriesApi(client); |
| 29 | + const snapshotsApi = new SnapshotsApi(client); |
| 30 | + |
| 31 | + const repos = await reposApi.listRepositories(<ListRepositoriesRequest>{ |
| 32 | + origin: 'community,red_hat', |
| 33 | + }); |
| 34 | + expect(repos.data).toBeDefined(); |
| 35 | + |
| 36 | + const failedRepos: string[] = []; |
| 37 | + |
| 38 | + for (const repo of repos.data!) { |
| 39 | + const snaps = await snapshotsApi.listSnapshotsForRepo(<ListSnapshotsForRepoRequest>{ |
| 40 | + uuid: repo.uuid, |
| 41 | + sortBy: 'created_at', |
| 42 | + limit: 2, |
| 43 | + }); |
| 44 | + expect(snaps.data).toBeDefined(); |
| 45 | + |
| 46 | + // Repo must keep at least 1 snapshot, skip if there's only one |
| 47 | + if (snaps.data!.length === 1) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + const expired: ApiSnapshotResponse[] = []; |
| 52 | + |
| 53 | + for (const snapshot of snaps.data!) { |
| 54 | + if (snapshot.createdAt) { |
| 55 | + const snapshotDate = new Date(snapshot.createdAt); |
| 56 | + if (snapshotDate < retentionDate) { |
| 57 | + expired.push(snapshot); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (expired.length > 0) { |
| 63 | + const expiredDatesList: string[] = []; |
| 64 | + for (const snapshot of expired) { |
| 65 | + if (snapshot.createdAt) { |
| 66 | + expiredDatesList.push(snapshot.createdAt); |
| 67 | + } |
| 68 | + } |
| 69 | + const expiredDates = expiredDatesList.join(', '); |
| 70 | + failedRepos.push(`${repo.name} (created: ${expiredDates})`); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + expect( |
| 75 | + failedRepos.length, |
| 76 | + `Found expired snapshots in ${failedRepos.length} repos:\n${failedRepos.join('\n')}`, |
| 77 | + ).toBe(0); |
| 78 | + }); |
| 79 | +}); |
0 commit comments