Skip to content

Commit

Permalink
Merge pull request #1 from c-pius/fix-failing-tests-after-ssa-update
Browse files Browse the repository at this point in the history
fix: Failing tests after ssa update
  • Loading branch information
LeelaChacha authored Jul 3, 2024
2 parents a5a9102 + 1627e46 commit b6cdeca
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: operator.kyma-project.io/v1beta2
kind: ModuleTemplate
metadata:
name: moduletemplate-template-operator
namespace: default
labels:
"operator.kyma-project.io/module-name": "template-operator"
spec:
channel: regular
data:
apiVersion: operator.kyma-project.io/v1alpha1
kind: Sample
metadata:
name: sample-yaml
spec:
initKey: valueUpdated
resourceFilePath: "./module-data/yaml"
descriptor:
component:
componentReferences: []
name: kyma-project.io/template-operator
provider: internal
repositoryContexts:
- baseUrl: registry.docker.io/kyma-project/sap-kyma-jellyfish-dev/template-operator
componentNameMapping: urlPath
type: ociRegistry
resources:
- access:
digest: sha256:db86408caca4c94250d8291aa79655b84146f9cc45e0da49f05a52b3722d74a0
type: localOciBlob
name: config
relation: local
type: yaml
version: v1.7.2
- access:
digest: sha256:1735cfa45bf07b63427c8e11717278f8847e352a66af7633611db902386d18ed
type: localOciBlob
name: raw-manifest
relation: local
type: yaml
version: v1.7.2
sources:
- access:
commit: 4e4b9d47cb655ca23e5c706462485ff7605e8d71
repoUrl: github.com/kyma-project/template-operator
type: gitHub
labels:
- name: git.kyma-project.io/ref
value: refs/heads/main
version: v1
name: module-sources
type: git
version: v1.7.2
version: v1.7.2
meta:
schemaVersion: v2
15 changes: 0 additions & 15 deletions internal/descriptor/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
var (
ErrTypeAssert = errors.New("failed to convert to v1beta2.Descriptor")
ErrDecode = errors.New("failed to decode to descriptor target")
ErrEncode = errors.New("failed to encode the descriptor")
ErrTemplateNil = errors.New("module template is nil")
ErrDescriptorNil = errors.New("module template contains nil descriptor")
)
Expand Down Expand Up @@ -71,20 +70,6 @@ func (c *CachedDescriptorProvider) GetDescriptor(template *v1beta2.ModuleTemplat
return descriptor, nil
}

func (c *CachedDescriptorProvider) SetDescriptor(template *v1beta2.ModuleTemplate, descriptor *v1beta2.Descriptor,
) error {
if template == nil {
return ErrTemplateNil
}
template.Spec.Descriptor.Object = descriptor
encoded, err := compdesc.Encode(descriptor.ComponentDescriptor, compdesc.DefaultJSONCodec)
template.Spec.Descriptor.Raw = encoded
if err != nil {
return errors.Join(ErrEncode, err)
}
return nil
}

