Skip to content

Commit

Permalink
reporegistry: add new LoadAllRepositoriesFromFS and use in tests
Browse files Browse the repository at this point in the history
This commit is a (hopefully) less controversial version of:
osbuild#1037

Here only a new helper to load repository information from an
`fs.FS` is added and only used for loading the test repositories.

This will help to have a single test repository that can be
embedded accross osbuild-composer, image-builder and images.

It also removes the public "NewTestedDefault()" in favor of a
new `testrepos.New()` helper so that the `reporegistry` only
has non-test API left.
  • Loading branch information
mvo5 authored and thozza committed Nov 16, 2024
1 parent 0584c20 commit d7ae209
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 44 deletions.
4 changes: 2 additions & 2 deletions cmd/gen-manifests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"github.com/osbuild/images/pkg/dnfjson"
"github.com/osbuild/images/pkg/manifest"
"github.com/osbuild/images/pkg/ostree"
"github.com/osbuild/images/pkg/reporegistry"
"github.com/osbuild/images/pkg/rhsm/facts"
"github.com/osbuild/images/pkg/rpmmd"
"github.com/osbuild/images/pkg/sbom"
testrepos "github.com/osbuild/images/test/data/repositories"
)

type buildRequest struct {
Expand Down Expand Up @@ -513,7 +513,7 @@ func main() {

flag.Parse()

testedRepoRegistry, err := reporegistry.NewTestedDefault()
testedRepoRegistry, err := testrepos.New()
if err != nil {
panic(fmt.Sprintf("failed to create repo registry with tested distros: %v", err))
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/list-images/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/gobwas/glob"
"github.com/osbuild/images/pkg/distrofactory"
"github.com/osbuild/images/pkg/reporegistry"
testrepos "github.com/osbuild/images/test/data/repositories"
)

type multiValue []string
Expand Down Expand Up @@ -75,7 +75,7 @@ func main() {
flag.BoolVar(&json, "json", false, "print configs as json")
flag.Parse()

testedRepoRegistry, err := reporegistry.NewTestedDefault()
testedRepoRegistry, err := testrepos.New()
if err != nil {
panic(fmt.Sprintf("failed to create repo registry with tested distros: %v", err))
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/osbuild-composer-image-definitions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"os"

"github.com/osbuild/images/pkg/distrofactory"
"github.com/osbuild/images/pkg/reporegistry"
testrepos "github.com/osbuild/images/test/data/repositories"
)

func main() {
definitions := map[string]map[string][]string{}
distroFac := distrofactory.NewDefault()

testedRepoRegistry, err := reporegistry.NewTestedDefault()
testedRepoRegistry, err := testrepos.New()
if err != nil {
panic(fmt.Sprintf("failed to create repo registry with tested distros: %v", err))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/distro/distro_test_common/distro_test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/ostree"
"github.com/osbuild/images/pkg/reporegistry"
testrepos "github.com/osbuild/images/test/data/repositories"
)

const RandomTestSeed = 0
Expand Down Expand Up @@ -463,7 +463,7 @@ func TestDistro_OSTreeOptions(t *testing.T, d distro.Distro) {

// ListTestedDistros returns a list of distro names that are explicitly tested
func ListTestedDistros(t *testing.T) []string {
testRepoRegistry, err := reporegistry.NewTestedDefault()
testRepoRegistry, err := testrepos.New()
require.Nil(t, err)
require.NotEmpty(t, testRepoRegistry)
distros := testRepoRegistry.ListDistros()
Expand Down
6 changes: 3 additions & 3 deletions pkg/imagefilter/imagefilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (

"github.com/osbuild/images/pkg/distrofactory"
"github.com/osbuild/images/pkg/imagefilter"
"github.com/osbuild/images/pkg/reporegistry"
testrepos "github.com/osbuild/images/test/data/repositories"
)

func TestImageFilterSmoke(t *testing.T) {
logrus.SetLevel(logrus.WarnLevel)

fac := distrofactory.NewDefault()
repos, err := reporegistry.NewTestedDefault()
repos, err := testrepos.New()
require.NoError(t, err)

imgFilter, err := imagefilter.New(fac, repos)
Expand All @@ -29,7 +29,7 @@ func TestImageFilterSmoke(t *testing.T) {

func TestImageFilterFilter(t *testing.T) {
fac := distrofactory.NewDefault()
repos, err := reporegistry.NewTestedDefault()
repos, err := testrepos.New()
require.NoError(t, err)

imgFilter, err := imagefilter.New(fac, repos)
Expand Down
15 changes: 0 additions & 15 deletions pkg/reporegistry/reporegistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package reporegistry

import (
"fmt"
"path/filepath"
"runtime"

"github.com/osbuild/images/pkg/distroidparser"
"github.com/osbuild/images/pkg/rpmmd"
Expand All @@ -27,19 +25,6 @@ func New(repoConfigPaths []string) (*RepoRegistry, error) {
return &RepoRegistry{repositories}, nil
}

// NewTestedDefault returns a new RepoRegistry instance with the data
// loaded from the default test repositories
func NewTestedDefault() (*RepoRegistry, error) {
_, callerSrc, _, ok := runtime.Caller(0)
var testReposPath []string
if !ok {
testReposPath = append(testReposPath, "../../test/data")
} else {
testReposPath = append(testReposPath, filepath.Join(filepath.Dir(callerSrc), "../../test/data"))
}
return New(testReposPath)
}

func NewFromDistrosRepoConfigs(distrosRepoConfigs rpmmd.DistrosRepoConfigs) *RepoRegistry {
return &RepoRegistry{distrosRepoConfigs}
}
Expand Down
10 changes: 2 additions & 8 deletions pkg/reporegistry/reporegistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"

"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distro/test_distro"
"github.com/osbuild/images/pkg/rpmmd"
"github.com/stretchr/testify/assert"
)

func getTestingRepoRegistry() *RepoRegistry {
Expand Down Expand Up @@ -370,10 +371,3 @@ func TestInvalidReposByArchName(t *testing.T) {
})
}
}

func Test_NewTestedDefault(t *testing.T) {
rr, err := NewTestedDefault()
assert.Nil(t, err)
assert.NotNil(t, rr)
assert.NotEmpty(t, rr.ListDistros())
}
30 changes: 21 additions & 9 deletions pkg/reporegistry/repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package reporegistry

import (
"io/fs"
"os"
"path/filepath"
"strings"
Expand All @@ -14,12 +15,24 @@ import (
// LoadAllRepositories loads all repositories for given distros from the given list of paths.
// Behavior is the same as with the LoadRepositories() method.
func LoadAllRepositories(confPaths []string) (rpmmd.DistrosRepoConfigs, error) {
distrosRepoConfigs := rpmmd.DistrosRepoConfigs{}
var confFSes []fs.FS

for _, confPath := range confPaths {
reposPath := filepath.Join(confPath, "repositories")
confFSes = append(confFSes, os.DirFS(filepath.Join(confPath, "repositories")))
}

distrosRepoConfigs, err := LoadAllRepositoriesFromFS(confFSes)
if len(distrosRepoConfigs) == 0 {
return nil, &NoReposLoadedError{confPaths}
}
return distrosRepoConfigs, err
}

func LoadAllRepositoriesFromFS(confPaths []fs.FS) (rpmmd.DistrosRepoConfigs, error) {
distrosRepoConfigs := rpmmd.DistrosRepoConfigs{}

fileEntries, err := os.ReadDir(reposPath)
for _, confPath := range confPaths {
fileEntries, err := fs.ReadDir(confPath, ".")
if os.IsNotExist(err) {
continue
} else if err != nil {
Expand Down Expand Up @@ -53,8 +66,11 @@ func LoadAllRepositories(confPaths []string) (rpmmd.DistrosRepoConfigs, error) {
continue
}

configFile := filepath.Join(reposPath, fileEntry.Name())
distroRepos, err := rpmmd.LoadRepositoriesFromFile(configFile)
configFile, err := confPath.Open(fileEntry.Name())
if err != nil {
return nil, err
}
distroRepos, err := rpmmd.LoadRepositoriesFromReader(configFile)
if err != nil {
return nil, err
}
Expand All @@ -66,10 +82,6 @@ func LoadAllRepositories(confPaths []string) (rpmmd.DistrosRepoConfigs, error) {
}
}

if len(distrosRepoConfigs) == 0 {
return nil, &NoReposLoadedError{confPaths}
}

return distrosRepoConfigs, nil
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/rpmmd/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -232,10 +233,14 @@ func LoadRepositoriesFromFile(filename string) (map[string][]RepoConfig, error)
}
defer f.Close()

return LoadRepositoriesFromReader(f)
}

func LoadRepositoriesFromReader(r io.Reader) (map[string][]RepoConfig, error) {
var reposMap map[string][]repository
repoConfigs := make(map[string][]RepoConfig)

err = json.NewDecoder(f).Decode(&reposMap)
err := json.NewDecoder(r).Decode(&reposMap)
if err != nil {
return nil, err
}
Expand Down
19 changes: 19 additions & 0 deletions test/data/repositories/testrepos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package testrepos

import (
"embed"
"io/fs"

"github.com/osbuild/images/pkg/reporegistry"
)

//go:embed *.json
var FS embed.FS

func New() (*reporegistry.RepoRegistry, error) {
repositories, err := reporegistry.LoadAllRepositoriesFromFS([]fs.FS{FS})
if err != nil {
return nil, err
}
return reporegistry.NewFromDistrosRepoConfigs(repositories), nil
}

0 comments on commit d7ae209

Please sign in to comment.