forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_tenant_queries_test.go
66 lines (53 loc) · 2.11 KB
/
multi_tenant_queries_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package integration
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/loki/integration/client"
"github.com/grafana/loki/integration/cluster"
)
func TestMultiTenantQuery(t *testing.T) {
clu := cluster.New(nil)
defer func() {
assert.NoError(t, clu.Cleanup())
}()
var (
tAll = clu.AddComponent(
"all",
"-target=all",
)
)
require.NoError(t, clu.Run())
cliTenant1 := client.New("org1", "", tAll.HTTPURL())
cliTenant2 := client.New("org2", "", tAll.HTTPURL())
cliMultitenant := client.New("org1|org2", "", tAll.HTTPURL())
// ingest log lines for tenant 1 and tenant 2.
require.NoError(t, cliTenant1.PushLogLineWithTimestamp("lineA", cliTenant1.Now.Add(-45*time.Minute), map[string]string{"job": "fake1"}))
require.NoError(t, cliTenant2.PushLogLineWithTimestamp("lineB", cliTenant2.Now.Add(-45*time.Minute), map[string]string{"job": "fake2"}))
// check that tenant1 only have access to log line A.
matchLines(t, cliTenant1, `{job="fake2"}`, []string{})
matchLines(t, cliTenant1, `{job=~"fake.*"}`, []string{"lineA"})
matchLines(t, cliTenant1, `{job="fake1"}`, []string{"lineA"})
// check that tenant2 only have access to log line B.
matchLines(t, cliTenant2, `{job="fake1"}`, []string{})
matchLines(t, cliTenant2, `{job=~"fake.*"}`, []string{"lineB"})
matchLines(t, cliTenant2, `{job="fake2"}`, []string{"lineB"})
// check that multitenant has access to all log lines on same query.
matchLines(t, cliMultitenant, `{job=~"fake.*"}`, []string{"lineA", "lineB"})
matchLines(t, cliMultitenant, `{job="fake1"}`, []string{"lineA"})
matchLines(t, cliMultitenant, `{job="fake2"}`, []string{"lineB"})
matchLines(t, cliMultitenant, `{job="fake3"}`, []string{})
}
func matchLines(t *testing.T, client *client.Client, labels string, expectedLines []string) {
resp, err := client.RunRangeQuery(context.Background(), labels)
require.NoError(t, err)
var lines []string
for _, stream := range resp.Data.Stream {
for _, val := range stream.Values {
lines = append(lines, val[1])
}
}
require.ElementsMatch(t, expectedLines, lines)
}