Skip to content

Commit

Permalink
tests: add unit tests for the dev command
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jul 22, 2024
1 parent db1b0a1 commit ad17b91
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 11 deletions.
10 changes: 2 additions & 8 deletions pkg/cmd/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
msg "github.com/aziontech/azion-cli/messages/dev"
"github.com/aziontech/azion-cli/pkg/cmd/build"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/iostreams"
"github.com/aziontech/azion-cli/pkg/logger"
"github.com/aziontech/azion-cli/pkg/output"
vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan"
"github.com/aziontech/azion-cli/utils"
"github.com/spf13/cobra"
)
Expand All @@ -25,6 +25,7 @@ type DevCmd struct {
CommandRunInteractive func(f *cmdutil.Factory, comm string) error
BuildCmd func(f *cmdutil.Factory) *build.BuildCmd
F *cmdutil.Factory
Vulcan func() *vulcanPkg.VulcanPkg
}

func NewDevCmd(f *cmdutil.Factory) *DevCmd {
Expand Down Expand Up @@ -74,13 +75,6 @@ func (cmd *DevCmd) Run(f *cmdutil.Factory) error {
return output.Print(&outGen)
}

contract := &contracts.BuildInfo{}

if isFirewall {
contract.IsFirewall = isFirewall
contract.OwnWorker = "true"
}

err := vulcan(cmd, isFirewall)
if err != nil {
return err
Expand Down
93 changes: 93 additions & 0 deletions pkg/cmd/dev/dev_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package dev

import (
"errors"
"testing"

"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"
vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
)

func TestDev(t *testing.T) {
logger.New(zapcore.DebugLevel)
tests := []struct {
name string
mockVulcan func() *vulcanPkg.VulcanPkg
mockCommandRun func(f *cmdutil.Factory, comm string) error
isFirewall bool
expectedError error
}{
{
name: "dev - successful execution without firewall",
mockVulcan: func() *vulcanPkg.VulcanPkg {
return &vulcanPkg.VulcanPkg{
Command: func(flags, params string, f *cmdutil.Factory) string {
return "echo 1"
},
}
},
mockCommandRun: func(f *cmdutil.Factory, comm string) error {
return nil
},
isFirewall: false,
expectedError: nil,
},
{
name: "dev - successful execution with firewall",
mockVulcan: func() *vulcanPkg.VulcanPkg {
return &vulcanPkg.VulcanPkg{
Command: func(flags, params string, f *cmdutil.Factory) string {
return "echo 1"
},
}
},
mockCommandRun: func(f *cmdutil.Factory, comm string) error {
return nil
},
isFirewall: true,
expectedError: nil,
},
{
name: "dev - failed command execution",
mockVulcan: func() *vulcanPkg.VulcanPkg {
return &vulcanPkg.VulcanPkg{
Command: func(flags, params string, f *cmdutil.Factory) string {
return "echo 1"
},
}
},
mockCommandRun: func(f *cmdutil.Factory, comm string) error {
return errors.New("failed to run command")
},
isFirewall: false,
expectedError: errors.New("Error executing Vulcan: Failed to run dev command. Verify if the command is correct and check the output above for more details. Run the 'azion dev' command again or contact Azion's support"),
},
}

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

devCmd := NewDevCmd(f)
devCmd.Vulcan = tt.mockVulcan
devCmd.CommandRunInteractive = tt.mockCommandRun

isFirewall = tt.isFirewall

err := devCmd.Run(f)
if tt.expectedError != nil {
require.Error(t, err)
assert.Equal(t, tt.expectedError.Error(), err.Error())
} else {
require.NoError(t, err)
}
})
}
}
3 changes: 1 addition & 2 deletions pkg/cmd/dev/vulcan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (

msg "github.com/aziontech/azion-cli/messages/dev"
"github.com/aziontech/azion-cli/pkg/logger"
vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan"
"go.uber.org/zap"
)

func vulcan(cmd *DevCmd, isFirewall bool) error {

vul := vulcanPkg.NewVulcan()
vul := cmd.Vulcan()
command := vul.Command("", "dev", cmd.F)
if isFirewall {
command = vul.Command("", "dev --firewall", cmd.F)
Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ func TestSync(t *testing.T) {
syncCmd.GetAzionJsonContent = tt.mockGetContentFunc
syncCmd.WriteAzionJsonContent = tt.mockWriteFunc

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

err := Sync(syncCmd)
Expand Down

0 comments on commit ad17b91

Please sign in to comment.