Skip to content

Commit

Permalink
Add componentdescriptor package
Browse files Browse the repository at this point in the history
  • Loading branch information
nesmabadr committed Sep 10, 2024
1 parent facfcb3 commit 334b96b
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
1 change: 0 additions & 1 deletion docs/gen-docs/modulectl_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ Build a simple module and push it to a remote registry
-o, --output string File to write the module template if the module is uploaded to a registry (default "template.yaml").
--registry string Context URL of the repository. The repository URL will be automatically added to the repository contexts in the module descriptor.
--registry-cred-selector string Label selector to identify an externally created Secret of type "kubernetes.io/dockerconfigjson". It allows the image to be accessed in private image registries. It can be used when you push your module to a registry with authenticated access. For example, "label1=value1,label2=value2".
--sec-scanners-config string Path to the file holding the security scan configuration (default "sec-scanners-config.yaml").
```
## See also
Expand Down
33 changes: 33 additions & 0 deletions internal/service/componentdescriptor/component_descriptor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package componentdescriptor

import (
"fmt"

commonerrors "github.com/kyma-project/modulectl/internal/common/errors"
)

type ModuleDefinition struct {
Name string
Version string
SchemaVersion string

// TODO: Define Layer struct based on the new ocm and remember to add the raw-manifest layer
// Layers []Layer

}

func (d *ModuleDefinition) ValidateModuleDefinition() error {
if d.Name == "" {
return fmt.Errorf("%w: module name must not be empty", commonerrors.ErrInvalidArg)
}

if d.Version == "" {
return fmt.Errorf("%w: module version must not be empty", commonerrors.ErrInvalidArg)
}

if d.SchemaVersion == "" {
return fmt.Errorf("%w: module schema version must not be empty", commonerrors.ErrInvalidArg)
}

return nil
}
91 changes: 91 additions & 0 deletions internal/service/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"github.com/stretchr/testify/require"

commonerrors "github.com/kyma-project/modulectl/internal/common/errors"
"github.com/kyma-project/modulectl/internal/service/contentprovider"
"github.com/kyma-project/modulectl/internal/service/create"
iotools "github.com/kyma-project/modulectl/tools/io"
"io"
)

func Test_NewService_ReturnsError_WhenModuleConfigServiceIsNil(t *testing.T) {
Expand All @@ -16,3 +19,91 @@ func Test_NewService_ReturnsError_WhenModuleConfigServiceIsNil(t *testing.T) {
require.ErrorIs(t, err, commonerrors.ErrInvalidArg)
assert.Contains(t, err.Error(), "moduleConfigService")
}

type moduleConfigServiceMock struct{}

func (*moduleConfigServiceMock) ParseModuleConfig(_ string) (*contentprovider.ModuleConfig, error) {
return nil, nil
}

func (*moduleConfigServiceMock) ValidateModuleConfig(_ *contentprovider.ModuleConfig) error {
return nil
}

func (*moduleConfigServiceMock) GetDefaultCRPath(_ string) (string, error) {
return "", nil
}

func (*moduleConfigServiceMock) GetManifestPath(_ string) (string, error) {
return "", nil
}

func Test_CreateModule_ReturnsError_WhenModuleConfigFileIsEmpty(t *testing.T) {
svc, err := create.NewService(&moduleConfigServiceMock{})
require.NoError(t, err)

opts := newCreateOptionsBuilder().withModuleConfigFile("").build()

err = svc.CreateModule(opts)

require.ErrorIs(t, err, commonerrors.ErrInvalidOption)
assert.Contains(t, err.Error(), "opts.ModuleConfigFile")
}

func Test_CreateModule_ReturnsError_WhenOutIsNil(t *testing.T) {
svc, err := create.NewService(&moduleConfigServiceMock{})
require.NoError(t, err)

opts := newCreateOptionsBuilder().withOut(nil).build()

err = svc.CreateModule(opts)

require.ErrorIs(t, err, commonerrors.ErrInvalidOption)
assert.Contains(t, err.Error(), "opts.Out")
}

type createOptionsBuilder struct {
options create.Options
}

func newCreateOptionsBuilder() *createOptionsBuilder {
builder := &createOptionsBuilder{
options: create.Options{},
}

return builder.
withOut(iotools.NewDefaultOut(io.Discard)).
withModuleConfigFile("create-module-config.yaml").
withRegistryURL("https://registry.kyma.cx").
withGitRemote("origin").
withTemplateOutput("test")
}

func (b *createOptionsBuilder) build() create.Options {
return b.options
}

func (b *createOptionsBuilder) withOut(out iotools.Out) *createOptionsBuilder {
b.options.Out = out
return b
}

func (b *createOptionsBuilder) withModuleConfigFile(moduleConfigFile string) *createOptionsBuilder {
b.options.ModuleConfigFile = moduleConfigFile
return b
}

func (b *createOptionsBuilder) withRegistryURL(registryURL string) *createOptionsBuilder {
b.options.RegistryURL = registryURL
return b
}

func (b *createOptionsBuilder) withGitRemote(gitRemote string) *createOptionsBuilder {
b.options.GitRemote = gitRemote
return b
}

func (b *createOptionsBuilder) withTemplateOutput(templateOutput string) *createOptionsBuilder {
b.options.TemplateOutput = templateOutput
return b
}

0 comments on commit 334b96b

Please sign in to comment.