Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
hilmarf committed Jan 14, 2025
1 parent 7922302 commit 1b7bb35
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func (c *ComponentVersionContainer) Check() error {
} else {
return errors.ErrInvalid("component version", c.GetDescriptor().Version)
}
return errors.ErrInvalid("component version", c.GetDescriptor().Version)
}
if c.comp.name != c.GetDescriptor().Name {
return errors.ErrInvalid("component name", c.GetDescriptor().Name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package genericocireg

import (
"testing"

"github.com/stretchr/testify/assert"

"ocm.software/ocm/api/ocm/internal"
"ocm.software/ocm/api/utils/accessobj"
)

func TestComponentVersionContainer_Check(t *testing.T) {
// Setup
state, err := accessobj.NewBlobStateForBlob(accessobj.ACC_READONLY, nil, NewStateHandler("mock.test.com/state_handler", "1.0.0"))
assert.NoError(t, err)
repo := &RepositoryImpl{ctx: internal.DefaultContext}
comp := &componentAccessImpl{repo: repo}
cvc := &ComponentVersionContainer{state: state, comp: comp}

// Test cases
tests := []struct {
name string
setup func()
expectErr bool
}{
{
name: "valid version and name",
setup: func() {
cvc.version = "1.0.0"
cvc.GetDescriptor().Version = "1.0.0"
cvc.comp.name = "test-component"
cvc.GetDescriptor().Name = "test-component"
},
expectErr: false,
},
{
name: "half valid version - containing META_SEPARATOR = " + META_SEPARATOR,
setup: func() {
cvc.version = "0.0.1-20250108132333.build-af79499"
cvc.GetDescriptor().Version = "0.0.1-20250108132333+af79499"
cvc.comp.name = "test-component"
cvc.GetDescriptor().Name = "test-component"
},
expectErr: false,
},
{
name: "valid version - containing '+'",
setup: func() {
cvc.version = "0.0.1-20250108132333+af79499"
cvc.GetDescriptor().Version = "0.0.1-20250108132333+af79499"
cvc.comp.name = "test-component"
cvc.GetDescriptor().Name = "test-component"
},
expectErr: false,
},
{
name: "invalid version",
setup: func() {
cvc.version = "1.0.0"
cvc.GetDescriptor().Version = "2.0.0"
},
expectErr: true,
},
{
name: "invalid name",
setup: func() {
cvc.comp.name = "test-component"
cvc.GetDescriptor().Name = "invalid-component"
},
expectErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.setup()
err := cvc.Check()
if test.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit 1b7bb35

Please sign in to comment.