Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
e2e: Invoke monitoring tests from single location
Browse files Browse the repository at this point in the history
This commit adds a test function which initialises the prometheus client
and then passes it to the prometheus tests that need it.

This approach is preferable over creating a new port-forward for every
test is because initialising a port forward takes around 3-4 seconds and
this can significantly increase the total running time of tests. Reusing
port forward connection and prometheus client saves time for test.

And having a single place of creating port forward and client
intialisation ensures that we close that connection gracefully.

Signed-off-by: Suraj Deshmukh <suraj@kinvolk.io>
  • Loading branch information
surajssd committed Mar 19, 2020
1 parent c8b0a0b commit 9a3b1b7
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/monitoring/monitoring_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2020 The Lokomotive 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.

// +build aws packet
// +build poste2e

package monitoring

import (
"fmt"
"testing"

testutil "github.com/kinvolk/lokomotive/test/components/util"

"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
)

func TestPrometheus(t *testing.T) {
const prometheusPodPort = 9090

p := &testutil.PortForwardInfo{
PodName: "prometheus-prometheus-operator-prometheus-0",
Namespace: "monitoring",
PodPort: prometheusPodPort,
}

p.PortForward(t)
defer p.CloseChan()
p.WaitUntilForwardingAvailable(t)

promClient, err := api.NewClient(api.Config{
Address: fmt.Sprintf("http://127.0.0.1:%d", p.LocalPort),
})
if err != nil {
t.Fatalf("Error creating client: %v", err)
}

v1api := v1.NewAPI(promClient)

// Add to the list all the prometheus tests that should be invoked one at a time
tests := []struct {
Name string
Func func(*testing.T, v1.API)
}{
{
Name: "ControlPlaneMetrics",
Func: testControlPlanePrometheusMetrics,
},
}

// Invoke the test functions passing them the test object and the prometheus client.
for _, test := range tests {
test := test
t.Run(test.Name, func(t *testing.T) {
test.Func(t, v1api)
})
}
}

0 comments on commit 9a3b1b7

Please sign in to comment.