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

[e2e] check all default collector metrics are available #1616

Merged
merged 2 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 0 additions & 9 deletions tests/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ SUDO=${SUDO:-}
OS=$(uname -s | awk '{print tolower($0)}')
OS=${OS:-linux}

EXCLUDED_RESOURCE_REGEX="verticalpodautoscaler"

function finish() {
echo "calling cleanup function"
# kill kubectl proxy in background
Expand Down Expand Up @@ -165,13 +163,6 @@ mkdir -p ${KUBE_STATE_METRICS_LOG_DIR}
echo "access kube-state-metrics metrics endpoint"
curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy/metrics" >${KUBE_STATE_METRICS_LOG_DIR}/metrics

resources=$(find internal/store/ -maxdepth 1 -name "*.go" -not -name "*_test.go" -not -name "builder.go" -not -name "testutils.go" -not -name "utils.go" -print0 | xargs -0 -n1 basename | awk -F. '{print $1}'| grep -v "$EXCLUDED_RESOURCE_REGEX")
echo "available resources: $resources"
for resource in ${resources}; do
echo "checking that kube_${resource}* metrics exists"
grep "^kube_${resource}_" ${KUBE_STATE_METRICS_LOG_DIR}/metrics
done

KUBE_STATE_METRICS_STATUS=$(curl -s "http://localhost:8001/api/v1/namespaces/kube-system/services/kube-state-metrics:http-metrics/proxy/healthz")
if [[ "${KUBE_STATE_METRICS_STATUS}" == "OK" ]]; then
echo "kube-state-metrics is still running after accessing metrics endpoint"
Expand Down
53 changes: 53 additions & 0 deletions tests/e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"regexp"
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -242,3 +244,54 @@ func prettyPrintCounter(name string, metric *dto.Metric) string {
}
return fmt.Sprintf("%s{%s} %d", name, strings.Join(labelStrings, ","), int(metric.GetCounter().GetValue()))
}

func TestDefaultCollectorMetricsAvailable(t *testing.T) {
buf := &bytes.Buffer{}

err := framework.KsmClient.Metrics(buf)
if err != nil {
t.Fatalf("failed to get metrics from kube-state-metrics: %v", err)
}

resources := map[string]struct{}{}
files, err := ioutil.ReadDir("../../internal/store/")
if err != nil {
t.Fatalf("failed to read dir to get all resouces name: %v", err)
}

re := regexp.MustCompile(`^([a-z]*).go$`)
olivierlemasle marked this conversation as resolved.
Show resolved Hide resolved
for _, file := range files {
params := re.FindStringSubmatch(file.Name())
if len(params) != 2 {
continue
}
if params[1] == "builder" || params[1] == "utils" || params[1] == "testutils" || params[1] == "verticalpodautoscaler" {
olivierlemasle marked this conversation as resolved.
Show resolved Hide resolved
continue
}
resources[params[1]] = struct{}{}
}

re = regexp.MustCompile(`^kube_([a-z]*)_`)
olivierlemasle marked this conversation as resolved.
Show resolved Hide resolved
scanner := bufio.NewScanner(buf)
for scanner.Scan() {
params := re.FindStringSubmatch(scanner.Text())
if len(params) != 2 {
continue
}
delete(resources, params[1])
}

err = scanner.Err()
if err != nil {
t.Fatalf("failed to scan metrics: %v", err)
}

if len(resources) != 0 {
s := []string{}
for k := range resources {
s = append(s, k)
}
sort.Strings(s)
t.Fatalf("failed to find metrics of resources: %s", strings.Join(s, ", "))
}
}