Skip to content

Commit

Permalink
updates based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dbw7 committed Nov 16, 2023
1 parent eb15885 commit 4e9e3a7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 89 deletions.
51 changes: 27 additions & 24 deletions pkg/build/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,58 +19,61 @@ const (
var modifyRPMScript string

func (b *Builder) processRPMs() error {
RPMSourceDir, err := b.generateRPMPath()
rpmSourceDir, err := b.generateRPMPath()
if err != nil {
return fmt.Errorf("generating RPM path: %w", err)
}
// Only proceed with processing the RPMs if the directory exists
if RPMSourceDir == "" {
if rpmSourceDir == "" {
return nil
}

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

err = copyRPMs(RPMSourceDir, b.combustionDir, RPMFileNames)
err = copyRPMs(rpmSourceDir, b.combustionDir, rpmFileNames)

Check failure on line 36 in pkg/build/rpm.go

View workflow job for this annotation

GitHub Actions / build

b.combustionDir undefined (type *Builder has no field or method combustionDir)
if err != nil {
return fmt.Errorf("copying RPMs over: %w", err)
}

err = b.writeRPMScript(RPMFileNames)
err = b.writeRPMScript(rpmFileNames)
if err != nil {
return fmt.Errorf("writing the RPM install script %s: %w", modifyRPMScriptName, err)
}

return nil
}

func getRPMFileNames(RPMSourceDir string) ([]string, error) {
var RPMFileNames []string
func getRPMFileNames(rpmSourceDir string) ([]string, error) {
var rpmFileNames []string

RPMs, err := os.ReadDir(RPMSourceDir)
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())
for _, rpmFile := range rpms {
if filepath.Ext(rpmFile.Name()) == ".rpm" {
rpmFileNames = append(rpmFileNames, rpmFile.Name())
}
}

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

return RPMFileNames, nil
return rpmFileNames, nil
}

func copyRPMs(RPMSourceDir string, RPMDestDir string, RPMFileNames []string) error {
for _, RPM := range RPMFileNames {
sourcePath := filepath.Join(RPMSourceDir, RPM)
destPath := filepath.Join(RPMDestDir, RPM)
func copyRPMs(rpmSourceDir string, rpmDestDir string, rpmFileNames []string) error {
if rpmDestDir == "" {
return fmt.Errorf("RPM destination directory cannot be empty")
}
for _, rpm := range rpmFileNames {
sourcePath := filepath.Join(rpmSourceDir, rpm)
destPath := filepath.Join(rpmDestDir, rpm)

err := fileio.CopyFile(sourcePath, destPath)
if err != nil {
Expand All @@ -81,16 +84,16 @@ func copyRPMs(RPMSourceDir string, RPMDestDir string, RPMFileNames []string) err
return nil
}

func (b *Builder) writeRPMScript(RPMFileNames []string) error {
func (b *Builder) writeRPMScript(rpmFileNames []string) error {
values := struct {
RPMs string
}{
RPMs: strings.Join(RPMFileNames, " "),
RPMs: strings.Join(rpmFileNames, " "),
}

writtenFilename, err := b.writeCombustionFile(modifyRPMScriptName, modifyRPMScript, &values)
if err != nil {
return fmt.Errorf("writing RPM script %s: %w", modifyRPMScriptName, err)
return fmt.Errorf("writing RPM script: %w", err)
}
err = os.Chmod(writtenFilename, modifyScriptMode)
if err != nil {
Expand All @@ -103,14 +106,14 @@ func (b *Builder) writeRPMScript(RPMFileNames []string) error {
}

func (b *Builder) generateRPMPath() (string, error) {
RPMSourceDir := filepath.Join(b.buildConfig.ImageConfigDir, "rpms")
_, err := os.Stat(RPMSourceDir)
rpmSourceDir := filepath.Join(b.buildConfig.ImageConfigDir, "rpms")

Check failure on line 109 in pkg/build/rpm.go

View workflow job for this annotation

GitHub Actions / build

b.buildConfig undefined (type *Builder has no field or method buildConfig)
_, 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)
return "", fmt.Errorf("checking for RPM directory at %s: %w", rpmSourceDir, err)
}

return RPMSourceDir, nil
return rpmSourceDir, nil
}
130 changes: 65 additions & 65 deletions pkg/build/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,28 @@ func TestGetRPMFileNames(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

RPMSourceDir := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(RPMSourceDir, 0o755)
rpmSourceDir := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(rpmSourceDir, 0o755)
require.NoError(t, err)

bc := config.BuildConfig{}
builder := New(nil, &bc)
err = builder.prepareBuildDir()
require.NoError(t, err)

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

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

// Test
RPMFileNames, err := getRPMFileNames(RPMSourceDir)
rpmFileNames, err := getRPMFileNames(rpmSourceDir)

// Verify
require.NoError(t, err)

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

func TestCopyRPMs(t *testing.T) {
Expand All @@ -51,38 +46,34 @@ func TestCopyRPMs(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

RPMSourceDir := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(RPMSourceDir, 0o755)
rpmSourceDir := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(rpmSourceDir, 0o755)
require.NoError(t, err)

bc := config.BuildConfig{}
builder := New(nil, &bc)
err = builder.prepareBuildDir()
tmpDestDir := filepath.Join(tmpDir, "dest-dir")
err = os.Mkdir(tmpDestDir, 0o755)
require.NoError(t, err)

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

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

RPMFileNames, err := getRPMFileNames(RPMSourceDir)
require.NoError(t, err)

// Test
err = copyRPMs(RPMSourceDir, builder.combustionDir, RPMFileNames)
err = copyRPMs(rpmSourceDir, tmpDestDir, []string{"rpm1.rpm", "rpm2.rpm"})

// Verify
require.NoError(t, err)

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

_, err = os.Stat(filepath.Join(builder.combustionDir, "rpm2.rpm"))
_, err = os.Stat(filepath.Join(tmpDestDir, "rpm2.rpm"))
require.NoError(t, err)
}

Expand All @@ -92,43 +83,57 @@ func TestGetRPMFileNamesNoRPMs(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

RPMSourceDir := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(RPMSourceDir, 0o755)
require.NoError(t, err)

bc := config.BuildConfig{}
builder := New(nil, &bc)
err = builder.prepareBuildDir()
rpmSourceDir := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(rpmSourceDir, 0o755)
require.NoError(t, err)

// Test
RPMFileNames, err := getRPMFileNames(RPMSourceDir)
rpmFileNames, err := getRPMFileNames(rpmSourceDir)

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

assert.Empty(t, RPMFileNames)
assert.Empty(t, rpmFileNames)
}

func TestCopyRPMsNoRPMDir(t *testing.T) {
func TestCopyRPMsNoRPMDestDir(t *testing.T) {
// Setup
tmpDir, err := os.MkdirTemp("", "eib-copy-RPMs-test-no-dir-")
tmpSrcDir, err := os.MkdirTemp("", "eib-copy-RPMs-test-")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
defer os.RemoveAll(tmpSrcDir)

bc := config.BuildConfig{}
builder := New(nil, &bc)
err = builder.prepareBuildDir()
rpmSourceDir := filepath.Join(tmpSrcDir, "rpms")
err = os.Mkdir(rpmSourceDir, 0o755)
require.NoError(t, err)

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

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

// Test
err = copyRPMs(RPMSourceDir, builder.combustionDir, nil)
err = copyRPMs(rpmSourceDir, "", []string{"rpm1.rpm", "rpm2.rpm"})

// Verify
require.ErrorContains(t, err, "RPM destination directory cannot be empty")
}

func TestCopyRPMsNoRPMSrcDir(t *testing.T) {
// Setup
tmpDestDir, err := os.MkdirTemp("", "eib-copy-RPMs-test-")
require.NoError(t, err)
defer os.RemoveAll(tmpDestDir)

// Test
err = copyRPMs("", tmpDestDir, []string{"rpm1.rpm", "rpm2.rpm"})

// Verify
require.ErrorContains(t, err, "opening source file")
}

func TestWriteRPMScript(t *testing.T) {
Expand All @@ -137,29 +142,26 @@ func TestWriteRPMScript(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

RPMSourceDir := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(RPMSourceDir, 0o755)
rpmSourceDir := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(rpmSourceDir, 0o755)
require.NoError(t, err)

buildConfig := config.BuildConfig{}
builder := New(nil, &buildConfig)
require.NoError(t, builder.prepareBuildDir())

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

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

RPMFileNames, err := getRPMFileNames(RPMSourceDir)
require.NoError(t, err)

// Test
err = builder.writeRPMScript(RPMFileNames)
err = builder.writeRPMScript([]string{"rpm1.rpm", "rpm2.rpm"})

// Verify
require.NoError(t, err)
Expand All @@ -183,27 +185,27 @@ func TestProcessRPMs(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

RPMSourceDir := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(RPMSourceDir, 0o755)
rpmSourceDir := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(rpmSourceDir, 0o755)
require.NoError(t, err)

bc := config.BuildConfig{
ImageConfigDir: tmpDir,
}
builder := New(nil, &bc)
err = builder.prepareBuildDir()
require.NoError(t, err)

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

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

bc := config.BuildConfig{
ImageConfigDir: tmpDir,
}
builder := New(nil, &bc)
err = builder.prepareBuildDir()
require.NoError(t, err)

// Test
err = builder.processRPMs()

Expand Down Expand Up @@ -231,17 +233,15 @@ func TestGenerateRPMPath(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(tmpDir)

RPMSourceDir := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(RPMSourceDir, 0o755)
expectedPath := filepath.Join(tmpDir, "rpms")
err = os.Mkdir(expectedPath, 0o755)
require.NoError(t, err)

bc := config.BuildConfig{
ImageConfigDir: tmpDir,
}
builder := New(nil, &bc)

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

// Test
generatedPath, err := builder.generateRPMPath()

Expand Down
Empty file removed pkg/config/testdata/rpms/.gitkeep
Empty file.

0 comments on commit 4e9e3a7

Please sign in to comment.