test

package
v1.5.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2024 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Overview

Package test holds helpers for tests in the claircore module.

Compatibility

This hierarchy is intended for packages in this module and other packages developed in close collaboration, and as such are considered exempt from API compatibility considerations.

Index

Constants

View Source
const MediaType = `application/vnd.oci.image.layer.nondistributable.v1.tar`

MediaType is a media type that can be used in [claircore.LayerDescription]s in tests.

Variables

View Source
var AnyDescription = claircore.LayerDescription{
	Digest:    `sha256:` + strings.Repeat(`deadbeef`, 8),
	URI:       `example:test.AnyDescription`,
	MediaType: MediaType,
	Headers:   make(map[string][]string),
}

AnyDescription is pre-made [LayerDescription] for cases where the actual contents of the description *shouldn't* matter.

Functions

func GenDuplicatePackages

func GenDuplicatePackages(n int) ([]*claircore.Package, error)

GenDuplicatePackages creates an array of packages with duplicates.

The array will will take n/2 and use this as a mod operand along with the current index in the for loop. It is an error to set n less than 2.

func GenEnrichments added in v1.5.21

func GenEnrichments(n int) []driver.EnrichmentRecord

GenEnrichments creates an array of enrichment records, with no meaningful content.

func GenUniqueDistributionScanners

func GenUniqueDistributionScanners(n int) indexer.VersionedScanners

GenUniqueDistributionScanners creates n number of unique DistributionScanners. the array is gauranteed to not have any scanner fields be duplicates

func GenUniqueDistributions

func GenUniqueDistributions(n int) []*claircore.Distribution

GenUniqueDistributions creates an array of unique distributions.

The array is guaranteed not to have any duplicated fields.

func GenUniquePackageScanners

func GenUniquePackageScanners(n int) indexer.VersionedScanners

GenUniquePackageScanners creates n number of unique PackageScanners. the array is gauranteed to not have any scanner fields be duplicates

func GenUniquePackages

func GenUniquePackages(n int) []*claircore.Package

GenUniquePackages creates an array of unique packages.

The array is guaranteed not to have any duplicated fields. Source packages are given an n + 1 ID to avoid duplicated primary keys on insert.

func GenUniqueRepositories

func GenUniqueRepositories(n int, opts ...GenRepoOption) []*claircore.Repository

GenUniqueRepositories creates an array of unique repositories.

The array is guaranteed not to have any duplicated fields.

func GenUniqueRepositoryScanners

func GenUniqueRepositoryScanners(n int) indexer.VersionedScanners

GenUniqueRepositoryScanners creates n number of unique RepositoryScanners. the array is gauranteed to not have any scanner fields be duplicates

func GenUniqueVulnerabilities

func GenUniqueVulnerabilities(n int, updater string) []*claircore.Vulnerability

GenUniqueVulnerabilities creates an array of unique Vulnerabilities.

The array is guaranteed not to have any duplicated fields.

func GenerateFixture added in v1.5.20

func GenerateFixture(t testing.TB, name string, stamp time.Time, gen func(testing.TB, *os.File)) string

GenerateFixture is a helper for generating a test fixture. A path that can be used to open the file is returned.

If the test fails, the cached file is removed. It is the caller's responsibility to ensure that "name" is unique per-package.

func Modtime added in v1.5.20

func Modtime(t testing.TB, path string) time.Time

Modtime is a helper for picking a timestamp to use with GenerateFixture and [GenerateLayer].

It reports the modtime of the passed path. If the file does not exist, the start of the UNIX epoch is returned. If the file is not regular or a directory, the test is failed. If the file is a directory, the newest time of all the entries is reported.

func RandomSHA256Digest added in v0.0.14

func RandomSHA256Digest(t testing.TB) claircore.Digest

RandomSHA256Digest returns a random Digest.

func RealizeLayer added in v1.5.20

func RealizeLayer(ctx context.Context, t *testing.T, ref LayerRef) *claircore.Layer

RealizeLayer is a helper around RealizeLayers for a single layer.

This is useful for testing a Scanner implementation.

func RealizeLayers added in v0.0.13

func RealizeLayers(ctx context.Context, t *testing.T, refs ...LayerRef) []claircore.Layer

RealizeLayers uses fetch.Layer to populate a directory and returns a slice of Layers describing them.

Any needed cleanup is handled via the passed testing.T.

func ServeLayers added in v0.0.13

func ServeLayers(t *testing.T, n int) (*http.Client, []claircore.LayerDescription)

ServeLayers constructs "n" random layers, arranges to serve them, and returns corresponding LayerDescriptions.

func WFN added in v0.0.24

func WFN(i int) cpe.WFN

WFN returns a cpe.WFN with "i" as the CPE version.

Types

type CachedArena added in v1.5.20

type CachedArena struct {
	// contains filtered or unexported fields
}

CachedArena is an indexer.FetchArena that populates Layers out of the testing caches.

func NewCachedArena added in v1.5.20

func NewCachedArena(t testing.TB) *CachedArena

NewCachedArena returns an initialized CachedArena.

func (*CachedArena) Close added in v1.5.20

func (a *CachedArena) Close(_ context.Context) error

Close implements indexer.FetchArena.

func (*CachedArena) GenerateLayer added in v1.5.20

func (a *CachedArena) GenerateLayer(t testing.TB, name string, stamp time.Time, gen func(testing.TB, *os.File))

GenerateLayer is used for tests that generate their layer data rather than fetch it from a registry.

If the test fails, the cached file is removed. If successful, the layer can be referenced by using a relative file URI for "name". That is, if the passed name is "layer.tar", a claircore.LayerDescription should use a URI of "file:layer.tar".

It is the caller's responsibility to ensure that "name" is unique per-package.

func (*CachedArena) LoadLayerFromRegistry added in v1.5.20

func (a *CachedArena) LoadLayerFromRegistry(ctx context.Context, t testing.TB, ref LayerRef)

LoadLayerFromRegistry fetches a layer from a registry into the appropriate cache.

func (*CachedArena) Realizer added in v1.5.20

func (a *CachedArena) Realizer(_ context.Context) indexer.Realizer

Realizer implements indexer.FetchArena.

type CachedRealizer added in v1.5.20

type CachedRealizer struct {
	// contains filtered or unexported fields
}

CachedRealizer is the indexer.Realizer returned by CachedArena.

func (*CachedRealizer) Close added in v1.5.20

func (r *CachedRealizer) Close() error

Close implements indexer.Realizer and indexer.DescriptionRealizer.

func (*CachedRealizer) Realize added in v1.5.20

func (r *CachedRealizer) Realize(ctx context.Context, ls []*claircore.Layer) error

Realize implements indexer.Realizer.

func (*CachedRealizer) RealizeDescriptions added in v1.5.20

func (r *CachedRealizer) RealizeDescriptions(ctx context.Context, descs []claircore.LayerDescription) ([]claircore.Layer, error)

RealizeDescriptions implements indexer.DescriptionRealizer.

type DigestMatcher added in v1.5.20

type DigestMatcher string

DigestMatcher is a [gomock.Matcher] for [claircore.Digest]s.

func (*DigestMatcher) Matches added in v1.5.20

func (d *DigestMatcher) Matches(x any) bool

Matches implements [gomock.Matcher].

func (*DigestMatcher) String added in v1.5.20

func (d *DigestMatcher) String() string

String implements [gomock.Matcher].

type GenRepoOption added in v0.0.13

type GenRepoOption func(*claircore.Repository)

GenRepoOption is a functional option to GenUniqueRepositories.

type LayerMatcher added in v1.5.20

type LayerMatcher struct {
	*claircore.LayerDescription
}

LayerMatcher is a [gomock.Matcher] for [claircore.LayerDescription]s that allows them to match both [LayerDescription] and [Layer].

func NewLayerMatcher added in v1.5.20

func NewLayerMatcher(desc *claircore.LayerDescription) *LayerMatcher

NewLayerMatcher returns a LayerMatcher.

func (*LayerMatcher) DigestMatcher added in v1.5.20

func (m *LayerMatcher) DigestMatcher() *DigestMatcher

DigestMatcher returns a [gomock.Matcher] implementation for the digest of the layer.

func (*LayerMatcher) Matches added in v1.5.20

func (m *LayerMatcher) Matches(x any) bool

Matches implements [gomock.Matcher].

func (*LayerMatcher) String added in v1.5.20

func (m *LayerMatcher) String() string

String implements [gomock.Matcher].

type LayerRef added in v1.5.20

type LayerRef struct {
	Registry string
	Name     string
	Digest   string
}

LayerRef is a remote layer.

type ScannerTestcase added in v0.0.16

type ScannerTestcase struct {
	Domain  string
	Name    string
	Hash    string
	Want    []*claircore.Package
	Scanner indexer.PackageScanner
}

ScannerTestcase can be used for testing layers found in the wild against a scanner.

Tests that use this struct should not be marked as integration tests, as the Run method does that internally if it needs to talk to the network.

func (ScannerTestcase) Digest added in v0.0.16

func (tc ScannerTestcase) Digest() claircore.Digest

Digest reports the digest in the Hash member.

Panics if an error is returned from ParseDigest.

func (ScannerTestcase) Run added in v0.0.16

func (tc ScannerTestcase) Run(ctx context.Context) func(*testing.T)

Run returns a function suitable for using with (*testing.T).Run.

This function assumes the returned values must exactly match tc.Want. If tc.Want only covers a subset of potential returned values, then use RunSubset.

func (ScannerTestcase) RunSubset added in v1.4.20

func (tc ScannerTestcase) RunSubset(ctx context.Context, n int) func(*testing.T)

RunSubset returns a function suitable for using with (*testing.T).Run.

This function is similar to except it assumes tc.Want is a subset of all potential values. n is the total number of expected packages, ie len(got).

Directories

Path Synopsis
Bisect is a git bisect helper.
Bisect is a git bisect helper.
Package fetch implements just enough of a client for the OCI Distribution specification for use in tests.
Package fetch implements just enough of a client for the OCI Distribution specification for use in tests.
Package integration is a helper for running integration tests.
Package integration is a helper for running integration tests.
mock
driver
Package mock_driver is a generated GoMock package.
Package mock_driver is a generated GoMock package.
indexer
Package mock_indexer is a generated GoMock package.
Package mock_indexer is a generated GoMock package.
updater
Package mock_updater is a generated GoMock package.
Package mock_updater is a generated GoMock package.
updater/driver/v1
Package mock_driver is a generated GoMock package.
Package mock_driver is a generated GoMock package.
Ovaldebug is a helper for debugging the ovalutil package.
Ovaldebug is a helper for debugging the ovalutil package.
Package periodic contains tests meant to be run periodically in CI.
Package periodic contains tests meant to be run periodically in CI.
Package postgres contains testing helpers for PostgreSQL databases.
Package postgres contains testing helpers for PostgreSQL databases.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL