Skip to content

Commit 600f5a8

Browse files
authored
Add basic cloudfoundry integration tests (#19018)
1 parent 925b36e commit 600f5a8

File tree

6 files changed

+259
-0
lines changed

6 files changed

+259
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
// +build integration
6+
// +build cloudfoundry
7+
8+
package cloudfoundry
9+
10+
import (
11+
"testing"
12+
"time"
13+
14+
"github.com/stretchr/testify/require"
15+
16+
"github.com/elastic/beats/v7/filebeat/channel"
17+
"github.com/elastic/beats/v7/filebeat/input"
18+
"github.com/elastic/beats/v7/libbeat/beat"
19+
"github.com/elastic/beats/v7/libbeat/common"
20+
cftest "github.com/elastic/beats/v7/x-pack/libbeat/common/cloudfoundry/test"
21+
)
22+
23+
func TestInput(t *testing.T) {
24+
config := common.MustNewConfigFrom(cftest.GetConfigFromEnv(t))
25+
26+
events := make(chan beat.Event)
27+
connector := channel.ConnectorFunc(func(*common.Config, beat.ClientConfig) (channel.Outleter, error) {
28+
return newOutleter(events), nil
29+
})
30+
31+
inputCtx := input.Context{Done: make(chan struct{})}
32+
33+
input, err := NewInput(config, connector, inputCtx)
34+
require.NoError(t, err)
35+
36+
go input.Run()
37+
defer input.Stop()
38+
39+
select {
40+
case e := <-events:
41+
t.Logf("Event received: %+v", e)
42+
case <-time.After(10 * time.Second):
43+
t.Fatal("timeout waiting for events")
44+
}
45+
}
46+
47+
type outleter struct {
48+
events chan<- beat.Event
49+
done chan struct{}
50+
}
51+
52+
func newOutleter(events chan<- beat.Event) *outleter {
53+
return &outleter{
54+
events: events,
55+
done: make(chan struct{}),
56+
}
57+
}
58+
59+
func (o *outleter) Close() error {
60+
close(o.done)
61+
return nil
62+
}
63+
64+
func (o *outleter) Done() <-chan struct{} {
65+
return o.done
66+
}
67+
68+
func (o *outleter) OnEvent(e beat.Event) bool {
69+
select {
70+
case o.events <- e:
71+
return true
72+
default:
73+
return false
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package test
6+
7+
import (
8+
"os"
9+
"testing"
10+
)
11+
12+
func GetConfigFromEnv(t *testing.T) map[string]interface{} {
13+
t.Helper()
14+
15+
config := map[string]interface{}{
16+
"api_address": lookupEnv(t, "CLOUDFOUNDRY_API_ADDRESS"),
17+
"client_id": lookupEnv(t, "CLOUDFOUNDRY_CLIENT_ID"),
18+
"client_secret": lookupEnv(t, "CLOUDFOUNDRY_CLIENT_SECRET"),
19+
20+
"ssl.verification_mode": "none",
21+
}
22+
23+
optionalConfig(config, "uaa_address", "CLOUDFOUNDRY_UAA_ADDRESS")
24+
optionalConfig(config, "rlp_address", "CLOUDFOUNDRY_RLP_ADDRESS")
25+
optionalConfig(config, "doppler_address", "CLOUDFOUNDRY_DOPPLER_ADDRESS")
26+
27+
if t.Failed() {
28+
t.FailNow()
29+
}
30+
31+
return config
32+
}
33+
34+
func lookupEnv(t *testing.T, name string) string {
35+
value, ok := os.LookupEnv(name)
36+
if !ok {
37+
t.Errorf("Environment variable %s is not set", name)
38+
}
39+
return value
40+
}
41+
42+
func optionalConfig(config map[string]interface{}, key string, envVar string) {
43+
if value, ok := os.LookupEnv(envVar); ok {
44+
config[key] = value
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
// +build integration
6+
// +build cloudfoundry
7+
8+
package container
9+
10+
import (
11+
"testing"
12+
"time"
13+
14+
"github.com/stretchr/testify/require"
15+
16+
mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
17+
"github.com/elastic/beats/v7/x-pack/metricbeat/module/cloudfoundry/mtest"
18+
)
19+
20+
func TestFetch(t *testing.T) {
21+
config := mtest.GetConfig(t, "container")
22+
23+
ms := mbtest.NewPushMetricSetV2(t, config)
24+
events := mbtest.RunPushMetricSetV2(60*time.Second, 1, ms)
25+
26+
require.NotEmpty(t, events)
27+
}
28+
29+
func TestData(t *testing.T) {
30+
config := mtest.GetConfig(t, "container")
31+
32+
ms := mbtest.NewPushMetricSetV2(t, config)
33+
events := mbtest.RunPushMetricSetV2(60*time.Second, 1, ms)
34+
35+
require.NotEmpty(t, events)
36+
37+
beatEvent := mbtest.StandardizeEvent(ms, events[0])
38+
mbtest.WriteEventToDataJSON(t, beatEvent, "")
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
// +build integration
6+
// +build cloudfoundry
7+
8+
package counter
9+
10+
import (
11+
"testing"
12+
"time"
13+
14+
"github.com/stretchr/testify/require"
15+
16+
mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
17+
"github.com/elastic/beats/v7/x-pack/metricbeat/module/cloudfoundry/mtest"
18+
)
19+
20+
func TestFetch(t *testing.T) {
21+
config := mtest.GetConfig(t, "counter")
22+
23+
ms := mbtest.NewPushMetricSetV2(t, config)
24+
events := mbtest.RunPushMetricSetV2(10*time.Second, 1, ms)
25+
26+
require.NotEmpty(t, events)
27+
}
28+
29+
func TestData(t *testing.T) {
30+
config := mtest.GetConfig(t, "counter")
31+
32+
ms := mbtest.NewPushMetricSetV2(t, config)
33+
events := mbtest.RunPushMetricSetV2(10*time.Second, 1, ms)
34+
35+
require.NotEmpty(t, events)
36+
37+
beatEvent := mbtest.StandardizeEvent(ms, events[0])
38+
mbtest.WriteEventToDataJSON(t, beatEvent, "")
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package mtest
6+
7+
import (
8+
"testing"
9+
10+
cftest "github.com/elastic/beats/v7/x-pack/libbeat/common/cloudfoundry/test"
11+
)
12+
13+
func GetConfig(t *testing.T, metricset string) map[string]interface{} {
14+
t.Helper()
15+
16+
config := cftest.GetConfigFromEnv(t)
17+
config["module"] = "cloudfoundry"
18+
config["metricsets"] = []string{metricset}
19+
20+
return config
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
// +build integration
6+
// +build cloudfoundry
7+
8+
package value
9+
10+
import (
11+
"testing"
12+
"time"
13+
14+
"github.com/stretchr/testify/require"
15+
16+
mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
17+
"github.com/elastic/beats/v7/x-pack/metricbeat/module/cloudfoundry/mtest"
18+
)
19+
20+
func TestFetch(t *testing.T) {
21+
config := mtest.GetConfig(t, "value")
22+
23+
ms := mbtest.NewPushMetricSetV2(t, config)
24+
events := mbtest.RunPushMetricSetV2(10*time.Second, 1, ms)
25+
26+
require.NotEmpty(t, events)
27+
}
28+
29+
func TestData(t *testing.T) {
30+
config := mtest.GetConfig(t, "value")
31+
32+
ms := mbtest.NewPushMetricSetV2(t, config)
33+
events := mbtest.RunPushMetricSetV2(10*time.Second, 1, ms)
34+
35+
require.NotEmpty(t, events)
36+
37+
beatEvent := mbtest.StandardizeEvent(ms, events[0])
38+
mbtest.WriteEventToDataJSON(t, beatEvent, "")
39+
}

0 commit comments

Comments
 (0)