diff --git a/jfrog/main.test.ts b/jfrog/main.test.ts index 293037e..7a797b6 100644 --- a/jfrog/main.test.ts +++ b/jfrog/main.test.ts @@ -1,18 +1,41 @@ -import { describe, expect, it } from "bun:test"; +import { serve } from "bun"; +import { describe } from "bun:test"; import { - executeScriptInContainer, - runTerraformApply, + createJSONResponse, runTerraformInit, - testRequiredVariables, + testRequiredVariables } from "../test"; describe("jfrog", async () => { await runTerraformInit(import.meta.dir); + // Run a fake JFrog server so the provider can initialize + // correctly. This saves us from having to make remote requests! + const fakeFrogHost = serve({ + fetch: (req) => { + const url = new URL(req.url); + // See https://jfrog.com/help/r/jfrog-rest-apis/license-information + if (url.pathname === "/artifactory/api/system/license") + return createJSONResponse({ + type: "Commercial", + licensedTo: "JFrog inc.", + validThrough: "May 15, 2036", + }); + if (url.pathname === "/access/api/v1/tokens") + return createJSONResponse({ + token_id: "xxx", + access_token: "xxx", + scope: "any", + }); + return createJSONResponse({}); + }, + port: 0, + }); + testRequiredVariables(import.meta.dir, { agent_id: "some-agent-id", - jfrog_host: "YYYY.jfrog.io", + jfrog_url: "http://" + fakeFrogHost.hostname + ":" + fakeFrogHost.port, artifactory_access_token: "XXXX", - package_managers: '', + package_managers: "{}", }); }); diff --git a/jfrog/main.tf b/jfrog/main.tf index fbbd267..50ba477 100644 --- a/jfrog/main.tf +++ b/jfrog/main.tf @@ -13,9 +13,9 @@ terraform { } } -variable "jfrog_host" { +variable "jfrog_url" { type = string - description = "JFrog instance hostname. e.g. YYY.jfrog.io" + description = "JFrog instance URL. e.g. https://YYY.jfrog.io" } variable "artifactory_access_token" { @@ -25,7 +25,7 @@ variable "artifactory_access_token" { # Configure the Artifactory provider provider "artifactory" { - url = "https://${var.jfrog_host}/artifactory" + url = join("/", [var.jfrog_url, "artifactory"]) access_token = var.artifactory_access_token } resource "artifactory_scoped_token" "me" { @@ -59,7 +59,7 @@ resource "coder_script" "jfrog" { display_name = "jfrog" icon = "/icon/jfrog.svg" script = templatefile("${path.module}/run.sh", { - JFROG_HOST : var.jfrog_host, + JFROG_URL : var.jfrog_url, ARTIFACTORY_USERNAME : data.coder_workspace.me.owner_email, ARTIFACTORY_ACCESS_TOKEN : artifactory_scoped_token.me.access_token, REPOSITORY_NPM : lookup(var.package_managers, "npm", ""), diff --git a/jfrog/run.sh b/jfrog/run.sh index f5821b8..0cd5a6d 100644 --- a/jfrog/run.sh +++ b/jfrog/run.sh @@ -12,17 +12,17 @@ sudo chmod 755 /usr/local/bin/jf export CI=true # Authenticate with the JFrog CLI. jf c rm 0 || true -echo "${ARTIFACTORY_ACCESS_TOKEN}" | jf c add --access-token-stdin --url "https://${JFROG_HOST}" 0 +echo "${ARTIFACTORY_ACCESS_TOKEN}" | jf c add --access-token-stdin --url "${JFROG_URL}" 0 # Configure the `npm` CLI to use the Artifactory "npm" repository. if [ -z "${REPOSITORY_NPM}" ]; then echo "🤔 REPOSITORY_NPM is not set, skipping npm configuration." else echo "📦 Configuring npm..." - jf npmc --global --repo-resolve "https://${JFROG_HOST}/artifactory/api/npm/${REPOSITORY_NPM}" + jf npmc --global --repo-resolve "${JFROG_URL}/artifactory/api/npm/${REPOSITORY_NPM}" cat << EOF > ~/.npmrc email = ${ARTIFACTORY_USERNAME} -registry = https://${JFROG_HOST}/artifactory/api/npm/${REPOSITORY_NPM} +registry = ${JFROG_URL}/artifactory/api/npm/${REPOSITORY_NPM} EOF jf rt curl /api/npm/auth >> ~/.npmrc fi @@ -35,7 +35,7 @@ else mkdir -p ~/.pip cat << EOF > ~/.pip/pip.conf [global] -index-url = https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}@${JFROG_HOST}/artifactory/api/pypi/${REPOSITORY_PYPI}/simple +index-url = https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}@${JFROG_URL}/artifactory/api/pypi/${REPOSITORY_PYPI}/simple EOF fi @@ -44,6 +44,6 @@ if [ -z "${REPOSITORY_GO}" ]; then echo "🤔 REPOSITORY_GO is not set, skipping go configuration." else echo "🐹 Configuring go..." - export GOPROXY="https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}@${JFROG_HOST}/artifactory/api/go/${REPOSITORY_GO}" + export GOPROXY="https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}@${JFROG_URL}/artifactory/api/go/${REPOSITORY_GO}" fi echo "🥳 Configuration complete!" \ No newline at end of file diff --git a/test.ts b/test.ts index b4c384f..747601d 100644 --- a/test.ts +++ b/test.ts @@ -153,7 +153,7 @@ export const testRequiredVariables = ( await runTerraformApply(dir, localVars); } catch (ex) { expect(ex.message).toContain( - `input variable \"${varName}\" is not set, and has no default`, + `input variable \"${varName}\" is not set`, ); return; } @@ -180,6 +180,7 @@ export const runTerraformApply = async ( "-input=false", "-auto-approve", "-state", + "-no-color", stateFile, ], { @@ -210,3 +211,12 @@ export const runTerraformInit = async (dir: string) => { throw new Error(text); } }; + +export const createJSONResponse = (obj: object, statusCode = 200): Response => { + return new Response(JSON.stringify(obj), { + headers: { + "Content-Type": "application/json", + }, + status: statusCode, + }) +} \ No newline at end of file