Skip to content

Commit

Permalink
tests: add unit tests for unlink command
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jul 2, 2024
1 parent ac45151 commit ec17b60
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pkg/cmd/delete/edge_application/cascade.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"github.com/aziontech/azion-cli/pkg/output"
)

func (del *DeleteCmd) Cascade(ctx context.Context) error {
func CascadeDelete(ctx context.Context, del *DeleteCmd) error {
azionJson, err := del.GetAzion(ProjectConf)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return msg.ErrorMissingAzionJson
} else {
return err //message with error
return err
}
}
if azionJson.Application.ID == 0 {
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/delete/edge_application/edge_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type DeleteCmd struct {
GetAzion func(confPath string) (*contracts.AzionApplicationOptions, error)
f *cmdutil.Factory
UpdateJson func(cmd *DeleteCmd) error
Cascade func(ctx context.Context, del *DeleteCmd) error
}

func NewCmd(f *cmdutil.Factory) *cobra.Command {
Expand All @@ -40,6 +41,7 @@ func NewDeleteCmd(f *cmdutil.Factory) *DeleteCmd {
GetAzion: utils.GetAzionJsonContent,
f: f,
UpdateJson: updateAzionJson,
Cascade: CascadeDelete,
}
}

Expand Down Expand Up @@ -72,7 +74,7 @@ func (del *DeleteCmd) run(cmd *cobra.Command, application_id int64) error {
ctx := context.Background()

if cmd.Flags().Changed("cascade") {
err := del.Cascade(ctx)
err := del.Cascade(ctx, del)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type SyncCmd struct {
GetAzionJsonContent func(confPath string) (*contracts.AzionApplicationOptions, error)
WriteAzionJsonContent func(conf *contracts.AzionApplicationOptions, confPath string) error
F *cmdutil.Factory
SyncResources func(f *cmdutil.Factory, info contracts.SyncOpts, synch *SyncCmd) error
}

func NewSync(f *cmdutil.Factory) *SyncCmd {
Expand All @@ -27,6 +28,7 @@ func NewSync(f *cmdutil.Factory) *SyncCmd {
Io: f.IOStreams,
GetAzionJsonContent: utils.GetAzionJsonContent,
WriteAzionJsonContent: utils.WriteAzionJsonContent,
SyncResources: SyncLocalResources,
}
}

Expand Down Expand Up @@ -92,7 +94,7 @@ func Sync(cmdFac *SyncCmd) error {
Conf: conf,
}

err = cmdFac.SyncResources(cmdFac.F, info)
err = cmdFac.SyncResources(cmdFac.F, info, cmdFac)
if err != nil {
return err
}
Expand Down
97 changes: 97 additions & 0 deletions pkg/cmd/sync/sync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package sync

import (
"errors"
"fmt"
"testing"

msg "github.com/aziontech/azion-cli/messages/sync"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/httpmock"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
)

func TestSync(t *testing.T) {
logger.New(zapcore.DebugLevel)
tests := []struct {
name string
mockGetContentFunc func(confPath string) (*contracts.AzionApplicationOptions, error)
mockWriteFunc func(conf *contracts.AzionApplicationOptions, confPath string) error
mockSyncResources func(f *cmdutil.Factory, info contracts.SyncOpts, synch *SyncCmd) error
expectedError error
}{
{
name: "sync - successful synchronization",
mockGetContentFunc: func(confPath string) (*contracts.AzionApplicationOptions, error) {
return &contracts.AzionApplicationOptions{
// Mock relevant fields
}, nil
},
mockWriteFunc: func(conf *contracts.AzionApplicationOptions, confPath string) error {
return nil
},
mockSyncResources: func(f *cmdutil.Factory, info contracts.SyncOpts, synch *SyncCmd) error {
// Mock synchronization logic
return nil
},
expectedError: nil,
},
{
name: "sync - failed to get content",
mockGetContentFunc: func(confPath string) (*contracts.AzionApplicationOptions, error) {
return nil, errors.New("Failed to synchronize local resources with remote resources: failed to get azion.json content")
},
mockWriteFunc: func(conf *contracts.AzionApplicationOptions, confPath string) error {
return nil
},
mockSyncResources: func(f *cmdutil.Factory, info contracts.SyncOpts, synch *SyncCmd) error {
return nil
},
expectedError: fmt.Errorf(msg.ERRORSYNC, "failed to get azion.json content"),
},
{
name: "sync - failed to write content",
mockGetContentFunc: func(confPath string) (*contracts.AzionApplicationOptions, error) {
return &contracts.AzionApplicationOptions{
// Mock relevant fields
}, nil
},
mockWriteFunc: func(conf *contracts.AzionApplicationOptions, confPath string) error {
return errors.New("failed to write azion.json content")
},
mockSyncResources: func(f *cmdutil.Factory, info contracts.SyncOpts, synch *SyncCmd) error {
return errors.New("failed to write azion.json content")
},
expectedError: errors.New("failed to write azion.json content"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := &httpmock.Registry{}
f, _, _ := testutils.NewFactory(mock)

syncCmd := NewSync(f)

// Mock GetAzionJsonContent and WriteAzionJsonContent functions
syncCmd.GetAzionJsonContent = tt.mockGetContentFunc
syncCmd.WriteAzionJsonContent = tt.mockWriteFunc

// Replace syncResources function with mock
syncCmd.SyncResources = tt.mockSyncResources

err := Sync(syncCmd)
if tt.expectedError != nil {
require.Error(t, err)
assert.Equal(t, tt.expectedError.Error(), err.Error())
} else {
require.NoError(t, err)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/cmd/sync/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
ctx context.Context = context.Background()
)

func (synch *SyncCmd) SyncResources(f *cmdutil.Factory, info contracts.SyncOpts) error {
func SyncLocalResources(f *cmdutil.Factory, info contracts.SyncOpts, synch *SyncCmd) error {
opts = &contracts.ListOptions{
PageSize: 1000,
Page: 1,
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/unlink/unlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/MakeNowJust/heredoc"
msg "github.com/aziontech/azion-cli/messages/unlink"
app "github.com/aziontech/azion-cli/pkg/cmd/delete/edge_application"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/utils"
"github.com/spf13/cobra"
Expand All @@ -16,6 +17,7 @@ type UnlinkCmd struct {
IsDirEmpty func(dirpath string) (bool, error)
CleanDir func(dirpath string) error
F *cmdutil.Factory
DeleteCmd func(f *cmdutil.Factory) *app.DeleteCmd
}

func NewUnlinkCmd(f *cmdutil.Factory) *UnlinkCmd {
Expand All @@ -25,6 +27,7 @@ func NewUnlinkCmd(f *cmdutil.Factory) *UnlinkCmd {
Clean: clean,
IsDirEmpty: utils.IsDirEmpty,
CleanDir: utils.CleanDirectory,
DeleteCmd: app.NewDeleteCmd,
}
}

Expand Down
118 changes: 118 additions & 0 deletions pkg/cmd/unlink/unlink_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package unlink

import (
"context"
"errors"
"testing"

app "github.com/aziontech/azion-cli/pkg/cmd/delete/edge_application"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/httpmock"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
)

func mockDelSuccess() *app.DeleteCmd {
return &app.DeleteCmd{
Cascade: func(ctx context.Context, del *app.DeleteCmd) error {
return nil
},
}
}

func mockDelFail() *app.DeleteCmd {
return &app.DeleteCmd{
Cascade: func(ctx context.Context, del *app.DeleteCmd) error {
return errors.New("Failed to cascade delete")
},
}
}

func TestUnlink(t *testing.T) {
logger.New(zapcore.DebugLevel)
tests := []struct {
name string
isDirEmpty bool
cleanDirError error
expectedOutput string
expectedError string
mockDeleteCmd *app.DeleteCmd
}{
{
name: "unlink - directory is empty",
isDirEmpty: true,
cleanDirError: nil,
expectedOutput: "Unliked successfully",
expectedError: "",
mockDeleteCmd: mockDelSuccess(),
},
{
name: "unlink - clean directory successfully",
isDirEmpty: false,
cleanDirError: nil,
expectedOutput: "Unliked successfully",
expectedError: "",
mockDeleteCmd: mockDelSuccess(),
},
{
name: "unlink - failed to clean directory",
isDirEmpty: false,
cleanDirError: errors.New("failed to clean directory"),
expectedOutput: "",
expectedError: "failed to clean directory",
mockDeleteCmd: mockDelSuccess(),
},
{
name: "unlink - cascade delete fails",
isDirEmpty: false,
cleanDirError: nil,
expectedOutput: "",
expectedError: "Failed to cascade delete",
mockDeleteCmd: mockDelFail(),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockIsDirEmpty := func(dirpath string) (bool, error) {
return tt.isDirEmpty, nil
}

mockCleanDir := func(dirpath string) error {
return tt.cleanDirError
}

mockDeleteCmd := tt.mockDeleteCmd

mock := &httpmock.Registry{}

f, out, _ := testutils.NewFactory(mock)
f.GlobalFlagAll = true // Simulate --yes flag

unlinkCmd := &UnlinkCmd{
F: f,
IsDirEmpty: mockIsDirEmpty,
CleanDir: mockCleanDir,
ShouldClean: shouldClean,
Clean: clean,
DeleteCmd: func(f *cmdutil.Factory) *app.DeleteCmd {
return mockDeleteCmd
},
}
cmd := NewCobraCmd(unlinkCmd, f)

_, err := cmd.ExecuteC()
if tt.expectedError != "" {
require.Error(t, err)
assert.Equal(t, tt.expectedError, err.Error())
} else {
require.NoError(t, err)
assert.Equal(t, tt.expectedOutput, out.String())
}

})
}
}
5 changes: 2 additions & 3 deletions pkg/cmd/unlink/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package unlink
import (
"context"

app "github.com/aziontech/azion-cli/pkg/cmd/delete/edge_application"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/logger"
helpers "github.com/aziontech/azion-cli/utils"
Expand All @@ -27,9 +26,9 @@ func clean(f *cmdutil.Factory, cmd *UnlinkCmd) error {
}

if shouldCascade {
cmd := app.NewDeleteCmd(f)
delCmd := cmd.DeleteCmd(f)
ctx := context.Background()
err := cmd.Cascade(ctx)
err := delCmd.Cascade(ctx, delCmd)
if err != nil {
return err
}
Expand Down

0 comments on commit ec17b60

Please sign in to comment.