Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added handling for adding rpms to an image as well as the relevant tests #28

Merged
merged 11 commits into from
Nov 2, 2023
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@

# Go workspace file
go.work

# IDE
.idea
27 changes: 27 additions & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
_ "embed"
"fmt"
"io"
"os"
"path/filepath"

Expand Down Expand Up @@ -48,6 +49,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 @@ -173,3 +179,24 @@ func (b *Builder) generateBaseImageFilename() string {
filename := filepath.Join(b.buildConfig.ImageConfigDir, "images", b.imageConfig.Image.BaseImage)
return filename
}

func copyFile(sourcePath string, destPath string) error {
sourceFile, err := os.Open(sourcePath)
if err != nil {
return fmt.Errorf("opening file from source path: %w", err)
}
defer sourceFile.Close()

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

_, err = io.Copy(destFile, sourceFile)
if err != nil {
return fmt.Errorf("copying file from source path to dest path: %w", err)
}

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

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

func (b *Builder) getRPMFileNames(rpmSourceDir string) ([]string, error) {
var rpmFileNames []string

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

for _, rpmFile := range rpms {
if filepath.Ext(rpmFile.Name()) == ".rpm" {
rpmFileNames = append(rpmFileNames, rpmFile.Name())
}
}

if len(rpmFileNames) == 0 {
return nil, fmt.Errorf("no rpms found")
}

return rpmFileNames, nil
}

func (b *Builder) copyRPMs() error {
rpmSourceDir := filepath.Join(b.buildConfig.ImageConfigDir, "rpms")
// Only proceed with copying the RPMs if the directory exists
_, err := os.Stat(rpmSourceDir)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("checking for rpm directory at %s: %w", rpmSourceDir, err)
}
rpmDestDir := b.combustionDir

rpmFileNames, err := b.getRPMFileNames(rpmSourceDir)
if err != nil {
return fmt.Errorf("getting rpm file names: %w", err)
}

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

err = copyFile(sourcePath, destPath)
if err != nil {
return fmt.Errorf("copying file %s: %w", sourcePath, err)
}
}

return nil
}
125 changes: 125 additions & 0 deletions pkg/build/rpm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
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",
atanasdinov marked this conversation as resolved.
Show resolved Hide resolved
}
builder := New(nil, &bc)
err := builder.prepareBuildDir()
require.NoError(t, err)
defer os.Remove(builder.eibBuildDir)

rpmSourceDir := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms")

file1Path := filepath.Join(rpmSourceDir, "rpm1.rpm")
defer os.Remove(file1Path)
file1, err := os.Create(file1Path)
require.NoError(t, err)

file2Path := filepath.Join(rpmSourceDir, "rpm2.rpm")
defer os.Remove(file2Path)
file2, err := os.Create(file2Path)
require.NoError(t, err)

// Test
rpmFileNames, err := builder.getRPMFileNames(rpmSourceDir)

// Verify
require.NoError(t, err)

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

// Cleanup
assert.NoError(t, file1.Close())
assert.NoError(t, file2.Close())
}

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)

rpmSourceDir := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms")
rpmDestDir := builder.combustionDir

file1Path := filepath.Join(rpmSourceDir, "rpm1.rpm")
defer os.Remove(file1Path)
file1, err := os.Create(file1Path)
require.NoError(t, err)

file2Path := filepath.Join(rpmSourceDir, "rpm2.rpm")
defer os.Remove(file2Path)
file2, err := os.Create(file2Path)
require.NoError(t, err)

// Test
err = builder.copyRPMs()

// Verify
require.NoError(t, err)

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

_, err = os.Stat(filepath.Join(rpmDestDir, "rpm2.rpm"))
require.NoError(t, err)

// Cleanup
assert.NoError(t, file1.Close())
assert.NoError(t, file2.Close())
}

func TestGetRPMFileNamesNoRPMs(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)

rpmSourceDir := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms")

// Test
rpmFileNames, err := builder.getRPMFileNames(rpmSourceDir)

// Verify
require.ErrorContains(t, err, "no rpms found")

assert.Empty(t, rpmFileNames)
}

func TestCopyRPMsNoRPMDir(t *testing.T) {
// Setup
bc := config.BuildConfig{
ImageConfigDir: "../config/ThisDirDoesNotExist",
}
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)
}
Empty file.