-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added handling for adding rpms to an image as well as the relevant tests * remove rpm dir from combustion * update to loop for linter * updates based on feedback * added .gitkeep * handling for .gitkeep and and reformatting for linter * updates based on feedback copyFile is no longer attached to the build struct. getRPMFileNames now checks for the .rpm extension, to remove the previously unnecessary (and frankly ugly) logic. I also added more abstraction for readability. Finally, cleaned up the tests based on feedback and altered them to match changes. * removed unnecessary filepath.Join * update .gitignore and remove .idea * added handling and tests for when user doesn't add rpms * removing else statement for linter
- Loading branch information
Showing
5 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,6 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
# IDE | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
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.