github.com/weaviate/weaviate@v1.24.6/usecases/backup/fakes_test.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package backup
    13  
    14  import (
    15  	"bytes"
    16  	"context"
    17  	"encoding/json"
    18  	"io"
    19  	"os"
    20  	"sync"
    21  
    22  	"github.com/stretchr/testify/mock"
    23  	"github.com/weaviate/weaviate/entities/backup"
    24  	"github.com/weaviate/weaviate/entities/modulecapabilities"
    25  )
    26  
    27  var chunks map[string][]byte
    28  
    29  func init() {
    30  	path := "test_data/chunk-1.tar.gz"
    31  	data, err := os.ReadFile(path)
    32  	if err != nil {
    33  		panic("missing test file: " + path)
    34  	}
    35  
    36  	chunks = map[string][]byte{
    37  		chunkKey("Article", 1): data,
    38  	}
    39  }
    40  
    41  type fakeBackupBackendProvider struct {
    42  	backend modulecapabilities.BackupBackend
    43  	err     error
    44  }
    45  
    46  func (bsp *fakeBackupBackendProvider) BackupBackend(backend string) (modulecapabilities.BackupBackend, error) {
    47  	return bsp.backend, bsp.err
    48  }
    49  
    50  type fakeSourcer struct {
    51  	mock.Mock
    52  }
    53  
    54  func (s *fakeSourcer) ReleaseBackup(ctx context.Context, id, class string) error {
    55  	args := s.Called(ctx, id, class)
    56  	return args.Error(0)
    57  }
    58  
    59  func (s *fakeSourcer) Backupable(ctx context.Context, classes []string) error {
    60  	args := s.Called(ctx, classes)
    61  	return args.Error(0)
    62  }
    63  
    64  func (s *fakeSourcer) ListBackupable() []string {
    65  	args := s.Called()
    66  	return args.Get(0).([]string)
    67  }
    68  
    69  func (s *fakeSourcer) BackupDescriptors(ctx context.Context, bakid string, classes []string,
    70  ) <-chan backup.ClassDescriptor {
    71  	args := s.Called(ctx, bakid, classes)
    72  	return args.Get(0).(<-chan backup.ClassDescriptor)
    73  }
    74  
    75  func (s *fakeSourcer) ClassExists(name string) bool {
    76  	args := s.Called(name)
    77  	return args.Bool(0)
    78  }
    79  
    80  type fakeBackend struct {
    81  	mock.Mock
    82  	sync.RWMutex
    83  	meta     backup.BackupDescriptor
    84  	glMeta   backup.DistributedBackupDescriptor
    85  	files    map[string][]byte
    86  	chunks   map[string][]byte
    87  	doneChan chan bool
    88  }
    89  
    90  func newFakeBackend() *fakeBackend {
    91  	return &fakeBackend{
    92  		doneChan: make(chan bool),
    93  		files:    map[string][]byte{},
    94  		chunks:   chunks,
    95  	}
    96  }
    97  
    98  func (fb *fakeBackend) HomeDir(backupID string) string {
    99  	fb.RLock()
   100  	defer fb.RUnlock()
   101  	args := fb.Called(backupID)
   102  	return args.String(0)
   103  }
   104  
   105  func (fb *fakeBackend) PutFile(ctx context.Context, backupID, key, srcPath string) error {
   106  	fb.Lock()
   107  	defer fb.Unlock()
   108  	args := fb.Called(ctx, backupID, key, srcPath)
   109  	return args.Error(0)
   110  }
   111  
   112  func (fb *fakeBackend) PutObject(ctx context.Context, backupID, key string, bytes []byte) error {
   113  	fb.Lock()
   114  	defer fb.Unlock()
   115  	args := fb.Called(ctx, backupID, key, bytes)
   116  	if key == BackupFile {
   117  		json.Unmarshal(bytes, &fb.meta)
   118  	} else if key == GlobalBackupFile || key == GlobalRestoreFile {
   119  		json.Unmarshal(bytes, &fb.glMeta)
   120  		if fb.glMeta.Status == backup.Success || fb.glMeta.Status == backup.Failed {
   121  			close(fb.doneChan)
   122  		}
   123  	}
   124  	return args.Error(0)
   125  }
   126  
   127  func (fb *fakeBackend) GetObject(ctx context.Context, backupID, key string) ([]byte, error) {
   128  	fb.RLock()
   129  	defer fb.RUnlock()
   130  	args := fb.Called(ctx, backupID, key)
   131  	if args.Get(0) != nil {
   132  		return args.Get(0).([]byte), args.Error(1)
   133  	}
   134  	return nil, args.Error(1)
   135  }
   136  
   137  func (fb *fakeBackend) Initialize(ctx context.Context, backupID string) error {
   138  	fb.Lock()
   139  	defer fb.Unlock()
   140  	args := fb.Called(ctx, backupID)
   141  	return args.Error(0)
   142  }
   143  
   144  func (fb *fakeBackend) SourceDataPath() string {
   145  	fb.RLock()
   146  	defer fb.RUnlock()
   147  	args := fb.Called()
   148  	return args.String(0)
   149  }
   150  
   151  func (fb *fakeBackend) IsExternal() bool {
   152  	return true
   153  }
   154  
   155  func (fb *fakeBackend) Name() string {
   156  	return "fakeBackend"
   157  }
   158  
   159  func (fb *fakeBackend) WriteToFile(ctx context.Context, backupID, key, destPath string) error {
   160  	fb.Lock()
   161  	defer fb.Unlock()
   162  	args := fb.Called(ctx, backupID, key, destPath)
   163  	return args.Error(0)
   164  }
   165  
   166  func (fb *fakeBackend) Read(ctx context.Context, backupID, key string, w io.WriteCloser) (int64, error) {
   167  	fb.Lock()
   168  	defer fb.Unlock()
   169  	defer w.Close()
   170  
   171  	args := fb.Called(ctx, backupID, key, w)
   172  	if err := args.Error(1); err != nil {
   173  		return 0, err
   174  	}
   175  
   176  	if data := fb.chunks[key]; data != nil {
   177  		io.Copy(w, bytes.NewReader(data))
   178  	}
   179  	return 0, args.Error(1)
   180  }
   181  
   182  func (fb *fakeBackend) Write(ctx context.Context, backupID, key string, r io.ReadCloser) (int64, error) {
   183  	fb.Lock()
   184  	defer fb.Unlock()
   185  	defer r.Close()
   186  
   187  	args := fb.Called(ctx, backupID, key, r)
   188  	if err := args.Error(1); err != nil {
   189  		return 0, err
   190  	}
   191  	buf := bytes.Buffer{}
   192  	n, err := io.Copy(&buf, r)
   193  	fb.files[backupID+"/"+key] = buf.Bytes()
   194  
   195  	return n, err
   196  }