Skip to content
Snippets Groups Projects
Commit 3d9d30f69999 authored by Florent Aide's avatar Florent Aide
Browse files

add test coverage for beaver namespaces as variables

parent ac27a21a752d
No related branches found
No related tags found
No related merge requests found
Pipeline #68427 passed
......@@ -155,8 +155,7 @@
## Beaver variables
`beaver` variables can be used inside your value files, and **only** inside value
files, using the following syntax:
`beaver` variables can be used inside your value files, using the following syntax:
```
<[variable_name]>
......@@ -199,6 +198,67 @@
here `pg_tag` value will be `13.7-alpine` if you run
`beaver build environments/demo`.
### Beaver variables in the beaver namespace itself
You can set some variables in the 'namespace' keyword of a beaver file.
```yaml
# example/base/beaver.yml
namespace: <[myns]>
charts:
demoytt:
type: ytt
path: demoytt.tmpl.yaml
```
in this case this means this base is not useable by itself but can now be adapted by the caller by setting a beaver
variable to fill the slot
```yaml
# example/ns1/beaver.yml
namespace: ns1
inherit: ../base
variables:
- name: myns
value: ns1yo
```
This is a somewhat warped example but in this case the resulting ouput dir (namespace for beaver) will be example/build/ns1yo
### variables inside the charts.disabled flag
imagine a base with a chart that is disabled by default
```yaml
# example/base/beaver.yml
namespace: example
charts:
demoytt:
type: ytt
path: demoytt.tmpl.yaml
disabled: <[configmapDisabled]>
variables:
- name: configmapDisabled
value: true
```
and another file that inherits from this base and want to have this chart enabled
```yaml
# example/configmapenabled/beaver.yml
namespace: ns1
inherit: ../base
variables:
- name: configmapDisabled
value: false
```
This can be used to allow for options in some inheritance cases where you want to enable/disable a certain backend like
a Redis server, a Postgresql server.
Your base provides the diverse options and your inheritance will pick the ones they need.
## Output files
`beaver` output files have the following format:
......@@ -209,7 +269,7 @@
all `apiVersion` slashes (`/`) are replaced by underscores (`_`).
This convention will help you reviewing merge requests.
This convention will help you review merge requests.
By default `beaver` will store those files inside `${PWD}/build/<namespace>`, you
can use `-o` or `--output` to specify an output directory.
......@@ -247,7 +307,7 @@
[ytt overlays](https://carvel.dev/ytt/docs/v0.39.0/ytt-overlays/) by providing
`ytt.yaml` or `ytt.yml` files or a `ytt` folder inside your `beaver` project(s).
You can use `beaver` variables inside ytt files (outside of ytt folder), because
You can use `beaver` variables inside ytt files (outside ytt folder), because
`beaver` considers those as value files.
## Create resources using kubectl create
......
......@@ -21,6 +21,7 @@
shaFixtures = "fixtures/f2"
helmNamespaceFixtures = "fixtures/f3"
disabledAsVar = "fixtures/fDisabledAsVar"
namespaceAsVar = "fixtures/fNamespaceAsVar"
)
func TestConfig(t *testing.T) {
......@@ -292,7 +293,70 @@
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
// file should not exist since it is disabled by a variable in the ns2/beaver.yaml
require.True(t, errors.Is(err, os.ErrNotExist))
} else {
_, err = os.Stat(outPutConfigMapName)
// file should exist
require.NoError(t, err)
}
}
type namespaceTCase struct {
Name string
TestPath string
ExpectedBuildDir string
FilePresent bool
}
func TestNamaspaceAsVariable(t *testing.T) {
tCases := []namespaceTCase{
{
Name: "ns1yo",
TestPath: "ns1",
ExpectedBuildDir: filepath.Join(namespaceAsVar, "build", "ns1yo"),
FilePresent: true,
},
{
Name: "ns2yo",
TestPath: "ns2",
ExpectedBuildDir: filepath.Join(namespaceAsVar, "build", "ns2yo"),
FilePresent: true,
},
}
for _, tCase := range tCases {
t.Run(tCase.Name, func(t *testing.T) {
runTestBeaverNamespaceAsVariable(t, tCase)
})
}
}
func runTestBeaverNamespaceAsVariable(t *testing.T, tCase namespaceTCase) {
t.Helper()
defer func() {
require.NoError(t, runner.CleanDir(tCase.ExpectedBuildDir))
require.NoError(t, os.RemoveAll(tCase.ExpectedBuildDir))
}()
tl := testutils.NewTestLogger(t)
absRootDir, err := filepath.Abs(namespaceAsVar)
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))
// the output file should be in a directory that matches the variable namespace
outPutConfigMapName := filepath.Join(tCase.ExpectedBuildDir, "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 ns2/beaver.yaml
require.True(t, errors.Is(err, os.ErrNotExist))
} else {
_, err = os.Stat(outPutConfigMapName)
......
......@@ -4,3 +4,6 @@
type: ytt
path: demoytt.tmpl.yaml
disabled: <[configmapDisabled]>
variables:
- name: configmapDisabled
value: true
namespace: example
namespace: <[myns]>
charts:
demoytt:
type: ytt
path: demoytt.tmpl.yaml
......@@ -2,5 +2,4 @@
charts:
demoytt:
type: ytt
path: demoytt.tmpl.yaml
disabled: <[configmapDisabled]>
namespace: ns1
inherit: ../base
variables:
- name: configmapDisabled
value: false
- name: myns
value: ns1yo
namespace: ns1
namespace: ns2
inherit: ../base
variables:
......@@ -2,4 +2,4 @@
inherit: ../base
variables:
- name: configmapDisabled
value: true
- name: myns
value: ns2yo
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment