Skip to content

Commit

Permalink
Clean up Prometheus-based measurements unit testing framework
Browse files Browse the repository at this point in the history
  • Loading branch information
tosi3k committed Jun 21, 2022
1 parent 07a84bc commit 4080bf8
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package common

import (
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -82,14 +81,9 @@ func TestCiliumEndpointPropagationDelayMeasurement(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f, err := createRulesFile("../../prometheus/manifests/prometheus-rules.yaml")
executor, err := executors.NewPromqlExecutor(fmt.Sprintf("testdata/cilium_endpoint_propagation_delay/%s", tc.testSeriesFile))
if err != nil {
t.Fatalf("Failed to create rules file: %v", err)
}
defer os.Remove(f.Name())
executor, err := executors.NewPromqlExecutor(fmt.Sprintf("slos/testdata/cilium_endpoint_propagation_delay/%s", tc.testSeriesFile), f.Name())
if err != nil {
t.Fatalf("Failed to create PromQL executor: %v", err)
t.Fatalf("failed to create PromQL executor: %v", err)
}
defer executor.Close()
gatherer := &cepPropagationDelayGatherer{}
Expand Down
61 changes: 2 additions & 59 deletions clusterloader2/pkg/measurement/common/container_restarts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,68 +18,16 @@ package common

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"

"k8s.io/perf-tests/clusterloader2/pkg/measurement"
"k8s.io/perf-tests/clusterloader2/pkg/measurement/common/executors"
)

type rule struct {
Expr string `yaml:"expr"`
Record string `yaml:"record"`
Labels struct {
Quantile string `yaml:"quantile"`
} `yaml:"labels"`
}

type group struct {
Name string `yaml:"name"`
Rules []rule `yaml:"rules"`
}

//prometheusRuleManifest mimics the structure of PrometheusRule object used by prometheus operator
//https://github.com/prometheus-operator/prometheus-operator/blob/main/pkg/apis/monitoring/v1/types.go#L1393
type prometheusRuleManifest struct {
Spec struct {
Groups []group `yaml:"groups"`
} `yaml:"spec"`
}

func createRulesFile(rulesManifestFile string) (*os.File, error) {
r, err := ioutil.ReadFile(rulesManifestFile)
if err != nil {
return nil, err
}

rulesManifest := new(prometheusRuleManifest)
err = yaml.Unmarshal(r, rulesManifest)
if err != nil {
return nil, err
}

tempFile, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
b, err := yaml.Marshal(rulesManifest.Spec)
if err != nil {
return nil, err
}

_, err = tempFile.Write(b)
if err != nil {
return nil, err
}
return tempFile, nil
}

func TestContainerRestartsMeasurement(t *testing.T) {
splitter := func(yamlLines []string) string {
return strings.Join(yamlLines, "\n")
Expand Down Expand Up @@ -166,14 +114,9 @@ func TestContainerRestartsMeasurement(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f, err := createRulesFile("../../prometheus/manifests/prometheus-rules.yaml")
if err != nil {
t.Fatalf("failed to create rules file: %v", err)
}
defer os.Remove(f.Name())
executor, err := executors.NewPromqlExecutor(fmt.Sprintf("slos/testdata/container_restarts/%s", tc.testSeriesFile), f.Name())
executor, err := executors.NewPromqlExecutor(fmt.Sprintf("testdata/container_restarts/%s", tc.testSeriesFile))
if err != nil {
t.Fatalf("failed to create rules file: %v", err)
t.Fatalf("failed to create PromQL executor: %v", err)
}
defer executor.Close()
gatherer := &containerRestartsGatherer{}
Expand Down
16 changes: 13 additions & 3 deletions clusterloader2/pkg/measurement/common/executors/promql_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"io/ioutil"
"os"
"time"

"github.com/prometheus/common/model"
Expand All @@ -28,6 +29,10 @@ import (
"gopkg.in/yaml.v2"
)

const (
pathToPrometheusRules = "$GOPATH/src/k8s.io/perf-tests/clusterloader2/pkg/prometheus/manifests/prometheus-rules.yaml"
)

func toModelSample(s promql.Sample) *model.Sample {
ls := make(model.Metric)
for _, l := range s.Metric {
Expand Down Expand Up @@ -82,8 +87,7 @@ type PromqlExecutor struct {

// NewPromqlExecutor creates a new executor with time series and rules loaded from file
// Samples loaded from test file starts at time.Time.UTC(0,0)
func NewPromqlExecutor(timeSeriesFile, ruleFile string) (*PromqlExecutor, error) {

func NewPromqlExecutor(timeSeriesFile string) (*PromqlExecutor, error) {
//Load time series from file
f, err := loadFromFile(timeSeriesFile)
if err != nil {
Expand All @@ -109,7 +113,13 @@ func NewPromqlExecutor(timeSeriesFile, ruleFile string) (*PromqlExecutor, error)
Logger: nil,
}
m := rules.NewManager(opts)
groupsMap, ers := m.LoadGroups(interval, nil, ruleFile)

rulesFile, err := createRulesFile(os.ExpandEnv(pathToPrometheusRules))
if err != nil {
return nil, fmt.Errorf("could not create rules file: %v", err)
}
defer os.Remove(rulesFile.Name())
groupsMap, ers := m.LoadGroups(interval, nil, rulesFile.Name())
if ers != nil {
return nil, fmt.Errorf("could not load rules file: %v", ers)
}
Expand Down
73 changes: 73 additions & 0 deletions clusterloader2/pkg/measurement/common/executors/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2022 The Kubernetes 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.
*/

package executors

import (
"io/ioutil"
"os"

"gopkg.in/yaml.v2"
)

type rule struct {
Expr string `yaml:"expr"`
Record string `yaml:"record"`
Labels struct {
Quantile string `yaml:"quantile"`
} `yaml:"labels"`
}

type group struct {
Name string `yaml:"name"`
Rules []rule `yaml:"rules"`
}

//prometheusRuleManifest mimics the structure of PrometheusRule object used by prometheus operator
//https://github.com/prometheus-operator/prometheus-operator/blob/main/pkg/apis/monitoring/v1/types.go#L1393
type prometheusRuleManifest struct {
Spec struct {
Groups []group `yaml:"groups"`
} `yaml:"spec"`
}

func createRulesFile(rulesManifestFile string) (*os.File, error) {
r, err := ioutil.ReadFile(rulesManifestFile)
if err != nil {
return nil, err
}

rulesManifest := new(prometheusRuleManifest)
err = yaml.Unmarshal(r, rulesManifest)
if err != nil {
return nil, err
}

tempFile, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
b, err := yaml.Marshal(rulesManifest.Spec)
if err != nil {
return nil, err
}

_, err = tempFile.Write(b)
if err != nil {
return nil, err
}
return tempFile, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
"time"

"github.com/prometheus/common/model"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"k8s.io/klog"
"k8s.io/perf-tests/clusterloader2/pkg/errors"
"k8s.io/perf-tests/clusterloader2/pkg/measurement"
Expand Down Expand Up @@ -91,55 +88,6 @@ type fakeQueryExecutor struct {
samples []*sample
}

type rule struct {
Expr string `yaml:"expr"`
Record string `yaml:"record"`
Labels struct {
Quantile string `yaml:"quantile"`
} `yaml:"labels"`
}

type group struct {
Name string `yaml:"name"`
Rules []rule `yaml:"rules"`
}

//prometheusRuleManifest mimics the structure of PrometheusRule object used by prometheus operator
//https://github.com/prometheus-operator/prometheus-operator/blob/main/pkg/apis/monitoring/v1/types.go#L1393
type prometheusRuleManifest struct {
Spec struct {
Groups []group `yaml:"groups"`
} `yaml:"spec"`
}

func createRulesFile(rulesManifestFile string) (*os.File, error) {
r, err := ioutil.ReadFile(rulesManifestFile)
if err != nil {
return nil, err
}

rulesManifest := new(prometheusRuleManifest)
err = yaml.Unmarshal(r, rulesManifest)
if err != nil {
return nil, err
}

tempFile, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
b, err := yaml.Marshal(rulesManifest.Spec)
if err != nil {
return nil, err
}

_, err = tempFile.Write(b)
if err != nil {
return nil, err
}
return tempFile, nil
}

func (ex *fakeQueryExecutor) Query(query string, queryTime time.Time) ([]*model.Sample, error) {
samples := make([]*model.Sample, 0)
for _, s := range ex.samples {
Expand Down Expand Up @@ -268,14 +216,9 @@ func TestAPIResponsivenessSLOFailures(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f, err := createRulesFile("../../../prometheus/manifests/prometheus-rules.yaml")
if err != nil {
t.Fatalf("failed to create ruels file: %v", err)
}
defer os.Remove(f.Name())
executor, err := executors.NewPromqlExecutor(fmt.Sprintf("testdata/%s", tc.testSeriesFile), f.Name())
executor, err := executors.NewPromqlExecutor(fmt.Sprintf("../testdata/api_responsiveness_prometheus/%s", tc.testSeriesFile))
if err != nil {
t.Fatalf("failed to create ruels file: %v", err)
t.Fatalf("failed to create PromQL executor: %v", err)
}
defer executor.Close()
gatherer := &apiResponsivenessGatherer{}
Expand Down

0 comments on commit 4080bf8

Please sign in to comment.