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

Rules API: Add e2e test #198

Merged
merged 21 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
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
73 changes: 73 additions & 0 deletions test/e2e/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type testType string

const (
metrics testType = "metrics"
rules testType = "rules"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make format should fix these rogue whitespaces 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weirdly if I run make format locally I get:

jessica@cupsofwonder:~/workspace/api|rules-add-e2e ⇒  make format
/home/jessica/go/bin/golangci-lint-v1.21.0 run --fix --enable-all -c .golangci.yml
WARN [runner/nolint] Found unknown linters in //nolint directives: cyclop, exhaustivestruct, goerr113, gomnd, intefacer, paralleltest, testpackage 

so somehow it did not give any more output than that and also did not change any of the files.

I then tried to run with my local golangci-lint (with the latest version - 1.43.0) and it found some issues in the code, so I think we may tackle this separately? I wonder if would be good to also update the golangci-lint version (with the latest version I get also some warnings of linters that are deprecated and we use them in the nolinter directive (e.g. WARN [runner/nolint] Found unknown linters in //nolint directives: intefacer)

Should I create a separate issue for that? Maybe makes sense to also link to this issue @matej-g already opened: #212
For now I run manually gofmt to this config file, so the whitespaces should be fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, shouldn't the lint step fail?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be fixed now 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean the whitespaces or make format?
To fix the whitespaces I ran gofmt manually, but I thought would be fixed by running make format instead (which does not seem to be the case) :(

logs testType = "logs"
tenants testType = "tenants"
interactive testType = "interactive"
Expand All @@ -30,6 +31,7 @@ const (
configsContainerPath = dockerLocalSharedDir + "/" + configSharedDir

envMetricsName = "e2e_metrics_read_write"
envRulesAPIName = "e2e_rules_api"
envLogsName = "e2e_logs_read_write_tail"
envTenantsName = "e2e_tenants"
envInteractive = "e2e_interactive"
Expand Down Expand Up @@ -166,3 +168,74 @@ func createDexYAML(
)
testutil.Ok(t, err)
}

const rulesYAMLTpl = `
type: S3
config:
bucket: %s
endpoint: %s
access_key: %s
insecure: true
secret_key: %s
`

func createRulesYAML(
t *testing.T,
e e2e.Environment,
bucket, endpoint, accessKey, secretKey string,
) {
yamlContent := []byte(fmt.Sprintf(
rulesYAMLTpl,
bucket,
endpoint,
accessKey,
secretKey,
))

err := ioutil.WriteFile(
filepath.Join(e.SharedDir(), configSharedDir, "rules-objstore.yaml"),
yamlContent,
os.FileMode(0755),
)
testutil.Ok(t, err)
}

const recordingRuleYamlTpl = `
groups:
- name: example
rules:
- record: job:http_inprogress_requests:sum
expr: sum by (job) (http_inprogress_requests)
`

const alertingRuleYamlTpl = `
groups:
- name: example
rules:
- alert: HighRequestLatency
expr: job:request_latency_seconds:mean5m{job="myjob"} > 0.5
for: 10m
labels:
severity: page
annotations:
summary: High request latency
`
const recordAndAlertingRulesYamlTpl = `
groups:
- name: node_rules
rules:
- record: job:up:avg
expr: avg without(instance)(up{job="node"})
- alert: ManyInstancesDown
expr: job:up:avg{job="node"} < 0.5
`

const invalidRulesYamlTpl = `
invalid:
- name: testing
invalid_rules:
- rule1: job:up:avg
expr: avg without(instance)(up{job="node"})
- rule2: ManyInstancesDown
expr: job:up:avg{job="node"} < 0.5
`
12 changes: 12 additions & 0 deletions test/e2e/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func getContainerName(t *testing.T, tt testType, serviceName string) string {
return envLogsName + "-" + serviceName
case metrics:
return envMetricsName + "-" + serviceName
case rules:
return envRulesAPIName + "-" + serviceName
case tenants:
return envTenantsName + "-" + serviceName
case interactive:
Expand Down Expand Up @@ -116,3 +118,13 @@ func assertResponse(t *testing.T, response string, expected string) {
fmt.Sprintf("failed to assert that the response '%s' contains '%s'", response, expected),
)
}

type tokenRoundTripper struct {
rt http.RoundTripper
token string
}

func (rt *tokenRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Add("Authorization", "bearer "+rt.token)
return rt.rt.RoundTrip(r)
}
10 changes: 0 additions & 10 deletions test/e2e/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,3 @@ func TestMetricsReadAndWrite(t *testing.T) {
})
})
}

