-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(cli): Adding support for Variable Sets #3018
Changes from 2 commits
c0c0bca
dd70fca
c716d7a
8ad39cc
77010df
7fc0ec2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,10 @@ func init() { | |
variableSetClient, | ||
) | ||
|
||
if runParams.EnvID != "" { | ||
runParams.VarsID = runParams.EnvID | ||
} | ||
|
||
runParams := runner.RunOptions{ | ||
ID: runParams.ID, | ||
DefinitionFile: runParams.DefinitionFile, | ||
|
@@ -67,6 +71,12 @@ func init() { | |
runCmd.Flags().BoolVarP(&runParams.SkipResultWait, "skip-result-wait", "W", false, "do not wait for results. exit immediately after test run started") | ||
runCmd.Flags().StringVarP(&runParams.JUnitOuptutFile, "junit", "j", "", "file path to save test results in junit format") | ||
runCmd.Flags().StringSliceVar(&runParams.RequriedGates, "required-gates", []string{}, "override default required gate. "+validRequiredGatesMsg()) | ||
|
||
//deprecated | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still supporting this in case users need it |
||
runCmd.Flags().StringVarP(&runParams.EnvID, "env", "e", "", "environment file or ID to be used") | ||
runCmd.Flags().MarkDeprecated("env", "use --vars instead") | ||
runCmd.Flags().MarkShorthandDeprecated("e", "use --vars instead") | ||
|
||
rootCmd.AddCommand(runCmd) | ||
} | ||
|
||
|
@@ -83,6 +93,7 @@ type runParameters struct { | |
ID string | ||
DefinitionFile string | ||
VarsID string | ||
EnvID string | ||
SkipResultWait bool | ||
JUnitOuptutFile string | ||
RequriedGates []string | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,8 @@ var ( | |
var ( | ||
httpClient = &resourcemanager.HTTPClient{} | ||
|
||
variableSetClient = resourcemanager.NewClient( | ||
variableSetPreprocessor = preprocessor.VariableSet(cliLogger) | ||
variableSetClient = resourcemanager.NewClient( | ||
httpClient, cliLogger, | ||
"variableset", "variablesets", | ||
resourcemanager.WithTableConfig(resourcemanager.TableConfig{ | ||
|
@@ -46,6 +47,8 @@ var ( | |
}, | ||
}), | ||
resourcemanager.WithResourceType("VariableSet"), | ||
resourcemanager.WithApplyPreProcessor(variableSetPreprocessor.Preprocess), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preprocessor to switch |
||
resourcemanager.WithDeprecatedAlias("Environment"), | ||
) | ||
|
||
testPreprocessor = preprocessor.Test(cliLogger) | ||
|
@@ -121,6 +124,13 @@ var ( | |
resourcemanager.WithApplyPreProcessor(transactionPreprocessor.Preprocess), | ||
) | ||
|
||
// deprecated resources | ||
deprecatedEnvironmentClient = resourcemanager.NewClient( | ||
httpClient, cliLogger, | ||
"environment", "environments", | ||
resourcemanager.WithProxyResource("variableset"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Used to create an entry in the registry that will point to the variable set client |
||
) | ||
|
||
resources = resourcemanager.NewRegistry(). | ||
Register( | ||
resourcemanager.NewClient( | ||
|
@@ -231,7 +241,10 @@ var ( | |
). | ||
Register(variableSetClient). | ||
Register(transactionClient). | ||
Register(testClient) | ||
Register(testClient). | ||
|
||
// deprecated resources | ||
Register(deprecatedEnvironmentClient) | ||
) | ||
|
||
func resourceList() string { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package resourcemanager | |
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/agnivade/levenshtein" | ||
|
@@ -30,13 +31,33 @@ func (r *Registry) Get(resourceName string) (Client, error) { | |
return Client{}, ErrResourceNotFound | ||
} | ||
|
||
if c.options.proxyResource != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If proxy, check if the proxied resource exists |
||
c.logger.Warn(fmt.Sprintf("The resource `%s` is deprecated and will be removed in a future version. Please use `%s` instead.", c.resourceName, c.options.proxyResource)) | ||
return r.Get(c.options.proxyResource) | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (r *Registry) Exists(resourceName string) bool { | ||
c, ok := r.resources[resourceName] | ||
if !ok { | ||
return false | ||
} | ||
|
||
if c.options.proxyResource != "" { | ||
return r.Exists(c.options.proxyResource) | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (r *Registry) List() []string { | ||
var resources []string | ||
for k := range r.resources { | ||
resources = append(resources, k) | ||
for k, c := range r.resources { | ||
if c.options.proxyResource == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Deleting the proxy resources from the list |
||
resources = append(resources, k) | ||
} | ||
} | ||
|
||
sort.Strings(resources) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package preprocessor | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/goccy/go-yaml" | ||
"github.com/kubeshop/tracetest/cli/pkg/fileutil" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type variableSet struct { | ||
logger *zap.Logger | ||
} | ||
|
||
type generic struct { | ||
Type string `yaml:"type"` | ||
Spec interface{} `yaml:"spec"` | ||
} | ||
|
||
func VariableSet(logger *zap.Logger) variableSet { | ||
return variableSet{ | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (vs variableSet) Preprocess(ctx context.Context, input fileutil.File) (fileutil.File, error) { | ||
var resource generic | ||
err := yaml.Unmarshal(input.Contents(), &resource) | ||
if err != nil { | ||
vs.logger.Error("error parsing test", zap.String("content", string(input.Contents())), zap.Error(err)) | ||
return input, fmt.Errorf("could not unmarshal test yaml: %w", err) | ||
} | ||
|
||
if resource.Type == "Environment" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updates Environment to VariableSet There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to add a |
||
resource.Type = "VariableSet" | ||
} | ||
|
||
marshalled, err := yaml.Marshal(resource) | ||
if err != nil { | ||
return input, fmt.Errorf("could not marshal test yaml: %w", err) | ||
} | ||
|
||
return fileutil.New(input.AbsPath(), marshalled), nil | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ TRACETEST_CLI?="${PROJECT_ROOT}/../../dist/tracetest" | |
TEST_ENVIRONMENT?="jaeger" | ||
TAG?="dev" | ||
ENABLE_CLI_DEBUG?="false" | ||
TEST_SCENARIO?=test | ||
|
||
help: Makefile ## show list of commands | ||
@echo "Choose a command run:" | ||
|
@@ -17,6 +18,14 @@ test: # run tests for this application | |
go clean -testcache; \ | ||
go test -v -timeout 300s -p 1 ./...; | ||
|
||
test/scenario: # run tests for this application | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will allows us execute a single testscenario There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! 🎉 |
||
export TRACETEST_CLI=$(TRACETEST_CLI); \ | ||
export TEST_ENVIRONMENT=$(TEST_ENVIRONMENT); \ | ||
export TAG=$(TAG); \ | ||
export ENABLE_CLI_DEBUG=$(ENABLE_CLI_DEBUG); \ | ||
go clean -testcache; \ | ||
go test -v -timeout 300s -p 1 "$(PROJECT_ROOT)/testscenarios/$(TEST_SCENARIO)"; | ||
|
||
test/debug: # run tests for this application with debug mode enabled | ||
export ENABLE_CLI_DEBUG="true"; \ | ||
make test; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type: Environment | ||
spec: | ||
id: deprecated-pokeapi-env | ||
name: deprecated-pokeapi-env | ||
values: | ||
- key: POKEMON_NAME | ||
value: snorlax | ||
- key: POKEMON_URL | ||
value: https://assets.pokemon.com/assets/cms2/img/pokedex/full/143.png |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,33 @@ func TestRunTestWithHttpTriggerAndVariableSetFile(t *testing.T) { | |
require.Equal("https://assets.pokemon.com/assets/cms2/img/pokedex/full/143.png", environmentVars.Spec.Values[1].Value) | ||
}) | ||
|
||
t.Run("should pass when using the deprecated environment definition file", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a test to validate that the old way still works as expected |
||
result := tracetestcli.Exec(t, "get environment --id deprecated-pokeapi-env", tracetestcli.WithCLIConfig(cliConfig)) | ||
helpers.RequireExitCodeEqual(t, result, 0) | ||
require.Contains(result.StdOut, "The resource `environment` is deprecated and will be removed in a future version. Please use `variableset` instead.") | ||
require.Contains(result.StdOut, "Resource variableset with ID deprecated-pokeapi-env not found") | ||
|
||
// When I try to run a test with a http trigger and a variable set file | ||
// Then it should pass | ||
environmentFile := env.GetTestResourcePath(t, "deprecated-environment") | ||
testFile := env.GetTestResourcePath(t, "http-trigger-with-environment-file") | ||
|
||
command := fmt.Sprintf("run test -f %s --env %s", testFile, environmentFile) | ||
result = tracetestcli.Exec(t, command, tracetestcli.WithCLIConfig(cliConfig)) | ||
helpers.RequireExitCodeEqual(t, result, 0) | ||
require.Contains(result.StdOut, "✔ It should add a Pokemon correctly") | ||
require.Contains(result.StdOut, "✔ It should save the correct data") | ||
|
||
// When I try to get the variable set created on the previous step | ||
// Then it should retrieve it correctly | ||
result = tracetestcli.Exec(t, "get environment --id deprecated-pokeapi-env", tracetestcli.WithCLIConfig(cliConfig)) | ||
helpers.RequireExitCodeEqual(t, result, 0) | ||
|
||
require.Contains(result.StdOut, "The resource `environment` is deprecated and will be removed in a future version. Please use `variableset` instead.") | ||
require.Contains(result.StdOut, "VariableSet") | ||
require.Contains(result.StdOut, "https://assets.pokemon.com/assets/cms2/img/pokedex/full/143.png") | ||
}) | ||
|
||
t.Run("should pass when using variable set id", func(t *testing.T) { | ||
// Given I am a Tracetest CLI user | ||
// And I have my server recently created | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this to use a different function because
Get
now writes the warning to the console if deprecated