Skip to content

Commit

Permalink
fix: don't update transaction file other than adding id to it (#2884)
Browse files Browse the repository at this point in the history
* fix: don't update transaction file other than adding id to it

* add e2e tests

* ensure test file has no id when running test
  • Loading branch information
mathnogueira committed Jul 7, 2023
1 parent 30e9d43 commit 5bb04da
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
7 changes: 4 additions & 3 deletions cli/pkg/resourcemanager/apply.go
Expand Up @@ -15,6 +15,7 @@ const VerbApply Verb = "apply"
type applyPreProcessorFn func(context.Context, fileutil.File) (fileutil.File, error)

func (c Client) Apply(ctx context.Context, inputFile fileutil.File, requestedFormat Format) (string, error) {
originalInputFile := inputFile

if c.options.applyPreProcessor != nil {
var err error
Expand Down Expand Up @@ -72,7 +73,7 @@ func (c Client) Apply(ctx context.Context, inputFile fileutil.File, requestedFor

// if the original file doesn't have an ID, we need to get the server generated ID from the response
// and write it to the original file
if !inputFile.HasID() {
if !originalInputFile.HasID() {

jsonBody, err := requestedFormat.ToJSON(body)
if err != nil {
Expand All @@ -89,12 +90,12 @@ func (c Client) Apply(ctx context.Context, inputFile fileutil.File, requestedFor
return "", fmt.Errorf("cannot get ID from Apply response")
}

inputFile, err = inputFile.SetID(id)
originalInputFile, err = originalInputFile.SetID(id)
if err != nil {
return "", fmt.Errorf("cannot set ID on input file: %w", err)
}

_, err = inputFile.Write()
_, err = originalInputFile.Write()
if err != nil {
return "", fmt.Errorf("cannot write updated input file: %w", err)
}
Expand Down
14 changes: 14 additions & 0 deletions testing/cli-e2etest/helpers/common.go
Expand Up @@ -97,3 +97,17 @@ func Copy(source, dst string) {
panic(err)
}
}

func RemoveIDFromTransactionFile(t *testing.T, filePath string) {
fileContent, err := os.ReadFile(filePath)
require.NoError(t, err)

transaction := UnmarshalYAML[types.TransactionResource](t, string(fileContent))
transaction.Spec.ID = ""

newFileContent, err := yaml.Marshal(transaction)
require.NoError(t, err)

err = os.WriteFile(filePath, newFileContent, os.ModeAppend)
require.NoError(t, err)
}
Expand Up @@ -2,6 +2,7 @@ package transaction

import (
"fmt"
"os"
"testing"

"github.com/kubeshop/tracetest/cli-e2etest/environment"
Expand Down Expand Up @@ -85,4 +86,27 @@ func TestApplyTransaction(t *testing.T) {
require.Equal("9wtAH2_Vg", updatedTransaction.Spec.Steps[0])
require.Equal("ajksdkasjbd", updatedTransaction.Spec.Steps[1])
require.Equal("ajksdkasjbd", updatedTransaction.Spec.Steps[2])

// When I try to set up a new transaction without any id
// Then it should be applied with success and it should not update
// the steps with its ids.
transactionWithoutIDPath := env.GetTestResourcePath(t, "new-transaction-without-id")

helpers.RemoveIDFromTransactionFile(t, transactionWithoutIDPath)

transactionWithoutIDResult := tracetestcli.Exec(t, fmt.Sprintf("apply transaction --file %s", transactionWithoutIDPath), tracetestcli.WithCLIConfig(cliConfig))
helpers.RequireExitCodeEqual(t, transactionWithoutIDResult, 0)

content, err := os.ReadFile(transactionWithoutIDPath)
require.NoError(err)

transactionWithoutID := helpers.UnmarshalYAML[types.TransactionResource](t, string(content))

require.Equal("Transaction", transactionWithoutID.Type)
require.NotEmpty(transactionWithoutID.Spec.ID)
require.Equal("New Transaction", transactionWithoutID.Spec.Name)
require.Equal("a transaction", transactionWithoutID.Spec.Description)
require.Len(transactionWithoutID.Spec.Steps, 2)
require.Equal("./transaction-step-1.yaml", transactionWithoutID.Spec.Steps[0])
require.Equal("./transaction-step-2.yaml", transactionWithoutID.Spec.Steps[1])
}
@@ -0,0 +1,7 @@
type: Transaction
spec:
name: New Transaction
description: a transaction
steps:
- ./transaction-step-1.yaml
- ./transaction-step-2.yaml
2 changes: 1 addition & 1 deletion testing/cli-e2etest/testscenarios/types/transaction.go
Expand Up @@ -4,7 +4,7 @@ package types
// however they are defined here to avoid bias with the current implementation

type Transaction struct {
ID string `json:"id"`
ID string `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Steps []string `json:"steps"`
Expand Down

0 comments on commit 5bb04da

Please sign in to comment.