Skip to content

Commit

Permalink
feat: tostring method (#4220)
Browse files Browse the repository at this point in the history
* feat: add tostring to template

* feat: vartypeptrtostring template method

* feat: add tmp method to job ext

* feat: add helper method for template

* fix: spelling

* fix: doc format
  • Loading branch information
vsukhin authored Jul 31, 2023
1 parent 9192853 commit 68aec5f
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 11 deletions.
2 changes: 1 addition & 1 deletion contrib/container/pytest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ of the Docker image in executor CRD, use command
`kubectl apply -f container-executor-pytest.yaml`
`kubectl apply -f pytest_test.yaml`

## Run resulted tests
## Run created tests
Use
`kubectl testkube run test container-executor-pytest-failed-sample`
`kubectl testkube run test container-executor-pytest-passed-sample`
10 changes: 5 additions & 5 deletions contrib/executor/jmeter/pkg/parser/xjtl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (

// TestResults is a root element of junit xml report
type TestResults struct {
XMLName xml.Name `xml:"testResults"`
HTTPSamples []HTTPSample `xml:"httpSample,omitempty"`
XMLName xml.Name `xml:"testResults"`
HTTPSamples []Example `xml:"httpSample,omitempty"`
Samples []Example `xml:"sample,omitempty"`
}

// HTTPSample is http sample details
type HTTPSample struct {
XMLName xml.Name `xml:"httpSample"`
// Example is example details
type Example struct {
Time int `xml:"t,attr"`
Success bool `xml:"s,attr"`
Label string `xml:"lb,attr"`
Expand Down
37 changes: 37 additions & 0 deletions contrib/executor/jmeter/pkg/parser/xjtl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ const (
</assertionResult>
</httpSample>
</testResults>
`

mixedXML = `
<testResults version="1.2">
<sample t="2" it="0" lt="0" ct="0" ts="1690724003768" s="true" lb="========== Starting GR Alarm verification =========" rc="200" rm="OK" tn="Verify GR Alarms 1-1" dt="text" by="7109" sby="0" ng="1" na="1">
</sample>
<httpSample t="946" it="0" lt="935" ct="737" ts="1690724004825" s="true" lb="Get Token - HTTP Request" rc="200" rm="OK" tn="Verify GR Alarms 1-1" dt="text" by="3774" sby="350" ng="1" na="1">
</httpSample>
<sample t="0" it="0" lt="0" ct="0" ts="1690724048711" s="true" lb="Dummy cmdb Alarm cleared at : 2022-03-22T09:05:01.000Z " rc="200" rm="OK" tn="Verify GR Alarms 1-1" dt="text" by="10328" sby="0" ng="1" na="1">
</sample>
<sample t="1" it="0" lt="0" ct="0" ts="1690724048871" s="false" lb="Alarms status are inactive. Unexpected Result! Failing the test!" rc="200" rm="OK" tn="Verify GR Alarms 1-1" dt="text" by="10328" sby="0" ng="1" na="1">
<assertionResult>
<name>Fail Test</name>
<failure>true</failure>
<error>false</error>
<failureMessage>Test FAILED</failureMessage>
</assertionResult>
</sample>
</testResults>
`
)

Expand Down Expand Up @@ -73,4 +92,22 @@ func TestParseXML(t *testing.T) {

assert.EqualError(t, err, "EOF")
})

t.Run("parse XML mixed test", func(t *testing.T) {
t.Parallel()

results, err := ParseXML([]byte(mixedXML))

assert.NoError(t, err)
assert.Equal(t, 1, len(results.HTTPSamples))
assert.True(t, results.HTTPSamples[0].Success)
assert.Equal(t, 946, results.HTTPSamples[0].Time)
assert.Equal(t, "Get Token - HTTP Request", results.HTTPSamples[0].Label)

assert.Equal(t, 3, len(results.Samples))
assert.False(t, results.Samples[2].Success)
assert.Equal(t, 1, results.Samples[2].Time)
assert.Equal(t, "Alarms status are inactive. Unexpected Result! Failing the test!", results.Samples[2].Label)
assert.Equal(t, "Fail Test", results.Samples[2].AssertionResult.Name)
})
}
3 changes: 2 additions & 1 deletion contrib/executor/jmeter/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ func MapTestResultsToExecutionResults(out []byte, results parser.TestResults) (r
result.Output = string(out)
result.OutputType = "text/plain"

for _, r := range results.HTTPSamples {
samples := append(results.HTTPSamples, results.Samples...)
for _, r := range samples {
if !r.Success {
result.Status = testkube.ExecutionStatusFailed
if r.AssertionResult != nil {
Expand Down
13 changes: 13 additions & 0 deletions docs/docs/articles/creating-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,19 @@ spec:

```

We also provide special helper methods to use in the job template:
`vartypeptrtostring` - method to convert a pointer to a variable type to a string type

Usage example:
```yaml
{{- range $key, $value := .Variables }}
{{ if eq ($value.Type_ | vartypeptrtostring) "basic" }}
- name: TEST
value: "TEST"
{{- end }}
{{- end }}
```

Add `imagePullSecrets` option if you use your own Image Registry. This will add the secret for both `init` and `executor` containers.


Expand Down
4 changes: 4 additions & 0 deletions pkg/api/v1/testkube/model_variable_type_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ func VariableTypePtr(stepType VariableType) *VariableType {

var VariableTypeBasic = VariableTypePtr(BASIC_VariableType)
var VariableTypeSecret = VariableTypePtr(SECRET_VariableType)

func VariableTypeString(ptr *VariableType) string {
return string(*ptr)
}
6 changes: 4 additions & 2 deletions pkg/executor/client/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,8 @@ func NewJobSpec(log *zap.SugaredLogger, options JobOptions) (*batchv1.Job, error
secretEnvVars := append(envManager.PrepareSecrets(options.SecretEnvs, options.Variables),
envManager.PrepareGitCredentials(options.UsernameSecret, options.TokenSecret)...)

tmpl, err := template.New("job").Parse(options.JobTemplate)
tmpl, err := template.New("job").Funcs(template.FuncMap{"vartypeptrtostring": testkube.VariableTypeString}).
Parse(options.JobTemplate)
if err != nil {
return nil, errors.Errorf("creating job spec from options.JobTemplate error: %v", err)
}
Expand All @@ -688,7 +689,8 @@ func NewJobSpec(log *zap.SugaredLogger, options JobOptions) (*batchv1.Job, error
var job batchv1.Job
jobSpec := buffer.String()
if options.JobTemplateExtensions != "" {
tmplExt, err := template.New("jobExt").Parse(options.JobTemplateExtensions)
tmplExt, err := template.New("jobExt").Funcs(template.FuncMap{"vartypeptrtostring": testkube.VariableTypeString}).
Parse(options.JobTemplateExtensions)
if err != nil {
return nil, errors.Errorf("creating job extensions spec from template error: %v", err)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/executor/containerexecutor/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func NewExecutorJobSpec(log *zap.SugaredLogger, options *JobOptions) (*batchv1.J
secretEnvVars := append(envManager.PrepareSecrets(options.SecretEnvs, options.Variables),
envManager.PrepareGitCredentials(options.UsernameSecret, options.TokenSecret)...)

tmpl, err := template.New("job").Parse(options.JobTemplate)
tmpl, err := template.New("job").Funcs(template.FuncMap{"vartypeptrtostring": testkube.VariableTypeString}).
Parse(options.JobTemplate)
if err != nil {
return nil, fmt.Errorf("creating job spec from executor template error: %w", err)
}
Expand All @@ -65,7 +66,8 @@ func NewExecutorJobSpec(log *zap.SugaredLogger, options *JobOptions) (*batchv1.J
var job batchv1.Job
jobSpec := buffer.String()
if options.JobTemplateExtensions != "" {
tmplExt, err := template.New("jobExt").Parse(options.JobTemplateExtensions)
tmplExt, err := template.New("jobExt").Funcs(template.FuncMap{"vartypeptrtostring": testkube.VariableTypeString}).
Parse(options.JobTemplateExtensions)
if err != nil {
return nil, fmt.Errorf("creating job extensions spec from executor template error: %w", err)
}
Expand Down

0 comments on commit 68aec5f

Please sign in to comment.