github.com/quay/claircore@v1.5.28/test/package.go (about) 1 package test 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/quay/claircore" 8 ) 9 10 // GenDuplicatePackages creates an array of packages with duplicates. 11 // 12 // The array will will take n/2 and use this as a mod operand along with the 13 // current index in the for loop. It is an error to set n less than 2. 14 func GenDuplicatePackages(n int) ([]*claircore.Package, error) { 15 if n < 2 { 16 return nil, fmt.Errorf("cannot create duplicate packages with n = %d; must be > 1", n) 17 } 18 19 pkgs := []*claircore.Package{} 20 nn := n / 2 21 for i := 0; i < n; i++ { 22 ii := i % nn 23 pkgs = append(pkgs, createPackage(i, ii, n)) 24 } 25 26 return pkgs, nil 27 } 28 29 // GenUniquePackages creates an array of unique packages. 30 // 31 // The array is guaranteed not to have any duplicated fields. Source packages 32 // are given an n + 1 ID to avoid duplicated primary keys on insert. 33 func GenUniquePackages(n int) []*claircore.Package { 34 pkgs := []*claircore.Package{} 35 for i := 0; i < n; i++ { 36 pkgs = append(pkgs, createPackage(i, i, n)) 37 } 38 39 return pkgs 40 } 41 42 func createPackage(i int, ii int, n int) *claircore.Package { 43 return &claircore.Package{ 44 ID: strconv.Itoa(ii), 45 Name: fmt.Sprintf("package-%d", ii), 46 Version: fmt.Sprintf("version-%d", ii), 47 Arch: fmt.Sprintf("arch-%d", ii), 48 Kind: claircore.BINARY, 49 PackageDB: fmt.Sprintf("package-db-%d", i), 50 RepositoryHint: fmt.Sprintf("repository-hint-%d", i), 51 Module: fmt.Sprintf("module:%d", ii), 52 Source: &claircore.Package{ 53 ID: strconv.Itoa(n + i), 54 Name: fmt.Sprintf("source-package-%d", ii), 55 Version: fmt.Sprintf("source-version-%d", ii), 56 Arch: fmt.Sprintf("source-arch-%d", ii), 57 Kind: claircore.SOURCE, 58 Module: fmt.Sprintf("source-module:%d", ii), 59 }, 60 } 61 }