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

     1  package mocks
     2  
     3  import (
     4  	"context"
     5  
     6  	"zotregistry.io/zot/pkg/common"
     7  	cvemodel "zotregistry.io/zot/pkg/extensions/search/cve/model"
     8  )
     9  
    10  type CveInfoMock struct {
    11  	GetImageListForCVEFn       func(ctx context.Context, repo, cveID string) ([]cvemodel.TagInfo, error)
    12  	GetImageListWithCVEFixedFn func(ctx context.Context, repo, cveID string) ([]cvemodel.TagInfo, error)
    13  	GetCVEListForImageFn       func(ctx context.Context, repo string, reference string, searchedCVE string,
    14  		pageInput cvemodel.PageInput) ([]cvemodel.CVE, common.PageInfo, error)
    15  	GetCVESummaryForImageMediaFn func(ctx context.Context, repo string, digest, mediaType string,
    16  	) (cvemodel.ImageCVESummary, error)
    17  }
    18  
    19  func (cveInfo CveInfoMock) GetImageListForCVE(ctx context.Context, repo, cveID string) ([]cvemodel.TagInfo, error) {
    20  	if cveInfo.GetImageListForCVEFn != nil {
    21  		return cveInfo.GetImageListForCVEFn(ctx, repo, cveID)
    22  	}
    23  
    24  	return []cvemodel.TagInfo{}, nil
    25  }
    26  
    27  func (cveInfo CveInfoMock) GetImageListWithCVEFixed(ctx context.Context, repo, cveID string,
    28  ) ([]cvemodel.TagInfo, error) {
    29  	if cveInfo.GetImageListWithCVEFixedFn != nil {
    30  		return cveInfo.GetImageListWithCVEFixedFn(ctx, repo, cveID)
    31  	}
    32  
    33  	return []cvemodel.TagInfo{}, nil
    34  }
    35  
    36  func (cveInfo CveInfoMock) GetCVEListForImage(ctx context.Context, repo string, reference string,
    37  	searchedCVE string, pageInput cvemodel.PageInput,
    38  ) (
    39  	[]cvemodel.CVE,
    40  	common.PageInfo,
    41  	error,
    42  ) {
    43  	if cveInfo.GetCVEListForImageFn != nil {
    44  		return cveInfo.GetCVEListForImageFn(ctx, repo, reference, searchedCVE, pageInput)
    45  	}
    46  
    47  	return []cvemodel.CVE{}, common.PageInfo{}, nil
    48  }
    49  
    50  func (cveInfo CveInfoMock) GetCVESummaryForImageMedia(ctx context.Context, repo, digest, mediaType string,
    51  ) (cvemodel.ImageCVESummary, error) {
    52  	if cveInfo.GetCVESummaryForImageMediaFn != nil {
    53  		return cveInfo.GetCVESummaryForImageMediaFn(ctx, repo, digest, mediaType)
    54  	}
    55  
    56  	return cvemodel.ImageCVESummary{}, nil
    57  }
    58  
    59  type CveScannerMock struct {
    60  	IsImageFormatScannableFn func(repo string, reference string) (bool, error)
    61  	IsImageMediaScannableFn  func(repo string, digest, mediaType string) (bool, error)
    62  	IsResultCachedFn         func(digest string) bool
    63  	GetCachedResultFn        func(digest string) map[string]cvemodel.CVE
    64  	ScanImageFn              func(ctx context.Context, image string) (map[string]cvemodel.CVE, error)
    65  	UpdateDBFn               func(ctx context.Context) error
    66  }
    67  
    68  func (scanner CveScannerMock) IsImageFormatScannable(repo string, reference string) (bool, error) {
    69  	if scanner.IsImageFormatScannableFn != nil {
    70  		return scanner.IsImageFormatScannableFn(repo, reference)
    71  	}
    72  
    73  	return true, nil
    74  }
    75  
    76  func (scanner CveScannerMock) IsImageMediaScannable(repo string, digest, mediaType string) (bool, error) {
    77  	if scanner.IsImageMediaScannableFn != nil {
    78  		return scanner.IsImageMediaScannableFn(repo, digest, mediaType)
    79  	}
    80  
    81  	return true, nil
    82  }
    83  
    84  func (scanner CveScannerMock) IsResultCached(digest string) bool {
    85  	if scanner.IsResultCachedFn != nil {
    86  		return scanner.IsResultCachedFn(digest)
    87  	}
    88  
    89  	return false
    90  }
    91  
    92  func (scanner CveScannerMock) GetCachedResult(digest string) map[string]cvemodel.CVE {
    93  	if scanner.GetCachedResultFn != nil {
    94  		return scanner.GetCachedResultFn(digest)
    95  	}
    96  
    97  	return map[string]cvemodel.CVE{}
    98  }
    99  
   100  func (scanner CveScannerMock) ScanImage(ctx context.Context, image string) (map[string]cvemodel.CVE, error) {
   101  	if scanner.ScanImageFn != nil {
   102  		return scanner.ScanImageFn(ctx, image)
   103  	}
   104  
   105  	return map[string]cvemodel.CVE{}, nil
   106  }
   107  
   108  func (scanner CveScannerMock) UpdateDB(ctx context.Context) error {
   109  	if scanner.UpdateDBFn != nil {
   110  		return scanner.UpdateDBFn(ctx)
   111  	}
   112  
   113  	return nil
   114  }