From 54e42e6ed62a05b945aea6539a5a43fda3395b5c Mon Sep 17 00:00:00 2001 From: dbw7 Date: Thu, 28 Mar 2024 14:16:56 -0400 Subject: [PATCH] refactor remove image logic --- RELEASE_NOTES.md | 1 + pkg/build/build.go | 9 +++++++ pkg/build/build_test.go | 55 +++++++++++++++++++++++++++++++++++++++++ pkg/build/iso.go | 11 +-------- pkg/build/iso_test.go | 55 ----------------------------------------- pkg/build/raw.go | 4 +++ 6 files changed, 70 insertions(+), 65 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 2876c844..bcc96d63 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -10,6 +10,7 @@ * [#319](https://github.com/suse-edge/edge-image-builder/issues/319) - Combustion fails when combustion directory content is larger than half of the RAM of the system * [#233](https://github.com/suse-edge/edge-image-builder/issues/233) - Use different Helm chart sources for development and production builds +* [#337](https://github.com/suse-edge/edge-image-builder/issues/337) - Re-running raw builds should remove the previous built image --- diff --git a/pkg/build/build.go b/pkg/build/build.go index c7c09ba2..cf9ebd43 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -69,6 +69,15 @@ func (b *Builder) generateBaseImageFilename() string { return filename } +func (b *Builder) deleteExistingOutputImage() error { + outputFilename := b.generateOutputImageFilename() + err := os.Remove(outputFilename) + if err != nil && !os.IsNotExist(err) { + return fmt.Errorf("error deleting file %s: %w", outputFilename, err) + } + return nil +} + func SetupBuildDirectory(rootDir string) (string, error) { timestamp := time.Now().Format("Jan02_15-04-05") buildDir := filepath.Join(rootDir, fmt.Sprintf("build-%s", timestamp)) diff --git a/pkg/build/build_test.go b/pkg/build/build_test.go index 672ff862..4cc8ba81 100644 --- a/pkg/build/build_test.go +++ b/pkg/build/build_test.go @@ -80,3 +80,58 @@ func TestGenerateBuildDirFilename(t *testing.T) { expectedFilename := filepath.Join(builder.context.BuildDir, testFilename) require.Equal(t, expectedFilename, filename) } + +func TestDeleteNoExistingImage(t *testing.T) { + // Setup + tmpDir, err := os.MkdirTemp("", "eib-") + require.NoError(t, err) + defer os.RemoveAll(tmpDir) + + builder := Builder{ + context: &image.Context{ + ImageConfigDir: tmpDir, + ImageDefinition: &image.Definition{ + Image: image.Image{ + OutputImageName: "not-there", + }, + }, + }, + } + + // Test + err = builder.deleteExistingOutputImage() + + // Verify + require.NoError(t, err) +} + +func TestDeleteExistingImage(t *testing.T) { + // Setup + tmpDir, err := os.MkdirTemp("", "eib-") + require.NoError(t, err) + defer os.RemoveAll(tmpDir) + + builder := Builder{ + context: &image.Context{ + ImageConfigDir: tmpDir, + ImageDefinition: &image.Definition{ + Image: image.Image{ + OutputImageName: "not-there", + }, + }, + }, + } + + _, err = os.Create(builder.generateOutputImageFilename()) + require.NoError(t, err) + + // Test + err = builder.deleteExistingOutputImage() + + // Verify + require.NoError(t, err) + + _, err = os.Stat(builder.generateOutputImageFilename()) + require.Error(t, err) + require.True(t, os.IsNotExist(err)) +} diff --git a/pkg/build/iso.go b/pkg/build/iso.go index 8e508d8f..74e0d282 100644 --- a/pkg/build/iso.go +++ b/pkg/build/iso.go @@ -29,7 +29,7 @@ var extractIsoTemplate string var rebuildIsoTemplate string func (b *Builder) buildIsoImage() error { - if err := b.deleteExistingOutputIso(); err != nil { + if err := b.deleteExistingOutputImage(); err != nil { return fmt.Errorf("deleting existing ISO image: %w", err) } @@ -96,15 +96,6 @@ func (b *Builder) rebuildIso() error { return nil } -func (b *Builder) deleteExistingOutputIso() error { - outputFilename := b.generateOutputImageFilename() - err := os.Remove(outputFilename) - if err != nil && !os.IsNotExist(err) { - return fmt.Errorf("error deleting file %s: %w", outputFilename, err) - } - return nil -} - func (b *Builder) writeIsoScript(templateContents, outputFilename string) error { scriptName := filepath.Join(b.context.BuildDir, outputFilename) isoExtractPath := filepath.Join(b.context.BuildDir, isoExtractDir) diff --git a/pkg/build/iso_test.go b/pkg/build/iso_test.go index 66ec3591..fdd5bdbf 100644 --- a/pkg/build/iso_test.go +++ b/pkg/build/iso_test.go @@ -38,61 +38,6 @@ func setupContext(t *testing.T) (ctx *image.Context, teardown func()) { } } -func TestDeleteNoExistingImage(t *testing.T) { - // Setup - tmpDir, err := os.MkdirTemp("", "eib-") - require.NoError(t, err) - defer os.RemoveAll(tmpDir) - - builder := Builder{ - context: &image.Context{ - ImageConfigDir: tmpDir, - ImageDefinition: &image.Definition{ - Image: image.Image{ - OutputImageName: "not-there", - }, - }, - }, - } - - // Test - err = builder.deleteExistingOutputIso() - - // Verify - require.NoError(t, err) -} - -func TestDeleteExistingImage(t *testing.T) { - // Setup - tmpDir, err := os.MkdirTemp("", "eib-") - require.NoError(t, err) - defer os.RemoveAll(tmpDir) - - builder := Builder{ - context: &image.Context{ - ImageConfigDir: tmpDir, - ImageDefinition: &image.Definition{ - Image: image.Image{ - OutputImageName: "not-there", - }, - }, - }, - } - - _, err = os.Create(builder.generateOutputImageFilename()) - require.NoError(t, err) - - // Test - err = builder.deleteExistingOutputIso() - - // Verify - require.NoError(t, err) - - _, err = os.Stat(builder.generateOutputImageFilename()) - require.Error(t, err) - require.True(t, os.IsNotExist(err)) -} - func TestWriteIsoScript_Extract(t *testing.T) { // Setup ctx, teardown := setupContext(t) diff --git a/pkg/build/raw.go b/pkg/build/raw.go index 5b197679..bf003ed5 100644 --- a/pkg/build/raw.go +++ b/pkg/build/raw.go @@ -23,6 +23,10 @@ const ( var modifyRawImageTemplate string func (b *Builder) buildRawImage() error { + if err := b.deleteExistingOutputImage(); err != nil { + return fmt.Errorf("deleting existing RAW image: %w", err) + } + cmd := b.createRawImageCopyCommand() if err := cmd.Run(); err != nil { return fmt.Errorf("copying the base image %s to the output image location %s: %w",