Skip to content

Commit 3dbda38

Browse files
committed
make it nicer
1 parent e1674ae commit 3dbda38

File tree

4 files changed

+99
-82
lines changed

4 files changed

+99
-82
lines changed

internal/deployment-repo/deploymentRepoManager.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import (
1010

1111
"github.com/go-git/go-billy/v5"
1212
"github.com/go-git/go-git/v5"
13-
"github.com/openmcp-project/bootstrapper/internal/config"
1413
"github.com/openmcp-project/controller-utils/pkg/clusters"
1514
"sigs.k8s.io/kustomize/api/krusty"
1615
"sigs.k8s.io/kustomize/kyaml/filesys"
1716
"sigs.k8s.io/yaml"
1817

18+
"github.com/openmcp-project/bootstrapper/internal/config"
19+
1920
gitconfig "github.com/openmcp-project/bootstrapper/internal/git-config"
2021
"github.com/openmcp-project/bootstrapper/internal/log"
2122
ocmcli "github.com/openmcp-project/bootstrapper/internal/ocm-cli"
@@ -78,7 +79,8 @@ type DeploymentRepoManager struct {
7879
// fluxcdCV is the component version of the fluxcd source controller component
7980
fluxcdCV *ocmcli.ComponentVersion
8081
// crdFiles is a list of CRD files downloaded from the openmcp-operator component
81-
crdFiles []string
82+
crdFiles []string
83+
// extraManifests is a list of extra manifest files copied from the ExtraManifestDir to the deployment repository
8284
extraManifests []string
8385
}
8486

@@ -191,7 +193,7 @@ func (m *DeploymentRepoManager) Cleanup() {
191193
func (m *DeploymentRepoManager) ApplyTemplates(ctx context.Context) error {
192194
logger := log.GetLogger()
193195

194-
logger.Infof("Applying templates from %s/%s to deployment repository", m.Config.Component.FluxcdTemplateResourcePath, m.Config.Component.OpenMCPOperatorTemplateResourcePath)
196+
logger.Infof("Applying templates from %q/%q to deployment repository", m.Config.Component.FluxcdTemplateResourcePath, m.Config.Component.OpenMCPOperatorTemplateResourcePath)
195197
templateInput := make(map[string]interface{})
196198

197199
openMCPOperatorImageResources := m.openMCPOperatorCV.GetResourcesByType(ocmcli.OCIImageResourceType)
@@ -305,6 +307,9 @@ func (m *DeploymentRepoManager) ApplyCustomResourceDefinitions(ctx context.Conte
305307
logger.Infof("Applying Custom Resource Definitions to deployment repository")
306308

307309
err := m.applyCRDsForComponentVersion(ctx, m.openMCPOperatorCV, crdDirectory)
310+
if err != nil {
311+
return fmt.Errorf("failed to apply CRDs for openmcp-operator component: %w", err)
312+
}
308313

309314
for _, clusterProvider := range m.Config.Providers.ClusterProviders {
310315
clusterProviderCV, err := m.compGetter.GetReferencedComponentVersionRecursive(ctx, m.compGetter.RootComponentVersion(), "cluster-provider-"+clusterProvider)
@@ -552,6 +557,7 @@ func (m *DeploymentRepoManager) UpdateResourcesKustomization() error {
552557
return nil
553558
}
554559

560+
// RunKustomizeAndApply runs kustomize on the environment directory and applies the resulting manifests to the target cluster.
555561
func (m *DeploymentRepoManager) RunKustomizeAndApply(ctx context.Context) error {
556562
logger := log.GetLogger()
557563
fs := filesys.MakeFsOnDisk()
@@ -596,6 +602,7 @@ func (m *DeploymentRepoManager) CommitAndPushChanges(_ context.Context) error {
596602
return nil
597603
}
598604

605+
// GitRepoDir returns the path to the cloned deployment repository.
599606
func (m *DeploymentRepoManager) GitRepoDir() string {
600607
return m.gitRepoDir
601608
}

internal/deployment-repo/deploymentRepoManager_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88

99
"github.com/go-git/go-git/v5"
1010
"github.com/go-git/go-git/v5/plumbing"
11-
"github.com/openmcp-project/bootstrapper/internal/config"
1211
"github.com/openmcp-project/controller-utils/pkg/clusters"
1312
"github.com/stretchr/testify/assert"
1413
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1514
"k8s.io/apimachinery/pkg/runtime/schema"
1615
"sigs.k8s.io/controller-runtime/pkg/client"
1716
"sigs.k8s.io/controller-runtime/pkg/client/fake"
1817

18+
"github.com/openmcp-project/bootstrapper/internal/config"
19+
1920
deploymentrepo "github.com/openmcp-project/bootstrapper/internal/deployment-repo"
2021
testutils "github.com/openmcp-project/bootstrapper/test/utils"
2122
)

test/utils/filesystem.go

Lines changed: 85 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -129,69 +129,15 @@ func AssertDirectoriesEqualWithIgnore(t *testing.T, expectedDir, actualDir strin
129129
}
130130
}
131131

132-
// AssertDirectoryContains checks if the actual directory contains all files from the expected directory
133-
// and their content matches. It allows extra files in the actual directory that are not in expected.
134-
func AssertDirectoryContains(t *testing.T, expectedDir, actualDir string) {
135-
t.Helper()
136-
137-
// Collect all files from expected directory
138-
expectedFiles := make(map[string][]byte)
139-
140-
// Walk expected directory
141-
err := filepath.Walk(expectedDir, func(path string, info os.FileInfo, err error) error {
142-
if err != nil {
143-
return err
144-
}
145-
if !info.IsDir() {
146-
relPath, err := filepath.Rel(expectedDir, path)
147-
if err != nil {
148-
return err
149-
}
150-
content, err := os.ReadFile(path)
151-
if err != nil {
152-
return err
153-
}
154-
expectedFiles[relPath] = content
155-
}
156-
return nil
157-
})
158-
assert.NoError(t, err, "Failed to walk expected directory: %s", expectedDir)
159-
160-
// Check each expected file exists in actual directory
161-
for relPath, expectedContent := range expectedFiles {
162-
actualPath := filepath.Join(actualDir, relPath)
163-
actualContent, err := os.ReadFile(actualPath)
164-
assert.NoError(t, err, "File %s should exist in actual directory", relPath)
165-
if err == nil {
166-
assert.Equal(t, string(expectedContent), string(actualContent), "Content of file %s should match", relPath)
167-
}
168-
}
169-
}
170-
171-
// AssertFileExists checks if a file exists at the specified path within a directory
172-
func AssertFileExists(t *testing.T, dir, relativeFilePath string) {
173-
t.Helper()
174-
175-
fullPath := filepath.Join(dir, relativeFilePath)
176-
_, err := os.Stat(fullPath)
177-
assert.NoError(t, err, "File %s should exist in directory %s", relativeFilePath, dir)
178-
}
179-
180-
// AssertFileContent checks if a file exists and has the expected content
181-
func AssertFileContent(t *testing.T, dir, relativeFilePath, expectedContent string) {
182-
t.Helper()
183-
184-
fullPath := filepath.Join(dir, relativeFilePath)
185-
actualContent, err := os.ReadFile(fullPath)
186-
assert.NoError(t, err, "Should be able to read file %s", relativeFilePath)
187-
if err == nil {
188-
assert.Equal(t, expectedContent, string(actualContent), "Content of file %s should match", relativeFilePath)
189-
}
190-
}
191-
192132
// AssertDirectoriesEqualWithNormalization compares two directories recursively with content normalization
193133
// for handling dynamic values like temporary paths, timestamps, etc.
194134
func AssertDirectoriesEqualWithNormalization(t *testing.T, expectedDir, actualDir string, normalizer func(string, string) string) {
135+
AssertDirectoriesEqualWithNormalizationWithIgnore(t, expectedDir, actualDir, defaultIgnoredDirs, normalizer)
136+
}
137+
138+
// AssertDirectoriesEqualWithNormalizationWithIgnore compares two directories recursively with content normalization
139+
// and a custom ignore list
140+
func AssertDirectoriesEqualWithNormalizationWithIgnore(t *testing.T, expectedDir, actualDir string, ignoredDirs []string, normalizer func(string, string) string) {
195141
t.Helper()
196142

197143
// Collect all files from both directories
@@ -240,7 +186,7 @@ func AssertDirectoriesEqualWithNormalization(t *testing.T, expectedDir, actualDi
240186
}
241187

242188
// Check if we should ignore this directory
243-
if info.IsDir() && shouldIgnoreDir(info.Name(), defaultIgnoredDirs) {
189+
if info.IsDir() && shouldIgnoreDir(info.Name(), ignoredDirs) {
244190
return filepath.SkipDir
245191
}
246192

@@ -253,7 +199,7 @@ func AssertDirectoriesEqualWithNormalization(t *testing.T, expectedDir, actualDi
253199
// Skip files in ignored directories
254200
pathParts := strings.Split(relPath, string(filepath.Separator))
255201
for _, part := range pathParts {
256-
if shouldIgnoreDir(part, defaultIgnoredDirs) {
202+
if shouldIgnoreDir(part, ignoredDirs) {
257203
return nil
258204
}
259205
}
@@ -288,3 +234,80 @@ func AssertDirectoriesEqualWithNormalization(t *testing.T, expectedDir, actualDi
288234
assert.True(t, exists, "Unexpected file %s found in actual directory", relPath)
289235
}
290236
}
237+
238+
// AssertDirectoryContains checks if the actual directory contains all files from the expected directory
239+
// and their content matches. It allows extra files in the actual directory that are not in expected.
240+
func AssertDirectoryContains(t *testing.T, expectedDir, actualDir string) {
241+
t.Helper()
242+
243+
// Collect all files from expected directory
244+
expectedFiles := make(map[string][]byte)
245+
246+
// Walk expected directory
247+
err := filepath.Walk(expectedDir, func(path string, info os.FileInfo, err error) error {
248+
if err != nil {
249+
return err
250+
}
251+
if !info.IsDir() {
252+
relPath, err := filepath.Rel(expectedDir, path)
253+
if err != nil {
254+
return err
255+
}
256+
content, err := os.ReadFile(path)
257+
if err != nil {
258+
return err
259+
}
260+
expectedFiles[relPath] = content
261+
}
262+
return nil
263+
})
264+
assert.NoError(t, err, "Failed to walk expected directory: %s", expectedDir)
265+
266+
// Check each expected file exists in actual directory
267+
for relPath, expectedContent := range expectedFiles {
268+
actualPath := filepath.Join(actualDir, relPath)
269+
actualContent, err := os.ReadFile(actualPath)
270+
assert.NoError(t, err, "File %s should exist in actual directory", relPath)
271+
if err == nil {
272+
assert.Equal(t, string(expectedContent), string(actualContent), "Content of file %s should match", relPath)
273+
}
274+
}
275+
}
276+
277+
// AssertFileExists checks if a file exists at the specified path within a directory
278+
func AssertFileExists(t *testing.T, dir, relativeFilePath string) {
279+
t.Helper()
280+
281+
fullPath := filepath.Join(dir, relativeFilePath)
282+
_, err := os.Stat(fullPath)
283+
assert.NoError(t, err, "File %s should exist in directory %s", relativeFilePath, dir)
284+
}
285+
286+
// AssertFileContent checks if a file exists and has the expected content
287+
func AssertFileContent(t *testing.T, dir, relativeFilePath, expectedContent string) {
288+
t.Helper()
289+
290+
fullPath := filepath.Join(dir, relativeFilePath)
291+
actualContent, err := os.ReadFile(fullPath)
292+
assert.NoError(t, err, "Should be able to read file %s", relativeFilePath)
293+
if err == nil {
294+
assert.Equal(t, expectedContent, string(actualContent), "Content of file %s should match", relativeFilePath)
295+
}
296+
}
297+
298+
// WriteToFile writes content to a file at the specified path
299+
func WriteToFile(t *testing.T, filePath, content string) {
300+
err := os.WriteFile(filePath, []byte(content), 0644)
301+
if err != nil {
302+
t.Fatalf("failed to write to file %s: %v", filePath, err)
303+
}
304+
}
305+
306+
// ReadFromFile reads content from a file at the specified path
307+
func ReadFromFile(t *testing.T, filePath string) string {
308+
data, err := os.ReadFile(filePath)
309+
if err != nil {
310+
t.Fatalf("failed to read from file %s: %v", filePath, err)
311+
}
312+
return string(data)
313+
}

test/utils/git.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
11
package utils
22

33
import (
4-
"os"
54
"testing"
65
"time"
76

87
"github.com/go-git/go-git/v5"
98
"github.com/go-git/go-git/v5/plumbing/object"
109
)
1110

12-
func WriteToFile(t *testing.T, filePath, content string) {
13-
err := os.WriteFile(filePath, []byte(content), 0644)
14-
if err != nil {
15-
t.Fatalf("failed to write to file %s: %v", filePath, err)
16-
}
17-
}
18-
19-
func ReadFromFile(t *testing.T, filePath string) string {
20-
data, err := os.ReadFile(filePath)
21-
if err != nil {
22-
t.Fatalf("failed to read from file %s: %v", filePath, err)
23-
}
24-
return string(data)
25-
}
26-
11+
// AddFileToWorkTree stages a file in the given worktree
2712
func AddFileToWorkTree(t *testing.T, workTree *git.Worktree, filePath string) {
2813
_, err := workTree.Add(filePath)
2914
if err != nil {
3015
t.Fatalf("failed to add file %s to worktree: %v", filePath, err)
3116
}
3217
}
3318

19+
// WorkTreeCommit commits staged changes in the given worktree with the specified message
3420
func WorkTreeCommit(t *testing.T, workTree *git.Worktree, message string) {
3521
_, err := workTree.Commit(message, &git.CommitOptions{
3622
Author: &object.Signature{

0 commit comments

Comments
 (0)