# HG changeset patch
# User Florent Aide <florent.aide@gmail.com>
# Date 1697555889 -7200
#      Tue Oct 17 17:18:09 2023 +0200
# Node ID ac27a21a752d0019cb108f683ad38bb9aea3e2aa
# Parent  2e270a1fd7d2433581a6bc71733fad93125e8273
add tests to prove the chart disabled as a variable

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -8,7 +8,7 @@
 
 unit_tests:
   stage: test
-  image: quay.orus.io/cloudcrane/golang:1.20.6-bullseye
+  image: quay.orus.io/cc/golang:1.21.3-bullseye
   variables:
     YTT_VERSION: v0.41.1
     YTT_SHA256: 65dbc4f3a4a2ed84296dd1b323e8e7bd77e488fa7540d12dd36cf7fb2fc77c03
@@ -43,7 +43,7 @@
 
 lint:
   stage: lint
-  image: quay.orus.io/cloudcrane/golang:1.20.6-bullseye
+  image: quay.orus.io/cc/golang:1.21.3-bullseye
   cache:
     key: tooling
     paths:
@@ -56,7 +56,7 @@
 
 vulncheck:
   stage: vulncheck
-  image: quay.orus.io/cloudcrane/golang:1.20.6-bullseye
+  image: quay.orus.io/cc/golang:1.21.3-bullseye
   cache:
     key: tooling
     paths:
@@ -69,7 +69,7 @@
 
 build:
   stage: build
-  image: quay.orus.io/cloudcrane/golang:1.20.6-bullseye
+  image: quay.orus.io/cc/golang:1.21.3-bullseye
   artifacts:
     paths:
       - build
diff --git a/runner/config_test.go b/runner/config_test.go
--- a/runner/config_test.go
+++ b/runner/config_test.go
@@ -2,6 +2,7 @@
 
 import (
 	"bytes"
+	"errors"
 	"fmt"
 	"os"
 	"path/filepath"
@@ -15,10 +16,11 @@
 	"orus.io/orus-io/beaver/testutils"
 )
 
-var (
+const (
 	fixtures              = "fixtures/f1"
 	shaFixtures           = "fixtures/f2"
 	helmNamespaceFixtures = "fixtures/f3"
+	disabledAsVar         = "fixtures/fDisabledAsVar"
 )
 
 func TestConfig(t *testing.T) {
@@ -240,6 +242,65 @@
 	}
 }
 