type tokenRoundTripper struct {
rt http.RoundTripper
token string
}

func (rt *tokenRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Add("Authorization", "bearer "+rt.token)
return rt.rt.RoundTrip(r)
}
170 changes: 170 additions & 0 deletions test/e2e/rules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// +build integration

package e2e

import (
"bytes"
"io/ioutil"
"net/http"
"testing"

"github.com/efficientgo/e2e"
"github.com/efficientgo/tools/core/pkg/testutil"
)

func TestRulesAPI(t *testing.T) {
t.Parallel()

e, err := e2e.NewDockerEnvironment(envRulesAPIName)
testutil.Ok(t, err)
t.Cleanup(e.Close)

prepareConfigsAndCerts(t, rules, e)
_, token, rateLimiterAddr := startBaseServices(t, e, rules)
rulesEndpoint := startServicesForRules(t, e)

api, err := newObservatoriumAPIService(
e,
withRulesEndpoint("http://"+rulesEndpoint),
withRateLimiter(rateLimiterAddr),
)
testutil.Ok(t, err)
testutil.Ok(t, e2e.StartAndWaitReady(api))

rulesEndpointURL := "https://" + api.Endpoint("https") + "/api/metrics/v1/test-oidc/api/v1/rules/raw"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the test-oidc tenant name defined anywhere else? I just imagine if we change that we'll unexpectedly break this test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point..we define it manually everywhere in the tests: https://github.com/observatorium/api/search?q=test-oidc
maybe makes sense to define it as a constant in configs.go and use its value? I could open another pr for that or just already change it here (?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added as a constant here: 0b60005

tr := &http.Transport{
TLSClientConfig: getTLSClientConfig(t, e),
}

client := &http.Client{
Transport: &tokenRoundTripper{rt: tr, token: token},
}

t.Run("get-put-recording-rules", func(t *testing.T) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get-put-recording-rules this is kind of confusing to me as to what the test is aiming to do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed to read-write-recording-rules (also to the further ones) - hopefully now is clearer?
It basically tests the reading (GET) / writing (PUT) of rules via the rules API defined in observatorium/api going through rules-objectstore https://github.com/observatorium/api/pull/198/files#diff-368252fd7020798243fc9b3793b03207f23440bf25424123e38b77eabf42c3d3R28

// Try to list rules
r, err := http.NewRequest(
http.MethodGet,
rulesEndpointURL,
nil,
)
testutil.Ok(t, err)

res, err := client.Do(r)
testutil.Ok(t, err)
testutil.Equals(t, http.StatusNotFound, res.StatusCode)

// Set a file containing a recording rule
recordingRule := []byte(recordingRuleYamlTpl)
r, err = http.NewRequest(
http.MethodPut,
rulesEndpointURL,
bytes.NewReader(recordingRule),
)
testutil.Ok(t, err)

res, err = client.Do(r)
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, res.StatusCode)

// Check if recording rule is listed
r, err = http.NewRequest(
http.MethodGet,
rulesEndpointURL,
nil,
)
testutil.Ok(t, err)

res, err = client.Do(r)
defer res.Body.Close()

testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, res.StatusCode)

body, err := ioutil.ReadAll(res.Body)
bodyStr := string(body)

assertResponse(t, bodyStr, "sum by (job) (http_inprogress_requests)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to check for tenant labels here? 🙂
With something like: assertResponse(t, bodyStr, "tenant_id: 1610b0c3-c509-4592-a256-a1871353dbfa") (id taken from here).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea! added here: 7d50a67

})

