Skip to content

Commit

Permalink
[m3comparator] adaption of Prometheus testdata to validate m3query (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gediminasgu authored Jun 11, 2020
1 parent 0d01d62 commit 2c4f65c
Show file tree
Hide file tree
Showing 16 changed files with 3,064 additions and 1 deletion.
3 changes: 3 additions & 0 deletions scripts/comparator/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,6 @@ $comparator -input=$QUERY_FILE \
-e=$END \
-comparator=$COMPARATOR_WRITE \
-regressionDir=$REGRESSION_DIR

# Run PromQL testdata tests
go test -v -timeout 300s -tags=compatibility -count=1 github.com/m3db/m3/src/query/test/
2 changes: 1 addition & 1 deletion src/cmd/services/m3comparator/main/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (q *querier) FetchCompressed(
return m3.SeriesFetchResult{}, noop, err
}
}

if iters == nil || iters.Len() == 0 {
randomSeries, ignoreFilter, err = q.generateRandomSeries(query)
if err != nil {
Expand Down
74 changes: 74 additions & 0 deletions src/query/test/m3comparator_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package test

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)

type m3comparatorClient struct {
host string
port int
}

func newM3ComparatorClient(host string, port int) *m3comparatorClient {
return &m3comparatorClient{
host: host,
port: port,
}
}

func (c *m3comparatorClient) clear() error {
comparatorURL := fmt.Sprintf("http://%s:%d", c.host, c.port)
req, err := http.NewRequest(http.MethodDelete, comparatorURL, nil)
if err != nil {
return err
}

_, err = http.DefaultClient.Do(req)
if err != nil {
return err
}
return nil
}

func (c *m3comparatorClient) load(data []byte) error {
comparatorURL := fmt.Sprintf("http://%s:%d", c.host, c.port)
resp, err := http.Post(comparatorURL, "application/json", bytes.NewReader(data))
if err != nil {
return fmt.Errorf("got error loading data to comparator %v", err)
}
defer resp.Body.Close()

if resp.StatusCode/200 == 1 {
return nil
}

bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("load status code %d. Error: %v", resp.StatusCode, err)
}

return fmt.Errorf("load status code %d. Response: %s", resp.StatusCode, string(bodyBytes))
}
67 changes: 67 additions & 0 deletions src/query/test/m3query_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package test

import (
"fmt"
"io/ioutil"
"net/http"
"time"

"github.com/pkg/errors"
)

type m3queryClient struct {
host string
port int
}

func newM3QueryClient(host string, port int) *m3queryClient {
return &m3queryClient{
host: host,
port: port,
}
}

func (c *m3queryClient) query(expr string, t time.Time) ([]byte, error) {
url := fmt.Sprintf("http://%s:%d/m3query/api/v1/query", c.host, c.port)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}

q := req.URL.Query()
q.Add("query", expr)
q.Add("time", fmt.Sprint(t.Unix()))
req.URL.RawQuery = q.Encode()
resp, err := http.DefaultClient.Do(req)

if err != nil {
return nil, errors.Wrapf(err, "error evaluating query %s", expr)
}

defer resp.Body.Close()
if resp.StatusCode/200 != 1 {
return nil, fmt.Errorf("invalid status %+v received sending query: %+v", resp.StatusCode, req)
}

return ioutil.ReadAll(resp.Body)
}
59 changes: 59 additions & 0 deletions src/query/test/promql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// +build compatibility

// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Copyright 2015 The Prometheus 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.

// This code was taken from prometheus repo: https://github.com/prometheus/prometheus/blob/master/promql/promql_test.go

package test

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestEvaluations(t *testing.T) {
files, err := filepath.Glob("testdata/*.test")
require.NoError(t, err)

for _, fn := range files {
test, err := newTestFromFile(t, fn)
require.NoError(t, err)

require.NoError(t, test.Run())

test.Close()
}
}
Loading

0 comments on commit 2c4f65c

Please sign in to comment.