Skip to content

Commit

Permalink
[chore] document local e2e test execution, minor tweaks to tests (#222)
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Bacher <florian.bacher@dynatrace.com>
Co-authored-by: Moritz Wiesinger <moritz.wiesinger@dynatrace.com>
  • Loading branch information
bacherfl and mowies committed Jul 5, 2024
1 parent 4c7b8b1 commit ec42aec
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 11 deletions.
14 changes: 13 additions & 1 deletion internal/k8stest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package k8stest // import "github.com/open-telemetry/opentelemetry-collector-con
import (
"errors"
"fmt"
"os"

"k8s.io/client-go/discovery"
memory "k8s.io/client-go/discovery/cached"
Expand All @@ -14,13 +15,24 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

const (
testKubeConfig = "/tmp/kube-config-collector-e2e-testing"
kubeConfigEnvVar = "KUBECONFIG"
)

type K8sClient struct {
DynamicClient *dynamic.DynamicClient
DiscoveryClient *discovery.DiscoveryClient
Mapper *restmapper.DeferredDiscoveryRESTMapper
}

func NewK8sClient(kubeconfigPath string) (*K8sClient, error) {
func NewK8sClient() (*K8sClient, error) {

kubeconfigPath := testKubeConfig

if kubeConfigFromEnv := os.Getenv(kubeConfigEnvVar); kubeConfigFromEnv != "" {
kubeconfigPath = kubeConfigFromEnv
}

if kubeconfigPath == "" {
return nil, errors.New("Please provide file path to load kubeconfig")
Expand Down
7 changes: 4 additions & 3 deletions internal/k8stest/k8s_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ func CreateCollectorObjects(t *testing.T, client *K8sClient, testID string, mani
tmpl := template.Must(template.New(manifestFile.Name()).ParseFiles(filepath.Join(manifestsDir, manifestFile.Name())))
manifest := &bytes.Buffer{}
require.NoError(t, tmpl.Execute(manifest, map[string]string{
"Name": "otelcol-" + testID,
"HostEndpoint": host,
"TestID": testID,
"Name": "otelcol-" + testID,
"HostEndpoint": host,
"TestID": testID,
"ContainerRegistry": os.Getenv("CONTAINER_REGISTRY"),
}))
obj, err := CreateObject(client, manifest.Bytes())
require.NoErrorf(t, err, "failed to create collector object from manifest %s", manifestFile.Name())
Expand Down
76 changes: 75 additions & 1 deletion internal/testbed/integration/k8senrichment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This is the e2e test for the Collector use-case:

## Requirements to run the tests

- Docker
- Docker or Podman
- Kind

The tests require a running Kind k8s cluster. During the tests,
Expand All @@ -15,3 +15,77 @@ on the k8s cluster with configurations as per the Dynatrace documentation page.
Traces are generated and sent to the Collector, which then
exports to the test where the k8s attributes are asserted on the
received traces.

## Running the tests locally

To execute the tests locally, follow these steps:

### Start a `kind` cluster

To create a new cluster, execute the following command:

```shell
kind create cluster
```
### Build the collector binary

This is done using the `make build` command.
This will build the collector distro, and place the built binary
into the `bin` directory of your local copy of the repository.

**NOTE:** When using an M1 mac, the `make build` command will build the `arm64` binary, which
will not be able to run as a container on a `kind` cluster.
For now, as workaround you will need to use the `make build-all` command to build all binaries and then copy the
`linux_amd64` binary from the `dist` directory into `bin` under the name `dynatrace-otel-collector`.

### Build the container

From the `bin` directory, use `docker` or `podman` to build the collector image, and to load the built
image into the local registry of the `kind` cluster.

Note that this process differs between `podman` and `docker`, as with `podman`, you will have to
load the image using the `image-archive` argument:

**Build and load with `podman`:**
```shell
cd bin
podman buildx build -t dynatrace-otel-collector:e2e-test -f ../Dockerfile . --load
podman save -o dynatrace-otel-collector.tar dynatrace-otel-collector:e2e-test
kind load image-archive dynatrace-otel-collector.tar --name kind
cd ..
```

**Build and load with `docker`:**
```shell
cd bin
docker buildx build -t dynatrace-otel-collector:e2e-test -f ../Dockerfile . --load
docker save -o dynatrace-otel-collector.tar dynatrace-otel-collector:e2e-test
kind load docker-image dynatrace-otel-collector:e2e-test --name kind
cd ..
```

### Running the tests

After the above steps are completed, the E2E tests can be run.

Note that the tests will, by default, use the following `kubeconfig` path: `/tmp/kube-config-collector-e2e-testing`.
This path can be modified by setting the `KUBECONFIG` environment variable (in case you have a local kind cluster with the
kube config located in `~/.kube.config`).
Also, if you are using `podman`, the collector image will be prefixed with `localhost/` within the local
`kind` registry. In this case, you will need to set the `CONTAINER_REGISTRY` to `localhost/`.
When using `docker`, setting the `CONTAINER_REGISTRY` env var is not required.
Below are the commands to execute the `k8senrichment` e2e test:

** Using podman:**
```shell
cd internal/testbed/integration/k8senrichment
KUBECONFIG="~/.kube/config" CONTAINER_REGISTRY="localhost/" go test -v --tags=e2e
```

** Using docker:**
```shell
cd internal/testbed/integration/k8senrichment
KUBECONFIG="/Users/my-user/.kube/config" go test -v --tags=e2e
```


5 changes: 2 additions & 3 deletions internal/testbed/integration/k8senrichment/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ const (
equal = iota
regex
exist
testKubeConfig = "/tmp/kube-config-collector-e2e-testing"
uidRe = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"
uidRe = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}"
)

type expectedValue struct {
Expand All @@ -49,7 +48,7 @@ func newExpectedValue(mode int, value string) *expectedValue {
func TestE2E_ClusterRBAC(t *testing.T) {
testDir := filepath.Join("testdata")

k8sClient, err := k8stest.NewK8sClient(testKubeConfig)
k8sClient, err := k8stest.NewK8sClient()
require.NoError(t, err)

// Create the namespace specific for the test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spec:
command:
- /dynatrace-otel-collector
- --config=/conf/relay.yaml
image: "dynatrace-otel-collector:e2e-test"
image: "{{ .ContainerRegistry }}dynatrace-otel-collector:e2e-test"
ports:
- name: otlp
containerPort: 4317
Expand Down
2 changes: 1 addition & 1 deletion internal/testbed/integration/prometheus/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
func TestE2E_PrometheusNodeExporter(t *testing.T) {
testDir := filepath.Join("testdata")

k8sClient, err := k8stest.NewK8sClient(testKubeConfig)
k8sClient, err := k8stest.NewK8sClient()
require.NoError(t, err)

// Create the namespace specific for the test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spec:
command:
- /dynatrace-otel-collector
- --config=/conf/relay.yaml
image: "dynatrace-otel-collector:e2e-test"
image: "{{ .ContainerRegistry }}dynatrace-otel-collector:e2e-test"
ports:
- name: otlp
containerPort: 4317
Expand Down

0 comments on commit ec42aec

Please sign in to comment.