diff --git a/cmd/eib/main.go b/cmd/eib/main.go index dc6e65b6..4a67400e 100644 --- a/cmd/eib/main.go +++ b/cmd/eib/main.go @@ -20,7 +20,7 @@ const ( argVerbose = "verbose" ) -func processArgs() (*config.ImageConfig, *config.BuildConfig, error) { +func processArgs() (*config.ImageConfig, *build.Context, error) { var ( configFile string configDir string @@ -48,13 +48,13 @@ func processArgs() (*config.ImageConfig, *config.BuildConfig, error) { if err != nil { return nil, nil, fmt.Errorf("validating the config dir %s: %w", configDir, err) } - buildConfig := config.BuildConfig{ - ImageConfigDir: configDir, - BuildDir: buildDir, - DeleteBuildDir: deleteBuildDir, + + context, err := build.NewContext(configDir, buildDir, deleteBuildDir) + if err != nil { + return nil, nil, fmt.Errorf("building dir structure: %w", err) } - return imageConfig, &buildConfig, err + return imageConfig, context, err } func setupLogging(verbose bool) { @@ -111,14 +111,17 @@ func validateImageConfigDir(configDir string) error { } func main() { - imageConfig, buildConfig, err := processArgs() + imageConfig, context, err := processArgs() if err != nil { zap.L().Fatal("CLI arguments could not be parsed", zap.Error(err)) } - builder := build.New(imageConfig, buildConfig) - err = builder.Build() - if err != nil { + builder := build.New(imageConfig, context) + if err = builder.Build(); err != nil { zap.L().Fatal("An error occurred building the image", zap.Error(err)) } + + if err = build.CleanUpBuildDir(context); err != nil { + zap.L().Error("Failed to clean up build directory", zap.Error(err)) + } } diff --git a/pkg/build/build.go b/pkg/build/build.go index 474208d8..6bbe2716 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -16,27 +16,20 @@ var combustionScriptBaseCode string type Builder struct { imageConfig *config.ImageConfig - buildConfig *config.BuildConfig + context *Context - eibBuildDir string - combustionDir string combustionScripts []string } -func New(imageConfig *config.ImageConfig, buildConfig *config.BuildConfig) *Builder { +func New(imageConfig *config.ImageConfig, context *Context) *Builder { return &Builder{ imageConfig: imageConfig, - buildConfig: buildConfig, + context: context, } } func (b *Builder) Build() error { - err := b.prepareBuildDir() - if err != nil { - return fmt.Errorf("preparing the build directory: %w", err) - } - - err = b.configureMessage() + err := b.configureMessage() if err != nil { return fmt.Errorf("configuring the welcome message: %w", err) } @@ -58,63 +51,18 @@ func (b *Builder) Build() error { switch b.imageConfig.Image.ImageType { case config.ImageTypeISO: - err = b.buildIsoImage() + return b.buildIsoImage() case config.ImageTypeRAW: - err = b.buildRawImage() + return b.buildRawImage() default: - err = fmt.Errorf("invalid imageType value specified, must be either \"%s\" or \"%s\"", + return fmt.Errorf("invalid imageType value specified, must be either \"%s\" or \"%s\"", config.ImageTypeISO, config.ImageTypeRAW) } - - if err != nil { - return err - } - - err = b.cleanUpBuildDir() - if err != nil { - return fmt.Errorf("cleaning up the build directory: %w", err) - } - - return nil -} - -func (b *Builder) prepareBuildDir() error { - // Combustion works by creating a volume with a subdirectory named "combustion" - // and a file named "script". This function builds out that structure and updates - // the Builder so that the other functions can populate it as necessary. - - if b.buildConfig.BuildDir == "" { - tmpDir, err := os.MkdirTemp("", "eib-") - if err != nil { - return fmt.Errorf("creating a temporary build directory: %w", err) - } - b.eibBuildDir = tmpDir - } else { - b.eibBuildDir = b.buildConfig.BuildDir - } - b.combustionDir = filepath.Join(b.eibBuildDir, "combustion") - - err := os.MkdirAll(b.combustionDir, os.ModePerm) - if err != nil { - return fmt.Errorf("creating the build directory structure: %w", err) - } - - return nil -} - -func (b *Builder) cleanUpBuildDir() error { - if b.buildConfig.DeleteBuildDir { - err := os.RemoveAll(b.eibBuildDir) - if err != nil { - return fmt.Errorf("deleting build directory: %w", err) - } - } - return nil } func (b *Builder) generateCombustionScript() error { // The file must be located at "combustion/script" - scriptFilename := filepath.Join(b.combustionDir, "script") + scriptFilename := filepath.Join(b.context.CombustionDir, "script") scriptFile, err := os.Create(scriptFilename) if err != nil { return fmt.Errorf("creating the combustion \"script\" file: %w", err) @@ -142,12 +90,12 @@ func (b *Builder) generateCombustionScript() error { } func (b *Builder) writeBuildDirFile(filename string, contents string, templateData any) (string, error) { - destFilename := filepath.Join(b.eibBuildDir, filename) + destFilename := filepath.Join(b.context.BuildDir, filename) return destFilename, fileio.WriteFile(destFilename, contents, templateData) } func (b *Builder) writeCombustionFile(filename string, contents string, templateData any) (string, error) { - destFilename := filepath.Join(b.combustionDir, filename) + destFilename := filepath.Join(b.context.CombustionDir, filename) return destFilename, fileio.WriteFile(destFilename, contents, templateData) } @@ -160,11 +108,11 @@ func (b *Builder) registerCombustionScript(scriptName string) { } func (b *Builder) generateOutputImageFilename() string { - filename := filepath.Join(b.buildConfig.ImageConfigDir, b.imageConfig.Image.OutputImageName) + filename := filepath.Join(b.context.ImageConfigDir, b.imageConfig.Image.OutputImageName) return filename } func (b *Builder) generateBaseImageFilename() string { - filename := filepath.Join(b.buildConfig.ImageConfigDir, "images", b.imageConfig.Image.BaseImage) + filename := filepath.Join(b.context.ImageConfigDir, "images", b.imageConfig.Image.BaseImage) return filename } diff --git a/pkg/build/build_test.go b/pkg/build/build_test.go index b4b81602..650b3ae8 100644 --- a/pkg/build/build_test.go +++ b/pkg/build/build_test.go @@ -7,93 +7,19 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/suse-edge/edge-image-builder/pkg/config" ) -func TestPrepareBuildDir(t *testing.T) { - // Setup - bc := config.BuildConfig{} - builder := New(nil, &bc) - - // Test - err := builder.prepareBuildDir() - defer os.RemoveAll(builder.eibBuildDir) - - // Verify - require.NoError(t, err) - _, err = os.Stat(builder.eibBuildDir) - require.NoError(t, err) -} - -func TestPrepareBuildDirExistingDir(t *testing.T) { - // Setup - tmpDir, err := os.MkdirTemp("", "eib-test-") - require.NoError(t, err) - defer os.RemoveAll(tmpDir) - - bc := config.BuildConfig{BuildDir: tmpDir} - builder := New(nil, &bc) - - // Test - err = builder.prepareBuildDir() - - // Verify - require.NoError(t, err) - require.Equal(t, tmpDir, builder.eibBuildDir) -} - -func TestCleanUpBuildDirTrue(t *testing.T) { - // Setup - tmpDir, err := os.MkdirTemp("", "eib-test-") - require.NoError(t, err) - defer os.RemoveAll(tmpDir) - - bc := config.BuildConfig{ - BuildDir: tmpDir, - DeleteBuildDir: true, - } - builder := New(nil, &bc) - require.NoError(t, builder.prepareBuildDir()) - - // Test - err = builder.cleanUpBuildDir() - - // Verify - require.NoError(t, err) - _, err = os.Stat(tmpDir) - require.Error(t, err) - assert.True(t, os.IsNotExist(err)) -} - -func TestCleanUpBuildDirFalse(t *testing.T) { +func TestGenerateCombustionScript(t *testing.T) { // Setup - tmpDir, err := os.MkdirTemp("", "eib-test-") + context, err := NewContext("", "", true) require.NoError(t, err) - defer os.RemoveAll(tmpDir) + defer func() { + assert.NoError(t, CleanUpBuildDir(context)) + }() - bc := config.BuildConfig{ - BuildDir: tmpDir, - DeleteBuildDir: false, + builder := Builder{ + context: context, } - builder := New(nil, &bc) - require.NoError(t, builder.prepareBuildDir()) - - // Test - err = builder.cleanUpBuildDir() - - // Verify - require.NoError(t, err) - _, err = os.Stat(tmpDir) - require.NoError(t, err) -} - -func TestGenerateCombustionScript(t *testing.T) { - // Setup - bc := config.BuildConfig{} - builder := New(nil, &bc) - err := builder.prepareBuildDir() - require.NoError(t, err) - defer os.RemoveAll(builder.eibBuildDir) builder.combustionScripts = append(builder.combustionScripts, "foo.sh", "bar.sh") @@ -104,7 +30,7 @@ func TestGenerateCombustionScript(t *testing.T) { require.NoError(t, err) // - check the script contents itself - scriptBytes, err := os.ReadFile(filepath.Join(builder.combustionDir, "script")) + scriptBytes, err := os.ReadFile(filepath.Join(context.CombustionDir, "script")) require.NoError(t, err) scriptData := string(scriptBytes) assert.Contains(t, scriptData, "#!/bin/bash") @@ -118,10 +44,15 @@ func TestGenerateCombustionScript(t *testing.T) { func TestWriteCombustionFile(t *testing.T) { // Setup - builder := New(nil, &config.BuildConfig{}) - err := builder.prepareBuildDir() + context, err := NewContext("", "", true) require.NoError(t, err) - defer os.RemoveAll(builder.eibBuildDir) + defer func() { + assert.NoError(t, CleanUpBuildDir(context)) + }() + + builder := Builder{ + context: context, + } testData := "Edge Image Builder" testFilename := "combustion-file.sh" @@ -132,7 +63,7 @@ func TestWriteCombustionFile(t *testing.T) { // Verify require.NoError(t, err) - expectedFilename := filepath.Join(builder.combustionDir, testFilename) + expectedFilename := filepath.Join(context.CombustionDir, testFilename) foundData, err := os.ReadFile(expectedFilename) require.NoError(t, err) assert.Equal(t, expectedFilename, writtenFilename) @@ -144,10 +75,15 @@ func TestWriteCombustionFile(t *testing.T) { func TestWriteBuildDirFile(t *testing.T) { // Setup - builder := New(nil, &config.BuildConfig{}) - err := builder.prepareBuildDir() + context, err := NewContext("", "", true) require.NoError(t, err) - defer os.RemoveAll(builder.eibBuildDir) + defer func() { + assert.NoError(t, CleanUpBuildDir(context)) + }() + + builder := Builder{ + context: context, + } testData := "Edge Image Builder" testFilename := "build-dir-file.sh" @@ -158,7 +94,7 @@ func TestWriteBuildDirFile(t *testing.T) { // Verify require.NoError(t, err) - expectedFilename := filepath.Join(builder.eibBuildDir, testFilename) + expectedFilename := filepath.Join(context.BuildDir, testFilename) require.Equal(t, expectedFilename, writtenFilename) foundData, err := os.ReadFile(expectedFilename) require.NoError(t, err) diff --git a/pkg/build/context.go b/pkg/build/context.go new file mode 100644 index 00000000..c49e41ce --- /dev/null +++ b/pkg/build/context.go @@ -0,0 +1,50 @@ +package build + +import ( + "fmt" + "os" + "path/filepath" +) + +type Context struct { + // ImageConfigDir is the root directory storing all configuration files. + ImageConfigDir string + // BuildDir is the directory used for assembling the different components used in a build. + BuildDir string + // CombustionDir is a subdirectory under BuildDir containing the Combustion script and all related files. + CombustionDir string + // DeleteBuildDir indicates whether the BuildDir should be cleaned up after the image is built. + DeleteBuildDir bool +} + +func NewContext(imageConfigDir, buildDir string, deleteBuildDir bool) (*Context, error) { + if buildDir == "" { + tmpDir, err := os.MkdirTemp("", "eib-") + if err != nil { + return nil, fmt.Errorf("creating a temporary build directory: %w", err) + } + buildDir = tmpDir + } + combustionDir := filepath.Join(buildDir, "combustion") + + if err := os.MkdirAll(combustionDir, os.ModePerm); err != nil { + return nil, fmt.Errorf("creating the combustion directory: %w", err) + } + + return &Context{ + ImageConfigDir: imageConfigDir, + BuildDir: buildDir, + CombustionDir: combustionDir, + DeleteBuildDir: deleteBuildDir, + }, nil +} + +func CleanUpBuildDir(c *Context) error { + if c.DeleteBuildDir { + err := os.RemoveAll(c.BuildDir) + if err != nil { + return fmt.Errorf("deleting build directory: %w", err) + } + } + return nil +} diff --git a/pkg/build/context_test.go b/pkg/build/context_test.go new file mode 100644 index 00000000..25753606 --- /dev/null +++ b/pkg/build/context_test.go @@ -0,0 +1,71 @@ +package build + +import ( + "io/fs" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestContext_New(t *testing.T) { + context, err := NewContext("", "", false) + require.NoError(t, err) + defer os.RemoveAll(context.BuildDir) + + _, err = os.Stat(context.BuildDir) + require.NoError(t, err) +} + +func TestContext_New_ExistingBuildDir(t *testing.T) { + // Setup + tmpDir, err := os.MkdirTemp("", "eib-test-") + require.NoError(t, err) + defer os.RemoveAll(tmpDir) + + // Test + context, err := NewContext("", tmpDir, false) + require.NoError(t, err) + + // Verify + require.Equal(t, tmpDir, context.BuildDir) +} + +func TestCleanUpBuildDirTrue(t *testing.T) { + // Setup + tmpDir, err := os.MkdirTemp("", "eib-test-") + require.NoError(t, err) + defer os.RemoveAll(tmpDir) + + context := &Context{ + BuildDir: tmpDir, + DeleteBuildDir: true, + } + + // Test + require.NoError(t, CleanUpBuildDir(context)) + + // Verify + _, err = os.Stat(tmpDir) + assert.ErrorIs(t, err, fs.ErrNotExist) +} + +func TestCleanUpBuildDirFalse(t *testing.T) { + // Setup + tmpDir, err := os.MkdirTemp("", "eib-test-") + require.NoError(t, err) + defer os.RemoveAll(tmpDir) + + context := &Context{ + BuildDir: tmpDir, + DeleteBuildDir: false, + } + + // Test + require.NoError(t, CleanUpBuildDir(context)) + + // Verify + _, err = os.Stat(tmpDir) + require.NoError(t, err) +} diff --git a/pkg/build/iso.go b/pkg/build/iso.go index 4e278cd5..9fa17bf9 100644 --- a/pkg/build/iso.go +++ b/pkg/build/iso.go @@ -65,7 +65,7 @@ func (b *Builder) createXorrisoCommand() (*exec.Cmd, *os.File, error) { func (b *Builder) generateXorrisoArgs() []string { indevPath := b.generateBaseImageFilename() outdevPath := b.generateOutputImageFilename() - mapDir := b.combustionDir + mapDir := b.context.CombustionDir args := fmt.Sprintf(xorrisoArgsBase, indevPath, outdevPath, mapDir) splitArgs := strings.Split(args, " ") @@ -75,6 +75,6 @@ func (b *Builder) generateXorrisoArgs() []string { func (b *Builder) generateIsoLogFilename() string { timestamp := time.Now().Format("Jan02_15-04-05") filename := fmt.Sprintf(xorrisoLogFile, timestamp) - logFilename := filepath.Join(b.eibBuildDir, filename) + logFilename := filepath.Join(b.context.BuildDir, filename) return logFilename } diff --git a/pkg/build/iso_test.go b/pkg/build/iso_test.go index 1892f5b7..75f2031a 100644 --- a/pkg/build/iso_test.go +++ b/pkg/build/iso_test.go @@ -21,10 +21,10 @@ func TestDeleteNoExistingImage(t *testing.T) { OutputImageName: "not-there", }, } - buildConfig := config.BuildConfig{ + context := Context{ ImageConfigDir: tmpDir, } - builder := New(&imageConfig, &buildConfig) + builder := New(&imageConfig, &context) // Test err = builder.deleteExistingOutputIso() @@ -44,10 +44,10 @@ func TestDeleteExistingImage(t *testing.T) { OutputImageName: "not-there", }, } - buildConfig := config.BuildConfig{ + context := Context{ ImageConfigDir: tmpDir, } - builder := New(&imageConfig, &buildConfig) + builder := New(&imageConfig, &context) _, err = os.Create(builder.generateOutputImageFilename()) require.NoError(t, err) @@ -71,11 +71,11 @@ func TestCreateXorrisoCommand(t *testing.T) { OutputImageName: "build-image", }, } - buildConfig := config.BuildConfig{ + context := Context{ ImageConfigDir: "config-dir", + CombustionDir: "combustion", } - builder := New(&imageConfig, &buildConfig) - builder.combustionDir = "combustion" + builder := New(&imageConfig, &context) // Test cmd, logfile, err := builder.createXorrisoCommand() diff --git a/pkg/build/message_test.go b/pkg/build/message_test.go index 6961d3ff..5fc45627 100644 --- a/pkg/build/message_test.go +++ b/pkg/build/message_test.go @@ -7,16 +7,17 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/suse-edge/edge-image-builder/pkg/config" ) func TestConfigureMessage(t *testing.T) { // Setup - bc := config.BuildConfig{} - builder := New(nil, &bc) - err := builder.prepareBuildDir() + context, err := NewContext("", "", true) require.NoError(t, err) - defer os.Remove(builder.eibBuildDir) + defer func() { + assert.NoError(t, CleanUpBuildDir(context)) + }() + + builder := Builder{context: context} // Test err = builder.configureMessage() @@ -24,7 +25,7 @@ func TestConfigureMessage(t *testing.T) { // Verify require.NoError(t, err) - _, err = os.Stat(filepath.Join(builder.combustionDir, messageScriptName)) + _, err = os.Stat(filepath.Join(builder.context.CombustionDir, messageScriptName)) require.NoError(t, err) require.Equal(t, 1, len(builder.combustionScripts)) diff --git a/pkg/build/raw.go b/pkg/build/raw.go index ade80281..5f167b00 100644 --- a/pkg/build/raw.go +++ b/pkg/build/raw.go @@ -62,7 +62,7 @@ func (b *Builder) writeModifyScript() error { ConfigureGRUB string }{ OutputImage: b.generateOutputImageFilename(), - CombustionDir: b.combustionDir, + CombustionDir: b.context.CombustionDir, ConfigureGRUB: grubConfiguration, } @@ -79,7 +79,7 @@ func (b *Builder) writeModifyScript() error { } func (b *Builder) createModifyCommand() *exec.Cmd { - scriptPath := filepath.Join(b.buildConfig.BuildDir, modifyScriptName) + scriptPath := filepath.Join(b.context.BuildDir, modifyScriptName) cmd := exec.Command(scriptPath) return cmd } diff --git a/pkg/build/raw_test.go b/pkg/build/raw_test.go index 88e8b414..119ffa79 100644 --- a/pkg/build/raw_test.go +++ b/pkg/build/raw_test.go @@ -19,10 +19,10 @@ func TestCreateRawImageCopyCommand(t *testing.T) { OutputImageName: "build-image", }, } - buildConfig := config.BuildConfig{ + context := Context{ ImageConfigDir: "config-dir", } - builder := New(&imageConfig, &buildConfig) + builder := New(&imageConfig, &context) // Test cmd := builder.createRawImageCopyCommand() @@ -53,12 +53,10 @@ func TestWriteModifyScript(t *testing.T) { KernelArgs: []string{"alpha", "beta"}, }, } - buildConfig := config.BuildConfig{ - ImageConfigDir: "config-dir", - BuildDir: tmpDir, - } - builder := New(&imageConfig, &buildConfig) - require.NoError(t, builder.prepareBuildDir()) + context, err := NewContext("config-dir", tmpDir, false) + require.NoError(t, err) + + builder := New(&imageConfig, context) // Test err = builder.writeModifyScript() @@ -76,16 +74,17 @@ func TestWriteModifyScript(t *testing.T) { foundContents := string(foundBytes) assert.Contains(t, foundContents, "guestfish --rw -a config-dir/output-image") - assert.Contains(t, foundContents, "copy-in "+builder.combustionDir) + assert.Contains(t, foundContents, "copy-in "+builder.context.CombustionDir) assert.Contains(t, foundContents, "download /boot/grub2/grub.cfg /tmp/grub.cfg") } func TestCreateModifyCommand(t *testing.T) { // Setup - buildConfig := config.BuildConfig{ - BuildDir: "build-dir", + builder := Builder{ + context: &Context{ + BuildDir: "build-dir", + }, } - builder := New(nil, &buildConfig) // Test cmd := builder.createModifyCommand() diff --git a/pkg/build/rpm.go b/pkg/build/rpm.go index ece27326..be556776 100644 --- a/pkg/build/rpm.go +++ b/pkg/build/rpm.go @@ -30,7 +30,7 @@ func (b *Builder) getRPMFileNames(rpmSourceDir string) ([]string, error) { } func (b *Builder) copyRPMs() error { - rpmSourceDir := filepath.Join(b.buildConfig.ImageConfigDir, "rpms") + rpmSourceDir := filepath.Join(b.context.ImageConfigDir, "rpms") // Only proceed with copying the RPMs if the directory exists _, err := os.Stat(rpmSourceDir) if err != nil { @@ -39,7 +39,7 @@ func (b *Builder) copyRPMs() error { } return fmt.Errorf("checking for rpm directory at %s: %w", rpmSourceDir, err) } - rpmDestDir := b.combustionDir + rpmDestDir := b.context.CombustionDir rpmFileNames, err := b.getRPMFileNames(rpmSourceDir) if err != nil { diff --git a/pkg/build/rpm_test.go b/pkg/build/rpm_test.go index b5420247..9866ecb1 100644 --- a/pkg/build/rpm_test.go +++ b/pkg/build/rpm_test.go @@ -7,20 +7,21 @@ import ( "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() + context, err := NewContext("../config/testdata", "", true) require.NoError(t, err) - defer os.Remove(builder.eibBuildDir) + defer func() { + assert.NoError(t, CleanUpBuildDir(context)) + }() + + builder := &Builder{ + context: context, + } - rpmSourceDir := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms") + rpmSourceDir := filepath.Join(context.ImageConfigDir, "rpms") file1Path := filepath.Join(rpmSourceDir, "rpm1.rpm") defer os.Remove(file1Path) @@ -48,16 +49,17 @@ func TestGetRPMFileNames(t *testing.T) { func TestCopyRPMs(t *testing.T) { // Setup - bc := config.BuildConfig{ - ImageConfigDir: "../config/testdata", - } - builder := New(nil, &bc) - err := builder.prepareBuildDir() + context, err := NewContext("../config/testdata", "", true) require.NoError(t, err) - defer os.Remove(builder.eibBuildDir) + defer func() { + assert.NoError(t, CleanUpBuildDir(context)) + }() + + builder := &Builder{ + context: context, + } - rpmSourceDir := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms") - rpmDestDir := builder.combustionDir + rpmSourceDir := filepath.Join(context.ImageConfigDir, "rpms") file1Path := filepath.Join(rpmSourceDir, "rpm1.rpm") defer os.Remove(file1Path) @@ -75,10 +77,10 @@ func TestCopyRPMs(t *testing.T) { // Verify require.NoError(t, err) - _, err = os.Stat(filepath.Join(rpmDestDir, "rpm1.rpm")) + _, err = os.Stat(filepath.Join(builder.context.CombustionDir, "rpm1.rpm")) require.NoError(t, err) - _, err = os.Stat(filepath.Join(rpmDestDir, "rpm2.rpm")) + _, err = os.Stat(filepath.Join(builder.context.CombustionDir, "rpm2.rpm")) require.NoError(t, err) // Cleanup @@ -88,15 +90,17 @@ func TestCopyRPMs(t *testing.T) { func TestGetRPMFileNamesNoRPMs(t *testing.T) { // Setup - bc := config.BuildConfig{ - ImageConfigDir: "../config/testdata", - } - builder := New(nil, &bc) - err := builder.prepareBuildDir() + context, err := NewContext("../config/testdata", "", true) require.NoError(t, err) - defer os.Remove(builder.eibBuildDir) + defer func() { + assert.NoError(t, CleanUpBuildDir(context)) + }() + + builder := &Builder{ + context: context, + } - rpmSourceDir := filepath.Join(builder.buildConfig.ImageConfigDir, "rpms") + rpmSourceDir := filepath.Join(context.ImageConfigDir, "rpms") // Test rpmFileNames, err := builder.getRPMFileNames(rpmSourceDir) @@ -109,13 +113,15 @@ func TestGetRPMFileNamesNoRPMs(t *testing.T) { func TestCopyRPMsNoRPMDir(t *testing.T) { // Setup - bc := config.BuildConfig{ - ImageConfigDir: "../config/ThisDirDoesNotExist", - } - builder := New(nil, &bc) - err := builder.prepareBuildDir() + context, err := NewContext("../config/ThisDirDoesNotExist", "", true) require.NoError(t, err) - defer os.Remove(builder.eibBuildDir) + defer func() { + assert.NoError(t, CleanUpBuildDir(context)) + }() + + builder := &Builder{ + context: context, + } // Test err = builder.copyRPMs() diff --git a/pkg/build/scripts.go b/pkg/build/scripts.go index 364b819d..fba2147f 100644 --- a/pkg/build/scripts.go +++ b/pkg/build/scripts.go @@ -14,7 +14,7 @@ const ( ) func (b *Builder) configureScripts() error { - fullScriptsDir := filepath.Join(b.buildConfig.ImageConfigDir, scriptsDir) + fullScriptsDir := filepath.Join(b.context.ImageConfigDir, scriptsDir) // Nothing to do if the image config dir doesn't have the scripts directory _, err := os.Stat(fullScriptsDir) @@ -37,7 +37,7 @@ func (b *Builder) configureScripts() error { for _, scriptEntry := range dirListing { copyMe := filepath.Join(fullScriptsDir, scriptEntry.Name()) - copyTo := filepath.Join(b.combustionDir, scriptEntry.Name()) + copyTo := filepath.Join(b.context.CombustionDir, scriptEntry.Name()) err = fileio.CopyFile(copyMe, copyTo) if err != nil { diff --git a/pkg/build/scripts_test.go b/pkg/build/scripts_test.go index 653ee336..32973fb7 100644 --- a/pkg/build/scripts_test.go +++ b/pkg/build/scripts_test.go @@ -8,7 +8,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/suse-edge/edge-image-builder/pkg/config" ) func TestConfigureScripts(t *testing.T) { @@ -34,8 +33,12 @@ func TestConfigureScripts(t *testing.T) { require.NoError(t, err) defer os.RemoveAll(tmpDestDir) - builder := New(nil, &config.BuildConfig{ImageConfigDir: tmpSrcDir}) - builder.combustionDir = tmpDestDir + builder := &Builder{ + context: &Context{ + ImageConfigDir: tmpSrcDir, + CombustionDir: tmpDestDir, + }, + } // Test err = builder.configureScripts() @@ -50,7 +53,7 @@ func TestConfigureScripts(t *testing.T) { // - make sure the copied files have the right permissions for _, entry := range foundDirListing { - fullEntryPath := filepath.Join(builder.combustionDir, entry.Name()) + fullEntryPath := filepath.Join(builder.context.CombustionDir, entry.Name()) stats, err := os.Stat(fullEntryPath) require.NoError(t, err) assert.Equal(t, fs.FileMode(scriptMode), stats.Mode()) @@ -67,7 +70,11 @@ func TestConfigureScriptsNoScriptsDir(t *testing.T) { require.NoError(t, err) defer os.RemoveAll(tmpSrcDir) - builder := New(nil, &config.BuildConfig{ImageConfigDir: tmpSrcDir}) + builder := &Builder{ + context: &Context{ + ImageConfigDir: tmpSrcDir, + }, + } // Test err = builder.configureScripts() @@ -88,7 +95,11 @@ func TestConfigureScriptsEmptyScriptsDir(t *testing.T) { err = os.MkdirAll(fullScriptsDir, os.ModePerm) require.NoError(t, err) - builder := New(nil, &config.BuildConfig{ImageConfigDir: tmpSrcDir}) + builder := &Builder{ + context: &Context{ + ImageConfigDir: tmpSrcDir, + }, + } // Test err = builder.configureScripts() diff --git a/pkg/config/build.go b/pkg/config/build.go deleted file mode 100644 index fe622d97..00000000 --- a/pkg/config/build.go +++ /dev/null @@ -1,7 +0,0 @@ -package config - -type BuildConfig struct { - ImageConfigDir string - BuildDir string - DeleteBuildDir bool -}