11// Use fork until https://github.com/piyush-bhatt/deno-port/pull/1 is merged
22import { getAvailablePort } from "https://raw.githubusercontent.com/Nitive/deno-port/fix-getting-random-port/mod.ts"
3-
4- import { exists } from "https://deno.land/std@0.107.0/fs/exists.ts"
5- import * as path from "https://deno.land/std@0.107.0/path/mod.ts"
6- import * as yaml from "https://deno.land/std@0.107.0/encoding/yaml.ts"
3+ import { exists } from "https://deno.land/std@0.160.0/fs/exists.ts"
4+ import * as path from "https://deno.land/std@0.160.0/path/mod.ts"
5+ import * as yaml from "https://deno.land/std@0.160.0/encoding/yaml.ts"
76import {
87 assertEquals ,
98 assertStringIncludes ,
10- } from "https://deno.land/std@0.107 .0/testing/asserts.ts"
9+ } from "https://deno.land/std@0.160 .0/testing/asserts.ts"
1110import { ignoreNotFoundError } from "../src/utils/ignore-not-found-error.ts"
1211import { waitForProcess } from "../src/utils/process.ts"
1312
@@ -61,7 +60,7 @@ async function assertYamlParsable(yamlFileContent: string) {
6160 try {
6261 await yaml . parseAll ( yamlFileContent )
6362 } catch ( err ) {
64- throw new Error ( `Cloud parse yaml context ${ err } ${ yamlFileContent } ` )
63+ throw new Error ( `Could not parse yaml context ${ err } ${ yamlFileContent } ` )
6564 }
6665}
6766
@@ -163,9 +162,10 @@ Supported helm [command] is:
163162 - template
164163 - install
165164 - upgrade
165+ - push
166166 - diff (helm plugin)
167167 - secrets (helm plugin)
168- - push (helm plugin)
168+ - cm- push (helm plugin)
169169
170170You must use the options of the supported commands in strict order:
171171 $ helm <secrets> <diff> [upgrade/template/install] [RELEASE] [CHART] <flags>
@@ -256,7 +256,7 @@ Deno.test({
256256
257257 assertEquals (
258258 stderr ,
259- "[helm-secrets] Decrypt: tmp-secrets.test-values.yaml\n\n[helm-secrets] Removed: ./ tmp-secrets.test-values.yaml.dec\n"
259+ "[helm-secrets] Decrypt: tmp-secrets.test-values.yaml\n\n[helm-secrets] Removed: tmp-secrets.test-values.yaml.dec\n"
260260 )
261261 assertEquals ( yaml . parseAll ( stdout ) , [
262262 {
@@ -395,7 +395,7 @@ async function startHelmRegistry() {
395395 // eslint-disable-next-line no-constant-condition
396396 while ( true ) {
397397 if ( errorsCount > 10 ) {
398- throw new Error ( "To many errors" )
398+ throw new Error ( "Too many errors" )
399399 }
400400 try {
401401 const response = await fetch ( `http://localhost:${ port } /health` )
@@ -424,8 +424,57 @@ async function startHelmRegistry() {
424424 }
425425}
426426
427+ async function startOCIRegistry ( ) {
428+ const port = await getAvailablePort ( )
429+ const { status, stdout, stderr } = await run ( [
430+ "docker" ,
431+ "run" ,
432+ "--rm" ,
433+ "--detach" ,
434+ `--publish=${ port } :5000` ,
435+ "registry:2.8.1@sha256:11bb1b1a54493dc3626f4bd3cdd74f83e4e5157239ea607a70cbe634f50bb89c" ,
436+ ] )
437+
438+ if ( ! status . success ) {
439+ throw new Error ( `Could not start oci registry ${ stderr } ` )
440+ }
441+ const containerID = stdout . trim ( )
442+
443+ let errorsCount = 0
444+ // eslint-disable-next-line no-constant-condition
445+ while ( true ) {
446+ if ( errorsCount > 10 ) {
447+ throw new Error ( "Too many errors" )
448+ }
449+ try {
450+ const response = await fetch ( `http://localhost:${ port } /v2/` )
451+ await response . text ( )
452+ if ( response . ok ) {
453+ break
454+ }
455+ } catch {
456+ errorsCount ++
457+ }
458+ await sleep ( 200 )
459+ }
460+
461+ return {
462+ url : `oci://localhost:${ port } ` ,
463+ async stop ( ) {
464+ const { status, stderr } = await run ( [ "docker" , "stop" , containerID ] )
465+
466+ if ( ! status . success ) {
467+ const dockerStopError = stderr
468+ throw new Error (
469+ `Could not stop oci registry docker container ${ containerID } ${ dockerStopError } `
470+ )
471+ }
472+ } ,
473+ }
474+ }
475+
427476Deno . test ( {
428- name : "should precompile chart during helm deno push" ,
477+ name : "should precompile chart during helm deno cm- push" ,
429478 ignore : ! runAllTests ,
430479 async fn ( ) {
431480 const helmRegistry = await startHelmRegistry ( )
@@ -437,7 +486,7 @@ Deno.test({
437486 const chartPath = path . join ( chartsBin , "one-service" )
438487
439488 const { status, stderr } = await runHelmDeno ( [
440- "push" ,
489+ "cm- push" ,
441490 chartPath ,
442491 helmRegistry . url ,
443492 ] )
@@ -487,7 +536,107 @@ Deno.test({
487536} )
488537
489538Deno . test ( {
490- name : "should clean deno-bundle.js if push wasn't successful" ,
539+ name : "should precompile chart during helm deno push" ,
540+ ignore : ! runAllTests ,
541+ async fn ( ) {
542+ const ociRegistry = await startOCIRegistry ( )
543+ const fetchDirectory = await Deno . makeTempDir ( {
544+ prefix : "helm-deno-tests-" ,
545+ } )
546+
547+ try {
548+ const chartPath = path . join ( chartsBin , "one-service" )
549+
550+ const { status, stderr } = await runHelmDeno ( [
551+ "push" ,
552+ chartPath ,
553+ ociRegistry . url ,
554+ ] )
555+
556+ if ( ! status . success ) {
557+ console . log ( stderr )
558+ }
559+ assertEquals ( status . success , true , "should successfully push" )
560+
561+ const isDenoBundleExists = await exists (
562+ path . join ( chartPath , "deno-bundle.js" )
563+ )
564+ assertEquals (
565+ isDenoBundleExists ,
566+ false ,
567+ "should not have left temporary file deno-bundle.js"
568+ )
569+
570+ const isHelmPackageExists = await exists (
571+ path . join ( chartPath , "one-service-1.0.0.tgz" )
572+ )
573+ assertEquals (
574+ isHelmPackageExists ,
575+ false ,
576+ "should not have left temporary file one-service-1.0.0.tgz"
577+ )
578+
579+ const fetchResult = await runHelmDeno ( [
580+ "fetch" ,
581+ `${ ociRegistry . url } /one-service` ,
582+ "--untar" ,
583+ "--untardir" ,
584+ fetchDirectory ,
585+ ] )
586+ assertEquals (
587+ fetchResult . status . success ,
588+ true ,
589+ "should successfully fetch"
590+ )
591+
592+ const isDenoBundleInFetchedChartExists = await exists (
593+ path . join ( fetchDirectory , "one-service/deno-bundle.js" )
594+ )
595+ assertEquals (
596+ isDenoBundleInFetchedChartExists ,
597+ true ,
598+ "should have file deno-bundle.js in fetched chart"
599+ )
600+ } finally {
601+ await removeIfExists ( fetchDirectory )
602+ await ociRegistry . stop ( )
603+ }
604+ } ,
605+ } )
606+
607+ Deno . test ( {
608+ name : "should clean deno-bundle.js if cm-push wasn't successful" ,
609+ ignore : ! runAllTests ,
610+ async fn ( ) {
611+ const chartPath = path . join ( chartsBin , "one-service" )
612+ const denoBundlePath = path . join ( chartPath , "deno-bundle.js" )
613+
614+ try {
615+ const { status } = await runHelmDeno ( [
616+ "cm-push" ,
617+ chartPath ,
618+ "http://127.0.0.1:1" ,
619+ ] )
620+
621+ if ( status . success ) {
622+ assertEquals ( status . success , false , "should not successfully push" )
623+ }
624+
625+ const isDenoBundleExists = await exists ( denoBundlePath )
626+ assertEquals (
627+ isDenoBundleExists ,
628+ false ,
629+ "should not have left temporary file deno-bundle.js"
630+ )
631+ } finally {
632+ await removeIfExists ( denoBundlePath )
633+ }
634+ } ,
635+ } )
636+
637+ Deno . test ( {
638+ name :
639+ "should clean deno-bundle.js and helm package if push wasn't successful" ,
491640 ignore : ! runAllTests ,
492641 async fn ( ) {
493642 const chartPath = path . join ( chartsBin , "one-service" )
@@ -510,6 +659,15 @@ Deno.test({
510659 false ,
511660 "should not have left temporary file deno-bundle.js"
512661 )
662+
663+ const isHelmPackageExists = await exists (
664+ path . join ( chartPath , "one-service-1.0.0.tgz" )
665+ )
666+ assertEquals (
667+ isHelmPackageExists ,
668+ false ,
669+ "should not have left temporary file one-service-1.0.0.tgz"
670+ )
513671 } finally {
514672 await removeIfExists ( denoBundlePath )
515673 }
0 commit comments