Skip to content

Commit 00ef89a

Browse files
committed
feat: unit tests
1 parent 18dec9b commit 00ef89a

File tree

7 files changed

+39
-6
lines changed

7 files changed

+39
-6
lines changed

internal/flux_deployer/deployer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func (d *FluxDeployer) Template() (err error) {
207207
return fmt.Errorf("failed to apply fluxcd image automation controller template input: %w", err)
208208
}
209209

210-
if err = TemplateDirectory(d.templatesDir, templateInput, d.repoDir, d.log); err != nil {
210+
if err = TemplateDirectory(d.templatesDir, d.repoDir, templateInput, d.log); err != nil {
211211
return fmt.Errorf("failed to apply templates from directory %s: %w", d.templatesDir, err)
212212
}
213213

File renamed without changes.

internal/flux_deployer/template.go renamed to internal/flux_deployer/templater.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
)
1212

1313
// TemplateDirectory processes the template files in the specified directory and writes
14-
// the rendered content to the corresponding files in the Git repository's worktree.
15-
// It uses the provided template directory and Git repository to perform the operations.
16-
func TemplateDirectory(templateDirectory string, templateInput TemplateInput, repo string, logger *logrus.Logger) error {
14+
// the rendered content to the corresponding files in the result directory.
15+
func TemplateDirectory(templateDirectory, resultDirectory string, templateInput TemplateInput, log *logrus.Logger) error {
16+
log.Info("Templating")
1717

1818
templateDir, err := os.Open(templateDirectory)
1919
if err != nil {
@@ -47,15 +47,15 @@ func TemplateDirectory(templateDirectory string, templateInput TemplateInput, re
4747
if errInWalk != nil {
4848
return fmt.Errorf("failed to get relative path for %s: %w", path, errInWalk)
4949
}
50-
pathInWorkTree := filepath.Join(repo, relativePath)
50+
pathInWorkTree := filepath.Join(resultDirectory, relativePath)
5151

5252
if d.IsDir() {
5353
err = os.MkdirAll(pathInWorkTree, 0755)
5454
if err != nil {
5555
return fmt.Errorf("failed to create directory %s: %w", path, err)
5656
}
5757
} else {
58-
logger.Debugf("Found template file: %s", relativePath)
58+
log.Debugf("Found template file: %s", relativePath)
5959

6060
templateFromFile, errInWalk = os.ReadFile(path)
6161
if errInWalk != nil {
@@ -84,6 +84,7 @@ func TemplateDirectory(templateDirectory string, templateInput TemplateInput, re
8484
}
8585
}
8686

87+
log.Info("Templating done")
8788
return nil
8889
})
8990

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package flux_deployer_test
2+
3+
import (
4+
"os"
5+
"path"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
10+
"github.com/openmcp-project/bootstrapper/internal/flux_deployer"
11+
logging "github.com/openmcp-project/bootstrapper/internal/log"
12+
)
13+
14+
func TestTemplateDirectory(t *testing.T) {
15+
ti := flux_deployer.TemplateInput{
16+
"test1": "foo",
17+
"test2": "bar",
18+
}
19+
resultDir := t.TempDir()
20+
err := flux_deployer.TemplateDirectory("./testdata/03/templates", resultDir, ti, logging.GetLogger())
21+
assert.NoError(t, err, "error templating directory")
22+
23+
contentA, err := os.ReadFile(path.Join(resultDir, "a.yaml"))
24+
assert.NoError(t, err, "error reading templated a.yaml")
25+
assert.Equal(t, "test: foo", string(contentA), "templated content of a.yaml does not match expected")
26+
27+
contentB, err := os.ReadFile(path.Join(resultDir, "b/c.yaml"))
28+
assert.NoError(t, err, "error reading templated b/c.yaml")
29+
assert.Equal(t, "test: bar", string(contentB), "templated content of b/c.yaml does not match expected")
30+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test: {{ .Values.test1 }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test: {{ .Values.test2 }}

0 commit comments

Comments
 (0)