diff --git a/pkg/build/build.go b/pkg/build/build.go index f29c3ea8..d85b0cb0 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -3,6 +3,7 @@ package build import ( _ "embed" "fmt" + "io" "os" "path/filepath" @@ -23,8 +24,6 @@ type Builder struct { eibBuildDir string combustionDir string combustionScripts []string - rpmFileNames []string - rpmSourceDir string } func New(imageConfig *config.ImageConfig, buildConfig *config.BuildConfig) *Builder { @@ -95,7 +94,7 @@ func (b *Builder) prepareBuildDir() error { err := os.MkdirAll(b.combustionDir, os.ModePerm) if err != nil { - return fmt.Errorf("creating the build directory structure for combustion: %w", err) + return fmt.Errorf("creating the build directory structure: %w", err) } return nil @@ -180,3 +179,24 @@ func (b *Builder) generateBaseImageFilename() string { filename := filepath.Join(b.buildConfig.ImageConfigDir, "images", b.imageConfig.Image.BaseImage) return filename } + +func (b *Builder) 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 +} diff --git a/pkg/build/rpm.go b/pkg/build/rpm.go index 9e15b7bc..bdb4df5f 100644 --- a/pkg/build/rpm.go +++ b/pkg/build/rpm.go @@ -2,53 +2,46 @@ package build import ( "fmt" - "io" "os" "path/filepath" ) -func (b *Builder) getRPMFileNames() error { - b.rpmSourceDir = filepath.Join(b.buildConfig.ImageConfigDir, "rpms") +func (b *Builder) getRPMFileNames() ([]string, error) { + var rpmFileNames []string + rpmSourceDir := filepath.Join(b.buildConfig.ImageConfigDir, "rpms") - rpms, err := os.ReadDir(b.rpmSourceDir) + rpms, err := os.ReadDir(rpmSourceDir) if err != nil { - return fmt.Errorf("reading rpm source dir: %w", err) + return nil, fmt.Errorf("reading rpm source dir: %w", err) } for _, rpmFile := range rpms { - b.rpmFileNames = append(b.rpmFileNames, rpmFile.Name()) + rpmFileNames = append(rpmFileNames, rpmFile.Name()) + } + + if len(rpmFileNames) == 0 { + return nil, fmt.Errorf("no rpms found") } - return nil + return rpmFileNames, nil } func (b *Builder) copyRPMs() error { - err := b.getRPMFileNames() + rpmFileNames, 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.combustionDir, rpm) - - sourceFile, err := os.Open(sourcePath) - if err != nil { - return fmt.Errorf("opening rpm source path: %w", err) - } + rpmSourceDir := filepath.Join(b.buildConfig.ImageConfigDir, "rpms") - destFile, err := os.Create(destPath) - if err != nil { - return fmt.Errorf("opening rpm dest path: %w", err) - } + for _, rpm := range rpmFileNames { + sourcePath := filepath.Join(rpmSourceDir, rpm) + destPath := filepath.Join(b.combustionDir, rpm) - _, err = io.Copy(destFile, sourceFile) + err = b.copyFile(sourcePath, destPath) if err != nil { - return fmt.Errorf("copying rpm: %w", err) + return fmt.Errorf("looping through rpms to copy: %w", err) } - - sourceFile.Close() - destFile.Close() } return nil diff --git a/pkg/build/rpm_test.go b/pkg/build/rpm_test.go index 87e07a0c..b9fa4d64 100644 --- a/pkg/build/rpm_test.go +++ b/pkg/build/rpm_test.go @@ -20,14 +20,36 @@ func TestGetRPMFileNames(t *testing.T) { require.NoError(t, err) defer os.Remove(builder.eibBuildDir) + file1Path := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms", "rpm1.rpm") + file2Path := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms", "rpm2.rpm") + + file1, err := os.Create(file1Path) + require.NoError(t, err) + + file2, err := os.Create(file2Path) + require.NoError(t, err) + // Test - err = builder.getRPMFileNames() + rpmFileNames, err := builder.getRPMFileNames() // Verify require.NoError(t, err) - assert.Contains(t, builder.rpmFileNames, "rpm1.rpm") - assert.Contains(t, builder.rpmFileNames, "rpm2.rpm") + assert.Contains(t, rpmFileNames, "rpm1.rpm") + assert.Contains(t, rpmFileNames, "rpm2.rpm") + + // Cleanup + err = file1.Close() + require.NoError(t, err) + + err = file2.Close() + require.NoError(t, err) + + err = os.Remove(file1Path) + require.NoError(t, err) + + err = os.Remove(file2Path) + require.NoError(t, err) } func TestCopyRPMs(t *testing.T) { @@ -40,6 +62,15 @@ func TestCopyRPMs(t *testing.T) { require.NoError(t, err) defer os.Remove(builder.eibBuildDir) + file1Path := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms", "rpm1.rpm") + file2Path := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms", "rpm2.rpm") + + file1, err := os.Create(file1Path) + require.NoError(t, err) + + file2, err := os.Create(file2Path) + require.NoError(t, err) + // Test err = builder.copyRPMs() @@ -51,4 +82,36 @@ func TestCopyRPMs(t *testing.T) { _, err = os.Stat(filepath.Join(builder.combustionDir, "rpm2.rpm")) require.NoError(t, err) + + // Cleanup + err = file1.Close() + require.NoError(t, err) + + err = file2.Close() + require.NoError(t, err) + + err = os.Remove(file1Path) + require.NoError(t, err) + + err = os.Remove(file2Path) + require.NoError(t, err) +} + +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) + + // Test + rpmFileNames, err := builder.getRPMFileNames() + + // Verify + require.ErrorContains(t, err, "no rpms found") + + assert.Empty(t, rpmFileNames) } diff --git a/pkg/config/testdata/rpms/rpm1.rpm b/pkg/config/testdata/rpms/rpm1.rpm deleted file mode 100644 index e69de29b..00000000 diff --git a/pkg/config/testdata/rpms/rpm2.rpm b/pkg/config/testdata/rpms/rpm2.rpm deleted file mode 100644 index e69de29b..00000000