zotregistry.io/zot@v1.4.4-0.20231124084042-02a8ed785457/pkg/test/mocks/image_store_mock.go (about)

     1  package mocks
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"time"
     7  
     8  	godigest "github.com/opencontainers/go-digest"
     9  	ispec "github.com/opencontainers/image-spec/specs-go/v1"
    10  	artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
    11  
    12  	"zotregistry.io/zot/pkg/scheduler"
    13  )
    14  
    15  type MockedImageStore struct {
    16  	NameFn              func() string
    17  	DirExistsFn         func(d string) bool
    18  	RootDirFn           func() string
    19  	InitRepoFn          func(name string) error
    20  	ValidateRepoFn      func(name string) (bool, error)
    21  	GetRepositoriesFn   func() ([]string, error)
    22  	GetNextRepositoryFn func(repo string) (string, error)
    23  	GetImageTagsFn      func(repo string) ([]string, error)
    24  	GetImageManifestFn  func(repo string, reference string) ([]byte, godigest.Digest, string, error)
    25  	PutImageManifestFn  func(repo string, reference string, mediaType string, body []byte) (godigest.Digest,
    26  		godigest.Digest, error)
    27  	DeleteImageManifestFn  func(repo string, reference string, detectCollision bool) error
    28  	BlobUploadPathFn       func(repo string, uuid string) string
    29  	NewBlobUploadFn        func(repo string) (string, error)
    30  	GetBlobUploadFn        func(repo string, uuid string) (int64, error)
    31  	BlobUploadInfoFn       func(repo string, uuid string) (int64, error)
    32  	PutBlobChunkStreamedFn func(repo string, uuid string, body io.Reader) (int64, error)
    33  	PutBlobChunkFn         func(repo string, uuid string, from int64, to int64, body io.Reader) (int64, error)
    34  	FinishBlobUploadFn     func(repo string, uuid string, body io.Reader, digest godigest.Digest) error
    35  	FullBlobUploadFn       func(repo string, body io.Reader, digest godigest.Digest) (string, int64, error)
    36  	DedupeBlobFn           func(src string, dstDigest godigest.Digest, dstRepo, dst string) error
    37  	DeleteBlobUploadFn     func(repo string, uuid string) error
    38  	BlobPathFn             func(repo string, digest godigest.Digest) string
    39  	CheckBlobFn            func(repo string, digest godigest.Digest) (bool, int64, error)
    40  	StatBlobFn             func(repo string, digest godigest.Digest) (bool, int64, time.Time, error)
    41  	GetBlobPartialFn       func(repo string, digest godigest.Digest, mediaType string, from, to int64,
    42  	) (io.ReadCloser, int64, int64, error)
    43  	GetBlobFn          func(repo string, digest godigest.Digest, mediaType string) (io.ReadCloser, int64, error)
    44  	DeleteBlobFn       func(repo string, digest godigest.Digest) error
    45  	GetIndexContentFn  func(repo string) ([]byte, error)
    46  	GetBlobContentFn   func(repo string, digest godigest.Digest) ([]byte, error)
    47  	GetReferrersFn     func(repo string, digest godigest.Digest, artifactTypes []string) (ispec.Index, error)
    48  	GetOrasReferrersFn func(repo string, digest godigest.Digest, artifactType string,
    49  	) ([]artifactspec.Descriptor, error)
    50  	URLForPathFn         func(path string) (string, error)
    51  	RunGCRepoFn          func(repo string) error
    52  	RunGCPeriodicallyFn  func(interval time.Duration, sch *scheduler.Scheduler)
    53  	RunDedupeBlobsFn     func(interval time.Duration, sch *scheduler.Scheduler)
    54  	RunDedupeForDigestFn func(ctx context.Context, digest godigest.Digest, dedupe bool,
    55  		duplicateBlobs []string) error
    56  	GetNextDigestWithBlobPathsFn func(repos []string, lastDigests []godigest.Digest) (godigest.Digest, []string, error)
    57  	GetAllBlobsFn                func(repo string) ([]string, error)
    58  	CleanupRepoFn                func(repo string, blobs []godigest.Digest, removeRepo bool) (int, error)
    59  	PutIndexContentFn            func(repo string, index ispec.Index) error
    60  	PopulateStorageMetricsFn     func(interval time.Duration, sch *scheduler.Scheduler)
    61  	StatIndexFn                  func(repo string) (bool, int64, time.Time, error)
    62  }
    63  
    64  func (is MockedImageStore) StatIndex(repo string) (bool, int64, time.Time, error) {
    65  	if is.StatIndexFn != nil {
    66  		return is.StatIndexFn(repo)
    67  	}
    68  
    69  	return true, 0, time.Time{}, nil
    70  }
    71  
    72  func (is MockedImageStore) Lock(t *time.Time) {
    73  }
    74  
    75  func (is MockedImageStore) Unlock(t *time.Time) {
    76  }
    77  
    78  func (is MockedImageStore) RUnlock(t *time.Time) {
    79  }
    80  
    81  func (is MockedImageStore) RLock(t *time.Time) {
    82  }
    83  
    84  func (is MockedImageStore) Name() string {
    85  	if is.NameFn != nil {
    86  		return is.NameFn()
    87  	}
    88  
    89  	return ""
    90  }
    91  
    92  func (is MockedImageStore) DirExists(d string) bool {
    93  	if is.DirExistsFn != nil {
    94  		return is.DirExistsFn(d)
    95  	}
    96  
    97  	return true
    98  }
    99  
   100  func (is MockedImageStore) RootDir() string {
   101  	if is.RootDirFn != nil {
   102  		return is.RootDirFn()
   103  	}
   104  
   105  	return ""
   106  }
   107  
   108  func (is MockedImageStore) InitRepo(name string) error {
   109  	if is.InitRepoFn != nil {
   110  		return is.InitRepoFn(name)
   111  	}
   112  
   113  	return nil
   114  }
   115  
   116  func (is MockedImageStore) ValidateRepo(name string) (bool, error) {
   117  	if is.ValidateRepoFn != nil {
   118  		return is.ValidateRepoFn(name)
   119  	}
   120  
   121  	return true, nil
   122  }
   123  
   124  func (is MockedImageStore) GetRepositories() ([]string, error) {
   125  	if is.GetRepositoriesFn != nil {
   126  		return is.GetRepositoriesFn()
   127  	}
   128  
   129  	return []string{}, nil
   130  }
   131  
   132  func (is MockedImageStore) GetNextRepository(repo string) (string, error) {
   133  	if is.GetNextRepositoryFn != nil {
   134  		return is.GetNextRepositoryFn(repo)
   135  	}
   136  
   137  	return "", nil
   138  }
   139  
   140  func (is MockedImageStore) GetImageManifest(repo string, reference string) ([]byte, godigest.Digest, string, error) {
   141  	if is.GetImageManifestFn != nil {
   142  		return is.GetImageManifestFn(repo, reference)
   143  	}
   144  
   145  	return []byte{}, "", "", nil
   146  }
   147  
   148  func (is MockedImageStore) PutImageManifest(
   149  	repo string,
   150  	reference string,
   151  	mediaType string,
   152  	body []byte,
   153  ) (godigest.Digest, godigest.Digest, error) {
   154  	if is.PutImageManifestFn != nil {
   155  		return is.PutImageManifestFn(repo, reference, mediaType, body)
   156  	}
   157  
   158  	return "", "", nil
   159  }
   160  
   161  func (is MockedImageStore) GetImageTags(name string) ([]string, error) {
   162  	if is.GetImageTagsFn != nil {
   163  		return is.GetImageTagsFn(name)
   164  	}
   165  
   166  	return []string{}, nil
   167  }
   168  
   169  func (is MockedImageStore) GetAllBlobs(repo string) ([]string, error) {
   170  	if is.GetAllBlobsFn != nil {
   171  		return is.GetAllBlobsFn(repo)
   172  	}
   173  
   174  	return []string{}, nil
   175  }
   176  
   177  func (is MockedImageStore) DeleteImageManifest(name string, reference string, detectCollision bool) error {
   178  	if is.DeleteImageManifestFn != nil {
   179  		return is.DeleteImageManifestFn(name, reference, detectCollision)
   180  	}
   181  
   182  	return nil
   183  }
   184  
   185  func (is MockedImageStore) NewBlobUpload(repo string) (string, error) {
   186  	if is.NewBlobUploadFn != nil {
   187  		return is.NewBlobUploadFn(repo)
   188  	}
   189  
   190  	return "", nil
   191  }
   192  
   193  func (is MockedImageStore) GetBlobUpload(repo string, uuid string) (int64, error) {
   194  	if is.GetBlobUploadFn != nil {
   195  		return is.GetBlobUploadFn(repo, uuid)
   196  	}
   197  
   198  	return 0, nil
   199  }
   200  
   201  func (is MockedImageStore) BlobUploadInfo(repo string, uuid string) (int64, error) {
   202  	if is.BlobUploadInfoFn != nil {
   203  		return is.BlobUploadInfoFn(repo, uuid)
   204  	}
   205  
   206  	return 0, nil
   207  }
   208  
   209  func (is MockedImageStore) BlobUploadPath(repo string, uuid string) string {
   210  	if is.BlobUploadPathFn != nil {
   211  		return is.BlobUploadPathFn(repo, uuid)
   212  	}
   213  
   214  	return ""
   215  }
   216  
   217  func (is MockedImageStore) PutBlobChunkStreamed(repo string, uuid string, body io.Reader) (int64, error) {
   218  	if is.PutBlobChunkStreamedFn != nil {
   219  		return is.PutBlobChunkStreamedFn(repo, uuid, body)
   220  	}
   221  
   222  	return 0, nil
   223  }
   224  
   225  func (is MockedImageStore) PutBlobChunk(
   226  	repo string,
   227  	uuid string,
   228  	from int64,
   229  	to int64,
   230  	body io.Reader,
   231  ) (int64, error) {
   232  	if is.PutBlobChunkFn != nil {
   233  		return is.PutBlobChunkFn(repo, uuid, from, to, body)
   234  	}
   235  
   236  	return 0, nil
   237  }
   238  
   239  func (is MockedImageStore) FinishBlobUpload(repo string, uuid string, body io.Reader, digest godigest.Digest) error {
   240  	if is.FinishBlobUploadFn != nil {
   241  		return is.FinishBlobUploadFn(repo, uuid, body, digest)
   242  	}
   243  
   244  	return nil
   245  }
   246  
   247  func (is MockedImageStore) FullBlobUpload(repo string, body io.Reader, digest godigest.Digest) (string, int64, error) {
   248  	if is.FullBlobUploadFn != nil {
   249  		return is.FullBlobUploadFn(repo, body, digest)
   250  	}
   251  
   252  	return "", 0, nil
   253  }
   254  
   255  func (is MockedImageStore) DedupeBlob(src string, dstDigest godigest.Digest, dstRepo, dst string) error {
   256  	if is.DedupeBlobFn != nil {
   257  		return is.DedupeBlobFn(src, dstDigest, dstRepo, dst)
   258  	}
   259  
   260  	return nil
   261  }
   262  
   263  func (is MockedImageStore) DeleteBlob(repo string, digest godigest.Digest) error {
   264  	if is.DeleteBlobFn != nil {
   265  		return is.DeleteBlobFn(repo, digest)
   266  	}
   267  
   268  	return nil
   269  }
   270  
   271  func (is MockedImageStore) BlobPath(repo string, digest godigest.Digest) string {
   272  	if is.BlobPathFn != nil {
   273  		return is.BlobPathFn(repo, digest)
   274  	}
   275  
   276  	return ""
   277  }
   278  
   279  func (is MockedImageStore) CheckBlob(repo string, digest godigest.Digest) (bool, int64, error) {
   280  	if is.CheckBlobFn != nil {
   281  		return is.CheckBlobFn(repo, digest)
   282  	}
   283  
   284  	return true, 0, nil
   285  }
   286  
   287  func (is MockedImageStore) StatBlob(repo string, digest godigest.Digest) (bool, int64, time.Time, error) {
   288  	if is.StatBlobFn != nil {
   289  		return is.StatBlobFn(repo, digest)
   290  	}
   291  
   292  	return true, 0, time.Time{}, nil
   293  }
   294  
   295  func (is MockedImageStore) GetBlobPartial(repo string, digest godigest.Digest, mediaType string, from, to int64,
   296  ) (io.ReadCloser, int64, int64, error) {
   297  	if is.GetBlobPartialFn != nil {
   298  		return is.GetBlobPartialFn(repo, digest, mediaType, from, to)
   299  	}
   300  
   301  	return io.NopCloser(&io.LimitedReader{}), 0, 0, nil
   302  }
   303  
   304  func (is MockedImageStore) GetBlob(repo string, digest godigest.Digest, mediaType string,
   305  ) (io.ReadCloser, int64, error) {
   306  	if is.GetBlobFn != nil {
   307  		return is.GetBlobFn(repo, digest, mediaType)
   308  	}
   309  
   310  	return io.NopCloser(&io.LimitedReader{}), 0, nil
   311  }
   312  
   313  func (is MockedImageStore) DeleteBlobUpload(repo string, uuid string) error {
   314  	if is.DeleteBlobUploadFn != nil {
   315  		return is.DeleteBlobUploadFn(repo, uuid)
   316  	}
   317  
   318  	return nil
   319  }
   320  
   321  func (is MockedImageStore) GetIndexContent(repo string) ([]byte, error) {
   322  	if is.GetIndexContentFn != nil {
   323  		return is.GetIndexContentFn(repo)
   324  	}
   325  
   326  	return []byte{}, nil
   327  }
   328  
   329  func (is MockedImageStore) GetBlobContent(repo string, digest godigest.Digest) ([]byte, error) {
   330  	if is.GetBlobContentFn != nil {
   331  		return is.GetBlobContentFn(repo, digest)
   332  	}
   333  
   334  	return []byte{}, nil
   335  }
   336  
   337  func (is MockedImageStore) GetReferrers(
   338  	repo string, digest godigest.Digest,
   339  	artifactTypes []string,
   340  ) (ispec.Index, error) {
   341  	if is.GetReferrersFn != nil {
   342  		return is.GetReferrersFn(repo, digest, artifactTypes)
   343  	}
   344  
   345  	return ispec.Index{}, nil
   346  }
   347  
   348  func (is MockedImageStore) GetOrasReferrers(
   349  	repo string,
   350  	digest godigest.Digest,
   351  	artifactType string,
   352  ) ([]artifactspec.Descriptor, error) {
   353  	if is.GetOrasReferrersFn != nil {
   354  		return is.GetOrasReferrersFn(repo, digest, artifactType)
   355  	}
   356  
   357  	return []artifactspec.Descriptor{}, nil
   358  }
   359  
   360  func (is MockedImageStore) URLForPath(path string) (string, error) {
   361  	if is.URLForPathFn != nil {
   362  		return is.URLForPathFn(path)
   363  	}
   364  
   365  	return "", nil
   366  }
   367  
   368  func (is MockedImageStore) RunGCRepo(repo string) error {
   369  	if is.RunGCRepoFn != nil {
   370  		return is.RunGCRepoFn(repo)
   371  	}
   372  
   373  	return nil
   374  }
   375  
   376  func (is MockedImageStore) RunGCPeriodically(interval time.Duration, sch *scheduler.Scheduler) {
   377  	if is.RunGCPeriodicallyFn != nil {
   378  		is.RunGCPeriodicallyFn(interval, sch)
   379  	}
   380  }
   381  
   382  func (is MockedImageStore) RunDedupeBlobs(interval time.Duration, sch *scheduler.Scheduler) {
   383  	if is.RunDedupeBlobsFn != nil {
   384  		is.RunDedupeBlobsFn(interval, sch)
   385  	}
   386  }
   387  
   388  func (is MockedImageStore) RunDedupeForDigest(ctx context.Context, digest godigest.Digest, dedupe bool,
   389  	duplicateBlobs []string,
   390  ) error {
   391  	if is.RunDedupeForDigestFn != nil {
   392  		return is.RunDedupeForDigestFn(ctx, digest, dedupe, duplicateBlobs)
   393  	}
   394  
   395  	return nil
   396  }
   397  
   398  func (is MockedImageStore) GetNextDigestWithBlobPaths(repos []string, lastDigests []godigest.Digest,
   399  ) (godigest.Digest, []string, error) {
   400  	if is.GetNextDigestWithBlobPathsFn != nil {
   401  		return is.GetNextDigestWithBlobPathsFn(repos, lastDigests)
   402  	}
   403  
   404  	return "", []string{}, nil
   405  }
   406  
   407  func (is MockedImageStore) CleanupRepo(repo string, blobs []godigest.Digest, removeRepo bool) (int, error) {
   408  	if is.CleanupRepoFn != nil {
   409  		return is.CleanupRepoFn(repo, blobs, removeRepo)
   410  	}
   411  
   412  	return 0, nil
   413  }
   414  
   415  func (is MockedImageStore) PutIndexContent(repo string, index ispec.Index) error {
   416  	if is.PutIndexContentFn != nil {
   417  		return is.PutIndexContentFn(repo, index)
   418  	}
   419  
   420  	return nil
   421  }
   422  
   423  func (is MockedImageStore) PopulateStorageMetrics(interval time.Duration, sch *scheduler.Scheduler) {
   424  	if is.PopulateStorageMetricsFn != nil {
   425  		is.PopulateStorageMetricsFn(interval, sch)
   426  	}
   427  }