func (c *CachedDescriptorProvider) Add(template *v1beta2.ModuleTemplate) error {
if template == nil {
return ErrTemplateNil
Expand Down
25 changes: 0 additions & 25 deletions internal/descriptor/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,3 @@ func TestGetDescriptor_OnEmptyCache_AddsDescriptorFromTemplate(t *testing.T) {
assert.NotNil(t, entry)
assert.Equal(t, expected.Name, entry.Name)
}

func TestSetDescriptor_OnEmptyTemplate_ReturnsErrTemplateNil(t *testing.T) {
descriptorProvider := provider.NewCachedDescriptorProvider(nil)

err := descriptorProvider.SetDescriptor(nil, nil)

require.Error(t, err)
require.ErrorIs(t, err, provider.ErrTemplateNil)
}

func TestSetDescriptor_OnProvidedDescriptor_ReturnsNoError(t *testing.T) {
descriptorProvider := provider.NewCachedDescriptorProvider(nil)
descriptorBefore := &v1beta2.Descriptor{
ComponentDescriptor: &compdesc.ComponentDescriptor{Metadata: compdesc.Metadata{ConfiguredVersion: "v1"}},
}
template := builder.NewModuleTemplateBuilder().WithDescriptor(descriptorBefore).Build()

descriptorAfter := &v1beta2.Descriptor{
ComponentDescriptor: &compdesc.ComponentDescriptor{Metadata: compdesc.Metadata{ConfiguredVersion: "v2"}},
}
err := descriptorProvider.SetDescriptor(template, descriptorAfter)

require.NoError(t, err)
assert.Equal(t, descriptorAfter, template.Spec.Descriptor.Object)
}
5 changes: 5 additions & 0 deletions pkg/testutils/builder/moduletemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ func ComponentDescriptorFactoryFromSchema(schemaVersion compdesc.SchemaVersion)
return moduleTemplate.Spec.Descriptor
}

func ReadComponentDescriptorFromFile(template string, moduleTemplate *v1beta2.ModuleTemplate) {
// needs to be encapsulated in an outside call to make the runtime.Caller(1) find the proper path
readComponentDescriptorFromYaml(template, moduleTemplate)
}

func readComponentDescriptorFromYaml(template string, moduleTemplate *v1beta2.ModuleTemplate) {
_, filename, _, ok := runtime.Caller(1)
if !ok {
Expand Down
29 changes: 2 additions & 27 deletions pkg/testutils/moduletemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ func GetModuleTemplate(ctx context.Context,
return templateInfo.ModuleTemplate, nil
}

func UpdateModuleTemplate(ctx context.Context,
clnt client.Client,
moduleTemplate *v1beta2.ModuleTemplate,
) error {
if err := clnt.Update(ctx, moduleTemplate); err != nil {
return fmt.Errorf("update module tempate: %w", err)
}
return nil
}

func ModuleTemplateExists(ctx context.Context,
clnt client.Client,
module v1beta2.Module,
Expand Down Expand Up @@ -75,8 +65,8 @@ func UpdateModuleTemplateSpec(ctx context.Context,
return ErrManifestResourceIsNil
}
moduleTemplate.Spec.Data.Object["spec"] = map[string]any{key: newValue}
if err := UpdateModuleTemplate(ctx, clnt, moduleTemplate); err != nil {
return err
if err := clnt.Update(ctx, moduleTemplate); err != nil {
return fmt.Errorf("update module tempate: %w", err)
}
return nil
}
Expand Down Expand Up @@ -112,18 +102,3 @@ func ReadModuleVersionFromModuleTemplate(ctx context.Context, clnt client.Client

return ocmDesc.Version, nil
}

func ModifyModuleTemplateVersion(moduleTemplate *v1beta2.ModuleTemplate, newVersion string) error {
descriptorProvider := provider.NewCachedDescriptorProvider(nil)
ocmDesc, err := descriptorProvider.GetDescriptor(moduleTemplate)
if err != nil {
return fmt.Errorf("failed to get descriptor: %w", err)
}
ocmDesc.Version = newVersion
err = descriptorProvider.SetDescriptor(moduleTemplate, ocmDesc)
if err != nil {
return fmt.Errorf("failed to set descriptor: %w", err)
}

return nil
}
26 changes: 1 addition & 25 deletions tests/integration/controller/kyma/kyma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,33 +514,9 @@ func updateKCPModuleTemplateSpecData(kymaName, valueUpdated string) func() error
return err
}
for _, activeModule := range createdKyma.Spec.Modules {
err := UpdateModuleTemplateSpec(ctx, kcpClient,
return UpdateModuleTemplateSpec(ctx, kcpClient,
activeModule, InitSpecKey, valueUpdated, createdKyma.Spec.Channel)
if err != nil {
return err
}
err = changeKCPModuleTemplateVersion(ctx, activeModule, createdKyma.Spec.Channel, "v1.7.2") // required to allow SSA for manifest
if err != nil {
return err
}
}
return nil
}
}

func changeKCPModuleTemplateVersion(ctx context.Context,
module v1beta2.Module, channel, version string,
) error {
moduleTemplate, err := GetModuleTemplate(ctx, kcpClient, module, channel)
if err != nil {
return err
}
err = ModifyModuleTemplateVersion(moduleTemplate, version)
if err != nil {
return err
}
if err := UpdateModuleTemplate(ctx, kcpClient, moduleTemplate); err != nil {
return err
}
return nil
}
49 changes: 17 additions & 32 deletions tests/integration/controller/kyma/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/genericocireg/componentmapping"
"github.com/open-component-model/ocm/pkg/runtime"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/kyma-project/lifecycle-manager/internal/pkg/flags"

. "github.com/kyma-project/lifecycle-manager/pkg/testutils"
"github.com/kyma-project/lifecycle-manager/pkg/testutils/builder"
)

const (
Expand Down Expand Up @@ -91,42 +93,25 @@ var _ = Describe("Update Manifest CR", Ordered, func() {
WithArguments(kyma.GetName(), kyma.GetNamespace(), kcpClient, shared.StateReady).
Should(Succeed())

By("Update Module Template spec.data.spec field")
valueUpdated := "valueUpdated"
Eventually(updateKCPModuleTemplateSpecData(kyma.Name, valueUpdated), Timeout, Interval).Should(Succeed())
By("Update Module Template spec")
var moduleTemplateFromFile v1beta2.ModuleTemplate
builder.ReadComponentDescriptorFromFile("operator_v1beta2_moduletemplate_kcp-module_updated.yaml", &moduleTemplateFromFile)

By("CR updated with new value in spec.resource.spec")
Eventually(expectManifestSpecDataEquals(kyma.Name, valueUpdated), Timeout, Interval).Should(Succeed())

By("Update Module Template spec.descriptor.component values")
{
newComponentDescriptorRepositoryURL := func(moduleTemplate *v1beta2.ModuleTemplate) error {
descriptor, err := descriptorProvider.GetDescriptor(moduleTemplate)
if err != nil {
return err
}

repositoryContext := descriptor.GetEffectiveRepositoryContext().Object
_, ok := repositoryContext["baseUrl"].(string)
if !ok {
Fail("Can't find \"baseUrl\" property in ModuleTemplate spec")
}
repositoryContext["baseUrl"] = updateRepositoryURL
descriptor.Version = "v1.7.3" // required to allow for SSA of manifest
moduleTemplateInCluster := &v1beta2.ModuleTemplate{}
err := kcpClient.Get(ctx, client.ObjectKey{
Name: createModuleTemplateName(module),
Namespace: kyma.GetNamespace()}, moduleTemplateInCluster)
Expect(err).ToNot(HaveOccurred())

newDescriptorRaw, err := compdesc.Encode(descriptor.ComponentDescriptor, compdesc.DefaultJSONCodec)
Expect(err).ToNot(HaveOccurred())
moduleTemplate.Spec.Descriptor.Raw = newDescriptorRaw
moduleTemplateInCluster.Spec = moduleTemplateFromFile.Spec

return nil
}
Eventually(kcpClient.Update, Timeout, Interval).
WithContext(ctx).
WithArguments(moduleTemplateInCluster).
Should(Succeed())

updateKCPModuleTemplateWith := updateKCPModuleTemplate(module, kyma.Spec.Channel)
update := func() error {
return updateKCPModuleTemplateWith(newComponentDescriptorRepositoryURL)
}
Eventually(update, Timeout, Interval).Should(Succeed())
}
By("CR updated with new value in spec.resource.spec")
Eventually(expectManifestSpecDataEquals(kyma.Name, "valueUpdated"), Timeout, Interval).Should(Succeed())

By("Manifest is updated with new value in spec.install.source")
{
Expand Down

0 comments on commit b6cdeca

Please sign in to comment.