Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 72 additions & 45 deletions integration-tests/js-compute/cleanupAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ const {
SECRET_STORE_PREFIX,
} = getPrefixes();

function existingListIds(stores, prefix) {
return stores
.filter(
(store) =>
store.name?.startsWith(prefix) || store.Name?.startsWith(prefix),
)
.map((existing) => existing.id || existing.StoreID);
function existingList(stores, prefix) {
return stores.filter(
(store) => store.name?.startsWith(prefix) || store.Name?.startsWith(prefix),
);
}

const startTime = Date.now();
Expand All @@ -42,100 +39,130 @@ if (process.env.FASTLY_API_TOKEN === undefined) {
}
const FASTLY_API_TOKEN = process.env.FASTLY_API_TOKEN;

async function removeConfigStores(links, serviceIds) {
async function removeConfigStores(links, services) {
console.log('Removing config stores...');
const stores = JSON.parse(
await zx`fastly config-store list --quiet --json --token $FASTLY_API_TOKEN`,
);

let STORE_IDS = existingListIds(stores, DICTIONARY_PREFIX);
for (const STORE_ID of STORE_IDS) {
await zx`fastly config-store delete --store-id=${STORE_ID} --token $FASTLY_API_TOKEN`;
let dictionaries = existingList(stores, DICTIONARY_PREFIX);
for (const dictionary of dictionaries) {
console.log(`\tDeleting dictionary ${dictionary.name}`);
try {
await zx`fastly config-store delete --store-id=${dictionary.id} --token $FASTLY_API_TOKEN`;
} catch {}
}
console.log('All dictionaries removed');

STORE_IDS = existingListIds(stores, CONFIG_STORE_PREFIX);
for (const STORE_ID of STORE_IDS) {
let configStores = existingList(stores, CONFIG_STORE_PREFIX);
for (const store of configStores) {
console.log(`\tDeleting config store ${store.name}`);
try {
await zx`fastly config-store delete --store-id=${STORE_ID} --token $FASTLY_API_TOKEN`;
await zx`fastly config-store delete --store-id=${store.id} --token $FASTLY_API_TOKEN`;
} catch {}
}
console.log('All config stores removed');
}

async function removeKVStores() {
console.log('Removing KV stores...');
const stores = JSON.parse(
await zx`fastly kv-store list --quiet --json --token $FASTLY_API_TOKEN`,
).Data;

let STORE_IDS = existingListIds(stores, KV_STORE_PREFIX);
for (const STORE_ID of STORE_IDS) {
await zx`fastly kv-store delete --store-id=${STORE_ID} --quiet --all -y --token $FASTLY_API_TOKEN`;
let kvStores = existingList(stores, KV_STORE_PREFIX);
for (const store of kvStores) {
console.log(`\tDeleting KV store ${store.Name}`);
try {
await zx`fastly kv-store delete --store-id=${store.StoreID} --quiet --all -y --token $FASTLY_API_TOKEN`;
} catch {}
}
console.log('All KV stores removed');
}

async function removeSecretStores(serviceIds) {
async function removeSecretStores(services) {
console.log('Removing secret stores...');
const stores = JSON.parse(
await zx`fastly secret-store list --quiet --json --token $FASTLY_API_TOKEN`,
);
if (!stores) {
return;
}
const STORE_IDS = existingListIds(stores, SECRET_STORE_PREFIX);
for (const STORE_ID of STORE_IDS) {
const secretStores = existingList(stores, SECRET_STORE_PREFIX);
for (const store of secretStores) {
console.log(`\tDeleting secret store ${store.name}`);
try {
await zx`fastly secret-store delete --store-id=${STORE_ID} --token $FASTLY_API_TOKEN`;
await zx`fastly secret-store delete --store-id=${store.id} --token $FASTLY_API_TOKEN`;
} catch {}
}
console.log('All secret stores removed');
}

async function removeAcls(serviceIds) {
const ACL_IDS = existingListIds(
async function removeAcls(services) {
console.log('Removing ACLs...');
const acls = existingList(
JSON.parse(
await zx`fastly compute acl list-acls --quiet --json --token $FASTLY_API_TOKEN`,
).data,
ACL_PREFIX,
);

for (const ACL_ID of ACL_IDS) {
await zx`fastly compute acl delete --acl-id=${ACL_ID} --token $FASTLY_API_TOKEN`;
for (const acl of acls) {
console.log(`\tDeleting acl ${acl.name}`);
try {
await zx`fastly compute acl delete --acl-id=${acl.id} --token $FASTLY_API_TOKEN`;
} catch {}
}
console.log('All ACLs removed');
}

async function getServiceIds() {
async function getServices() {
console.log('Getting services...');
let services = JSON.parse(
await zx`fastly service list --token $FASTLY_API_TOKEN --json`,
);
return services
.filter(({ Name }) => Name.startsWith(SERVICE_PREFIX))
.map((service) => service.ServiceID);
services = services.filter(({ Name }) => Name.startsWith(SERVICE_PREFIX));
console.log('Services to delete:');
for (const service of services) {
console.log('\t', service.Name);
}
return services;
}

async function removeServices(serviceIds) {
for (const serviceId of serviceIds) {
await zx`fastly service delete --force --service-id=${serviceId}`;
async function removeServices(services) {
console.log('Removing services...');
for (const service of services) {
console.log(`\tDeleting service ${service.Name}`);
await zx`fastly service delete --force --service-id=${service.ServiceID}`;
}
console.log('ALl services removed');
}

async function removeLinks(serviceIds) {
for (const serviceId of serviceIds) {
async function removeLinks(services) {
console.log('Removing links...');
for (const service of services) {
const links = JSON.parse(
await zx`fastly resource-link list --service-id=${serviceId} --quiet --json --version latest --token $FASTLY_API_TOKEN`,
await zx`fastly resource-link list --service-id=${service.ServiceID} --quiet --json --version latest --token $FASTLY_API_TOKEN`,
);
for (const link of links) {
console.log(
`\tDeleting link between service ${service.Name} and resource ${link.name}`,
);
await zx`fastly resource-link delete --version latest --autoclone --id=${link.id} --service-id=${link.service_id} --token $FASTLY_API_TOKEN`;
await zx`fastly service-version activate --version latest --service-id=${link.service_id} --token $FASTLY_API_TOKEN`;
}
}
console.log('All links removed');
}

const serviceIds = await getServiceIds();
await removeLinks(serviceIds);
await Promise.all([
removeConfigStores(serviceIds),
removeKVStores(),
removeSecretStores(serviceIds),
removeAcls(serviceIds),
]);
await removeServices(serviceIds);
const services = await getServices();
await removeLinks(services);
await removeConfigStores(services);
await removeKVStores();
await removeSecretStores(services);
await removeAcls(services);
await removeServices(services);

console.log(
`Tear down has finished! Took ${(Date.now() - startTime) / 1000} seconds to complete`,
`Cleanup has finished! Took ${(Date.now() - startTime) / 1000} seconds to complete`,
);
33 changes: 21 additions & 12 deletions integration-tests/js-compute/env.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
export const GLOBAL_PREFIX = 'js_integration_test_';

function applyGlobalPrefix(map) {
for (const key in map) {
map[key] = GLOBAL_PREFIX + map[key];
}
return map;
}

export function getEnv(serviceName) {
return {
return applyGlobalPrefix({
DICTIONARY_NAME: `aZ2__2${serviceName ? '__' + serviceName.replace(/-/g, '_') : ''}`,
CONFIG_STORE_NAME: `testconfig${serviceName ? '__' + serviceName.replace(/-/g, '_') : ''}`,
KV_STORE_NAME: `example-test-kv-store${serviceName ? '--' + serviceName : ''}`,
SECRET_STORE_NAME: `example-test-secret-store${serviceName ? '--' + serviceName : ''}`,
ACL_NAME: `exampleacl${serviceName ? '__' + serviceName.replace(/-/g, '_') : ''}`,
};
CONFIG_STORE_NAME: `config${serviceName ? '__' + serviceName.replace(/-/g, '_') : ''}`,
KV_STORE_NAME: `kv-store${serviceName ? '--' + serviceName : ''}`,
SECRET_STORE_NAME: `secret-store${serviceName ? '--' + serviceName : ''}`,
ACL_NAME: `acl${serviceName ? '__' + serviceName.replace(/-/g, '_') : ''}`,
});
}

export function getPrefixes() {
return {
return applyGlobalPrefix({
SERVICE_PREFIX: `app-`,
DICTIONARY_PREFIX: `aZ2__2`,
CONFIG_STORE_PREFIX: `testconfig`,
KV_STORE_PREFIX: `example-test-kv-store`,
SECRET_STORE_PREFIX: `example-test-secret-store`,
ACL_PREFIX: `exampleacl`,
};
CONFIG_STORE_PREFIX: `config`,
KV_STORE_PREFIX: `kv-store`,
SECRET_STORE_PREFIX: `secret-store`,
ACL_PREFIX: `acl`,
});
}
4 changes: 2 additions & 2 deletions integration-tests/js-compute/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { existsSync } from 'node:fs';
import { copyFile, readFile, writeFile } from 'node:fs/promises';
import core from '@actions/core';
import TOML from '@iarna/toml';
import { getEnv } from './env.js';
import { getEnv, GLOBAL_PREFIX } from './env.js';

// test environment variable handling
process.env.LOCAL_TEST = 'local val';
Expand Down Expand Up @@ -81,7 +81,7 @@ const branchName = (await zx`git branch --show-current`).stdout
const fixture = !moduleMode ? 'app' : 'module-mode';

// Service names are carefully unique to support parallel runs
const serviceName = `app-${branchName}${aot ? '--aot' : ''}${httpCache ? '--http' : ''}${process.env.SUFFIX_STRING ? '--' + process.env.SUFFIX_STRING : ''}`;
const serviceName = `${GLOBAL_PREFIX}app-${branchName}${aot ? '--aot' : ''}${httpCache ? '--http' : ''}${process.env.SUFFIX_STRING ? '--' + process.env.SUFFIX_STRING : ''}`;
let domain, serviceId;
const fixturePath = join(__dirname, 'fixtures', fixture);
let localServer;
Expand Down
Loading