Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions cmd/image-builder/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,22 @@ import (
"fmt"
"io/fs"
"net/url"
"path/filepath"

"github.com/osbuild/images/data/repositories"
"github.com/osbuild/images/pkg/reporegistry"
"github.com/osbuild/images/pkg/rpmmd"
)

// defaultDataDirs contains the default search paths to look for
// defaultRepoDirs contains the default search paths to look for
// repository data. They contain a bunch of json files of the form
// "$distro_$version".json (but that is an implementation detail that
// the "images" library takes care of).
var defaultDataDirs = []string{
var defaultRepoDirs = []string{
"/etc/image-builder/repositories",
"/usr/share/image-builder/repositories",
}

type repoConfig struct {
DataDir string
ExtraRepos []string
}

func parseRepoURLs(repoURLs []string, what string) ([]rpmmd.RepoConfig, error) {
var repoConf []rpmmd.RepoConfig

Expand Down Expand Up @@ -58,14 +54,14 @@ func parseRepoURLs(repoURLs []string, what string) ([]rpmmd.RepoConfig, error) {
}

func newRepoRegistryImpl(dataDir string, extraRepos []string) (*reporegistry.RepoRegistry, error) {
var dataDirs []string
var repoDirs []string
if dataDir != "" {
dataDirs = []string{dataDir}
repoDirs = []string{filepath.Join(dataDir, "repositories")}
} else {
dataDirs = defaultDataDirs
repoDirs = defaultRepoDirs
}

conf, err := reporegistry.LoadAllRepositories(dataDirs, []fs.FS{repos.FS})
conf, err := reporegistry.LoadAllRepositories(repoDirs, []fs.FS{repos.FS})
if err != nil {
return nil, err
}
Expand Down
58 changes: 56 additions & 2 deletions cmd/image-builder/repos_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/osbuild/images/pkg/rpmmd"
)
Expand All @@ -16,7 +19,7 @@ func TestParseRepoURLsHappy(t *testing.T) {
"https://example.com/repo",
}, "forced")
assert.NoError(t, err)
assert.Equal(t, cfg, []rpmmd.RepoConfig{
assert.Equal(t, []rpmmd.RepoConfig{
{
Id: "forced-repo-0",
Name: "forced repo#0 /path/to/repo",
Expand All @@ -31,7 +34,7 @@ func TestParseRepoURLsHappy(t *testing.T) {
CheckGPG: &checkGPG,
CheckRepoGPG: &checkGPG,
},
})
}, cfg)
}

func TestParseExtraRepoSad(t *testing.T) {
Expand All @@ -41,3 +44,54 @@ func TestParseExtraRepoSad(t *testing.T) {
_, err = parseRepoURLs([]string{"https://example.com", "/just/a/path"}, "forced")
assert.EqualError(t, err, `scheme missing in "/just/a/path", please prefix with e.g. file:// or https://`)
}

func TestNewRepoRegistryImplSmoke(t *testing.T) {
registry, err := newRepoRegistryImpl("", nil)
require.NoError(t, err)
repos, err := registry.DistroHasRepos("rhel-10.2", "x86_64")
require.NoError(t, err)
assert.True(t, len(repos) > 0)
}

func TestNewRepoRegistryImplExtraReposGetAppended(t *testing.T) {
registry, err := newRepoRegistryImpl("", []string{"https://example.com/my/repo"})
require.NoError(t, err)
repos, err := registry.DistroHasRepos("rhel-10.2", "x86_64")
require.NoError(t, err)
assert.Equal(t, repos[len(repos)-1].BaseURLs[0], "https://example.com/my/repo")
}

func TestNewRepoRegistryImplDatadir(t *testing.T) {
// prereq test: no testdistro-1 in the default repos
registry, err := newRepoRegistryImpl("", nil)
require.NoError(t, err)
assert.NotContains(t, registry.ListDistros(), "testdistro-1")
_, err = registry.DistroHasRepos("testdistro-1", "x86_64")
require.EqualError(t, err, `requested repository not found: for distribution "testdistro-1"`)

// create a custom datadir with testdistro-1.json, the basefilename
// must match a distro nameVer
dataDir := t.TempDir()
repoFile := filepath.Join(dataDir, "repositories", "testdistro-1.json")
err = os.Mkdir(filepath.Dir(repoFile), 0755)
require.NoError(t, err)
repoContents := `{
"x86_64": [
{
"name": "testdistro-1-repo",
"baseurl": "https://example.com/test/test/distro/1"
}
]
}
`
err = os.WriteFile(repoFile, []byte(repoContents), 0644)
require.NoError(t, err)

// and ensure we have testdistro-1 now
registry, err = newRepoRegistryImpl(dataDir, nil)
require.NoError(t, err)
repos, err := registry.DistroHasRepos("testdistro-1", "x86_64")
require.NoError(t, err)
assert.Len(t, repos, 1)
assert.Equal(t, repos[0].Name, "testdistro-1-repo")
}
Loading