Skip to content

Commit

Permalink
test: force remove on sync mode
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-0acf4 committed Jan 9, 2025
1 parent 72e1a5b commit 481e9b8
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/sync/scripts/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0

export function hello({ name }: { name: string }) {
return `Hello ${name}`;
}
21 changes: 21 additions & 0 deletions tests/sync/sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
# SPDX-License-Identifier: MPL-2.0

from typegraph import t, typegraph, Policy, Graph
from typegraph.runtimes.deno import DenoRuntime


@typegraph()
def sync(g: Graph):
deno = DenoRuntime()
public = Policy.public()

g.expose(
hello=deno.import_(
t.struct({"name": t.string()}),
t.string(),
name="hello",
module="scripts/hello.ts",
secrets=["ULTRA_SECRET"],
).with_policy(public)
)
114 changes: 114 additions & 0 deletions tests/sync/sync_force_remove_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0

import { gql, Meta } from "test-utils/mod.ts";
import { connect } from "redis";
import { S3Client } from "aws-sdk/client-s3";
import { createBucket, listObjects, tryDeleteBucket } from "test-utils/s3.ts";
import { assertEquals } from "@std/assert";
import { clearSyncData, setupSync } from "test-utils/hooks.ts";
import { Typegate } from "@metatype/typegate/typegate/mod.ts";
import {
defaultTypegateConfigBase,
getTypegateConfig,
SyncConfig,
} from "@metatype/typegate/config.ts";

const redisKey = "typegraph";
const redisEventKey = "typegraph_event";

async function cleanUp(config: typeof syncConfig) {
using redis = await connect(config.redis);
await redis.del(redisKey);
await redis.del(redisEventKey);

const s3 = new S3Client(config.s3);
await tryDeleteBucket(s3, config.s3Bucket);
await createBucket(s3, config.s3Bucket);
s3.destroy();
await redis.quit();
}

const syncConfig = {
redis: {
hostname: "localhost",
port: 6379,
password: "password",
db: 1,
},
s3: {
endpoint: "http://localhost:9000",
region: "local",
credentials: {
accessKeyId: "minio",
secretAccessKey: "password",
},
forcePathStyle: true,
},
s3Bucket: "metatype-deno-runtime-sync-test",
};

async function spawnGate(syncConfig: SyncConfig) {
const config = getTypegateConfig({
base: {
...defaultTypegateConfigBase,
},
});

return await Typegate.init({
...config,
sync: syncConfig,
});
}

Meta.test(
{
name: "Force cleanup at boot on sync mode",
syncConfig,
async setup() {
await clearSyncData(syncConfig);
await setupSync(syncConfig);
},
async teardown() {
await cleanUp(syncConfig);
},
},
async (t) => {
await t.should(
"cleanup if forceRemove is true",
async () => {
const _engine = await t.engine("sync/sync.py", {
secrets: {
ULTRA_SECRET:
"if_you_can_read_me_on_an_ERROR_there_is_a_bug",
},
});

const s3 = new S3Client(syncConfig.s3);
const initialObjects = await listObjects(s3, syncConfig.s3Bucket);
assertEquals(initialObjects?.length, 3);

const gateNoRemove = await spawnGate(syncConfig);
const namesNoRemove = gateNoRemove.register.list().map(({ name }) =>
name
);

const gateAfterRemove = await spawnGate({
...syncConfig,
forceRemove: true,
});
const namesAfterRemove = gateAfterRemove.register.list().map((
{ name },
) => name);

t.addCleanup(async () => {
await gateNoRemove[Symbol.asyncDispose]();
await gateAfterRemove[Symbol.asyncDispose]();
});

assertEquals(namesNoRemove, ["sync"]);
assertEquals(namesAfterRemove, []); // !
},
);
},
);

0 comments on commit 481e9b8

Please sign in to comment.