|
| 1 | +package flux_deployer_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 9 | + |
| 10 | + "github.com/openmcp-project/bootstrapper/internal/flux_deployer" |
| 11 | +) |
| 12 | + |
| 13 | +func TestKustomize(t *testing.T) { |
| 14 | + resourcesYaml, err := flux_deployer.Kustomize("./testdata/02/dev/fluxcd") |
| 15 | + assert.NoError(t, err, "Error running kustomization") |
| 16 | + assert.NotNil(t, resourcesYaml, "Resources yaml should not be nil") |
| 17 | + |
| 18 | + resources, err := flux_deployer.ParseManifests(bytes.NewReader(resourcesYaml)) |
| 19 | + assert.NoError(t, err, "Error parsing manifests") |
| 20 | + assert.Len(t, resources, 3, "There should be 3 resources") |
| 21 | + |
| 22 | + deploy := getResource(resources, "Deployment", "source-controller", "flux-system") |
| 23 | + assert.NotNil(t, deploy, "Deployment should be present") |
| 24 | + image, found, err := unstructured.NestedString(deploy.Object, "spec", "template", "spec", "containers", "0", "image") |
| 25 | + assert.NoError(t, err, "Error getting Deployment image") |
| 26 | + assert.True(t, found, "Deployment image should be found") |
| 27 | + assert.Equal(t, "ghcr.io/fluxcd/source-controller-new:v0.2.0", image, "Deployment image should have the expected value") |
| 28 | + |
| 29 | + repo := getResource(resources, "GitRepository", "environments", "flux-system") |
| 30 | + assert.NotNil(t, repo, "GitRepository should be present") |
| 31 | + branch, found, err := unstructured.NestedString(repo.Object, "spec", "ref", "branch") |
| 32 | + assert.NoError(t, err, "Error getting GitRepository branch") |
| 33 | + assert.True(t, found, "GitRepository branch should be found") |
| 34 | + assert.Equal(t, "dev", branch, "GitRepository branch should have the expected value") |
| 35 | + |
| 36 | + kustomization := getResource(resources, "Kustomization", "flux-system", "flux-system") |
| 37 | + assert.NotNil(t, kustomization, "Kustomization should be present") |
| 38 | + path, found, err := unstructured.NestedString(kustomization.Object, "spec", "path") |
| 39 | + assert.NoError(t, err, "Error getting Kustomization path") |
| 40 | + assert.True(t, found, "Kustomization path should be found") |
| 41 | + assert.Equal(t, "dev/fluxcd", path, "Kustomization path should have the expected value") |
| 42 | +} |
| 43 | + |
| 44 | +func getResource(resources []*unstructured.Unstructured, kind, name, namespace string) *unstructured.Unstructured { |
| 45 | + for _, res := range resources { |
| 46 | + if res.GetKind() == kind && res.GetName() == name && res.GetNamespace() == namespace { |
| 47 | + return res |
| 48 | + } |
| 49 | + } |
| 50 | + return nil |
| 51 | +} |
0 commit comments