Skip to content

Commit

Permalink
Add basic cloudfoundry integration tests (#19018)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano authored Jun 12, 2020
1 parent 925b36e commit 600f5a8
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 0 deletions.
75 changes: 75 additions & 0 deletions x-pack/filebeat/input/cloudfoundry/input_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build cloudfoundry

package cloudfoundry

import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/filebeat/channel"
"github.com/elastic/beats/v7/filebeat/input"
"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
cftest "github.com/elastic/beats/v7/x-pack/libbeat/common/cloudfoundry/test"
)

func TestInput(t *testing.T) {
config := common.MustNewConfigFrom(cftest.GetConfigFromEnv(t))

events := make(chan beat.Event)
connector := channel.ConnectorFunc(func(*common.Config, beat.ClientConfig) (channel.Outleter, error) {
return newOutleter(events), nil
})

inputCtx := input.Context{Done: make(chan struct{})}

input, err := NewInput(config, connector, inputCtx)
require.NoError(t, err)

go input.Run()
defer input.Stop()

select {
case e := <-events:
t.Logf("Event received: %+v", e)
case <-time.After(10 * time.Second):
t.Fatal("timeout waiting for events")
}
}

type outleter struct {
events chan<- beat.Event
done chan struct{}
}

func newOutleter(events chan<- beat.Event) *outleter {
return &outleter{
events: events,
done: make(chan struct{}),
}
}

func (o *outleter) Close() error {
close(o.done)
return nil
}

func (o *outleter) Done() <-chan struct{} {
return o.done
}

func (o *outleter) OnEvent(e beat.Event) bool {
select {
case o.events <- e:
return true
default:
return false
}
}
46 changes: 46 additions & 0 deletions x-pack/libbeat/common/cloudfoundry/test/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package test

import (
"os"
"testing"
)

func GetConfigFromEnv(t *testing.T) map[string]interface{} {
t.Helper()

config := map[string]interface{}{
"api_address": lookupEnv(t, "CLOUDFOUNDRY_API_ADDRESS"),
"client_id": lookupEnv(t, "CLOUDFOUNDRY_CLIENT_ID"),
"client_secret": lookupEnv(t, "CLOUDFOUNDRY_CLIENT_SECRET"),

"ssl.verification_mode": "none",
}

optionalConfig(config, "uaa_address", "CLOUDFOUNDRY_UAA_ADDRESS")
optionalConfig(config, "rlp_address", "CLOUDFOUNDRY_RLP_ADDRESS")
optionalConfig(config, "doppler_address", "CLOUDFOUNDRY_DOPPLER_ADDRESS")

if t.Failed() {
t.FailNow()
}

return config
}

func lookupEnv(t *testing.T, name string) string {
value, ok := os.LookupEnv(name)
if !ok {
t.Errorf("Environment variable %s is not set", name)
}
return value
}

func optionalConfig(config map[string]interface{}, key string, envVar string) {
if value, ok := os.LookupEnv(envVar); ok {
config[key] = value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build cloudfoundry

package container

import (
"testing"
"time"

"github.com/stretchr/testify/require"

mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
"github.com/elastic/beats/v7/x-pack/metricbeat/module/cloudfoundry/mtest"
)

func TestFetch(t *testing.T) {
config := mtest.GetConfig(t, "container")

ms := mbtest.NewPushMetricSetV2(t, config)
events := mbtest.RunPushMetricSetV2(60*time.Second, 1, ms)

require.NotEmpty(t, events)
}

func TestData(t *testing.T) {
config := mtest.GetConfig(t, "container")

ms := mbtest.NewPushMetricSetV2(t, config)
events := mbtest.RunPushMetricSetV2(60*time.Second, 1, ms)

require.NotEmpty(t, events)

beatEvent := mbtest.StandardizeEvent(ms, events[0])
mbtest.WriteEventToDataJSON(t, beatEvent, "")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build cloudfoundry

package counter

import (
"testing"
"time"

"github.com/stretchr/testify/require"

mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
"github.com/elastic/beats/v7/x-pack/metricbeat/module/cloudfoundry/mtest"
)

func TestFetch(t *testing.T) {
config := mtest.GetConfig(t, "counter")

ms := mbtest.NewPushMetricSetV2(t, config)
events := mbtest.RunPushMetricSetV2(10*time.Second, 1, ms)

require.NotEmpty(t, events)
}

func TestData(t *testing.T) {
config := mtest.GetConfig(t, "counter")

ms := mbtest.NewPushMetricSetV2(t, config)
events := mbtest.RunPushMetricSetV2(10*time.Second, 1, ms)

require.NotEmpty(t, events)

beatEvent := mbtest.StandardizeEvent(ms, events[0])
mbtest.WriteEventToDataJSON(t, beatEvent, "")
}
21 changes: 21 additions & 0 deletions x-pack/metricbeat/module/cloudfoundry/mtest/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package mtest

import (
"testing"

cftest "github.com/elastic/beats/v7/x-pack/libbeat/common/cloudfoundry/test"
)

func GetConfig(t *testing.T, metricset string) map[string]interface{} {
t.Helper()

config := cftest.GetConfigFromEnv(t)
config["module"] = "cloudfoundry"
config["metricsets"] = []string{metricset}

return config
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

// +build integration
// +build cloudfoundry

package value

import (
"testing"
"time"

"github.com/stretchr/testify/require"

mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
"github.com/elastic/beats/v7/x-pack/metricbeat/module/cloudfoundry/mtest"
)

func TestFetch(t *testing.T) {
config := mtest.GetConfig(t, "value")

ms := mbtest.NewPushMetricSetV2(t, config)
events := mbtest.RunPushMetricSetV2(10*time.Second, 1, ms)

require.NotEmpty(t, events)
}

func TestData(t *testing.T) {
config := mtest.GetConfig(t, "value")

ms := mbtest.NewPushMetricSetV2(t, config)
events := mbtest.RunPushMetricSetV2(10*time.Second, 1, ms)

require.NotEmpty(t, events)

beatEvent := mbtest.StandardizeEvent(ms, events[0])
mbtest.WriteEventToDataJSON(t, beatEvent, "")
}

0 comments on commit 600f5a8

Please sign in to comment.