github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/cmd/spicedb/servetesting_race_test.go (about)

     1  //go:build docker && image
     2  // +build docker,image
     3  
     4  package main
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"path"
    10  	"path/filepath"
    11  	"runtime"
    12  	"testing"
    13  	"time"
    14  
    15  	v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
    16  	"github.com/google/uuid"
    17  	"github.com/ory/dockertest/v3"
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/stretchr/testify/require"
    20  	"google.golang.org/grpc"
    21  	"google.golang.org/grpc/credentials/insecure"
    22  	healthpb "google.golang.org/grpc/health/grpc_health_v1"
    23  )
    24  
    25  // Based on a test originally written by https://github.com/wscalf
    26  func TestCheckPermissionOnTesterNoFlakes(t *testing.T) {
    27  	_, b, _, _ := runtime.Caller(0)
    28  	basepath := filepath.Dir(b)
    29  	tester, err := newTester(t,
    30  		&dockertest.RunOptions{
    31  			Repository:   "authzed/spicedb",
    32  			Tag:          "ci",
    33  			Cmd:          []string{"serve-testing", "--load-configs", "/mnt/spicedb_bootstrap.yaml"},
    34  			Mounts:       []string{path.Join(basepath, "testdata/bootstrap.yaml") + ":/mnt/spicedb_bootstrap.yaml"},
    35  			ExposedPorts: []string{"50051/tcp", "50052/tcp", "8443/tcp", "8444/tcp"},
    36  		},
    37  		uuid.NewString(),
    38  		true,
    39  	)
    40  	require.NoError(t, err)
    41  	defer tester.cleanup()
    42  
    43  	for i := 0; i < 1000; i++ {
    44  		conn, err := grpc.Dial(fmt.Sprintf("localhost:%s", tester.port), grpc.WithTransportCredentials(insecure.NewCredentials()))
    45  		require.NoError(t, err)
    46  
    47  		require.Eventually(t, func() bool {
    48  			resp, err := healthpb.NewHealthClient(conn).Check(context.Background(), &healthpb.HealthCheckRequest{Service: "authzed.api.v1.SchemaService"})
    49  			if err != nil || resp.GetStatus() != healthpb.HealthCheckResponse_SERVING {
    50  				return false
    51  			}
    52  
    53  			return true
    54  		}, 5*time.Second, 1*time.Millisecond, "was unable to connect to running service")
    55  
    56  		client := v1.NewPermissionsServiceClient(conn)
    57  		result, err := client.CheckPermission(context.Background(), &v1.CheckPermissionRequest{
    58  			Resource: &v1.ObjectReference{
    59  				ObjectType: "access",
    60  				ObjectId:   "blue",
    61  			},
    62  			Permission: "assigned",
    63  			Subject: &v1.SubjectReference{
    64  				Object: &v1.ObjectReference{
    65  					ObjectType: "user",
    66  					ObjectId:   "alice",
    67  				},
    68  			},
    69  		})
    70  		conn.Close()
    71  
    72  		assert.NoError(t, err)
    73  		assert.Equal(t, v1.CheckPermissionResponse_PERMISSIONSHIP_HAS_PERMISSION, result.Permissionship, "Error on attempt #%d", i)
    74  	}
    75  }