2018-07-20 17:43:22 +00:00
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
2020-02-20 23:42:27 +00:00
|
|
|
"context"
|
2020-10-28 15:22:14 +00:00
|
|
|
"strings"
|
2018-11-07 18:55:52 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-07-20 17:43:22 +00:00
|
|
|
)
|
|
|
|
|
2018-12-28 23:02:19 +00:00
|
|
|
// TODO(goller): remove opPrefix argument
|
2018-11-07 18:55:52 +00:00
|
|
|
func diffPlatformErrors(name string, actual, expected error, opPrefix string, t *testing.T) {
|
2019-02-19 23:47:19 +00:00
|
|
|
t.Helper()
|
2019-01-16 19:41:57 +00:00
|
|
|
ErrorsEqual(t, actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorsEqual checks to see if the provided errors are equivalent.
|
|
|
|
func ErrorsEqual(t *testing.T, actual, expected error) {
|
2019-02-19 23:47:19 +00:00
|
|
|
t.Helper()
|
2018-12-12 18:24:33 +00:00
|
|
|
if expected == nil && actual == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-07 18:55:52 +00:00
|
|
|
if expected == nil && actual != nil {
|
2019-01-04 19:12:35 +00:00
|
|
|
t.Errorf("unexpected error %s", actual.Error())
|
2018-11-07 18:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if expected != nil && actual == nil {
|
2019-01-04 19:12:35 +00:00
|
|
|
t.Errorf("expected error %s but received nil", expected.Error())
|
2018-11-07 18:55:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
if influxdb.ErrorCode(expected) != influxdb.ErrorCode(actual) {
|
2019-04-11 15:17:05 +00:00
|
|
|
t.Logf("\nexpected: %v\nactual: %v\n\n", expected, actual)
|
2020-10-28 15:22:14 +00:00
|
|
|
t.Errorf("expected error code %q but received %q", influxdb.ErrorCode(expected), influxdb.ErrorCode(actual))
|
2018-11-07 18:55:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
if influxdb.ErrorMessage(expected) != influxdb.ErrorMessage(actual) {
|
2019-04-11 15:17:05 +00:00
|
|
|
t.Logf("\nexpected: %v\nactual: %v\n\n", expected, actual)
|
2020-10-28 15:22:14 +00:00
|
|
|
t.Errorf("expected error message %q but received %q", influxdb.ErrorMessage(expected), influxdb.ErrorMessage(actual))
|
2018-12-12 18:24:33 +00:00
|
|
|
}
|
2018-11-07 18:55:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 17:43:29 +00:00
|
|
|
// FloatPtr takes the ref of a float number.
|
|
|
|
func FloatPtr(f float64) *float64 {
|
|
|
|
p := new(float64)
|
|
|
|
*p = f
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
func idPtr(id influxdb.ID) *influxdb.ID {
|
2019-01-09 19:13:06 +00:00
|
|
|
return &id
|
|
|
|
}
|
|
|
|
|
2018-10-10 19:39:09 +00:00
|
|
|
// MustIDBase16 is an helper to ensure a correct ID is built during testing.
|
2020-10-28 15:22:14 +00:00
|
|
|
func MustIDBase16(s string) influxdb.ID {
|
|
|
|
id, err := influxdb.IDFromString(s)
|
2018-07-20 17:43:22 +00:00
|
|
|
if err != nil {
|
2018-07-20 10:24:07 +00:00
|
|
|
panic(err)
|
2018-07-20 17:43:22 +00:00
|
|
|
}
|
2018-07-30 14:29:52 +00:00
|
|
|
return *id
|
2018-07-20 17:43:22 +00:00
|
|
|
}
|
2019-12-12 17:31:49 +00:00
|
|
|
|
|
|
|
// MustIDBase16Ptr is an helper to ensure a correct ID ptr ref is built during testing.
|
2020-10-28 15:22:14 +00:00
|
|
|
func MustIDBase16Ptr(s string) *influxdb.ID {
|
2019-12-12 17:31:49 +00:00
|
|
|
id := MustIDBase16(s)
|
|
|
|
return &id
|
|
|
|
}
|
2020-02-20 23:42:27 +00:00
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
func MustCreateOrgs(ctx context.Context, svc influxdb.OrganizationService, os ...*influxdb.Organization) {
|
2020-02-20 23:42:27 +00:00
|
|
|
for _, o := range os {
|
|
|
|
if err := svc.CreateOrganization(ctx, o); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
func MustCreateLabels(ctx context.Context, svc influxdb.LabelService, labels ...*influxdb.Label) {
|
2020-02-20 23:42:27 +00:00
|
|
|
for _, l := range labels {
|
|
|
|
if err := svc.CreateLabel(ctx, l); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
func MustCreateUsers(ctx context.Context, svc influxdb.UserService, us ...*influxdb.User) {
|
2020-02-20 23:42:27 +00:00
|
|
|
for _, u := range us {
|
|
|
|
if err := svc.CreateUser(ctx, u); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
func MustCreateMappings(ctx context.Context, svc influxdb.UserResourceMappingService, ms ...*influxdb.UserResourceMapping) {
|
2020-02-20 23:42:27 +00:00
|
|
|
for _, m := range ms {
|
|
|
|
if err := svc.CreateUserResourceMapping(ctx, m); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
func MustMakeUsersOrgOwner(ctx context.Context, svc influxdb.UserResourceMappingService, oid influxdb.ID, uids ...influxdb.ID) {
|
|
|
|
ms := make([]*influxdb.UserResourceMapping, len(uids))
|
2020-02-20 23:42:27 +00:00
|
|
|
for i, uid := range uids {
|
2020-10-28 15:22:14 +00:00
|
|
|
ms[i] = &influxdb.UserResourceMapping{
|
2020-02-20 23:42:27 +00:00
|
|
|
UserID: uid,
|
2020-10-28 15:22:14 +00:00
|
|
|
UserType: influxdb.Owner,
|
|
|
|
ResourceType: influxdb.OrgsResourceType,
|
2020-02-20 23:42:27 +00:00
|
|
|
ResourceID: oid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MustCreateMappings(ctx, svc, ms...)
|
|
|
|
}
|
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
func MustMakeUsersOrgMember(ctx context.Context, svc influxdb.UserResourceMappingService, oid influxdb.ID, uids ...influxdb.ID) {
|
|
|
|
ms := make([]*influxdb.UserResourceMapping, len(uids))
|
2020-02-20 23:42:27 +00:00
|
|
|
for i, uid := range uids {
|
2020-10-28 15:22:14 +00:00
|
|
|
ms[i] = &influxdb.UserResourceMapping{
|
2020-02-20 23:42:27 +00:00
|
|
|
UserID: uid,
|
2020-10-28 15:22:14 +00:00
|
|
|
UserType: influxdb.Member,
|
|
|
|
ResourceType: influxdb.OrgsResourceType,
|
2020-02-20 23:42:27 +00:00
|
|
|
ResourceID: oid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MustCreateMappings(ctx, svc, ms...)
|
|
|
|
}
|
2020-08-14 22:09:40 +00:00
|
|
|
|
2020-10-28 15:22:14 +00:00
|
|
|
func MustNewPermissionAtID(id influxdb.ID, a influxdb.Action, rt influxdb.ResourceType, orgID influxdb.ID) *influxdb.Permission {
|
|
|
|
perm, err := influxdb.NewPermissionAtID(id, a, rt, orgID)
|
2020-08-14 22:09:40 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return perm
|
|
|
|
}
|
2020-10-28 15:22:14 +00:00
|
|
|
|
|
|
|
func influxErrsEqual(t *testing.T, expected *influxdb.Error, actual error) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if expected != nil {
|
|
|
|
require.Error(t, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected == nil {
|
|
|
|
require.NoError(t, actual)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
iErr, ok := actual.(*influxdb.Error)
|
|
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, expected.Code, iErr.Code)
|
|
|
|
assert.Truef(t, strings.HasPrefix(iErr.Error(), expected.Error()), "expected: %s got err: %s", expected.Error(), actual.Error())
|
|
|
|
}
|