From fd5ba0fab9d150a3de5d73d0c3bc769db05dfd53 Mon Sep 17 00:00:00 2001 From: bsouza Date: Fri, 20 Sep 2024 13:43:39 +0000 Subject: [PATCH] Port tests to oauth module and fix errors --- jfrog-oauth/main.test.ts | 100 +++++++++++++++++++++++++++++++++++---- jfrog-oauth/main.tf | 6 +-- 2 files changed, 94 insertions(+), 12 deletions(-) diff --git a/jfrog-oauth/main.test.ts b/jfrog-oauth/main.test.ts index 3397eeb..40ea77b 100644 --- a/jfrog-oauth/main.test.ts +++ b/jfrog-oauth/main.test.ts @@ -1,19 +1,101 @@ -import { serve } from "bun"; -import { describe } from "bun:test"; +import { describe, expect, it } from "bun:test"; import { - createJSONResponse, + findResourceInstance, runTerraformInit, + runTerraformApply, testRequiredVariables, } from "../test"; describe("jfrog-oauth", async () => { await runTerraformInit(import.meta.dir); - testRequiredVariables(import.meta.dir, { - agent_id: "some-agent-id", - jfrog_url: "http://localhost:8081", - package_managers: "{}", + const fakeFrogHostAndPort = "localhost:8081"; + const fakeFrogUrl = `http://${fakeFrogHostAndPort}`; + + it("can run apply with required variables", async () => { + testRequiredVariables(import.meta.dir, { + agent_id: "some-agent-id", + jfrog_url: fakeFrogUrl, + package_managers: "{}", + }); }); -}); -//TODO add more tests + it("generates an npmrc with scoped repos", async () => { + const state = await runTerraformApply(import.meta.dir, { + agent_id: "some-agent-id", + jfrog_url: fakeFrogUrl, + package_managers: JSON.stringify({ + npm: ["global", "@foo:foo", "@bar:bar"], + }), + }); + const coderScript = findResourceInstance(state, "coder_script"); + const npmrcStanza = `cat << EOF > ~/.npmrc +email=default@example.com +registry=${fakeFrogUrl}/artifactory/api/npm/global +//${fakeFrogHostAndPort}/artifactory/api/npm/global/:_authToken= +@foo:registry=${fakeFrogUrl}/artifactory/api/npm/foo +//${fakeFrogHostAndPort}/artifactory/api/npm/foo/:_authToken= +@bar:registry=${fakeFrogUrl}/artifactory/api/npm/bar +//${fakeFrogHostAndPort}/artifactory/api/npm/bar/:_authToken= + +EOF`; + expect(coderScript.script).toContain(npmrcStanza); + expect(coderScript.script).toContain('jf npmc --global --repo-resolve "global"'); + expect(coderScript.script).toContain('if [ -z "YES" ]; then\n not_configured npm'); + }); + + it("generates a pip config with extra-indexes", async () => { + const state = await runTerraformApply(import.meta.dir, { + agent_id: "some-agent-id", + jfrog_url: fakeFrogUrl, + package_managers: JSON.stringify({ + pypi: ["global", "foo", "bar"], + }), + }); + const coderScript = findResourceInstance(state, "coder_script"); + const pipStanza = `cat << EOF > ~/.pip/pip.conf +[global] +index-url = https://default:@${fakeFrogHostAndPort}/artifactory/api/pypi/global/simple +extra-index-url = + https://default:@${fakeFrogHostAndPort}/artifactory/api/pypi/foo/simple + https://default:@${fakeFrogHostAndPort}/artifactory/api/pypi/bar/simple + +EOF`; + expect(coderScript.script).toContain(pipStanza); + expect(coderScript.script).toContain('jf pipc --global --repo-resolve "global"'); + expect(coderScript.script).toContain('if [ -z "YES" ]; then\n not_configured pypi'); + }); + + it("registers multiple docker repos", async () => { + const state = await runTerraformApply(import.meta.dir, { + agent_id: "some-agent-id", + jfrog_url: fakeFrogUrl, + package_managers: JSON.stringify({ + docker: ["foo.jfrog.io", "bar.jfrog.io", "baz.jfrog.io"], + }), + }); + const coderScript = findResourceInstance(state, "coder_script"); + const dockerStanza = `register_docker "foo.jfrog.io" +register_docker "bar.jfrog.io" +register_docker "baz.jfrog.io"`; + expect(coderScript.script).toContain(dockerStanza); + expect(coderScript.script).toContain('if [ -z "YES" ]; then\n not_configured docker'); + }); + + it("sets goproxy with multiple repos", async () => { + const state = await runTerraformApply(import.meta.dir, { + agent_id: "some-agent-id", + jfrog_url: fakeFrogUrl, + package_managers: JSON.stringify({ + go: ["foo", "bar", "baz"], + }), + }); + const proxyEnv = findResourceInstance(state, "coder_env", "goproxy"); + const proxies = `https://default:@${fakeFrogHostAndPort}/artifactory/api/go/foo,https://default:@${fakeFrogHostAndPort}/artifactory/api/go/bar,https://default:@${fakeFrogHostAndPort}/artifactory/api/go/baz`; + expect(proxyEnv["value"]).toEqual(proxies); + + const coderScript = findResourceInstance(state, "coder_script"); + expect(coderScript.script).toContain('jf goc --global --repo-resolve "foo"'); + expect(coderScript.script).toContain('if [ -z "YES" ]; then\n not_configured go'); + }); +}); diff --git a/jfrog-oauth/main.tf b/jfrog-oauth/main.tf index 953a556..c5c32b2 100644 --- a/jfrog-oauth/main.tf +++ b/jfrog-oauth/main.tf @@ -74,7 +74,7 @@ variable "package_managers" { locals { # The username field to use for artifactory username = var.username_field == "email" ? data.coder_workspace_owner.me.email : data.coder_workspace_owner.me.name - jfrog_host = replace(var.jfrog_url, "https://", "") + jfrog_host = split("://", var.jfrog_url)[1] common_values = { JFROG_URL = var.jfrog_url JFROG_HOST = local.jfrog_host @@ -114,7 +114,7 @@ resource "coder_script" "jfrog" { script = templatefile("${path.module}/run.sh", merge( local.common_values, { - CONFIGURE_CODE_SERVER = var.configure_code_server + CONFIGURE_CODE_SERVER = var.configure_code_server HAS_NPM = length(var.package_managers.npm) == 0 ? "" : "YES" NPMRC = local.npmrc REPOSITORY_NPM = try(element(var.package_managers.npm, 0), "") @@ -157,7 +157,7 @@ resource "coder_env" "goproxy" { name = "GOPROXY" value = join(",", [ for repo in var.package_managers.go : - "https://${local.username}:${artifactory_scoped_token.me.access_token}@${local.jfrog_host}/artifactory/api/go/${repo}" + "https://${local.username}:${data.coder_external_auth.jfrog.access_token}@${local.jfrog_host}/artifactory/api/go/${repo}" ]) }