Skip to content

Commit

Permalink
fix(cli): fix upsert for environments (#2570)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoren committed May 24, 2023
1 parent 10fd98c commit e1b4bef
Show file tree
Hide file tree
Showing 37 changed files with 885 additions and 870 deletions.
33 changes: 21 additions & 12 deletions api/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ components:
serviceUnderTest:
$ref: "./triggers.yaml#/components/schemas/Trigger"
specs:
$ref: "#/components/schemas/TestSpecs"
type: array
items:
$ref: "#/components/schemas/TestSpec"
description: specification of assertions that are going to be made
outputs:
description: define test outputs, in a key/value format. The value is processed as an expression
Expand All @@ -45,6 +47,8 @@ components:
name:
type: string
selector:
type: string
selectorParsed:
$ref: "#/components/schemas/Selector"
value:
type: string
Expand Down Expand Up @@ -76,17 +80,8 @@ components:
specs:
type: array
items:
type: object
properties:
name:
type: string
nullable: true
selector:
$ref: "#/components/schemas/Selector"
assertions:
type: array
items:
type: string
$ref: "#/components/schemas/TestSpec"

example:
specs:
- selector:
Expand All @@ -104,6 +99,20 @@ components:
comparator: "="
expected: "200"

TestSpec:
type: object
properties:
name:
type: string
selector:
type: string
selectorParsed:
$ref: "#/components/schemas/Selector"
assertions:
type: array
items:
type: string

TestRun:
type: object
properties:
Expand Down
20 changes: 15 additions & 5 deletions cli/actions/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"context"
"errors"
"fmt"
"io/ioutil"

Expand Down Expand Up @@ -41,7 +42,7 @@ func (action environmentsActions) GetID(file *file.File) (string, error) {
return "", err
}

return *resource.(openapi.EnvironmentResource).Spec.Id, nil
return resource.(openapi.EnvironmentResource).Spec.GetId(), nil
}

