Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restructure and add more test cases
Browse files Browse the repository at this point in the history
amritanshusikdar committed Aug 27, 2024
1 parent 2d07ab6 commit 9cc599b
Showing 6 changed files with 166 additions and 15 deletions.
82 changes: 82 additions & 0 deletions cmd/modulectl/create/module/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package module_test

import (
"errors"
"os"
"testing"

modulecmd "github.com/kyma-project/modulectl/cmd/modulectl/create/module"
modulesvc "github.com/kyma-project/modulectl/internal/service/module"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_NewCmd_ReturnsError_WhenModuleServiceIsNil(t *testing.T) {
_, err := modulecmd.NewCmd(nil)

require.Error(t, err)
assert.Contains(t, err.Error(), "moduleService")
}

func Test_NewCmd_Succeeds(t *testing.T) {
_, err := modulecmd.NewCmd(&moduleServiceStub{})

require.NoError(t, err)
}

func Test_Execute_CallsModuleService(t *testing.T) {
svc := &moduleServiceStub{}
cmd, _ := modulecmd.NewCmd(svc)

err := cmd.Execute()

require.NoError(t, err)
require.True(t, svc.called)
}

func Test_Execute_ReturnsError_WhenModuleServiceReturnsError(t *testing.T) {
cmd, _ := modulecmd.NewCmd(&moduleServiceErrorStub{})

err := cmd.Execute()

require.ErrorIs(t, err, errSomeTestError)
}

func Test_Execute_ParsesAllModuleOptions(t *testing.T) {
moduleConfigFile := "some random file path"

os.Args = []string{
"module",
"--module-config-file", moduleConfigFile,
}

svc := &moduleServiceStub{}
cmd, _ := modulecmd.NewCmd(svc)

cmd.Execute()

assert.Equal(t, moduleConfigFile, svc.opts.ModuleConfigFile)
}

// ***************
// Test Stubs
// ***************

type moduleServiceStub struct {
called bool
opts modulesvc.Options
}

func (m *moduleServiceStub) CreateModule(opts modulesvc.Options) error {
m.called = true
m.opts = opts
return nil
}

type moduleServiceErrorStub struct{}

var errSomeTestError = errors.New("some test error")

func (s *moduleServiceErrorStub) CreateModule(_ modulesvc.Options) error {
return errSomeTestError
}
18 changes: 9 additions & 9 deletions cmd/modulectl/create/module/flags.go
Original file line number Diff line number Diff line change
@@ -25,14 +25,14 @@ const (
InsecureFlagDefault = false
insecureFlagUsage = "Uses an insecure connection to access the registry"

OutputFlagName = "output"
outputFlagShort = "o"
OutputFlagDefault = "template.yaml"
outputFlagUsage = `File to write the module template if the module is uploaded to a registry. (default "template.yaml")`
TemplateOutputFlagName = "output"
templateOutputFlagShort = "o"
TemplateOutputFlagDefault = "template.yaml"
templateOutputFlagUsage = `File to write the module template if the module is uploaded to a registry. (default "template.yaml")`

RegistryFlagName = "registry"
RegistryFlagDefault = ""
registryFlagUsage = "Context URL of the repository. The repository URL will be automatically added to the repository contexts in the module descriptor."
RegistryURLFlagName = "registry"
RegistryURLFlagDefault = ""
registryURLFlagUsage = "Context URL of the repository. The repository URL will be automatically added to the repository contexts in the module descriptor."

RegistryCredSelectorFlagName = "registry-cred-selector"
RegistryCredSelectorFlagDefault = ""
@@ -50,8 +50,8 @@ func parseFlags(flags *pflag.FlagSet, opts *module.Options) {
flags.StringVarP(&opts.Credentials, CredentialsFlagName, credentialsFlagShort, CredentialsFlagDefault, credentialsFlagUsage)
flags.StringVar(&opts.GitRemote, GitRemoteFlagName, GitRemoteFlagDefault, gitRemoteFlagUsage)
flags.BoolVar(&opts.Insecure, InsecureFlagName, InsecureFlagDefault, insecureFlagUsage)
flags.StringVarP(&opts.TemplateOutput, OutputFlagName, outputFlagShort, OutputFlagDefault, outputFlagUsage)
flags.StringVar(&opts.RegistryURL, RegistryFlagName, RegistryFlagDefault, registryFlagUsage)
flags.StringVarP(&opts.TemplateOutput, TemplateOutputFlagName, templateOutputFlagShort, TemplateOutputFlagDefault, templateOutputFlagUsage)
flags.StringVar(&opts.RegistryURL, RegistryURLFlagName, RegistryURLFlagDefault, registryURLFlagUsage)
flags.StringVar(&opts.RegistryCredSelector, RegistryCredSelectorFlagName, RegistryCredSelectorFlagDefault, registryCredSelectorFlagUsage)
flags.StringVar(&opts.SecurityScanConfig, SecScannersConfigFlagName, SecScannersConfigFlagDefault, secScannersConfigFlagUsage)

4 changes: 2 additions & 2 deletions cmd/modulectl/create/module/flags_test.go
Original file line number Diff line number Diff line change
@@ -18,8 +18,8 @@ func Test_ScaffoldFlagsDefaults(t *testing.T) {
{name: module.CredentialsFlagName, value: module.CredentialsFlagDefault, expected: ""},
{name: module.GitRemoteFlagName, value: module.GitRemoteFlagDefault, expected: "origin"},
{name: module.InsecureFlagName, value: strconv.FormatBool(module.InsecureFlagDefault), expected: "false"},
{name: module.OutputFlagName, value: module.OutputFlagDefault, expected: "template.yaml"},
{name: module.RegistryFlagName, value: module.RegistryFlagDefault, expected: ""},
{name: module.TemplateOutputFlagName, value: module.TemplateOutputFlagDefault, expected: "template.yaml"},
{name: module.RegistryURLFlagName, value: module.RegistryURLFlagDefault, expected: ""},
{name: module.RegistryCredSelectorFlagName, value: module.RegistryCredSelectorFlagDefault, expected: ""},
{name: module.SecScannersConfigFlagName, value: module.SecScannersConfigFlagDefault, expected: "sec-scanners-config.yaml"},
}
7 changes: 7 additions & 0 deletions internal/service/module/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package module

import "errors"

var (
ErrInvalidOption = errors.New("invalid option")
)
21 changes: 21 additions & 0 deletions internal/service/module/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package module

type ModuleConfigService interface {
}

type Service struct {
moduleConfigService ModuleConfigService
}

func NewService(moduleConfigService ModuleConfigService) (*Service, error) {
return &Service{
moduleConfigService: moduleConfigService,
}, nil
}

func (s *Service) CreateModule(opts Options) error {
if err := opts.validate(); err != nil {
return err
}
return nil
}
49 changes: 45 additions & 4 deletions internal/service/module/options.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,57 @@
package module

import "github.com/kyma-project/modulectl/tools/io"
import (
"fmt"
"regexp"
"strings"

"github.com/kyma-project/modulectl/tools/io"
)

// Options defines available options for the create module command
type Options struct {
Out io.Out // TODO: Can be extracted one level above
ModuleConfigFile string
GitRemote string
RegistryURL string
Credentials string
TemplateOutput string
GitRemote string
Insecure bool
TemplateOutput string
RegistryURL string
RegistryCredSelector string
SecurityScanConfig string
}

func (opts Options) validate() error {
if opts.Out == nil {
return fmt.Errorf("%w: opts.Out must not be nil", ErrInvalidOption)
}

if opts.ModuleConfigFile == "" {
return fmt.Errorf("%w: opts.ModuleConfigFile must not be empty", ErrInvalidOption)
}

matched, err := regexp.MatchString("/(.+):(.+)/g", opts.Credentials)
if err != nil {
return fmt.Errorf("%w: opts.Credentials could not be parsed: %w", ErrInvalidOption, err)
} else if !matched {
return fmt.Errorf("%w: opts.Credentials is in invalid format", ErrInvalidOption)
}

if opts.GitRemote == "" {
return fmt.Errorf("%w: opts.GitRemote must not be empty", ErrInvalidOption)
}

if opts.TemplateOutput == "" {
return fmt.Errorf("%w: opts.TemplateOutput must not be empty", ErrInvalidOption)
}

if !strings.HasPrefix(opts.RegistryURL, "http") {
return fmt.Errorf("%w: opts.RegistryURL does not start with http(s)", ErrInvalidOption)
}

if opts.SecurityScanConfig == "" {
return fmt.Errorf("%w: opts.SecurityScanConfig must not be empty", ErrInvalidOption)
}

return nil
}

0 comments on commit 9cc599b

Please sign in to comment.