Fix JFrog test

pull/50/head
Kyle Carberry 2 years ago
parent 5f729962dc
commit 40bb84b741

@ -1,18 +1,41 @@
import { describe, expect, it } from "bun:test"; import { serve } from "bun";
import { describe } from "bun:test";
import { import {
executeScriptInContainer, createJSONResponse,
runTerraformApply,
runTerraformInit, runTerraformInit,
testRequiredVariables, testRequiredVariables
} from "../test"; } from "../test";
describe("jfrog", async () => { describe("jfrog", async () => {
await runTerraformInit(import.meta.dir); 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, { testRequiredVariables(import.meta.dir, {
agent_id: "some-agent-id", agent_id: "some-agent-id",
jfrog_host: "YYYY.jfrog.io", jfrog_url: "http://" + fakeFrogHost.hostname + ":" + fakeFrogHost.port,
artifactory_access_token: "XXXX", artifactory_access_token: "XXXX",
package_managers: '', package_managers: "{}",
}); });
}); });

@ -13,9 +13,9 @@ terraform {
} }
} }
variable "jfrog_host" { variable "jfrog_url" {
type = string 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" { variable "artifactory_access_token" {
@ -25,7 +25,7 @@ variable "artifactory_access_token" {
# Configure the Artifactory provider # Configure the Artifactory provider
provider "artifactory" { provider "artifactory" {
url = "https://${var.jfrog_host}/artifactory" url = join("/", [var.jfrog_url, "artifactory"])
access_token = var.artifactory_access_token access_token = var.artifactory_access_token
} }
resource "artifactory_scoped_token" "me" { resource "artifactory_scoped_token" "me" {
@ -59,7 +59,7 @@ resource "coder_script" "jfrog" {
display_name = "jfrog" display_name = "jfrog"
icon = "/icon/jfrog.svg" icon = "/icon/jfrog.svg"
script = templatefile("${path.module}/run.sh", { 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_USERNAME : data.coder_workspace.me.owner_email,
ARTIFACTORY_ACCESS_TOKEN : artifactory_scoped_token.me.access_token, ARTIFACTORY_ACCESS_TOKEN : artifactory_scoped_token.me.access_token,
REPOSITORY_NPM : lookup(var.package_managers, "npm", ""), REPOSITORY_NPM : lookup(var.package_managers, "npm", ""),

@ -12,17 +12,17 @@ sudo chmod 755 /usr/local/bin/jf
export CI=true export CI=true
# Authenticate with the JFrog CLI. # Authenticate with the JFrog CLI.
jf c rm 0 || true 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. # Configure the `npm` CLI to use the Artifactory "npm" repository.
if [ -z "${REPOSITORY_NPM}" ]; then if [ -z "${REPOSITORY_NPM}" ]; then
echo "🤔 REPOSITORY_NPM is not set, skipping npm configuration." echo "🤔 REPOSITORY_NPM is not set, skipping npm configuration."
else else
echo "📦 Configuring npm..." 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 cat << EOF > ~/.npmrc
email = ${ARTIFACTORY_USERNAME} email = ${ARTIFACTORY_USERNAME}
registry = https://${JFROG_HOST}/artifactory/api/npm/${REPOSITORY_NPM} registry = ${JFROG_URL}/artifactory/api/npm/${REPOSITORY_NPM}
EOF EOF
jf rt curl /api/npm/auth >> ~/.npmrc jf rt curl /api/npm/auth >> ~/.npmrc
fi fi
@ -35,7 +35,7 @@ else
mkdir -p ~/.pip mkdir -p ~/.pip
cat << EOF > ~/.pip/pip.conf cat << EOF > ~/.pip/pip.conf
[global] [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 EOF
fi fi
@ -44,6 +44,6 @@ if [ -z "${REPOSITORY_GO}" ]; then
echo "🤔 REPOSITORY_GO is not set, skipping go configuration." echo "🤔 REPOSITORY_GO is not set, skipping go configuration."
else else
echo "🐹 Configuring go..." 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 fi
echo "🥳 Configuration complete!" echo "🥳 Configuration complete!"

@ -153,7 +153,7 @@ export const testRequiredVariables = (
await runTerraformApply(dir, localVars); await runTerraformApply(dir, localVars);
} catch (ex) { } catch (ex) {
expect(ex.message).toContain( expect(ex.message).toContain(
`input variable \"${varName}\" is not set, and has no default`, `input variable \"${varName}\" is not set`,
); );
return; return;
} }
@ -180,6 +180,7 @@ export const runTerraformApply = async (
"-input=false", "-input=false",
"-auto-approve", "-auto-approve",
"-state", "-state",
"-no-color",
stateFile, stateFile,
], ],
{ {
@ -210,3 +211,12 @@ export const runTerraformInit = async (dir: string) => {
throw new Error(text); 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,
})
}
Loading…
Cancel
Save