package testsuite import ( "context" "testing" "forge.cadoles.com/Cadoles/emissary/internal/datastore" "github.com/pkg/errors" ) type tenantRepositoryTestCase struct { Name string Skip bool Run func(ctx context.Context, repo datastore.TenantRepository) error } var tenantRepositoryTestCases = []tenantRepositoryTestCase{ { Name: "Create a new tenant", Run: func(ctx context.Context, repo datastore.TenantRepository) error { label := "Foo" tenant, err := repo.Create(ctx, "Foo") if err != nil { return errors.WithStack(err) } if tenant.CreatedAt.IsZero() { return errors.Errorf("tenant.CreatedAt should not be zero time") } if tenant.UpdatedAt.IsZero() { return errors.Errorf("tenant.UpdatedAt should not be zero time") } if e, g := label, tenant.Label; e != g { return errors.Errorf("tenant.Label: expected '%v', got '%v'", e, g) } if tenant.ID == "" { return errors.Errorf("tenant.ID should not be empty") } if _, err := datastore.ParseTenantID(string(tenant.ID)); err != nil { return errors.Wrapf(err, "tenant.ID should be valid") } return nil }, }, { Name: "Try to update an unexistant tenant", Run: func(ctx context.Context, repo datastore.TenantRepository) error { unexistantTenantID := datastore.TenantID("00000000-0000-0000-0000-000000000000") tenant, err := repo.Update(ctx, unexistantTenantID) if err == nil { return errors.New("error should not be nil") } if !errors.Is(err, datastore.ErrNotFound) { return errors.Errorf("error should be datastore.ErrNotFound, got '%+v'", err) } if tenant != nil { return errors.New("tenant should be nil") } return nil }, }, { Name: "Try to delete spec of an unexistant agent", Run: func(ctx context.Context, repo datastore.TenantRepository) error { unexistantTenantID := datastore.TenantID("00000000-0000-0000-0000-000000000000") err := repo.Delete(ctx, unexistantTenantID) if err == nil { return errors.New("error should not be nil") } if !errors.Is(err, datastore.ErrNotFound) { return errors.Errorf("error should be datastore.ErrNotFound, got '%+v'", err) } return nil }, }, } func runTenantRepositoryTests(t *testing.T, repo datastore.TenantRepository) { for _, tc := range tenantRepositoryTestCases { func(tc tenantRepositoryTestCase) { t.Run(tc.Name, func(t *testing.T) { t.Parallel() if tc.Skip { t.SkipNow() return } ctx := context.Background() if err := tc.Run(ctx, repo); err != nil { t.Errorf("%+v", errors.WithStack(err)) } }) }(tc) } }