+type disabledTCase struct {
+	Name        string
+	TestPath    string
+	FilePresent bool
+}
+
+func TestDisabledAsVariable(t *testing.T) {
+	tCases := []disabledTCase{
+		{
+			Name:        "disabled",
+			TestPath:    "noconfigmap",
+			FilePresent: false,
+		},
+		{
+			Name:        "enabled",
+			TestPath:    "configmapenabled",
+			FilePresent: true,
+		},
+	}
+	for _, tCase := range tCases {
+		t.Run(tCase.Name, func(t *testing.T) {
+			runTestDisabledAsVar(t, tCase)
+		})
+	}
+}
+
+func runTestDisabledAsVar(t *testing.T, tCase disabledTCase) {
+	t.Helper()
+	buildDir := filepath.Join(disabledAsVar, "build", "example")
+	defer func() {
+		require.NoError(t, runner.CleanDir(buildDir))
+	}()
+
+	tl := testutils.NewTestLogger(t)
+	absRootDir, err := filepath.Abs(disabledAsVar)
+	require.NoError(t, err)
+	c := runner.NewCmdConfig(tl.Logger(), absRootDir, tCase.TestPath, false, "", "")
+	tmpDir, err := os.MkdirTemp(os.TempDir(), "beaver-")
+	require.NoError(t, err)
+	defer func() {
+		assert.NoError(t, os.RemoveAll(tmpDir))
+	}()
+	require.NoError(t, c.Initialize(tmpDir))
+	r := runner.NewRunner(c)
+	require.NoError(t, r.Build(tmpDir))
+
+	outPutConfigMapName := filepath.Join(buildDir, "ConfigMap.v1.demo.yaml")
+	if !tCase.FilePresent {
+		_, err = os.Stat(outPutConfigMapName)
+		require.Error(t, err)
+		// file should not exist since it is disabled by a variable in the noconfigmap/beaver.yaml
+		require.True(t, errors.Is(err, os.ErrNotExist))
+	} else {
+		_, err = os.Stat(outPutConfigMapName)
+		// file should exist
+		require.NoError(t, err)
+	}
+}
+
 func TestSha(t *testing.T) {
 	shaValue := "2145bea9e32804c65d960e6d4af1c87f95ccc39fad7df5eec2f3925a193112ab"
 	buildDir := filepath.Join(shaFixtures, "build", "example")
diff --git a/runner/fixtures/f2/base/beaver.yaml b/runner/fixtures/fDisabledAsVar/base/beaver.yaml
copy from runner/fixtures/f2/base/beaver.yaml
copy to runner/fixtures/fDisabledAsVar/base/beaver.yaml
--- a/runner/fixtures/f2/base/beaver.yaml
+++ b/runner/fixtures/fDisabledAsVar/base/beaver.yaml
@@ -1,11 +1,6 @@
 namespace: example
-sha:
-- key: configmap_demo
-  resource: ConfigMap.v1.demo.yaml
 charts:
   demoytt:
     type: ytt
     path: demoytt.tmpl.yaml
-  demohelm:
-    type: helm
-    path: demohelm
+    disabled: <[configmapDisabled]>
diff --git a/runner/fixtures/f2/base/demoytt.tmpl.yaml b/runner/fixtures/fDisabledAsVar/base/demoytt.tmpl.yaml
copy from runner/fixtures/f2/base/demoytt.tmpl.yaml
copy to runner/fixtures/fDisabledAsVar/base/demoytt.tmpl.yaml
--- a/runner/fixtures/f2/base/demoytt.tmpl.yaml
+++ b/runner/fixtures/fDisabledAsVar/base/demoytt.tmpl.yaml
@@ -4,7 +4,5 @@
 kind: ConfigMap
 metadata:
   name: demo
-  labels:
-    mysha: #@ data.values.sha
 data:
   answer: #@ data.values.answer
diff --git a/runner/fixtures/f2/base/demoytt.yaml b/runner/fixtures/fDisabledAsVar/base/demoytt.yaml
copy from runner/fixtures/f2/base/demoytt.yaml
copy to runner/fixtures/fDisabledAsVar/base/demoytt.yaml
--- a/runner/fixtures/f2/base/demoytt.yaml
+++ b/runner/fixtures/fDisabledAsVar/base/demoytt.yaml
@@ -1,4 +1,3 @@
 #@data/values
 ---
 answer: 42
-sha: <[sha.configmap_demo]>
diff --git a/runner/fixtures/f1/environments/ns1/beaver.yml b/runner/fixtures/fDisabledAsVar/configmapenabled/beaver.yml
copy from runner/fixtures/f1/environments/ns1/beaver.yml
copy to runner/fixtures/fDisabledAsVar/configmapenabled/beaver.yml
--- a/runner/fixtures/f1/environments/ns1/beaver.yml
+++ b/runner/fixtures/fDisabledAsVar/configmapenabled/beaver.yml
@@ -1,13 +1,5 @@
 namespace: ns1
-inherit: ../../base
+inherit: ../base
 variables:
-- name: VAULT_KV
-  value: k8s.orus.io
-- name: test-nested.nested-value1
-  value: another value
-create:
-- type: configmap
-  name: xbus-pipelines
-  args:
-  - flag: --from-file
-    value: pipelines
+- name: configmapDisabled
+  value: false
diff --git a/runner/fixtures/f1/environments/ns1/beaver.yml b/runner/fixtures/fDisabledAsVar/noconfigmap/beaver.yml
copy from runner/fixtures/f1/environments/ns1/beaver.yml
copy to runner/fixtures/fDisabledAsVar/noconfigmap/beaver.yml
--- a/runner/fixtures/f1/environments/ns1/beaver.yml
+++ b/runner/fixtures/fDisabledAsVar/noconfigmap/beaver.yml
@@ -1,13 +1,5 @@
 namespace: ns1
-inherit: ../../base
+inherit: ../base
 variables:
-- name: VAULT_KV
-  value: k8s.orus.io
-- name: test-nested.nested-value1
-  value: another value
-create:
-- type: configmap
-  name: xbus-pipelines
-  args:
-  - flag: --from-file
-    value: pipelines
+- name: configmapDisabled
+  value: true
diff --git a/runner/hydrate.go b/runner/hydrate.go
--- a/runner/hydrate.go
+++ b/runner/hydrate.go
@@ -45,7 +45,7 @@
 		return err
 	}
 
-	// Search for over occurences of beaver variables
+	// Search for over occurrences of beaver variables
 	regex := regexp.MustCompile(`<\[([^<\[\]>]*)\]>`)
 	for regex.MatchString(s) {
 		return hydrateString(s, output, variables)
diff --git a/runner/main_test.go b/runner/main_test.go
--- a/runner/main_test.go
+++ b/runner/main_test.go
@@ -2,12 +2,13 @@
 
 import (
 	"testing"
+
 	"github.com/stretchr/testify/require"
 )
 
 type ToBoolTestCase struct {
 	Input       string
-	Ouput       bool
+	Output      bool
 	ExpectError bool // if you expect an error put true here
 }
 
@@ -15,37 +16,37 @@
 	tCases := []ToBoolTestCase{
 		{
 			Input:       "1",
-			Ouput:       true,
+			Output:      true,
 			ExpectError: false,
 		},
 		{
 			Input:       "0",
-			Ouput:       false,
+			Output:      false,
 			ExpectError: false,
 		},
 		{
 			Input:       "True",
-			Ouput:       true,
+			Output:      true,
 			ExpectError: false,
 		},
 		{
 			Input:       "true",
-			Ouput:       true,
+			Output:      true,
 			ExpectError: false,
 		},
 		{
 			Input:       "False",
-			Ouput:       false,
+			Output:      false,
 			ExpectError: false,
 		},
 		{
 			Input:       "false",
-			Ouput:       false,
+			Output:      false,
 			ExpectError: false,
 		},
 		{
 			Input:       "flase",
-			Ouput:       false,
+			Output:      false,
 			ExpectError: true,
 		},
 	}
@@ -55,7 +56,7 @@
 			require.Error(t, err)
 		} else {
 			require.NoError(t, err)
-			require.Equal(t, tCase.Ouput, res)
+			require.Equal(t, tCase.Output, res)
 		}
 	}
 }