Skip to content

Commit

Permalink
added helm chart end to end test
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Sep 12, 2023
1 parent 0270487 commit 0586c9f
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 16 deletions.
89 changes: 89 additions & 0 deletions e2e/helm_test.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
33 changes: 17 additions & 16 deletions e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -535,4 +536,4 @@ func reasonMatches(from fconditions.Getter, reason string) bool {
}
}
return false
}
}
14 changes: 14 additions & 0 deletions e2e/testdata/testHelmChartResource/component_version.yaml
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions e2e/testdata/testHelmChartResource/deployer.yaml
Original file line number Diff line number Diff line change
@@ -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

Binary file not shown.
16 changes: 16 additions & 0 deletions e2e/testdata/testHelmChartResource/resource.yaml
Original file line number Diff line number Diff line change
@@ -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
99 changes: 99 additions & 0 deletions pkg/ocm/ocm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
package ocm

import (
"bytes"
"context"
"io"
"os"
"path/filepath"
"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"
Expand Down Expand Up @@ -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()
Expand Down
Binary file added pkg/ocm/testdata/podinfo-6.3.5.tgz
Binary file not shown.

0 comments on commit 0586c9f

Please sign in to comment.