generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
restructure and add more test cases
1 parent
2d07ab6
commit 9cc599b
Showing
6 changed files
with
166 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package module | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrInvalidOption = errors.New("invalid option") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |