distro: don't use a global variable to store the distro register
Introduce a DistroRegister object. For now this does not introduce any functional changes, as the object is always instantited to be the same. However, in follow-up patches it will get options. Signed-off-by: Tom Gundersen <teg@jklm.no>
This commit is contained in:
parent
6c49560479
commit
5fcdd4bea5
11 changed files with 59 additions and 40 deletions
|
|
@ -49,8 +49,9 @@ func main() {
|
|||
jobListener := listeners[1]
|
||||
|
||||
rpm := rpmmd.NewRPMMD()
|
||||
distros := distro.NewRegistry()
|
||||
|
||||
distribution, err := distro.FromHost()
|
||||
distribution, err := distros.FromHost()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not determine distro from host: " + err.Error())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,8 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
d := distro.New(distroArg)
|
||||
distros := distro.NewRegistry()
|
||||
d := distros.GetDistro(distroArg)
|
||||
if d == nil {
|
||||
panic("unknown distro: " + distroArg)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,17 +40,29 @@ type Distro interface {
|
|||
Runner() string
|
||||
}
|
||||
|
||||
var registered map[string]Distro
|
||||
|
||||
func init() {
|
||||
registered = map[string]Distro{
|
||||
fedora30.Name: fedora30.New(),
|
||||
rhel82.Name: rhel82.New(),
|
||||
}
|
||||
type Registry struct {
|
||||
distros map[string]Distro
|
||||
}
|
||||
|
||||
func New(name string) Distro {
|
||||
distro, ok := registered[name]
|
||||
func NewRegistry() *Registry {
|
||||
distros := &Registry{
|
||||
distros: make(map[string]Distro),
|
||||
}
|
||||
distros.register(fedora30.New())
|
||||
distros.register(rhel82.New())
|
||||
return distros
|
||||
}
|
||||
|
||||
func (r *Registry) register(distro Distro) {
|
||||
name := distro.Name()
|
||||
if _, exists := r.distros[name]; exists {
|
||||
panic("a distro with this name already exists: " + name)
|
||||
}
|
||||
r.distros[name] = distro
|
||||
}
|
||||
|
||||
func (r *Registry) GetDistro(name string) Distro {
|
||||
distro, ok := r.distros[name]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -58,7 +70,7 @@ func New(name string) Distro {
|
|||
return distro
|
||||
}
|
||||
|
||||
func FromHost() (Distro, error) {
|
||||
func (r *Registry) FromHost() (Distro, error) {
|
||||
f, err := os.Open("/etc/os-release")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -72,7 +84,7 @@ func FromHost() (Distro, error) {
|
|||
|
||||
name := osrelease["ID"] + "-" + osrelease["VERSION_ID"]
|
||||
|
||||
d := New(name)
|
||||
d := r.GetDistro(name)
|
||||
if d == nil {
|
||||
return nil, errors.New("unknown distro: " + name)
|
||||
}
|
||||
|
|
@ -80,13 +92,6 @@ func FromHost() (Distro, error) {
|
|||
return d, nil
|
||||
}
|
||||
|
||||
func Register(name string, distro Distro) {
|
||||
if _, exists := registered[name]; exists {
|
||||
panic("a distro with this name already exists: " + name)
|
||||
}
|
||||
registered[name] = distro
|
||||
}
|
||||
|
||||
func readOSRelease(r io.Reader) (map[string]string, error) {
|
||||
osrelease := make(map[string]string)
|
||||
scanner := bufio.NewScanner(r)
|
||||
|
|
|
|||
|
|
@ -42,7 +42,8 @@ func TestDistro_Pipeline(t *testing.T) {
|
|||
continue
|
||||
}
|
||||
t.Run(tt.Compose.OutputFormat, func(t *testing.T) {
|
||||
d := distro.New(tt.Compose.Distro)
|
||||
distros := distro.NewRegistry()
|
||||
d := distros.GetDistro(tt.Compose.Distro)
|
||||
if d == nil {
|
||||
t.Errorf("unknown distro: %v", tt.Compose.Distro)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ func TestListOutputFormats(t *testing.T) {
|
|||
"vmdk",
|
||||
}
|
||||
|
||||
f30 := distro.New("fedora-30")
|
||||
distros := distro.NewRegistry()
|
||||
f30 := distros.GetDistro("fedora-30")
|
||||
if got := f30.ListOutputFormats(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("ListOutputFormats() = %v, want %v", got, want)
|
||||
}
|
||||
|
|
@ -92,7 +93,8 @@ func TestFilenameFromType(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f30 := distro.New("fedora-30")
|
||||
distros := distro.NewRegistry()
|
||||
f30 := distros.GetDistro("fedora-30")
|
||||
got, got1, err := f30.FilenameFromType(tt.args.outputFormat)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FilenameFromType() error = %v, wantErr %v", err, tt.wantErr)
|
||||
|
|
|
|||
|
|
@ -19,8 +19,9 @@ func TestListOutputFormats(t *testing.T) {
|
|||
"vmdk",
|
||||
}
|
||||
|
||||
f31 := distro.New("rhel-8.2")
|
||||
if got := f31.ListOutputFormats(); !reflect.DeepEqual(got, want) {
|
||||
distros := distro.NewRegistry()
|
||||
rhel82 := distros.GetDistro("rhel-8.2")
|
||||
if got := rhel82.ListOutputFormats(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("ListOutputFormats() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
|
@ -92,8 +93,9 @@ func TestFilenameFromType(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f31 := distro.New("rhel-8.2")
|
||||
got, got1, err := f31.FilenameFromType(tt.args.outputFormat)
|
||||
distros := distro.NewRegistry()
|
||||
rhel82 := distros.GetDistro("rhel-8.2")
|
||||
got, got1, err := rhel82.FilenameFromType(tt.args.outputFormat)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("FilenameFromType() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import (
|
|||
"errors"
|
||||
|
||||
"github.com/osbuild/osbuild-composer/internal/blueprint"
|
||||
"github.com/osbuild/osbuild-composer/internal/distro"
|
||||
"github.com/osbuild/osbuild-composer/internal/pipeline"
|
||||
"github.com/osbuild/osbuild-composer/internal/rpmmd"
|
||||
)
|
||||
|
|
@ -13,8 +12,8 @@ type TestDistro struct{}
|
|||
|
||||
const Name = "test"
|
||||
|
||||
func init() {
|
||||
distro.Register(Name, &TestDistro{})
|
||||
func New() *TestDistro {
|
||||
return &TestDistro{}
|
||||
}
|
||||
|
||||
func (d *TestDistro) Name() string {
|
||||
|
|
|
|||
|
|
@ -32,7 +32,9 @@ func TestBasic(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, c := range cases {
|
||||
api := jobqueue.New(nil, store.New(nil, distro.New("fedora-30")))
|
||||
distros := distro.NewRegistry()
|
||||
distro := distros.GetDistro("fedora-30")
|
||||
api := jobqueue.New(nil, store.New(nil, distro))
|
||||
|
||||
test.TestRoute(t, api, false, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON)
|
||||
}
|
||||
|
|
@ -40,7 +42,9 @@ func TestBasic(t *testing.T) {
|
|||
|
||||
func TestCreate(t *testing.T) {
|
||||
id, _ := uuid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff")
|
||||
store := store.New(nil, distro.New("fedora-30"))
|
||||
distros := distro.NewRegistry()
|
||||
distro := distros.GetDistro("fedora-30")
|
||||
store := store.New(nil, distro)
|
||||
api := jobqueue.New(nil, store)
|
||||
|
||||
err := store.PushCompose(id, &blueprint.Blueprint{}, map[string]string{"fedora": "test:foo"}, "x86_64", "tar", nil)
|
||||
|
|
@ -54,7 +58,9 @@ func TestCreate(t *testing.T) {
|
|||
|
||||
func testUpdateTransition(t *testing.T, from, to string, expectedStatus int) {
|
||||
id, _ := uuid.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff")
|
||||
store := store.New(nil, distro.New("fedora-30"))
|
||||
distros := distro.NewRegistry()
|
||||
distro := distros.GetDistro("fedora-30")
|
||||
store := store.New(nil, distro)
|
||||
api := jobqueue.New(nil, store)
|
||||
|
||||
if from != "VOID" {
|
||||
|
|
|
|||
|
|
@ -30,7 +30,8 @@ type JobStatus struct {
|
|||
}
|
||||
|
||||
func (job *Job) Run() (*store.Image, error, []error) {
|
||||
d := distro.New(job.Distro)
|
||||
distros := distro.NewRegistry()
|
||||
d := distros.GetDistro(job.Distro)
|
||||
if d == nil {
|
||||
return nil, fmt.Errorf("unknown distro: %s", job.Distro), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,6 @@ func createBaseStoreFixture() *store.Store {
|
|||
|
||||
var date = time.Date(2019, 11, 27, 13, 19, 0, 0, time.FixedZone("UTC+1", 60*60))
|
||||
|
||||
|
||||
var localTarget = &target.Target{
|
||||
Uuid: uuid.MustParse("20000000-0000-0000-0000-000000000000"),
|
||||
Name: "org.osbuild.local",
|
||||
|
|
@ -93,7 +92,8 @@ func createBaseStoreFixture() *store.Store {
|
|||
},
|
||||
}
|
||||
|
||||
d := distro.New("fedora-30")
|
||||
distros := distro.NewRegistry()
|
||||
d := distros.GetDistro("fedora-30")
|
||||
s := store.New(nil, d)
|
||||
|
||||
s.Blueprints[bName] = b
|
||||
|
|
@ -166,7 +166,8 @@ func createStoreWithoutComposesFixture() *store.Store {
|
|||
Customizations: nil,
|
||||
}
|
||||
|
||||
d := distro.New("fedora-30")
|
||||
distros := distro.NewRegistry()
|
||||
d := distros.GetDistro("fedora-30")
|
||||
s := store.New(nil, d)
|
||||
|
||||
s.Blueprints[bName] = b
|
||||
|
|
@ -247,7 +248,7 @@ func BadDepsolve() Fixture {
|
|||
func BadFetch() Fixture {
|
||||
return Fixture{
|
||||
fetchPackageList{
|
||||
ret: nil,
|
||||
ret: nil,
|
||||
checksums: nil,
|
||||
err: &rpmmd.DNFError{
|
||||
Kind: "FetchError",
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/osbuild/osbuild-composer/internal/blueprint"
|
||||
"github.com/osbuild/osbuild-composer/internal/distro"
|
||||
_ "github.com/osbuild/osbuild-composer/internal/distro/test"
|
||||
test_distro "github.com/osbuild/osbuild-composer/internal/distro/test"
|
||||
rpmmd_mock "github.com/osbuild/osbuild-composer/internal/mocks/rpmmd"
|
||||
"github.com/osbuild/osbuild-composer/internal/store"
|
||||
"github.com/osbuild/osbuild-composer/internal/target"
|
||||
|
|
@ -29,7 +29,7 @@ import (
|
|||
func createWeldrAPI(fixtureGenerator rpmmd_mock.FixtureGenerator) (*weldr.API, *store.Store) {
|
||||
fixture := fixtureGenerator()
|
||||
rpm := rpmmd_mock.NewRPMMDMock(fixture)
|
||||
d := distro.New("test")
|
||||
d := test_distro.New()
|
||||
|
||||
return weldr.New(rpm, "x86_64", d, nil, fixture.Store), fixture.Store
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue