From be4842e020a0e8880e75d1ae5e1307c94af24b87 Mon Sep 17 00:00:00 2001 From: PatrickMenoti <82882574+PatrickMenoti@users.noreply.github.com> Date: Mon, 8 Jul 2024 15:05:23 -0300 Subject: [PATCH] tests: increase delete edge-application tests --- .../edge_application/edge_application.go | 10 +++- .../edge_application/edge_application_test.go | 60 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/delete/edge_application/edge_application.go b/pkg/cmd/delete/edge_application/edge_application.go index c265fa53b..10eb1f668 100644 --- a/pkg/cmd/delete/edge_application/edge_application.go +++ b/pkg/cmd/delete/edge_application/edge_application.go @@ -3,6 +3,7 @@ package edgeapplication import ( "context" "fmt" + "io/fs" "os" "strconv" @@ -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 { @@ -43,6 +46,8 @@ func NewDeleteCmd(f *cmdutil.Factory) *DeleteCmd { UpdateJson: updateAzionJson, Cascade: CascadeDelete, AskInput: utils.AskInput, + ReadFile: os.ReadFile, + WriteFile: os.WriteFile, } } @@ -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) @@ -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) } diff --git a/pkg/cmd/delete/edge_application/edge_application_test.go b/pkg/cmd/delete/edge_application/edge_application_test.go index 551fb0d6b..b15562498 100644 --- a/pkg/cmd/delete/edge_application/edge_application_test.go +++ b/pkg/cmd/delete/edge_application/edge_application_test.go @@ -3,6 +3,7 @@ package edgeapplication import ( "encoding/json" "fmt" + "io/fs" "os" "testing" @@ -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) + }) +}