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"
|
|
|
|
"github.com/influxdata/influxdb/v2/http"
|
2020-08-11 14:56:42 +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 initHttpOrgService(f itesting.OrganizationFields, t *testing.T) (influxdb.OrganizationService, string, func()) {
|
|
|
|
t.Helper()
|
|
|
|
|
2021-08-31 20:43:45 +00:00
|
|
|
s := itesting.NewTestInmemStore(t)
|
2020-07-01 11:08:20 +00:00
|
|
|
storage := tenant.NewStore(s)
|
2020-08-11 14:56:42 +00:00
|
|
|
|
|
|
|
if f.OrgBucketIDs != nil {
|
|
|
|
storage.OrgIDGen = f.OrgBucketIDs
|
|
|
|
storage.BucketIDGen = f.OrgBucketIDs
|
|
|
|
}
|
|
|
|
|
|
|
|
// go direct to storage for test data
|
|
|
|
if err := s.Update(context.Background(), func(tx kv.Tx) error {
|
|
|
|
for _, o := range f.Organizations {
|
|
|
|
if err := storage.CreateOrg(tx.Context(), tx, o); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-27 14:56:22 +00:00
|
|
|
}
|
2020-08-11 14:56:42 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("failed to populate organizations: %s", err)
|
2020-03-27 14:56:22 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 14:56:42 +00:00
|
|
|
handler := tenant.NewHTTPOrgHandler(zaptest.NewLogger(t), tenant.NewService(storage), nil, nil)
|
2020-03-27 14:56:22 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Mount(handler.Prefix(), handler)
|
|
|
|
server := httptest.NewServer(r)
|
|
|
|
httpClient, err := http.NewHTTPClient(server.URL, "", false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
orgClient := tenant.OrgClientService{
|
|
|
|
Client: httpClient,
|
|
|
|
}
|
|
|
|
|
2021-08-31 20:43:45 +00:00
|
|
|
return &orgClient, "http_tenant", server.Close
|
2020-03-27 14:56:22 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 14:56:42 +00:00
|
|
|
func TestHTTPOrgService(t *testing.T) {
|
2020-03-27 14:56:22 +00:00
|
|
|
itesting.OrganizationService(initHttpOrgService, t)
|
|
|
|
}
|