Skip to content

Commit

Permalink
otkdistro: create test distro dynamically
Browse files Browse the repository at this point in the history
Remove test/otk/fakedistro and instead create it when running the unit
test.  This will make it easier to evolve the test to cover more
features.
  • Loading branch information
achilleas-k committed Jul 16, 2024
1 parent c4d7d1b commit 6b3fae0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 54 deletions.
95 changes: 85 additions & 10 deletions pkg/otkdistro/otkdistro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,104 @@ package otkdistro_test

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

"gopkg.in/yaml.v3"

"github.com/osbuild/images/pkg/otkdistro"
"github.com/stretchr/testify/require"
)

func makeFakeDistro(root string) ([]string, error) {
arches := []string{
"aarch64",
"fakearch",
"x86_64",
}

imageTypes := []string{
"qcow2",
"container",
}

props := map[string]interface{}{
"name": "FakeDistro",
"os_version": "42.0",
"release_version": "42",
"runner": "org.osbuild.fakedistro42",
}
propsfile, err := os.Create(filepath.Join(root, "properties.yaml"))
if err != nil {
return nil, fmt.Errorf("error creating properties file for test distro: %w", err)
}
if err := yaml.NewEncoder(propsfile).Encode(props); err != nil {
return nil, fmt.Errorf("error writing properties file for test distro: %w", err)
}

// make all otk files have the same content for now
otkcontent := map[string]interface{}{
"otk.version": "1",
"otk.target.osbuild.name": map[string]interface{}{
"pipelines": []map[string]interface{}{
{
"name": "build",
"stages": []map[string]interface{}{
{
"type": "org.osbuild.rpm",
"options": nil,
},
},
},
{
"name": "os",
"stages": []map[string]interface{}{
{
"type": "org.osbuild.rpm",
"options": nil,
},
},
},
},
},
}

expected := make([]string, 0, len(arches)*len(imageTypes))
for _, arch := range arches {
archpath := filepath.Join(root, arch)
if err := os.Mkdir(archpath, 0o777); err != nil {
return nil, fmt.Errorf("error creating architecture directory for test distro %q: %w", archpath, err)
}
for _, imageType := range imageTypes {
itpath := filepath.Join(archpath, imageType) + ".yaml"
itfile, err := os.Create(itpath)
if err != nil {
return nil, fmt.Errorf("error creating image type file for test distro %q: %w", itpath, err)
}
if err := yaml.NewEncoder(itfile).Encode(otkcontent); err != nil {
return nil, fmt.Errorf("error writing image type file for test distro %q: %w", itpath, err)
}
expected = append(expected, fmt.Sprintf("%s/%s", arch, imageType))
}
}

return expected, nil
}

func TestDistroLoad(t *testing.T) {
require := require.New(t)

// TODO: we can write the fragments during the test setup and make the
// whole test self-contained
distro, err := otkdistro.New("../../test/data/otk/fakedistro")
distroRoot := t.TempDir()
expected, err := makeFakeDistro(distroRoot)
require.NoError(err)

distro, err := otkdistro.New(distroRoot)
require.NoError(err)
require.Equal("FakeDistro", distro.Name())
require.Equal("42.0", distro.OsVersion())
require.Equal("42", distro.Releasever())
// TODO: check runner when it's added
// TODO: check runner when it's added to the distro interface

archImageTypes := make([]string, 0)
for _, archName := range distro.ListArches() {
Expand All @@ -30,11 +111,5 @@ func TestDistroLoad(t *testing.T) {
}
}

expected := []string{
"aarch64/qcow2",
"fakearch/qcow2",
"x86_64/qcow2",
}

require.ElementsMatch(expected, archImageTypes)
}
13 changes: 0 additions & 13 deletions test/data/otk/fakedistro/aarch64/qcow2.yml

This file was deleted.

13 changes: 0 additions & 13 deletions test/data/otk/fakedistro/fakearch/qcow2.yml

This file was deleted.

5 changes: 0 additions & 5 deletions test/data/otk/fakedistro/properties.yaml

This file was deleted.

13 changes: 0 additions & 13 deletions test/data/otk/fakedistro/x86_64/qcow2.yml

This file was deleted.

0 comments on commit 6b3fae0

Please sign in to comment.