github.com/influxdata/influxdb/v2@v2.7.6/dashboards/testing/util.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/influxdata/influxdb/v2"
     8  	"github.com/influxdata/influxdb/v2/kit/platform"
     9  	"github.com/influxdata/influxdb/v2/kit/platform/errors"
    10  )
    11  
    12  func diffPlatformErrors(actual, expected error, t *testing.T) {
    13  	t.Helper()
    14  	ErrorsEqual(t, actual, expected)
    15  }
    16  
    17  // ErrorsEqual checks to see if the provided errors are equivalent.
    18  func ErrorsEqual(t *testing.T, actual, expected error) {
    19  	t.Helper()
    20  	if expected == nil && actual == nil {
    21  		return
    22  	}
    23  
    24  	if expected == nil && actual != nil {
    25  		t.Errorf("unexpected error %s", actual.Error())
    26  	}
    27  
    28  	if expected != nil && actual == nil {
    29  		t.Errorf("expected error %s but received nil", expected.Error())
    30  	}
    31  
    32  	if errors.ErrorCode(expected) != errors.ErrorCode(actual) {
    33  		t.Logf("\nexpected: %v\nactual: %v\n\n", expected, actual)
    34  		t.Errorf("expected error code %q but received %q", errors.ErrorCode(expected), errors.ErrorCode(actual))
    35  	}
    36  
    37  	if errors.ErrorMessage(expected) != errors.ErrorMessage(actual) {
    38  		t.Logf("\nexpected: %v\nactual: %v\n\n", expected, actual)
    39  		t.Errorf("expected error message %q but received %q", errors.ErrorMessage(expected), errors.ErrorMessage(actual))
    40  	}
    41  }
    42  
    43  // FloatPtr takes the ref of a float number.
    44  func FloatPtr(f float64) *float64 {
    45  	p := new(float64)
    46  	*p = f
    47  	return p
    48  }
    49  
    50  func idPtr(id platform.ID) *platform.ID {
    51  	return &id
    52  }
    53  
    54  // MustIDBase16 is an helper to ensure a correct ID is built during testing.
    55  func MustIDBase16(s string) platform.ID {
    56  	id, err := platform.IDFromString(s)
    57  	if err != nil {
    58  		panic(err)
    59  	}
    60  	return *id
    61  }
    62  
    63  // MustIDBase16Ptr is an helper to ensure a correct ID ptr ref is built during testing.
    64  func MustIDBase16Ptr(s string) *platform.ID {
    65  	id := MustIDBase16(s)
    66  	return &id
    67  }
    68  
    69  func MustCreateOrgs(ctx context.Context, svc influxdb.OrganizationService, os ...*influxdb.Organization) {
    70  	for _, o := range os {
    71  		if err := svc.CreateOrganization(ctx, o); err != nil {
    72  			panic(err)
    73  		}
    74  	}
    75  }
    76  
    77  func MustCreateLabels(ctx context.Context, svc influxdb.LabelService, labels ...*influxdb.Label) {
    78  	for _, l := range labels {
    79  		if err := svc.CreateLabel(ctx, l); err != nil {
    80  			panic(err)
    81  		}
    82  	}
    83  }
    84  
    85  func MustCreateUsers(ctx context.Context, svc influxdb.UserService, us ...*influxdb.User) {
    86  	for _, u := range us {
    87  		if err := svc.CreateUser(ctx, u); err != nil {
    88  			panic(err)
    89  		}
    90  	}
    91  }
    92  
    93  func MustCreateMappings(ctx context.Context, svc influxdb.UserResourceMappingService, ms ...*influxdb.UserResourceMapping) {
    94  	for _, m := range ms {
    95  		if err := svc.CreateUserResourceMapping(ctx, m); err != nil {
    96  			panic(err)
    97  		}
    98  	}
    99  }
   100  
   101  func MustMakeUsersOrgOwner(ctx context.Context, svc influxdb.UserResourceMappingService, oid platform.ID, uids ...platform.ID) {
   102  	ms := make([]*influxdb.UserResourceMapping, len(uids))
   103  	for i, uid := range uids {
   104  		ms[i] = &influxdb.UserResourceMapping{
   105  			UserID:       uid,
   106  			UserType:     influxdb.Owner,
   107  			ResourceType: influxdb.OrgsResourceType,
   108  			ResourceID:   oid,
   109  		}
   110  	}
   111  	MustCreateMappings(ctx, svc, ms...)
   112  }
   113  
   114  func MustMakeUsersOrgMember(ctx context.Context, svc influxdb.UserResourceMappingService, oid platform.ID, uids ...platform.ID) {
   115  	ms := make([]*influxdb.UserResourceMapping, len(uids))
   116  	for i, uid := range uids {
   117  		ms[i] = &influxdb.UserResourceMapping{
   118  			UserID:       uid,
   119  			UserType:     influxdb.Member,
   120  			ResourceType: influxdb.OrgsResourceType,
   121  			ResourceID:   oid,
   122  		}
   123  	}
   124  	MustCreateMappings(ctx, svc, ms...)
   125  }
   126  
   127  func MustNewPermissionAtID(id platform.ID, a influxdb.Action, rt influxdb.ResourceType, orgID platform.ID) *influxdb.Permission {
   128  	perm, err := influxdb.NewPermissionAtID(id, a, rt, orgID)
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  	return perm
   133  }