Skip to content

Commit

Permalink
Unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nesmabadr committed Nov 28, 2023
1 parent cffecbf commit cb20c58
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 96 deletions.
11 changes: 6 additions & 5 deletions cmd/kyma/alpha/create/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,12 @@ func (cmd *command) validateDefaultCR(ctx context.Context, modDef *module.Defini

func (cmd *command) getRemote(nameMapping module.NameMapping) (*module.Remote, error) {
res := &module.Remote{
Registry: cmd.opts.RegistryURL,
NameMapping: nameMapping,
Credentials: cmd.opts.Credentials,
Token: cmd.opts.Token,
Insecure: cmd.opts.Insecure,
Registry: cmd.opts.RegistryURL,
NameMapping: nameMapping,
Credentials: cmd.opts.Credentials,
Token: cmd.opts.Token,
Insecure: cmd.opts.Insecure,
OciRepoAccess: &module.OciRepo{},
}

if strings.HasPrefix(strings.ToLower(cmd.opts.RegistryURL), "https:") {
Expand Down
14 changes: 8 additions & 6 deletions cmd/kyma/alpha/verify/module/module.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package module

import (
"github.com/spf13/cobra"

"github.com/kyma-project/cli/internal/cli"
"github.com/kyma-project/cli/pkg/module"
"github.com/spf13/cobra"
)

type command struct {
Expand Down Expand Up @@ -80,11 +81,12 @@ func (c *command) Run(_ []string) error {
}

remote := &module.Remote{
Registry: c.opts.RegistryURL,
NameMapping: nameMappingMode,
Credentials: c.opts.Credentials,
Token: c.opts.Token,
Insecure: c.opts.Insecure,
Registry: c.opts.RegistryURL,
NameMapping: nameMappingMode,
Credentials: c.opts.Credentials,
Token: c.opts.Token,
Insecure: c.opts.Insecure,
OciRepoAccess: &module.OciRepo{},
}

if err := module.Verify(signCfg, remote); err != nil {
Expand Down
46 changes: 32 additions & 14 deletions pkg/module/mocks/OciRepoAccess.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 51 additions & 1 deletion pkg/module/oci_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import (

"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/localblob"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/comparch"
"github.com/open-component-model/ocm/pkg/contexts/ocm/transfer"
"github.com/open-component-model/ocm/pkg/contexts/ocm/transfer/transferhandler/standard"
"github.com/open-component-model/ocm/pkg/runtime"
)

//go:generate mockery --name OciRepoAccess --replace-type github.com/open-component-model/ocm/pkg/contexts/ocm/internal=internal:github.com/open-component-model/ocm/pkg/contexts/ocm/cpi
//go:generate mockery --name OciRepoAccess --replace-type github.com/open-component-model/ocm/pkg/contexts/ocm/internal=ocm:github.com/open-component-model/ocm/pkg/contexts/ocm
type OciRepoAccess interface {
ComponentVersionExists(archive *comparch.ComponentArchive, repo cpi.Repository) (bool, error)
GetComponentVersion(archive *comparch.ComponentArchive, repo cpi.Repository) (ocm.ComponentVersionAccess, error)
PushComponentVersion(archive *comparch.ComponentArchive, repository cpi.Repository, overwrite bool) error
DescriptorResourcesAreEquivalent(archive *comparch.ComponentArchive, remoteVersion ocm.ComponentVersionAccess) bool
}

type OciRepo struct{}
Expand Down Expand Up @@ -45,3 +49,49 @@ func (r *OciRepo) PushComponentVersion(archive *comparch.ComponentArchive, repo
}
return nil
}

func (r *OciRepo) DescriptorResourcesAreEquivalent(archive *comparch.ComponentArchive,
remoteVersion ocm.ComponentVersionAccess) bool {
localResources := archive.GetDescriptor().Resources
remoteResources := remoteVersion.GetDescriptor().Resources
if len(localResources) != len(remoteResources) {
return false
}

localResourcesMap := map[string]compdesc.Resource{}
for _, res := range localResources {
localResourcesMap[res.Name] = res
}

for _, res := range remoteResources {
localResource := localResourcesMap[res.Name]
if res.Name == RawManifestLayerName {
remoteAccess, ok := res.Access.(*runtime.UnstructuredVersionedTypedObject)
if !ok {
return false
}

_, ok = localResourcesMap[res.Name]
if !ok {
return false
}
localAccessObject, ok := localResource.Access.(*localblob.AccessSpec)
if !ok {
return false
}

remoteAccessLocalReference, ok := remoteAccess.Object[accessLocalReferenceFieldName].(string)
if !ok {
return false
}
// Trimming 7 characters because locally the sha256 is followed by '.' but remote it is followed by ':'
if remoteAccessLocalReference[7:] != localAccessObject.LocalReference[7:] {
return false
}
} else if !res.IsEquivalent(&localResource) {
return false
}
}

return true
}
47 changes: 1 addition & 46 deletions pkg/module/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/credentials/repositories/dockerconfig"
oci "github.com/open-component-model/ocm/pkg/contexts/oci/repositories/ocireg"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/localblob"
"github.com/open-component-model/ocm/pkg/contexts/ocm/compdesc"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/repositories/comparch"
Expand Down Expand Up @@ -143,8 +142,7 @@ func (r *Remote) Push(archive *comparch.ComponentArchive, overwrite bool) (ocm.C
return nil, false, fmt.Errorf("could not lookup component version: %w", err)
}

if descriptorResourcesAreEquivalent(archive.GetDescriptor().Resources,
versionAccess.GetDescriptor().Resources) {
if r.DescriptorResourcesAreEquivalent(archive, versionAccess) {
return versionAccess, false, nil
}
return nil, false, fmt.Errorf("version %s already exists with different content, please use "+
Expand All @@ -171,46 +169,3 @@ func (h *customTransferHandler) TransferVersion(repo ocm.Repository, src ocm.Com
error) {
return h.TransferHandler.TransferVersion(repo, src, meta, tgt)
}

func descriptorResourcesAreEquivalent(localResources, remoteResources compdesc.Resources) bool {
if len(localResources) != len(remoteResources) {
return false
}

localResourcesMap := map[string]compdesc.Resource{}
for _, res := range localResources {
localResourcesMap[res.Name] = res
}

for _, res := range remoteResources {
localResource := localResourcesMap[res.Name]
if res.Name == RawManifestLayerName {
remoteAccess, ok := res.Access.(*runtime.UnstructuredVersionedTypedObject)
if !ok {
return false
}

_, ok = localResourcesMap[res.Name]
if !ok {
return false
}
localAccessObject, ok := localResource.Access.(*localblob.AccessSpec)
if !ok {
return false
}

remoteAccessLocalReference, ok := remoteAccess.Object[accessLocalReferenceFieldName].(string)
if !ok {
return false
}
// Trimming 7 characters because locally the sha256 is followed by '.' but remote it is followed by ':'
if remoteAccessLocalReference[7:] != localAccessObject.LocalReference[7:] {
return false
}
} else if !res.IsEquivalent(&localResource) {
return false
}
}

return true
}
Loading

0 comments on commit cb20c58

Please sign in to comment.