From 4798a58e0297526cb042b9e6a72ba28244b77df9 Mon Sep 17 00:00:00 2001 From: Geun-Oh Date: Thu, 28 Mar 2024 12:02:46 +0900 Subject: [PATCH] test: add testing shell yaml file --- .github/scripts/initialize-wrangler.sh | 101 ++++++++++++++++++++++++ .github/workflows/test-cloudflarekv.yml | 52 +++++++----- 2 files changed, 134 insertions(+), 19 deletions(-) create mode 100644 .github/scripts/initialize-wrangler.sh diff --git a/.github/scripts/initialize-wrangler.sh b/.github/scripts/initialize-wrangler.sh new file mode 100644 index 00000000..0be50154 --- /dev/null +++ b/.github/scripts/initialize-wrangler.sh @@ -0,0 +1,101 @@ +#!/bin/bash + +# generate index.ts +cat < index.ts +export default { async fetch(Request, env) { + + const namespace = env.TEST_NAMESPACE1; + + if (Request.url === "http://localhost:8787/writeworkerskvkeyvaluepair") { + const res = await Request.json(); + const { key, val } = res; + WriteWorkersKVKeyValuePair(namespace, key, val); + return new Response("Success"); + } + + else if (Request.url === "http://localhost:8787/listworkerskvkeys") { + const resp = await Request.json(); + const { limit, prefix, cursor } = resp; + const list = await ListWorkersKVKeys(namespace, limit, prefix, cursor); + return new Response(list); + } + + else if (Request.url === "http://localhost:8787/deleteworkerskvpairbykey") { + const res = await Request.json(); + const { key } = res; + await DeleteWorkersKVPairByKey(namespace, key); + + return new Response(key) + } + + else if (Request.url === "http://localhost:8787/getworkerskvvaluebykey") { + const key = (await Request.json()).key; + const res = await GetWorkersKVValueByKey(namespace, key); + + return new Response(res); + } + + else if (Request.url === "http://localhost:8787/deleteworkerskventries") { + const res = await Request.json(); + const { keys } = res; + const newKeys = keys.filter(x => x.length > 0); + await DeleteWorkersKVEntries(namespace, newKeys); + + return new Response("Success") + } +} +} + +const GetWorkersKVValueByKey = async (NAMESPACE, key) => { + const val = await NAMESPACE.get(key); + + return val; +} + +const WriteWorkersKVKeyValuePair = async (NAMESPACE, key, val) => { + await NAMESPACE.put(key, val); + + return "Wrote Successfully" +} + +const DeleteWorkersKVPairByKey = async (NAMESPACE, key) => { + await NAMESPACE.delete(key); + + return "Delete Successfully" +} + +const ListWorkersKVKeys = async (NAMESPACE, limit, prefix, cursor) => { + const resp = await NAMESPACE.list({ limit, prefix, cursor }); + + return JSON.stringify(resp.keys); +} + +const DeleteWorkersKVEntries = async (NAMESPACE, keys) => { + for (let key of keys) { + await NAMESPACE.delete(key); + } + + return "Delete Successfully" +} + + +EOF + +echo "wrangler.toml generated" + +# generate wrangler.toml +cat < wrangler.toml +main = "index.ts" + +kv_namespaces = [ + { binding = "TEST_NAMESPACE1", id = "hello", preview_id = "world" }, +] + +workers_dev = true + +compatibility_date = "2024-03-20" + +port = 8787 +EOF + +echo "wrangler.toml generated" diff --git a/.github/workflows/test-cloudflarekv.yml b/.github/workflows/test-cloudflarekv.yml index 14a013b9..8f06a759 100644 --- a/.github/workflows/test-cloudflarekv.yml +++ b/.github/workflows/test-cloudflarekv.yml @@ -1,28 +1,42 @@ +name: Run CloudflareKV Tests + on: push: branches: - - master - main - paths: - - "cloudflarekv/**" pull_request: - paths: - - "cloudflarekv/**" -name: "Tests Cloudflare KV" + branches: + - main + jobs: - Tests: + build-and-test: + name: Build and Test runs-on: ubuntu-latest - strategy: - matrix: - go-version: - - 1.21.x - - 1.22.x + steps: - - name: Fetch Repository - uses: actions/checkout@v4 - - name: Install Go - uses: actions/setup-go@v5 + - name: Checkout Repository + uses: actions/checkout@v2 + + - name: Setup Go + uses: actions/setup-go@v2 + with: + go-version: 1.22.x + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: '18' + + - uses: JarvusInnovations/background-action@v1 + name: Bootstrap System Under Test (SUT) with: - go-version: "${{ matrix.go-version }}" - - name: Run Test - run: cd ./cloudflarekv && go test ./... -v -race + run: .github/scripts/initialize-wrangler.sh + + wait-on: | + http://localhost:8787 + + - name: Go to cloudflarekv directory + run: cd cloudflarekv + + - name: Run Go Tests + run: go test