-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #870 from aziontech/unlink-tests
tests: add unit tests for unlink command
- Loading branch information
Showing
8 changed files
with
229 additions
and
8 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
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,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) | ||
} | ||
}) | ||
} | ||
} |
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,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()) | ||
} | ||
|
||
}) | ||
} | ||
} |
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