func (environment environmentsActions) Apply(ctx context.Context, fileContent file.File) (result *file.File, err error) {
Expand All @@ -51,12 +52,21 @@ func (environment environmentsActions) Apply(ctx context.Context, fileContent fi

mapstructure.Decode(fileContent.Definition().Spec, &envResource.Spec)

if envResource.Spec.Id == nil || *envResource.Spec.Id == "" {
result, err := environment.resourceClient.Create(ctx, fileContent)
return result, err
if envResource.Spec.GetId() != "" {
_, err := environment.Get(ctx, envResource.Spec.GetId())
if err != nil {
if !errors.Is(err, utils.ResourceNotFound) {
return nil, err
}

// doesn't exist, so create it
return environment.resourceClient.Create(ctx, fileContent)
}

return environment.resourceClient.Update(ctx, fileContent, envResource.Spec.GetId())
}

result, err = environment.resourceClient.Update(ctx, fileContent, *envResource.Spec.Id)
result, err = environment.resourceClient.Create(ctx, fileContent)
return result, err
}

Expand Down
16 changes: 12 additions & 4 deletions cli/actions/run_test_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,22 @@ func (a runTestAction) Run(ctx context.Context, args RunResourceArgs) error {
envID := args.EnvID

if utils.StringReferencesFile(args.EnvID) {
a.logger.Debug("resolve envID from file reference", zap.String("path", envID))
envResource, err := a.environmentActions.FromFile(ctx, args.EnvID)
if err != nil {
return fmt.Errorf("could not run definition: %w", err)
return fmt.Errorf("could not read environment file: %w", err)
}

_, err = a.environmentActions.ApplyResource(ctx, envResource)
if err != nil {
return fmt.Errorf("could not run definition: %w", err)
return fmt.Errorf("could not apply environmen file: %w", err)
}

envID = *envResource.Spec.Id
envID = envResource.Spec.GetId()
}

a.logger.Debug("resolved env", zap.String("envID", envID))

params := runDefParams{
DefinitionFile: args.DefinitionFile,
EnvID: envID,
Expand Down Expand Up @@ -286,14 +289,19 @@ func (a runTestAction) askForMissingVariables(resp *http.Response) (map[string]s
}

func (a runTestAction) getTransaction(ctx context.Context, id string) (openapi.Transaction, error) {
a.client.GetConfig().AddDefaultHeader("X-Tracetest-Augmented", "true")
transaction, _, err := a.client.ResourceApiApi.
GetTransaction(ctx, id).
Execute()

// reset augmented header
delete(a.client.GetConfig().DefaultHeader, "X-Tracetest-Augmented")

if err != nil {
return openapi.Transaction{}, fmt.Errorf("could not execute request: %w", err)
}

return *transaction.Spec, nil
return transaction.GetSpec(), nil
}

func (a runTestAction) getTest(ctx context.Context, id string) (openapi.Test, error) {
Expand Down
4 changes: 2 additions & 2 deletions cli/formatters/test_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (f testRun) formatSuccessfulTest(test openapi.Test, run openapi.TestRun) st
buffer.WriteString(message)

for i, specResult := range run.Result.Results {
title := f.getTestSpecTitle(test.Specs.Specs[i].GetName(), specResult)
title := f.getTestSpecTitle(test.Specs[i].GetName(), specResult)
message := f.formatMessage("\t%s %s\n", PASSED_TEST_ICON, title)
message = f.getColoredText(true, message)
buffer.WriteString(message)
Expand Down Expand Up @@ -182,7 +182,7 @@ func (f testRun) formatFailedTest(test openapi.Test, run openapi.TestRun) string
}
}

title := f.getTestSpecTitle(test.Specs.Specs[i].GetName(), specResult)
title := f.getTestSpecTitle(test.Specs[i].GetName(), specResult)
icon := f.getStateIcon(allPassed)
message := f.formatMessage("\t%s %s\n", icon, title)
message = f.getColoredText(allPassed, message)
Expand Down
52 changes: 18 additions & 34 deletions cli/formatters/test_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,10 @@ func TestSuccessfulTestRunOutputWithResult(t *testing.T) {
Test: openapi.Test{
Id: openapi.PtrString("9876543"),
Name: openapi.PtrString("Testcase 1"),
Specs: &openapi.TestSpecs{
Specs: []openapi.TestSpecsSpecsInner{
{
Selector: &openapi.Selector{
Query: openapi.PtrString(`span[name = "my span"]`),
},
Name: *openapi.NewNullableString(&testSpecName),
},
Specs: []openapi.TestSpec{
{
Selector: openapi.PtrString(`span[name = "my span"]`),
Name: openapi.PtrString(testSpecName),
},
},
},
Expand Down Expand Up @@ -127,19 +123,13 @@ func TestFailingTestOutput(t *testing.T) {
Test: openapi.Test{
Id: openapi.PtrString("9876543"),
Name: openapi.PtrString("Testcase 2"),
Specs: &openapi.TestSpecs{
Specs: []openapi.TestSpecsSpecsInner{
{
Selector: &openapi.Selector{
Query: openapi.PtrString(`span[name = "my span"]`),
},
Name: *openapi.NewNullableString(&testSpecName),
},
{
Selector: &openapi.Selector{
Query: openapi.PtrString(`span[name = "my other span"]`),
},
},
Specs: []openapi.TestSpec{
{
Selector: openapi.PtrString(`span[name = "my span"]`),
Name: openapi.PtrString(testSpecName),
},
{
Selector: openapi.PtrString(`span[name = "my other span"]`),
},
},
},
Expand Down Expand Up @@ -227,19 +217,13 @@ func TestFailingTestOutputWithPadding(t *testing.T) {
Test: openapi.Test{
Id: openapi.PtrString("9876543"),
Name: openapi.PtrString("Testcase 2"),
Specs: &openapi.TestSpecs{
Specs: []openapi.TestSpecsSpecsInner{
{
Selector: &openapi.Selector{
Query: openapi.PtrString(`span[name = "my span"]`),
},
Name: *openapi.NewNullableString(&testSpecName),
},
{
Selector: &openapi.Selector{
Query: openapi.PtrString(`span[name = "my other span"]`),
},
},
Specs: []openapi.TestSpec{
{
Selector: openapi.PtrString(`span[name = "my span"]`),
Name: openapi.PtrString(testSpecName),
},
{
Selector: openapi.PtrString(`span[name = "my other span"]`),
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion cli/formatters/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewTransactionsFormatter() TransactionFormatter {
}

func testSpecUnmarshaller(t *openapi.TestSpecs, b []byte) error {
t.Specs = make([]openapi.TestSpecsSpecsInner, 0)
t.Specs = make([]openapi.TestSpec, 0)
return yaml.Unmarshal(b, &t.Specs)
}

Expand Down
Loading

0 comments on commit e1b4bef

Please sign in to comment.