Skip to content

Commit

Permalink
Fix non promql test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh Kumar committed Jan 9, 2025
1 parent 037b4ac commit 129536f
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 110 deletions.
36 changes: 20 additions & 16 deletions pkg/alertbackfill/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/prometheus/common/model"
"github.com/prometheus/common/promslog"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/rules"
)
Expand All @@ -21,43 +22,46 @@ func TestFindGroupAndAlert(t *testing.T) {
"testalert", // name
nil, // expression
time.Hour, // hold
0,
labels.Labels{
labels.Label{"labelkey", "labelvalue"},
}, // labels
nil, // annotations
nil, // externalLabels
"", // externalURL
false, // restored
nil, // logger
nil, // annotations
nil, // externalLabels
"", // externalURL
false, // restored
promslog.NewNopLogger(), // logger
),
rules.NewAlertingRule(
"alertWithLabels", // name
nil, // expression
time.Hour, // hold
0,
labels.Labels{
labels.Label{"labelkey", "labelvalue"},
}, // labels
nil, // annotations
nil, // externalLabels
"", // externalURL
false, // restored
nil, // logger
nil, // annotations
nil, // externalLabels
"", // externalURL
false, // restored
promslog.NewNopLogger(), // logger
),
rules.NewAlertingRule(
"alertWithLabels", // name
nil, // expression
time.Hour, // hold
0,
labels.Labels{
labels.Label{"labelkey", "labelvalue2"},
}, // labels
nil, // annotations
nil, // externalLabels
"", // externalURL
false, // restored
nil, // logger
nil, // annotations
nil, // externalLabels
"", // externalURL
false, // restored
promslog.NewNopLogger(), // logger
),
},
Opts: &rules.ManagerOptions{},
Opts: &rules.ManagerOptions{Logger: promslog.NewNopLogger()},
}),
}

Expand Down
25 changes: 22 additions & 3 deletions pkg/promclient/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/promqltest"
"github.com/prometheus/prometheus/storage"
)

