github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/data/blobstore_test.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package data
    18  
    19  import (
    20  	"bytes"
    21  	"crypto/sha256"
    22  	"fmt"
    23  	"io"
    24  	"io/ioutil"
    25  	"math/rand"
    26  	"testing"
    27  	"testing/iotest"
    28  
    29  	"github.com/kaleido-io/firefly/mocks/databasemocks"
    30  	"github.com/kaleido-io/firefly/mocks/dataexchangemocks"
    31  	"github.com/kaleido-io/firefly/pkg/fftypes"
    32  	"github.com/stretchr/testify/assert"
    33  	"github.com/stretchr/testify/mock"
    34  )
    35  
    36  func TestUploadBlobOk(t *testing.T) {
    37  
    38  	dm, ctx, cancel := newTestDataManager(t)
    39  	defer cancel()
    40  
    41  	b := make([]byte, 10000+int(rand.Float32()*10000))
    42  	for i := 0; i < len(b); i++ {
    43  		b[i] = 'a' + byte(rand.Int()%26)
    44  	}
    45  
    46  	mdi := dm.database.(*databasemocks.Plugin)
    47  	mdi.On("UpsertData", mock.Anything, mock.Anything, false, false).Return(nil)
    48  
    49  	dxID := make(chan fftypes.UUID, 1)
    50  	mdx := dm.exchange.(*dataexchangemocks.Plugin)
    51  	dxUpload := mdx.On("UploadBLOB", ctx, "ns1", mock.Anything, mock.Anything)
    52  	dxUpload.RunFn = func(a mock.Arguments) {
    53  		readBytes, err := ioutil.ReadAll(a[3].(io.Reader))
    54  		assert.Nil(t, err)
    55  		assert.Equal(t, b, readBytes)
    56  		dxID <- a[2].(fftypes.UUID)
    57  		dxUpload.ReturnArguments = mock.Arguments{err}
    58  	}
    59  
    60  	data, err := dm.UploadBLOB(ctx, "ns1", bytes.NewReader(b))
    61  	assert.NoError(t, err)
    62  
    63  	// Check the hashes and other details of the data
    64  	assert.Equal(t, [32]byte(sha256.Sum256(b)), [32]byte(*data.Hash))
    65  	assert.Equal(t, <-dxID, *data.ID)
    66  	assert.Empty(t, data.Validator)
    67  	assert.Nil(t, data.Datatype)
    68  
    69  	mdi.AssertExpectations(t)
    70  	mdx.AssertExpectations(t)
    71  
    72  }
    73  
    74  func TestUploadBlobReadFail(t *testing.T) {
    75  
    76  	dm, ctx, cancel := newTestDataManager(t)
    77  	defer cancel()
    78  
    79  	mdx := dm.exchange.(*dataexchangemocks.Plugin)
    80  	dxUpload := mdx.On("UploadBLOB", ctx, "ns1", mock.Anything, mock.Anything).Return(nil)
    81  	dxUpload.RunFn = func(a mock.Arguments) {
    82  		_, err := ioutil.ReadAll(a[3].(io.Reader))
    83  		assert.NoError(t, err)
    84  	}
    85  
    86  	_, err := dm.UploadBLOB(ctx, "ns1", iotest.ErrReader(fmt.Errorf("pop")))
    87  	assert.Regexp(t, "FF10217.*pop", err)
    88  
    89  }
    90  
    91  func TestUploadBlobWriteFailDoesNotRead(t *testing.T) {
    92  
    93  	dm, ctx, cancel := newTestDataManager(t)
    94  	defer cancel()
    95  
    96  	mdx := dm.exchange.(*dataexchangemocks.Plugin)
    97  	mdx.On("UploadBLOB", ctx, "ns1", mock.Anything, mock.Anything).Return(fmt.Errorf("pop"))
    98  
    99  	_, err := dm.UploadBLOB(ctx, "ns1", bytes.NewReader([]byte(`any old data`)))
   100  	assert.Regexp(t, "pop", err)
   101  
   102  }
   103  
   104  func TestUploadBlobUpsertFail(t *testing.T) {
   105  
   106  	dm, ctx, cancel := newTestDataManager(t)
   107  	defer cancel()
   108  
   109  	mdx := dm.exchange.(*dataexchangemocks.Plugin)
   110  	dxUpload := mdx.On("UploadBLOB", ctx, "ns1", mock.Anything, mock.Anything).Return(nil)
   111  	dxUpload.RunFn = func(a mock.Arguments) {
   112  		_, err := ioutil.ReadAll(a[3].(io.Reader))
   113  		assert.Nil(t, err)
   114  	}
   115  	mdi := dm.database.(*databasemocks.Plugin)
   116  	mdi.On("UpsertData", mock.Anything, mock.Anything, false, false).Return(fmt.Errorf("pop"))
   117  
   118  	_, err := dm.UploadBLOB(ctx, "ns1", bytes.NewReader([]byte(`any old data`)))
   119  	assert.Regexp(t, "pop", err)
   120  
   121  }