Skip to content

Commit

Permalink
added handling for adding rpms to an image as well as the relevant tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dbw7 committed Oct 30, 2023
1 parent 0d26391 commit 8a56a02
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type Builder struct {
eibBuildDir string
combustionDir string
combustionScripts []string
rpmBuildDir string
rpmFileNames []string
rpmSourceDir string
}

func New(imageConfig *config.ImageConfig, buildConfig *config.BuildConfig) *Builder {
Expand All @@ -48,6 +51,11 @@ func (b *Builder) Build() error {
return fmt.Errorf("generating combustion script: %w", err)
}

err = b.copyRPMs()
if err != nil {
return fmt.Errorf("copying RPMs over: %w", err)
}

switch b.imageConfig.Image.ImageType {
case config.ImageTypeISO:
err = b.buildIsoImage()
Expand Down Expand Up @@ -85,10 +93,16 @@ func (b *Builder) prepareBuildDir() error {
b.eibBuildDir = b.buildConfig.BuildDir
}
b.combustionDir = filepath.Join(b.eibBuildDir, "combustion")
b.rpmBuildDir = filepath.Join(b.combustionDir, "rpms")

err := os.MkdirAll(b.combustionDir, os.ModePerm)
if err != nil {
return fmt.Errorf("creating the build directory structure: %w", err)
return fmt.Errorf("creating the build directory structure for combustion: %w", err)
}

err = os.MkdirAll(b.rpmBuildDir, os.ModePerm)
if err != nil {
return fmt.Errorf("creating the build directory structure for rpms: %w", err)
}

return nil
Expand Down
52 changes: 52 additions & 0 deletions pkg/build/rpm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package build

import (
"fmt"
"io"
"os"
"path/filepath"
)

func (b *Builder) getRPMFileNames() error {
b.rpmSourceDir = filepath.Join(b.buildConfig.ImageConfigDir, "rpms")

rpms, err := os.ReadDir(b.rpmSourceDir)
if err != nil {
return fmt.Errorf("reading rpm source dir: %w", err)
}

for _, rpmFile := range rpms {
b.rpmFileNames = append(b.rpmFileNames, rpmFile.Name())
}
return nil
}

func (b *Builder) copyRPMs() error {
err := b.getRPMFileNames()
if err != nil {
return fmt.Errorf("getting rpm file names: %w", err)
}

for _, rpm := range b.rpmFileNames {
sourcePath := filepath.Join(b.rpmSourceDir, rpm)
destPath := filepath.Join(b.rpmBuildDir, rpm)

sourceFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("opening rpm source path: %w", err)
}
defer sourceFile.Close()

destFile, err := os.Create(destPath)
if err != nil {
return fmt.Errorf("opening rpm dest path: %w", err)
}
defer destFile.Close()

_, err = io.Copy(destFile, sourceFile)
if err != nil {
return fmt.Errorf("copying rpm: %w", err)
}
}
return nil
}
54 changes: 54 additions & 0 deletions pkg/build/rpm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package build

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

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/suse-edge/edge-image-builder/pkg/config"
)

func TestGetRPMFileNames(t *testing.T) {
// Setup
bc := config.BuildConfig{
ImageConfigDir: "../config/testdata",
}
builder := New(nil, &bc)
err := builder.prepareBuildDir()
require.NoError(t, err)
defer os.Remove(builder.eibBuildDir)

// Test
err = builder.getRPMFileNames()

// Verify
require.NoError(t, err)

assert.Contains(t, builder.rpmFileNames, "rpm1.rpm")
assert.Contains(t, builder.rpmFileNames, "rpm2.rpm")
}

func TestCopyRPMs(t *testing.T) {
// Setup
bc := config.BuildConfig{
ImageConfigDir: "../config/testdata",
}
builder := New(nil, &bc)
err := builder.prepareBuildDir()
require.NoError(t, err)
defer os.Remove(builder.eibBuildDir)

// Test
err = builder.copyRPMs()

// Verify
require.NoError(t, err)

_, err = os.Stat(filepath.Join(builder.rpmBuildDir, "rpm1.rpm"))
require.NoError(t, err)

_, err = os.Stat(filepath.Join(builder.rpmBuildDir, "rpm2.rpm"))
require.NoError(t, err)
}
Empty file.
Empty file.

0 comments on commit 8a56a02

Please sign in to comment.