t.Run("get-put-alerting-rules", func(t *testing.T) {
// Set a file containing an alerting rule
alertingRule := []byte(alertingRuleYamlTpl)
r, err := http.NewRequest(
http.MethodPut,
rulesEndpointURL,
bytes.NewReader(alertingRule),
)
testutil.Ok(t, err)

res, err := client.Do(r)
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, res.StatusCode)

// Check if the alerting rule is listed
r, err = http.NewRequest(
http.MethodGet,
rulesEndpointURL,
nil,
)
testutil.Ok(t, err)

res, err = client.Do(r)
defer res.Body.Close()

testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, res.StatusCode)

body, err := ioutil.ReadAll(res.Body)
bodyStr := string(body)
assertResponse(t, bodyStr, "alert: HighRequestLatency")
})

t.Run("get-put-recording-alerting-rules", func(t *testing.T) {
// Set a file containing both recording and alerting rules
recordAndAlertingRules := []byte(recordAndAlertingRulesYamlTpl)
r, err := http.NewRequest(
http.MethodPut,
rulesEndpointURL,
bytes.NewReader(recordAndAlertingRules),
)
testutil.Ok(t, err)

res, err := client.Do(r)
testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, res.StatusCode)

// Check if both recording and alerting rules are listed
r, err = http.NewRequest(
http.MethodGet,
rulesEndpointURL,
nil,
)
testutil.Ok(t, err)

res, err = client.Do(r)
defer res.Body.Close()

testutil.Ok(t, err)
testutil.Equals(t, http.StatusOK, res.StatusCode)

body, err := ioutil.ReadAll(res.Body)
bodyStr := string(body)
assertResponse(t, bodyStr, "record: job:up:avg")
assertResponse(t, bodyStr, "alert: ManyInstancesDown")
})