func TestEngineAPI(t *testing.T) {
Expand All @@ -18,15 +20,32 @@ func TestEngineAPI(t *testing.T) {
t.Fatal(err)
}

test, err := promql.NewTest(t, string(content))
test, err := promqltest.NewTest(t, string(content))
if err != nil {
t.Fatal(err)
}
if err := test.Run(); err != nil {

engineOpts := promql.EngineOpts{
Logger: nil,
Reg: nil,
MaxSamples: 50000000,
Timeout: 10 * time.Minute,
ActiveQueryTracker: nil,
LookbackDelta: 0,
NoStepSubqueryIntervalFn: func(int64) int64 { return (1 * time.Minute).Milliseconds() },
EnableAtModifier: true,
EnableNegativeOffset: false,
EnablePerStepStats: false,
EnableDelayedNameRemoval: false,
}

queryEngine := promql.NewEngine(engineOpts)

if err := test.Run(queryEngine); err != nil {
t.Fatal(err)
}

api, err := NewEngineAPI(test.QueryEngine(), test.Queryable())
api, err := NewEngineAPI(queryEngine, test.Storage().(storage.SampleAndChunkQueryable))
if err != nil {
t.Fatal(err)
}
Expand Down
195 changes: 113 additions & 82 deletions pkg/promclient/test_server.go
Original file line number Diff line number Diff line change
@@ -1,87 +1,118 @@
package promclient

import (
"net"
"net/http"
"os"
"testing"
"time"

"github.com/prometheus/client_golang/api"
clientv1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/route"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/promqltest"
"github.com/prometheus/prometheus/storage"
v1 "github.com/prometheus/prometheus/web/api/v1"
)

// CreateTestServer simply will create a test HTTP server on the path defined and return
// an API client, a clode method, and any error when creating

//TODO Fix it later
//func CreateTestServer(t *testing.T, path string) (API, func(), error) {
// var close func()
// content, err := os.ReadFile(path)
// if err != nil {
// return nil, close, err
// }
//
// test, err := promqltest.NewTestEngine(t, string(content))
// if err != nil {
// return nil, nil, err
// }
// close = test.Close
//
// // Load the data
// if err := test.Run(); err != nil {
// return nil, close, err
// }
//
// ln, err := net.Listen("tcp", "")
// if err != nil {
// return nil, close, err
// }
//
// // Start up API server for engine
// cfgFunc := func() config.Config { return config.DefaultConfig }
// // Return 503 until ready (for us there isn't much startup, so this might not need to be implemented
// readyFunc := func(f http.HandlerFunc) http.HandlerFunc { return f }
//
// apiRouter := route.New()
// webv1.NewAPI(
// test.QueryEngine(), // Query Engine
// test.Storage().(storage.SampleAndChunkQueryable), // SampleAndChunkQueryable
// nil, //appendable
// nil, // exemplarQueryable
// nil, //factoryTr
// nil, //factoryAr
// nil,
// cfgFunc,
// nil, // flags
// webv1.GlobalURLOptions{
// ListenAddress: ln.Addr().String(),
// Host: "localhost",
// Scheme: "http",
// }, // global URL options
// readyFunc, // ready
// nil, // local storage
// "", // tsdb dir
// false, // enable admin API
// nil, // logger
// nil, // FactoryRr
// 50000000, // RemoteReadSampleLimit
// 1000, // RemoteReadConcurrencyLimit
// 1048576, // RemoteReadBytesInFrame
// false, // isAgent
// nil, // CORSOrigin
// nil, // runtimeInfo
// nil, // buildInfo
// nil,
// nil,
// nil, // gatherer
// nil, // registerer
// nil, // statsRenderer
// false,
// nil,
// false,
// ).Register(apiRouter.WithPrefix("/api/v1"))
//
// srv := &http.Server{Handler: apiRouter}
// go srv.Serve(ln) // TODO: cancel/stop ability
// close = func() {
// //test.Close()
// srv.Close()
// }
//
// client, err := api.NewClient(api.Config{Address: "http://" + ln.Addr().String()})
// if err != nil {
// return nil, close, err
// }
//
// return &PromAPIV1{clientv1.NewAPI(client)}, close, nil
//}
func CreateTestServer(t *testing.T, path string) (API, func(), error) {
var close func()
content, err := os.ReadFile(path)
if err != nil {
return nil, close, err
}

test, err := promqltest.NewTest(t, string(content))
if err != nil {
return nil, nil, err
}
close = test.Close

engineOpts := promql.EngineOpts{
Logger: nil,
Reg: nil,
MaxSamples: 50000000,
Timeout: 10 * time.Minute,
ActiveQueryTracker: nil,
LookbackDelta: 0,
NoStepSubqueryIntervalFn: func(int64) int64 { return (1 * time.Minute).Milliseconds() },
EnableAtModifier: true,
EnableNegativeOffset: false,
EnablePerStepStats: false,
EnableDelayedNameRemoval: false,
}

queryEngine := promql.NewEngine(engineOpts)

// Load the data
if err := test.Run(queryEngine); err != nil {
return nil, close, err
}

ln, err := net.Listen("tcp", "")
if err != nil {
return nil, close, err
}

// Start up API server for engine
cfgFunc := func() config.Config { return config.DefaultConfig }
// Return 503 until ready (for us there isn't much startup, so this might not need to be implemented
readyFunc := func(f http.HandlerFunc) http.HandlerFunc { return f }
apiRouter := route.New()
v1.NewAPI(
queryEngine, // Query Engine
test.Storage().(storage.SampleAndChunkQueryable), // SampleAndChunkQueryable
nil, //appendable
nil, // exemplarQueryable
nil, //factoryTr
nil, //factoryAr
nil,
cfgFunc,
nil, // flags
v1.GlobalURLOptions{
ListenAddress: ln.Addr().String(),
Host: "localhost",
Scheme: "http",
}, // global URL options
readyFunc, // ready
nil, // local storage
"", // tsdb dir
false, // enable admin API
nil, // logger
nil, // FactoryRr
50000000, // RemoteReadSampleLimit
1000, // RemoteReadConcurrencyLimit
1048576, // RemoteReadBytesInFrame
false, // isAgent
nil, // CORSOrigin
nil, // runtimeInfo
nil, // buildInfo
nil,
nil,
nil, // gatherer
nil, // registerer
nil, // statsRenderer
false,
nil,
false,
).Register(apiRouter.WithPrefix("/api/v1"))

srv := &http.Server{Handler: apiRouter}
go srv.Serve(ln) // TODO: cancel/stop ability
close = func() {
//test.Close()
srv.Close()
}

client, err := api.NewClient(api.Config{Address: "http://" + ln.Addr().String()})
if err != nil {
return nil, close, err
}

return &PromAPIV1{clientv1.NewAPI(client)}, close, nil
}
2 changes: 1 addition & 1 deletion pkg/promhttputil/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func WarningsConvert(ws v1.Warnings) annotations.Annotations {
w := make(annotations.Annotations, len(ws))
for i, item := range ws {
w[string(i)] = errors.New(item)
w[fmt.Sprint(i)] = errors.New(item)
}
return w
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/proxystorage/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestNodeReplacer(t *testing.T) {
// count_values is both a query downstream *AND* a tree replacement
{
in: "count_values(\"label\", foo)",
out: "sum by(label) ()",
out: "sum by (label) ()",
queries: []string{
"count_values(\"label\", foo) @ 10000",
},
Expand Down
14 changes: 7 additions & 7 deletions pkg/remote/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestExternalLabelsQuerierSelect(t *testing.T) {
externalLabels: model.LabelSet{"region": "europe"},
}
want := newSeriesSetFilter(mockSeriesSet{}, q.externalLabels)
have := q.Select(false, nil, matchers...)
have := q.Select(context.TODO(), false, nil, matchers...)
if err := have.Err(); err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func (m mockSeriesSet) Err() error {
return nil
}

func (mockQuerier) Select(bool, *storage.SelectHints, ...*labels.Matcher) storage.SeriesSet {
func (mockQuerier) Select(context.Context, bool, *storage.SelectHints, ...*labels.Matcher) storage.SeriesSet {
return mockSeriesSet{}
}

Expand Down Expand Up @@ -199,13 +199,13 @@ func TestPreferLocalStorageFilter(t *testing.T) {

for i, test := range tests {
f := PreferLocalStorageFilter(
storage.QueryableFunc(func(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
storage.QueryableFunc(func(mint, maxt int64) (storage.Querier, error) {
return mockQuerier{ctx: ctx, mint: mint, maxt: maxt}, nil
}),
func() (int64, error) { return test.localStartTime, nil },
)

q, err := f.Querier(ctx, test.mint, test.maxt)
q, err := f.Querier(test.mint, test.maxt)
if err != nil {
t.Fatal(err)
}
Expand All @@ -220,7 +220,7 @@ func TestRequiredMatchersFilter(t *testing.T) {
ctx := context.Background()

f := RequiredMatchersFilter(
storage.QueryableFunc(func(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
storage.QueryableFunc(func(mint, maxt int64) (storage.Querier, error) {
return mockQuerier{ctx: ctx, mint: mint, maxt: maxt}, nil
}),
[]*labels.Matcher{mustNewLabelMatcher(labels.MatchEqual, "special", "label")},
Expand All @@ -230,7 +230,7 @@ func TestRequiredMatchersFilter(t *testing.T) {
Querier: mockQuerier{ctx: ctx, mint: 0, maxt: 50},
requiredMatchers: []*labels.Matcher{mustNewLabelMatcher(labels.MatchEqual, "special", "label")},
}
have, err := f.Querier(ctx, 0, 50)
have, err := f.Querier(0, 50)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestRequiredLabelsQuerierSelect(t *testing.T) {
requiredMatchers: test.requiredMatchers,
}

have := q.Select(false, nil, test.matchers...)
have := q.Select(context.TODO(), false, nil, test.matchers...)
if err := have.Err(); err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 129536f

Please sign in to comment.