ServiceAPI tests an implementation of blobcache.Service.
(t *testing.T, mk func(t testing.TB) blobcache.Service)
| 14 | |
| 15 | // ServiceAPI tests an implementation of blobcache.Service. |
| 16 | func ServiceAPI(t *testing.T, mk func(t testing.TB) blobcache.Service) { |
| 17 | t.Run("Endpoint", func(t *testing.T) { |
| 18 | t.Parallel() |
| 19 | ctx := testutil.Context(t) |
| 20 | s := mk(t) |
| 21 | _, err := s.Endpoint(ctx) |
| 22 | require.NoError(t, err) |
| 23 | }) |
| 24 | t.Run("CreateVolume", func(t *testing.T) { |
| 25 | t.Parallel() |
| 26 | ctx := testutil.Context(t) |
| 27 | s := mk(t) |
| 28 | h, err := s.CreateVolume(ctx, nil, defaultLocalSpec()) |
| 29 | require.NoError(t, err) |
| 30 | require.NotNil(t, h) |
| 31 | }) |
| 32 | t.Run("VolumeEmpty", func(t *testing.T) { |
| 33 | t.Parallel() |
| 34 | // Check that an initial volume is empty. |
| 35 | ctx := testutil.Context(t) |
| 36 | s := mk(t) |
| 37 | volh, err := s.CreateVolume(ctx, nil, defaultLocalSpec()) |
| 38 | require.NoError(t, err) |
| 39 | require.NotNil(t, volh) |
| 40 | txh, err := s.BeginTx(ctx, *volh, blobcache.TxParams{Modify: false}) |
| 41 | require.NoError(t, err) |
| 42 | require.NotNil(t, txh) |
| 43 | buf := []byte{1, 2, 3} // arbitrary data |
| 44 | err = s.Load(ctx, *txh, &buf) |
| 45 | require.NoError(t, err) |
| 46 | require.Equal(t, 0, len(buf)) |
| 47 | }) |
| 48 | t.Run("HashAlgo", func(t *testing.T) { |
| 49 | t.Parallel() |
| 50 | ctx := testutil.Context(t) |
| 51 | s := mk(t) |
| 52 | for _, algo := range []blobcache.HashAlgo{ |
| 53 | blobcache.HashAlgo_BLAKE3_256, |
| 54 | blobcache.HashAlgo_BLAKE2b_256, |
| 55 | blobcache.HashAlgo_SHA2_256, |
| 56 | blobcache.HashAlgo_SHA3_256, |
| 57 | blobcache.HashAlgo_CSHAKE256, |
| 58 | } { |
| 59 | t.Run(string(algo), func(t *testing.T) { |
| 60 | spec := defaultLocalSpec() |
| 61 | spec.Local.HashAlgo = algo |
| 62 | hf := algo.Hash |
| 63 | volh, err := s.CreateVolume(ctx, nil, spec) |
| 64 | require.NoError(t, err) |
| 65 | require.NotNil(t, volh) |
| 66 | txh, err := s.BeginTx(ctx, *volh, blobcache.TxParams{Modify: true}) |
| 67 | require.NoError(t, err) |
| 68 | require.NotNil(t, txh) |
| 69 | defer s.Abort(ctx, *txh) |
| 70 | |
| 71 | data := []byte("hello world") |
| 72 | expected := hf(data) |
| 73 | cid, err := s.Post(ctx, *txh, data, blobcache.PostOpts{}) |