t.Run("put-invalid-rules", func(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@onprem for the case that invalid rules are submitted, I keep receiving no error/200 HTTP response status code, instead of https://github.com/observatorium/rules-objstore/blob/main/pkg/server/server.go#L80
I got this in the test output:

17:10:54 rules_objstore: level=debug name=rules-objstore ts=2022-01-19T20:10:54.115959093Z caller=server.go:81 component=server handler=setrules tenant=test-oidc msg="request body failed rule group validation" errs="unsupported value type"
17:10:54 observatorium_api: level=debug name=observatorium ts=2022-01-19T20:10:54.116175567Z caller=instrumentation.go:36 request=observatorium_api/NKsl1S6qrw-000009 proto=HTTP/1.1 method=PUT status=200 content= path=/api/metrics/v1/test-oidc/api/v1/rules/raw duration=1.492672ms bytes=42

I'll try to take a better look on this tomorrow, but I think either obs API is not fetching err/status code or rules-objstore is not returning/propagating them properly

// Set an invalid rules file
invalidRules := []byte(invalidRulesYamlTpl)
r, err := http.NewRequest(
http.MethodPut,
rulesEndpointURL,
bytes.NewReader(invalidRules),
)
testutil.Ok(t, err)
res, err := client.Do(r)
//TODO: an error/http status code is not being returned to the API
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're not fixing this issue in this PR then please let's create a ticket and reference it so we don't loose sight of this problem :)

TODO (MON-XXXX) - an error/http...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, I've removed the test case for now and will create a ticket by EOD :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a bug ticket here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should get fixed by #218. 🙂

//testutil.NotOk(t, err)
testutil.Equals(t, http.StatusOK, res.StatusCode) // should this be http.StatusBadRequest instead? (from: https://github.com/observatorium/rules-objstore/blob/main/pkg/server/server.go#L80)
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Missing newline

46 changes: 46 additions & 0 deletions test/e2e/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
dexImage = "dexidp/dex:v2.30.0"
opaImage = "openpolicyagent/opa:0.31.0"
gubernatorImage = "thrawn01/gubernator:1.0.0-rc.8"
rulesObjectStoreImage = "quay.io/observatorium/rules-objstore:main-2022-01-19-8650540"

logLevelError = "error"
logLevelDebug = "debug"
Expand All @@ -49,6 +50,19 @@ func startServicesForMetrics(t *testing.T, e e2e.Environment) (
thanosQuery.Endpoint("http")
}

func startServicesForRules(t *testing.T, e e2e.Environment) (metricsRulesEndpoint string) {
// Create S3 replacement for rules backend
bucket := "obs_rules_test"
m := e2edb.NewMinio(e, "rules-minio", bucket)
testutil.Ok(t, e2e.StartAndWaitReady(m))

createRulesYAML(t, e, bucket, m.InternalEndpoint(e2edb.AccessPortName), e2edb.MinioAccessKey, e2edb.MinioSecretKey)
rulesBackend := newRulesBackendService(e)
testutil.Ok(t, e2e.StartAndWaitReady(rulesBackend))

return rulesBackend.InternalEndpoint("http")
}

func startServicesForLogs(t *testing.T, e e2e.Environment) (
logsEndpoint string,
logsExtEndpoint string,
Expand Down Expand Up @@ -174,6 +188,27 @@ func newLokiService(e e2e.Environment) *e2e.InstrumentedRunnable {
)
}

func newRulesBackendService(e e2e.Environment) *e2e.InstrumentedRunnable {
ports := map[string]int{"http": 8080, "internal": 8081}

args := e2e.BuildArgs(map[string]string{
"--log.level": logLevelDebug,
"--web.listen": ":" + strconv.Itoa(ports["http"]),
"--web.internal.listen": ":" + strconv.Itoa(ports["internal"]),
"--web.healthchecks.url": "http://127.0.0.1:" + strconv.Itoa(ports["http"]),
"--objstore.config-file": filepath.Join(configsContainerPath, "rules-objstore.yaml"),
})

return e2e.NewInstrumentedRunnable(e, "rules_objstore", ports, "internal").Init(
e2e.StartOptions{
Image: rulesObjectStoreImage,
Command: e2e.NewCommand("", args...),
Readiness: e2e.NewHTTPReadinessProbe("internal", "/ready", 200, 200),
User: strconv.Itoa(os.Getuid()),
},
)
}

func newOPAService(e e2e.Environment) *e2e.InstrumentedRunnable {
ports := map[string]int{"http": 8181}

Expand All @@ -197,6 +232,7 @@ type apiOptions struct {
logsEndpoint string
metricsReadEndpoint string
metricsWriteEndpoint string
metricsRulesEndpoint string
ratelimiterAddr string
}

Expand All @@ -215,6 +251,12 @@ func withMetricsEndpoints(readEndpoint string, writeEndpoint string) apiOption {
}
}

func withRulesEndpoint(rulesEndpoint string) apiOption {
return func(o *apiOptions) {
o.metricsRulesEndpoint = rulesEndpoint
}
}

func withRateLimiter(addr string) apiOption {
return func(o *apiOptions) {
o.ratelimiterAddr = addr
Expand Down Expand Up @@ -252,6 +294,10 @@ func newObservatoriumAPIService(
args = append(args, "--metrics.write.endpoint="+opts.metricsWriteEndpoint)
}

if opts.metricsRulesEndpoint != "" {
args = append(args, "--metrics.rules.endpoint="+opts.metricsRulesEndpoint)
}

if opts.logsEndpoint != "" {
args = append(args, "--logs.read.endpoint="+opts.logsEndpoint)
args = append(args, "--logs.tail.endpoint="+opts.logsEndpoint)
Expand Down