Skip to content

Commit

Permalink
fix: module template integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
LeelaChacha committed Jun 30, 2024
1 parent 2163c47 commit 8e24d92
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
15 changes: 15 additions & 0 deletions internal/descriptor/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 @@ -70,6 +71,20 @@ 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
29 changes: 27 additions & 2 deletions pkg/testutils/moduletemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ 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 @@ -65,8 +75,8 @@ func UpdateModuleTemplateSpec(ctx context.Context,
return ErrManifestResourceIsNil
}
moduleTemplate.Spec.Data.Object["spec"] = map[string]any{key: newValue}
if err := clnt.Update(ctx, moduleTemplate); err != nil {
return fmt.Errorf("update module tempate: %w", err)
if err := UpdateModuleTemplate(ctx, clnt, moduleTemplate); err != nil {
return err
}
return nil
}
Expand Down Expand Up @@ -102,3 +112,18 @@ 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: 25 additions & 1 deletion tests/integration/controller/kyma/kyma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,33 @@ func updateKCPModuleTemplateSpecData(kymaName, valueUpdated string) func() error
return err
}
for _, activeModule := range createdKyma.Spec.Modules {
return UpdateModuleTemplateSpec(ctx, kcpClient,
err := 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
}
1 change: 1 addition & 0 deletions tests/integration/controller/kyma/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ var _ = Describe("Update Manifest CR", Ordered, func() {
Fail("Can't find \"baseUrl\" property in ModuleTemplate spec")
}
repositoryContext["baseUrl"] = updateRepositoryURL
descriptor.Version = "v1.7.3" // required to allow for SSA of manifest

newDescriptorRaw, err := compdesc.Encode(descriptor.ComponentDescriptor, compdesc.DefaultJSONCodec)
Expect(err).ToNot(HaveOccurred())
Expand Down

0 comments on commit 8e24d92

Please sign in to comment.