diff --git a/e2e/helm_test.go b/e2e/helm_test.go new file mode 100644 index 00000000..acfbfcea --- /dev/null +++ b/e2e/helm_test.go @@ -0,0 +1,89 @@ +// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Gardener contributors. +// +// SPDX-License-Identifier: Apache-2.0 + +//go:build e2e + +package e2e + +import ( + "os" + "path/filepath" + "testing" + + "github.com/open-component-model/ocm-e2e-framework/shared" + "github.com/stretchr/testify/require" + "sigs.k8s.io/e2e-framework/pkg/features" + + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta2" + sourcev1 "github.com/fluxcd/source-controller/api/v1beta2" + "github.com/open-component-model/ocm-controller/api/v1alpha1" + "github.com/open-component-model/ocm-e2e-framework/shared/steps/setup" +) + +func TestHelmChartResource(t *testing.T) { + t.Log("running e2e helm chart test") + + charts, err := os.ReadFile(filepath.Join("testdata", testHelmChartBasedResource, "podinfo-6.3.5.tgz")) + require.NoError(t, err) + + setupComponent := features.New("Add components to component-version"). + Setup(setup.AddComponentVersions(setup.Component{ + Component: shared.Component{ + Name: "github.com/open-component-model/helm-test", + Version: "1.0.0", + }, + ComponentVersionModifications: []shared.ComponentModification{ + shared.BlobResource(shared.Resource{ + Name: "charts", + Data: string(charts), + Type: "helmChart", + Version: "6.3.5", + }), + }, + })) + + management := features.New("Configure Management Repository"). + Setup(setup.AddScheme(v1alpha1.AddToScheme)). + Setup(setup.AddScheme(sourcev1.AddToScheme)). + Setup(setup.AddScheme(kustomizev1.AddToScheme)). + Setup(setup.AddGitRepository(testRepoName)). + Setup(setup.AddFluxSyncForRepo(testRepoName, destinationPrefix, ocmNamespace)) + + componentVersion := features.New("Create Manifests"). + Setup(setup.AddFilesToGitRepository(getHelmManifests(testHelmChartBasedResource, testRepoName)...)). + Assess("check that component version is ready and valid", checkIsComponentVersionReady("ocm-with-helm", ocmNamespace)) + + validationDeploymentBackend := checkDeploymentReadiness("fluxdeployer-podinfo-pipeline-backend", "ghcr.io/stefanprodan/podinfo") + + testEnv.Test(t, + setupComponent.Feature(), + management.Feature(), + componentVersion.Feature(), + validationDeploymentBackend.Feature(), + ) +} + +func getHelmManifests(testName string, gitRepositoryName string) []setup.File { + cvManifest := setup.File{ + RepoName: gitRepositoryName, + SourceFilepath: filepath.Join(testName, cvFile), + DestFilepath: destinationPrefix + testName + cvFile, + } + resourceManifest := setup.File{ + RepoName: gitRepositoryName, + SourceFilepath: filepath.Join(testName, resourceFile), + DestFilepath: destinationPrefix + testName + resourceFile, + } + deployerManifestBackend := setup.File{ + RepoName: gitRepositoryName, + SourceFilepath: filepath.Join(testName, deployerFile), + DestFilepath: destinationPrefix + deployerFile, + } + + return []setup.File{ + cvManifest, + resourceManifest, + deployerManifestBackend, + } +} diff --git a/e2e/main_test.go b/e2e/main_test.go index edbc9f23..e92cf000 100644 --- a/e2e/main_test.go +++ b/e2e/main_test.go @@ -43,20 +43,21 @@ import ( ) var ( - timeoutDuration = time.Minute * 2 - testRepoName = "ocm-controller-test" - testRepoSignedName = "ocm-controller-signed-test" - testOCMControllerPath = "testOCMController" - testSignedComponentsPath = "testSignedOCIRegistryComponents" - keyName = "rsa" - cvFile = "component_version.yaml" - localizationFile = "localization.yaml" - resourceFile = "resource.yaml" - configurationFile = "configuration.yaml" - deployerFile = "deployer.yaml" - destinationPrefix = "apps/" - identifier = "metadata.name" - version1 = "1.0.0" + timeoutDuration = time.Minute * 2 + testRepoName = "ocm-controller-test" + testRepoSignedName = "ocm-controller-signed-test" + testHelmChartBasedResource = "testHelmChartResource" + testOCMControllerPath = "testOCMController" + testSignedComponentsPath = "testSignedOCIRegistryComponents" + keyName = "rsa" + cvFile = "component_version.yaml" + localizationFile = "localization.yaml" + resourceFile = "resource.yaml" + configurationFile = "configuration.yaml" + deployerFile = "deployer.yaml" + destinationPrefix = "apps/" + identifier = "metadata.name" + version1 = "1.0.0" ) func TestOCMController(t *testing.T) { @@ -449,7 +450,7 @@ func checkCustomResourcesReadiness(path string) *features.FeatureBuilder { func checkDeploymentReadiness(deploymentName string, imageName string) *features.FeatureBuilder { return features.New("Validate OCM Pipeline: Deployment"). - Assess("check that deployment "+deploymentName+" was localized", + Assess("check that deployment "+deploymentName+" is ready", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { client, err := cfg.NewClient() if err != nil { @@ -535,4 +536,4 @@ func reasonMatches(from fconditions.Getter, reason string) bool { } } return false -} \ No newline at end of file +} diff --git a/e2e/testdata/testHelmChartResource/component_version.yaml b/e2e/testdata/testHelmChartResource/component_version.yaml new file mode 100644 index 00000000..124179c6 --- /dev/null +++ b/e2e/testdata/testHelmChartResource/component_version.yaml @@ -0,0 +1,14 @@ +apiVersion: delivery.ocm.software/v1alpha1 +kind: ComponentVersion +metadata: + name: ocm-with-helm + namespace: ocm-system +spec: + interval: 10m0s + component: github.com/skarlso/podinfo-helm + version: + semver: 1.0.0 + repository: + url: ghcr.io/skarlso + secretRef: + name: creds \ No newline at end of file diff --git a/e2e/testdata/testHelmChartResource/deployer.yaml b/e2e/testdata/testHelmChartResource/deployer.yaml new file mode 100644 index 00000000..c81a3128 --- /dev/null +++ b/e2e/testdata/testHelmChartResource/deployer.yaml @@ -0,0 +1,17 @@ +apiVersion: delivery.ocm.software/v1alpha1 +kind: FluxDeployer +metadata: + name: fluxdeployer-podinfo-pipeline-backend + namespace: ocm-system +spec: + interval: 1m0s + sourceRef: + kind: Resource + name: ocm-with-helm-deployment + helmReleaseTemplate: + chart: + spec: + chart: podinfo + version: 6.3.5 + interval: 5m + diff --git a/e2e/testdata/testHelmChartResource/podinfo-6.3.5.tgz b/e2e/testdata/testHelmChartResource/podinfo-6.3.5.tgz new file mode 100644 index 00000000..df0424f5 Binary files /dev/null and b/e2e/testdata/testHelmChartResource/podinfo-6.3.5.tgz differ diff --git a/e2e/testdata/testHelmChartResource/resource.yaml b/e2e/testdata/testHelmChartResource/resource.yaml new file mode 100644 index 00000000..a9c2050b --- /dev/null +++ b/e2e/testdata/testHelmChartResource/resource.yaml @@ -0,0 +1,16 @@ +apiVersion: delivery.ocm.software/v1alpha1 +kind: Resource +metadata: + name: ocm-with-helm-deployment + namespace: ocm-system +spec: + interval: 10m + sourceRef: + kind: ComponentVersion + name: ocm-with-helm + namespace: ocm-system + resourceRef: + name: charts + version: 6.3.5 + extraIdentity: + helmChart: podinfo diff --git a/pkg/ocm/ocm_test.go b/pkg/ocm/ocm_test.go index a43582ff..a084aaa1 100644 --- a/pkg/ocm/ocm_test.go +++ b/pkg/ocm/ocm_test.go @@ -5,6 +5,7 @@ package ocm import ( + "bytes" "context" "io" "os" @@ -12,6 +13,7 @@ import ( "strings" "testing" + "github.com/containers/image/v5/pkg/compression" _ "github.com/distribution/distribution/v3/registry/storage/driver/inmemory" "github.com/fluxcd/pkg/apis/meta" ocmcontext "github.com/open-component-model/ocm-controller/pkg/fakes" @@ -120,6 +122,103 @@ func TestClient_GetResource(t *testing.T) { assert.Equal(t, resourceRef.Version, args.Version) } +func TestClient_GetHelmResource(t *testing.T) { + component := "github.com/skarlso/ocm-demo-index" + resource := "remote-controller-demo" + resourceVersion := "v0.0.1" + data, err := os.ReadFile(filepath.Join("testdata", "podinfo-6.3.5.tgz")) + require.NoError(t, err) + + octx := ocmcontext.NewFakeOCMContext() + + comp := &ocmcontext.Component{ + Name: component, + Version: "v0.0.1", + } + res := &ocmcontext.Resource{ + Name: resource, + Version: resourceVersion, + Data: data, + Component: comp, + Kind: "helmChart", + Type: "helm", + } + comp.Resources = append(comp.Resources, res) + + _ = octx.AddComponent(comp) + + cd := &v1alpha1.ComponentDescriptor{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "default", + Name: "github.com-skarlso-ocm-demo-index-v0.0.1-12345", + }, + Spec: v1alpha1.ComponentDescriptorSpec{ + Version: "v0.0.1", + }, + } + + fakeKubeClient := env.FakeKubeClient(WithObjects(cd)) + cache := &fakes.FakeCache{} + cache.IsCachedReturns(false, nil) + cache.FetchDataByDigestReturns(io.NopCloser(strings.NewReader("mockdata")), nil) + cache.PushDataReturns("sha256:8fa155245ea8d3f2ea3add7d090d42dfb0e22799018fded6aae24f0c1a1c3f38", nil) + + ocmClient := NewClient(fakeKubeClient, cache) + + cv := &v1alpha1.ComponentVersion{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-name", + Namespace: "default", + }, + Spec: v1alpha1.ComponentVersionSpec{ + Component: component, + Version: v1alpha1.Version{ + Semver: "v0.0.1", + }, + Repository: v1alpha1.Repository{ + URL: "localhost", + }, + }, + Status: v1alpha1.ComponentVersionStatus{ + ReconciledVersion: "v0.0.1", + ComponentDescriptor: v1alpha1.Reference{ + Name: component, + Version: "v0.0.1", + ComponentDescriptorRef: meta.NamespacedObjectReference{ + Name: "github.com-skarlso-ocm-demo-index-v0.0.1-12345", + Namespace: "default", + }, + }, + }, + } + + resourceRef := &v1alpha1.ResourceReference{ + ElementMeta: v3alpha1.ElementMeta{ + Name: "remote-controller-demo", + Version: "v0.0.1", + }, + } + + reader, digest, err := ocmClient.GetResource(context.Background(), octx, cv, resourceRef) + assert.NoError(t, err) + content, err := io.ReadAll(reader) + require.NoError(t, err) + assert.Equal(t, "mockdata", string(content)) + assert.Equal(t, "sha256:8fa155245ea8d3f2ea3add7d090d42dfb0e22799018fded6aae24f0c1a1c3f38", digest) + + // verify that the cache has been called with the right resource data to cache. + args := cache.PushDataCallingArgumentsOnCall(0) + + decompressedDataReader, _, err := compression.AutoDecompress(bytes.NewBuffer(data)) + require.NoError(t, err) + decompressedData, err := io.ReadAll(decompressedDataReader) + require.NoError(t, err) + assert.Equal(t, string(decompressedData), args.Content) + + assert.Equal(t, "sha-2705577397727487661", args.Name, "pushed name did not match constructed name from identity of the resource") + assert.Equal(t, resourceRef.Version, args.Version) +} + func TestClient_GetComponentVersion(t *testing.T) { component := "github.com/skarlso/ocm-demo-index" octx := ocmcontext.NewFakeOCMContext() diff --git a/pkg/ocm/testdata/podinfo-6.3.5.tgz b/pkg/ocm/testdata/podinfo-6.3.5.tgz new file mode 100644 index 00000000..df0424f5 Binary files /dev/null and b/pkg/ocm/testdata/podinfo-6.3.5.tgz differ