2020-03-27 14:56:22 +00:00
|
|
|
package tenant_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
ihttp "github.com/influxdata/influxdb/v2/http"
|
2020-07-24 10:35:41 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kv"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/tenant"
|
|
|
|
itesting "github.com/influxdata/influxdb/v2/testing"
|
2020-03-27 14:56:22 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func initBucketHttpService(f itesting.BucketFields, t *testing.T) (influxdb.BucketService, string, func()) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
s, stCloser, err := NewTestInmemStore(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-24 10:35:41 +00:00
|
|
|
store := tenant.NewStore(s)
|
|
|
|
svc := tenant.NewService(store)
|
2020-03-27 14:56:22 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, o := range f.Organizations {
|
2020-07-24 10:35:41 +00:00
|
|
|
// use storage create org in order to avoid creating system buckets
|
|
|
|
if err := s.Update(ctx, func(tx kv.Tx) error {
|
|
|
|
return store.CreateOrg(tx.Context(), tx, o)
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("failed to populate organizations: %s", err)
|
2020-03-27 14:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, b := range f.Buckets {
|
|
|
|
if err := svc.CreateBucket(ctx, b); err != nil {
|
|
|
|
t.Fatalf("failed to populate buckets")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 20:48:42 +00:00
|
|
|
handler := tenant.NewHTTPBucketHandler(zaptest.NewLogger(t), svc, nil, nil, nil)
|
2020-03-27 14:56:22 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Mount(handler.Prefix(), handler)
|
|
|
|
server := httptest.NewServer(r)
|
|
|
|
httpClient, err := ihttp.NewHTTPClient(server.URL, "", false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := tenant.BucketClientService{
|
|
|
|
Client: httpClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &client, "http_tenant", func() {
|
|
|
|
server.Close()
|
|
|
|
stCloser()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBucketService(t *testing.T) {
|
2020-08-05 20:30:45 +00:00
|
|
|
itesting.BucketService(initBucketHttpService, t, itesting.WithoutHooks(), itesting.WithHTTPValidation())
|
2020-03-27 14:56:22 +00:00
|
|
|
}
|