|
| 1 | +package deploymentrepo_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/go-git/go-git/v5" |
| 8 | + "github.com/go-git/go-git/v5/plumbing" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "sigs.k8s.io/yaml" |
| 11 | + |
| 12 | + deploymentrepo "github.com/openmcp-project/bootstrapper/internal/deployment-repo" |
| 13 | + testutils "github.com/openmcp-project/bootstrapper/test/utils" |
| 14 | +) |
| 15 | + |
| 16 | +func TestDeploymentRepoManager(t *testing.T) { |
| 17 | + testutils.DownloadOCMAndAddToPath(t) |
| 18 | + |
| 19 | + ctfIn := testutils.BuildComponent("./testdata/01/component-constructor.yaml", t) |
| 20 | + |
| 21 | + originDir := t.TempDir() |
| 22 | + |
| 23 | + origin, err := git.PlainInit(originDir, false) |
| 24 | + assert.NoError(t, err) |
| 25 | + |
| 26 | + originWorkTree, err := origin.Worktree() |
| 27 | + assert.NoError(t, err) |
| 28 | + assert.NotNil(t, originWorkTree) |
| 29 | + |
| 30 | + dummyFilePath := filepath.Join(originDir, "dummy.txt") |
| 31 | + testutils.WriteToFile(t, dummyFilePath, "This is a dummy file.") |
| 32 | + testutils.AddFileToWorkTree(t, originWorkTree, "dummy.txt") |
| 33 | + testutils.WorkTreeCommit(t, originWorkTree, "Initial commit") |
| 34 | + |
| 35 | + clusterProviders := []string{"test"} |
| 36 | + serviceProviders := []string{"test"} |
| 37 | + platformServices := []string{"test"} |
| 38 | + imagePullSecrets := []string{"imgpull-a", "imgpull-b"} |
| 39 | + |
| 40 | + deploymentRepoManager, err := deploymentrepo.NewDeploymentRepoManager( |
| 41 | + ctfIn+"//github.com/openmcp-project/openmcp", |
| 42 | + "deployment-templates/templates", |
| 43 | + originDir, |
| 44 | + "incoming", |
| 45 | + "./testdata/01/git-config.yaml"). |
| 46 | + WithClusterProviders(clusterProviders). |
| 47 | + WithServiceProviders(serviceProviders). |
| 48 | + WithPlatformServices(platformServices). |
| 49 | + WithImagePullSecrets(imagePullSecrets). |
| 50 | + Complete(t.Context()) |
| 51 | + assert.NoError(t, err) |
| 52 | + assert.NotNil(t, deploymentRepoManager) |
| 53 | + |
| 54 | + repo := deploymentRepoManager.GitRepoDir() |
| 55 | + |
| 56 | + err = deploymentRepoManager.ApplyTemplates(t.Context()) |
| 57 | + assert.NoError(t, err) |
| 58 | + |
| 59 | + openMCPOperatorRaw := testutils.ReadFromFile(t, filepath.Join(repo, "openmcp-operator.yaml")) |
| 60 | + var openMCPOperator map[string]interface{} |
| 61 | + err = yaml.Unmarshal([]byte(openMCPOperatorRaw), &openMCPOperator) |
| 62 | + assert.NoError(t, err) |
| 63 | + assert.Contains(t, openMCPOperator, "openmcp-operator") |
| 64 | + root := openMCPOperator["openmcp-operator"].(map[string]interface{}) |
| 65 | + assert.Contains(t, root, "version") |
| 66 | + assert.Equal(t, "v0.2.1", root["version"]) |
| 67 | + assert.Contains(t, root, "image") |
| 68 | + assert.Equal(t, "ghcr.io/openmcp-project/images/openmcp-operator:v0.2.1", root["image"]) |
| 69 | + assert.Contains(t, root, "imagePullSecrets") |
| 70 | + imagePullSecretsList := root["imagePullSecrets"].([]interface{}) |
| 71 | + assert.Len(t, imagePullSecrets, 2) |
| 72 | + assert.Contains(t, imagePullSecretsList, map[string]interface{}{"name": "imgpull-a"}) |
| 73 | + assert.Contains(t, imagePullSecretsList, map[string]interface{}{"name": "imgpull-b"}) |
| 74 | + |
| 75 | + err = deploymentRepoManager.ApplyProviders(t.Context()) |
| 76 | + assert.NoError(t, err) |
| 77 | + |
| 78 | + clusterProviderTestRaw := testutils.ReadFromFile(t, filepath.Join(repo, "cluster-providers", "test.yaml")) |
| 79 | + var clusterProviderTest map[string]interface{} |
| 80 | + err = yaml.Unmarshal([]byte(clusterProviderTestRaw), &clusterProviderTest) |
| 81 | + assert.NoError(t, err) |
| 82 | + ValidateProvider(t, clusterProviderTest, "test", "ghcr.io/openmcp-project/images/cluster-provider-test:v0.1.0", []string{"imgpull-a", "imgpull-b"}) |
| 83 | + |
| 84 | + serviceProviderTestRaw := testutils.ReadFromFile(t, filepath.Join(repo, "service-providers", "test.yaml")) |
| 85 | + var serviceProviderTest map[string]interface{} |
| 86 | + err = yaml.Unmarshal([]byte(serviceProviderTestRaw), &serviceProviderTest) |
| 87 | + assert.NoError(t, err) |
| 88 | + ValidateProvider(t, serviceProviderTest, "test", "ghcr.io/openmcp-project/images/service-provider-test:v0.2.0", []string{"imgpull-a", "imgpull-b"}) |
| 89 | + |
| 90 | + platformServiceTestRaw := testutils.ReadFromFile(t, filepath.Join(repo, "platform-services", "test.yaml")) |
| 91 | + var platformServiceTest map[string]interface{} |
| 92 | + err = yaml.Unmarshal([]byte(platformServiceTestRaw), &platformServiceTest) |
| 93 | + assert.NoError(t, err) |
| 94 | + ValidateProvider(t, platformServiceTest, "test", "ghcr.io/openmcp-project/images/platform-service-test:v0.3.0", []string{"imgpull-a", "imgpull-b"}) |
| 95 | + |
| 96 | + err = deploymentRepoManager.ApplyCustomResourceDefinitions(t.Context()) |
| 97 | + assert.NoError(t, err) |
| 98 | + |
| 99 | + crdRaw := testutils.ReadFromFile(t, filepath.Join(repo, "crds", "crd.yaml")) |
| 100 | + assert.NotEmpty(t, crdRaw) |
| 101 | + |
| 102 | + err = deploymentRepoManager.CommitAndPushChanges(t.Context()) |
| 103 | + assert.NoError(t, err) |
| 104 | + |
| 105 | + err = originWorkTree.Checkout(&git.CheckoutOptions{Branch: plumbing.NewBranchReferenceName("incoming")}) |
| 106 | + assert.NoError(t, err) |
| 107 | + |
| 108 | + _ = testutils.ReadFromFile(t, filepath.Join(originDir, "dummy.txt")) |
| 109 | + _ = testutils.ReadFromFile(t, filepath.Join(originDir, "openmcp-operator.yaml")) |
| 110 | + _ = testutils.ReadFromFile(t, filepath.Join(originDir, "cluster-providers", "test.yaml")) |
| 111 | + _ = testutils.ReadFromFile(t, filepath.Join(originDir, "service-providers", "test.yaml")) |
| 112 | + _ = testutils.ReadFromFile(t, filepath.Join(originDir, "platform-services", "test.yaml")) |
| 113 | + _ = testutils.ReadFromFile(t, filepath.Join(originDir, "crds", "crd.yaml")) |
| 114 | +} |
0 commit comments