1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/integration/integration_influx_test.go
Sven Merk a1988f6808
feat(whitesourceExecuteScan): GitHub issue creation + SARIF (#3535)
* Add GH issue creation + SARIF

* Code cleanup

* Fix fmt, add debug

* Code enhancements

* Fix

* Added debug info

* Rework UA log scan

* Fix code

* read UA version

* Fix nil reference

* Extraction

* Credentials

* Issue creation

* Error handling

* Fix issue creation

* query escape

* Query escape 2

* Revert

* Test avoid update

* HTTP client

* Add support for custom TLS certs

* Fix code

* Fix code 2

* Fix code 3

* Disable cert check

* Fix auth

* Remove implicit trust

* Skip verification

* Fix

* Fix client

* Fix HTTP auth

* Fix trusted certs

* Trim version

* Code

* Add token

* Added token handling to client

* Fix token

* Cleanup

* Fix token

* Token rework

* Fix code

* Kick out oauth client

* Kick out oauth client

* Transport wrapping

* Token

* Simplification

* Refactor

* Variation

* Check

* Fix

* Debug

* Switch client

* Variation

* Debug

* Switch to cert check

* Add debug

* Parse self

* Cleanup

* Update resources/metadata/whitesourceExecuteScan.yaml

* Add debug

* Expose subjects

* Patch

* Debug

* Debug2

* Debug3

* Fix logging response body

* Cleanup

* Cleanup

* Fix request body logging

* Cleanup import

* Fix import cycle

* Cleanup

* Fix fmt

* Fix NopCloser reference

* Regenerate

* Reintroduce

* Fix test

* Fix tests

* Correction

* Fix error

* Code fix

* Fix tests

* Add tests

* Fix code climate issues

* Code climate

* Code climate again

* Code climate again

* Fix fmt

* Fix fmt 2

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
2022-02-23 09:30:19 +01:00

95 lines
3.3 KiB
Go

//go:build integration
// +build integration
// can be execute with go test -tags=integration ./integration/...
package main
import (
"context"
"fmt"
"testing"
"github.com/SAP/jenkins-library/pkg/influx"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
func TestWriteMetrics(t *testing.T) {
t.Parallel()
ctx := context.Background()
const authToken = "influx-token"
const username = "username"
const password = "password"
const bucket = "piper"
const organization = "org"
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
AlwaysPullImage: true,
Image: "influxdb:2.0",
ExposedPorts: []string{"8086/tcp"},
Env: map[string]string{
"DOCKER_INFLUXDB_INIT_MODE": "setup",
"DOCKER_INFLUXDB_INIT_USERNAME": username,
"DOCKER_INFLUXDB_INIT_PASSWORD": password,
"DOCKER_INFLUXDB_INIT_ORG": organization,
"DOCKER_INFLUXDB_INIT_BUCKET": bucket,
"DOCKER_INFLUXDB_INIT_ADMIN_TOKEN": authToken,
},
WaitingFor: wait.ForListeningPort("8086/tcp"),
},
Started: true,
}
influxContainer, err := testcontainers.GenericContainer(ctx, req)
assert.NoError(t, err)
defer influxContainer.Terminate(ctx)
ip, err := influxContainer.Host(ctx)
assert.NoError(t, err)
port, err := influxContainer.MappedPort(ctx, "8086")
host := fmt.Sprintf("http://%s:%s", ip, port.Port())
dataMap := map[string]map[string]interface{}{
"series_1": {"field_a": 11, "field_b": 12},
"series_2": {"field_c": 21, "field_d": 22},
}
dataMapTags := map[string]map[string]string{
"series_1": {"tag_a": "a", "tag_b": "b"},
"series_2": {"tag_c": "c", "tag_d": "d"},
}
influxClient := influxdb2.NewClient(host, authToken)
defer influxClient.Close()
client := influx.NewClient(influxClient, organization, bucket)
err = client.WriteMetrics(dataMap, dataMapTags)
assert.NoError(t, err)
queryAPI := influxClient.QueryAPI(organization)
result, err := queryAPI.Query(context.Background(),
`from(bucket:"piper")|> range(start: -1h) |> filter(fn: (r) => r._measurement == "series_1" or r._measurement == "series_2")`)
assert.NoError(t, err)
valuesMap := map[string]map[string]interface{}{}
expectedValuesMap := map[string]map[string]interface{}{
"series_1_field_a": {"_field": "field_a", "_measurement": "series_1", "_value": int64(11), "tag_a": "a", "tag_b": "b"},
"series_1_field_b": {"_field": "field_b", "_measurement": "series_1", "_value": int64(12), "tag_a": "a", "tag_b": "b"},
"series_2_field_c": {"_field": "field_c", "_measurement": "series_2", "_value": int64(21), "tag_c": "c", "tag_d": "d"},
"series_2_field_d": {"_field": "field_d", "_measurement": "series_2", "_value": int64(22), "tag_c": "c", "tag_d": "d"},
}
for result.Next() {
values := result.Record().Values()
measurement := values["_measurement"]
field := values["_field"]
delete(values, "_start")
delete(values, "_stop")
delete(values, "_time")
delete(values, "result")
delete(values, "table")
valuesMap[fmt.Sprintf("%v_%v", measurement, field)] = values
}
assert.NoError(t, result.Err())
assert.Equal(t, len(expectedValuesMap), len(valuesMap))
assert.Equal(t, expectedValuesMap, valuesMap)
}