1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/pkg/generator/helper/resources_test.go
Oliver Nocon a46b57e6b4
Persisted pipeline environment for golang library (#1091)
* Use commonPipelineEnvironment in go binary

* Update groovy part incl. tests

* Rework structure and naming

* Support influx resources in steps

* Update tests and some cleanups

* Add correct defer handling

* Address PR feedback

* Fix test

* Update resources.go

Co-authored-by: Sven Merk <33895725+nevskrem@users.noreply.github.com>
2020-01-15 12:16:25 +01:00

95 lines
2.5 KiB
Go

package helper
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestStructString(t *testing.T) {
tt := []struct {
in InfluxResource
expected string
}{
{
in: InfluxResource{
Name: "TestInflux",
StepName: "TestStep",
Measurements: []InfluxMeasurement{
{
Name: "m1",
Fields: []InfluxMetric{{Name: "field1_1"}, {Name: "field1_2"}},
Tags: []InfluxMetric{{Name: "tag1_1"}, {Name: "tag1_2"}},
},
{
Name: "m2",
Fields: []InfluxMetric{{Name: "field2_1"}, {Name: "field2_2"}},
Tags: []InfluxMetric{{Name: "tag2_1"}, {Name: "tag2_2"}},
},
},
},
expected: `type TestStepTestInflux struct {
m1 struct {
fields struct {
field1_1 string
field1_2 string
}
tags struct {
tag1_1 string
tag1_2 string
}
}
m2 struct {
fields struct {
field2_1 string
field2_2 string
}
tags struct {
tag2_1 string
tag2_2 string
}
}
}
func (i *TestStepTestInflux) persist(path, resourceName string) {
measurementContent := []struct{
measurement string
valType string
name string
value string
}{
{valType: config.InfluxField, measurement: "m1" , name: "field1_1", value: i.m1.fields.field1_1},
{valType: config.InfluxField, measurement: "m1" , name: "field1_2", value: i.m1.fields.field1_2},
{valType: config.InfluxTag, measurement: "m1" , name: "tag1_1", value: i.m1.tags.tag1_1},
{valType: config.InfluxTag, measurement: "m1" , name: "tag1_2", value: i.m1.tags.tag1_2},
{valType: config.InfluxField, measurement: "m2" , name: "field2_1", value: i.m2.fields.field2_1},
{valType: config.InfluxField, measurement: "m2" , name: "field2_2", value: i.m2.fields.field2_2},
{valType: config.InfluxTag, measurement: "m2" , name: "tag2_1", value: i.m2.tags.tag2_1},
{valType: config.InfluxTag, measurement: "m2" , name: "tag2_2", value: i.m2.tags.tag2_2},
}
errCount := 0
for _, metric := range measurementContent {
err := piperenv.SetResourceParameter(path, resourceName, filepath.Join(metric.measurement, fmt.Sprintf("%vs", metric.valType), metric.name), metric.value)
if err != nil {
log.Entry().WithError(err).Error("Error persisting influx environment.")
errCount++
}
}
if errCount > 0 {
os.Exit(1)
}
}`,
},
}
for run, test := range tt {
t.Run(fmt.Sprintf("Run %v", run), func(t *testing.T) {
got, err := test.in.StructString()
assert.NoError(t, err)
assert.Equal(t, test.expected, got)
})
}
}