Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/apache] Add integration test #7517

Merged
merged 4 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- `nginxreceiver`: Standardize integration test (#7515)
- `mysqlreceiver`: Update to use mdatagen v2 (#7507)
- `postgresqlreceiver`: Add integration tests (#7501)
- `apachereceiver`: Add integration test (#7517)
- `mysqlreceiver`: Use scrapererror to report errors (#7513)
- `postgresreceiver`: Update to mdatagen v2 (#7503)

Expand Down
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ ALL_MODULES ?= $(shell find . -type f -name "go.mod" -exec dirname {} \; | sort
# Modules to run integration tests on.
# XXX: Find a way to automatically populate this. Too slow to run across all modules when there are just a few.
INTEGRATION_TEST_MODULES := \
internal/common \
receiver/apachereceiver \
receiver/dockerstatsreceiver \
receiver/jmxreceiver/ \
receiver/redisreceiver \
receiver/zookeeperreceiver \
receiver/kafkametricsreceiver \
receiver/nginxreceiver \
receiver/memcachedreceiver \
receiver/mysqlreceiver \
receiver/nginxreceiver \
receiver/postgresqlreceiver \
internal/common \
receiver/redisreceiver \
receiver/zookeeperreceiver \
extension/observer/dockerobserver

.DEFAULT_GOAL := all
Expand Down
32 changes: 31 additions & 1 deletion receiver/apachereceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,50 @@ require (
)

require (
github.com/rogpeppe/go-internal v1.6.2 // indirect
github.com/testcontainers/testcontainers-go v0.12.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.17-0.20210211115548-6eac466e5fa3 // indirect
github.com/Microsoft/hcsshim v0.8.16 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/containerd/cgroups v0.0.0-20210114181951-8a68de567b68 // indirect
github.com/containerd/containerd v1.5.0-beta.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v20.10.11+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-logr/logr v1.2.1 // indirect
github.com/go-logr/stdr v1.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/klauspost/compress v1.14.2 // indirect
github.com/knadh/koanf v1.4.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/sys/mount v0.2.0 // indirect
github.com/moby/sys/mountinfo v0.5.0 // indirect
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.6.2 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.28.0 // indirect
Expand All @@ -38,8 +64,12 @@ require (
go.opentelemetry.io/otel/trace v1.3.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
golang.org/x/net v0.0.0-20211108170745-6635138e15ea // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/grpc v1.44.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

Expand Down
468 changes: 468 additions & 0 deletions receiver/apachereceiver/go.sum

Large diffs are not rendered by default.

135 changes: 135 additions & 0 deletions receiver/apachereceiver/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build integration
// +build integration

package apachereceiver

import (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"path"
"path/filepath"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest/golden"
)

func TestApacheIntegration(t *testing.T) {
t.Parallel()
container := getContainer(t, testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Context: path.Join("testdata", "integration"),
Dockerfile: "Dockerfile.apache",
},
ExposedPorts: []string{"8081:80"},
WaitingFor: waitStrategy{},
})
defer func() {
require.NoError(t, container.Terminate(context.Background()))
}()
hostname, err := container.Host(context.Background())
require.NoError(t, err)

f := NewFactory()
cfg := f.CreateDefaultConfig().(*Config)
cfg.ScraperControllerSettings.CollectionInterval = 100 * time.Millisecond
cfg.Endpoint = fmt.Sprintf("http://%s/server-status?auto", net.JoinHostPort(hostname, "8081"))

consumer := new(consumertest.MetricsSink)
settings := componenttest.NewNopReceiverCreateSettings()
rcvr, err := f.CreateMetricsReceiver(context.Background(), settings, cfg, consumer)
require.NoError(t, err, "failed creating metrics receiver")
require.NoError(t, rcvr.Start(context.Background(), componenttest.NewNopHost()))
require.Eventuallyf(t, func() bool {
return consumer.DataPointCount() > 0
}, 2*time.Minute, 1*time.Second, "failed to receive more than 0 metrics")
require.NoError(t, rcvr.Shutdown(context.Background()))

actualMetrics := consumer.AllMetrics()[0]

expectedFile := filepath.Join("testdata", "integration", "expected.json")
expectedMetrics, err := golden.ReadMetrics(expectedFile)
require.NoError(t, err)

require.NoError(t, scrapertest.CompareMetrics(expectedMetrics, actualMetrics, scrapertest.IgnoreMetricValues()))
}

func getContainer(t *testing.T, req testcontainers.ContainerRequest) testcontainers.Container {
require.NoError(t, req.Validate())
container, err := testcontainers.GenericContainer(
context.Background(),
testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
require.NoError(t, err)
return container
}

type waitStrategy struct{}

func (ws waitStrategy) WaitUntilReady(ctx context.Context, st wait.StrategyTarget) error {
if err := wait.ForListeningPort("80").
WithStartupTimeout(2*time.Minute).
WaitUntilReady(ctx, st); err != nil {
return err
}

hostname, err := st.Host(ctx)
if err != nil {
return err
}

for {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(5 * time.Second):
return fmt.Errorf("server startup problem")
case <-time.After(100 * time.Millisecond):
resp, err := http.Get(fmt.Sprintf("http://%s:8081/server-status?auto", hostname))
if err != nil {
continue
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
continue
}

if resp.Body.Close() != nil {
continue
}

// The server needs a moment to generate some stats
if strings.Contains(string(body), "ReqPerSec") {
return nil
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM httpd:2.4

COPY httpd.conf /usr/local/apache2/conf/httpd.conf
Loading