Skip to content

Commit

Permalink
tests: increase delete edge-application tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jul 8, 2024
1 parent 74838ce commit be4842e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/cmd/delete/edge_application/edge_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package edgeapplication
import (
"context"
"fmt"
"io/fs"
"os"
"strconv"

Expand All @@ -29,6 +30,8 @@ type DeleteCmd struct {
UpdateJson func(cmd *DeleteCmd) error
Cascade func(ctx context.Context, del *DeleteCmd) error
AskInput func(string) (string, error)
ReadFile func(name string) ([]byte, error)
WriteFile func(name string, data []byte, perm fs.FileMode) error
}

func NewCmd(f *cmdutil.Factory) *cobra.Command {
Expand All @@ -43,6 +46,8 @@ func NewDeleteCmd(f *cmdutil.Factory) *DeleteCmd {
UpdateJson: updateAzionJson,
Cascade: CascadeDelete,
AskInput: utils.AskInput,
ReadFile: os.ReadFile,
WriteFile: os.WriteFile,
}
}

Expand Down Expand Up @@ -118,8 +123,9 @@ func updateAzionJson(cmd *DeleteCmd) error {
return utils.ErrorInternalServerError
}
azionJson := path + "/azion/azion.json"
byteAzionJson, err := os.ReadFile(azionJson)
byteAzionJson, err := cmd.ReadFile(azionJson)
if err != nil {
logger.Debug("Error while parsing json", zap.Error(err))
return utils.ErrorUnmarshalAzionJsonFile
}
jsonReplaceFunc, err := sjson.Set(string(byteAzionJson), "function.id", 0)
Expand All @@ -137,7 +143,7 @@ func updateAzionJson(cmd *DeleteCmd) error {
return msg.ErrorFailedUpdateAzionJson
}

err = os.WriteFile(azionJson, []byte(jsonReplaceDomain), 0644)
err = cmd.WriteFile(azionJson, []byte(jsonReplaceDomain), 0644)
if err != nil {
return fmt.Errorf(utils.ErrorCreateFile.Error(), azionJson)
}
Expand Down
60 changes: 60 additions & 0 deletions pkg/cmd/delete/edge_application/edge_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package edgeapplication
import (
"encoding/json"
"fmt"
"io/fs"
"os"
"testing"

Expand Down Expand Up @@ -189,3 +190,62 @@ func TestCascadeDelete(t *testing.T) {
assert.Equal(t, msg.CascadeSuccess, stdout.String())
})
}

func TestUpdateAzionJson(t *testing.T) {
logger.New(zapcore.DebugLevel)

t.Run("update azion.json with new IDs", func(t *testing.T) {
// Mocking the content of azion.json
mockAzionJsonContent := `{
"function": {
"id": 1
},
"application": {
"id": 2
},
"domain": {
"id": 3
}
}`

// Create a temporary file for testing
tempFile, err := os.CreateTemp("", "azion.json")
require.NoError(t, err)
defer os.Remove(tempFile.Name())

// Write mock JSON content to the temporary file
_, err = tempFile.WriteString(mockAzionJsonContent)
require.NoError(t, err)
tempFile.Close()

mock := &httpmock.Registry{}

f, _, _ := testutils.NewFactory(mock)

// Prepare the DeleteCmd instance with mock dependencies
del := &DeleteCmd{
f: f,
Io: f.IOStreams,
GetAzion: func(confPath string) (*contracts.AzionApplicationOptions, error) {
conf := &contracts.AzionApplicationOptions{}
conf.Function.ID = 1
conf.Application.ID = 2
conf.Domain.Id = 3
return conf, nil
},
UpdateJson: func(cmd *DeleteCmd) error {
return nil
},
ReadFile: func(name string) ([]byte, error) {
return []byte(mockAzionJsonContent), nil
},
WriteFile: func(name string, data []byte, perm fs.FileMode) error {
return nil
},
}

// Run the test
err = updateAzionJson(del)
require.NoError(t, err)
})
}

0 comments on commit be4842e

Please sign in to comment.