From 0dabc8bfc75ff2816ce0f98fa78b3df8c11b92c7 Mon Sep 17 00:00:00 2001 From: Atanas Dinov Date: Tue, 21 Nov 2023 15:05:10 +0200 Subject: [PATCH] Initialize ImageConfig field in Context Signed-off-by: Atanas Dinov --- cmd/eib/main.go | 2 +- pkg/build/build_test.go | 11 ++-- pkg/build/raw_test.go | 8 +-- pkg/combustion/combustion_test.go | 34 ++++++++++ pkg/combustion/message_test.go | 8 +-- pkg/combustion/rpm_test.go | 102 +++++++++++++----------------- pkg/combustion/users_test.go | 21 ++---- pkg/context/context.go | 7 +- pkg/context/context_test.go | 4 +- 9 files changed, 105 insertions(+), 92 deletions(-) create mode 100644 pkg/combustion/combustion_test.go diff --git a/cmd/eib/main.go b/cmd/eib/main.go index d3d6719e..f6b273b9 100644 --- a/cmd/eib/main.go +++ b/cmd/eib/main.go @@ -51,7 +51,7 @@ func processArgs() (*config.ImageConfig, *context.Context, error) { return nil, nil, fmt.Errorf("validating the config dir %s: %w", configDir, err) } - ctx, err := context.NewContext(configDir, buildDir, deleteBuildDir) + ctx, err := context.NewContext(configDir, buildDir, deleteBuildDir, imageConfig) if err != nil { return nil, nil, fmt.Errorf("building dir structure: %w", err) } diff --git a/pkg/build/build_test.go b/pkg/build/build_test.go index d53f828f..6d565a30 100644 --- a/pkg/build/build_test.go +++ b/pkg/build/build_test.go @@ -1,6 +1,7 @@ package build import ( + "os" "path/filepath" "testing" @@ -11,14 +12,16 @@ import ( func TestGenerateBuildDirFilename(t *testing.T) { // Setup - ctx, err := context.NewContext("", "", true) + tmpDir, err := os.MkdirTemp("", "eib-") require.NoError(t, err) defer func() { - assert.NoError(t, context.CleanUpBuildDir(ctx)) + assert.NoError(t, os.RemoveAll(tmpDir)) }() builder := Builder{ - context: ctx, + context: &context.Context{ + BuildDir: tmpDir, + }, } testFilename := "build-dir-file.sh" @@ -27,6 +30,6 @@ func TestGenerateBuildDirFilename(t *testing.T) { filename := builder.generateBuildDirFilename(testFilename) // Verify - expectedFilename := filepath.Join(ctx.BuildDir, testFilename) + expectedFilename := filepath.Join(builder.context.BuildDir, testFilename) require.Equal(t, expectedFilename, filename) } diff --git a/pkg/build/raw_test.go b/pkg/build/raw_test.go index 489bb0f2..049edb82 100644 --- a/pkg/build/raw_test.go +++ b/pkg/build/raw_test.go @@ -47,9 +47,6 @@ func TestWriteModifyScript(t *testing.T) { require.NoError(t, err) defer os.RemoveAll(tmpDir) - ctx, err := context.NewContext("config-dir", tmpDir, false) - require.NoError(t, err) - builder := Builder{ imageConfig: &config.ImageConfig{ Image: config.Image{ @@ -59,7 +56,10 @@ func TestWriteModifyScript(t *testing.T) { KernelArgs: []string{"alpha", "beta"}, }, }, - context: ctx, + context: &context.Context{ + ImageConfigDir: "config-dir", + BuildDir: tmpDir, + }, } // Test diff --git a/pkg/combustion/combustion_test.go b/pkg/combustion/combustion_test.go new file mode 100644 index 00000000..cb0c8c14 --- /dev/null +++ b/pkg/combustion/combustion_test.go @@ -0,0 +1,34 @@ +package combustion + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/suse-edge/edge-image-builder/pkg/config" + "github.com/suse-edge/edge-image-builder/pkg/context" +) + +func setupContext(t *testing.T) (ctx *context.Context, teardown func()) { + configDir, err := os.MkdirTemp("", "eib-config-") + require.NoError(t, err) + + buildDir, err := os.MkdirTemp("", "eib-build-") + require.NoError(t, err) + + combustionDir, err := os.MkdirTemp("", "eib-combustion-") + require.NoError(t, err) + + ctx = &context.Context{ + ImageConfigDir: configDir, + BuildDir: buildDir, + CombustionDir: combustionDir, + ImageConfig: &config.ImageConfig{}, + } + + return ctx, func() { + assert.NoError(t, os.RemoveAll(buildDir)) + assert.NoError(t, os.RemoveAll(combustionDir)) + } +} diff --git a/pkg/combustion/message_test.go b/pkg/combustion/message_test.go index 19f629e9..1989be60 100644 --- a/pkg/combustion/message_test.go +++ b/pkg/combustion/message_test.go @@ -7,16 +7,12 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/suse-edge/edge-image-builder/pkg/context" ) func TestConfigureMessage(t *testing.T) { // Setup - ctx, err := context.NewContext("", "", true) - require.NoError(t, err) - defer func() { - assert.NoError(t, context.CleanUpBuildDir(ctx)) - }() + ctx, teardown := setupContext(t) + defer teardown() // Test scripts, err := configureMessage(ctx) diff --git a/pkg/combustion/rpm_test.go b/pkg/combustion/rpm_test.go index eb983df5..39401a1c 100644 --- a/pkg/combustion/rpm_test.go +++ b/pkg/combustion/rpm_test.go @@ -11,38 +11,29 @@ import ( "github.com/suse-edge/edge-image-builder/pkg/fileio" ) -func setupRPMSourceDir(t *testing.T, addFiles bool) (tmpDir string, rpmSourceDir string, teardown func()) { - tmpDir, err := os.MkdirTemp("", "eib-RPM-") +func setupRPMSourceDir(t *testing.T) (ctx *context.Context, rpmSourceDir string, teardown func()) { + ctx, teardownCtx := setupContext(t) + + rpmSourceDir = filepath.Join(ctx.ImageConfigDir, "rpms") + require.NoError(t, os.Mkdir(rpmSourceDir, 0o755)) + + file1, err := os.Create(filepath.Join(rpmSourceDir, "rpm1.rpm")) require.NoError(t, err) - rpmSourceDir = filepath.Join(tmpDir, "rpms") - err = os.Mkdir(rpmSourceDir, 0o755) + file2, err := os.Create(filepath.Join(rpmSourceDir, "rpm2.rpm")) require.NoError(t, err) - var file1 *os.File - var file2 *os.File - if addFiles { - file1Path := filepath.Join(rpmSourceDir, "rpm1.rpm") - file1, err = os.Create(file1Path) - require.NoError(t, err) + return ctx, rpmSourceDir, func() { + assert.NoError(t, file1.Close()) + assert.NoError(t, file2.Close()) - file2Path := filepath.Join(rpmSourceDir, "rpm2.rpm") - file2, err = os.Create(file2Path) - require.NoError(t, err) - } - - return tmpDir, rpmSourceDir, func() { - if addFiles { - assert.NoError(t, file1.Close()) - assert.NoError(t, file2.Close()) - } - assert.NoError(t, os.RemoveAll(tmpDir)) + teardownCtx() } } func TestGetRPMFileNames(t *testing.T) { // Setup - _, rpmSourceDir, teardown := setupRPMSourceDir(t, true) + _, rpmSourceDir, teardown := setupRPMSourceDir(t) defer teardown() // Test @@ -57,20 +48,17 @@ func TestGetRPMFileNames(t *testing.T) { func TestCopyRPMs(t *testing.T) { // Setup - tmpDir, rpmSourceDir, teardown := setupRPMSourceDir(t, true) + ctx, rpmSourceDir, teardown := setupRPMSourceDir(t) defer teardown() - tmpDestDir := filepath.Join(tmpDir, "temp-dest-dir") - err := os.Mkdir(tmpDestDir, 0o755) - require.NoError(t, err) + tmpDestDir := filepath.Join(ctx.BuildDir, "temp-dest-dir") + require.NoError(t, os.Mkdir(tmpDestDir, 0o755)) // Test - err = copyRPMs(rpmSourceDir, tmpDestDir, []string{"rpm1.rpm", "rpm2.rpm"}) + require.NoError(t, copyRPMs(rpmSourceDir, tmpDestDir, []string{"rpm1.rpm", "rpm2.rpm"})) // Verify - require.NoError(t, err) - - _, err = os.Stat(filepath.Join(tmpDestDir, "rpm1.rpm")) + _, err := os.Stat(filepath.Join(tmpDestDir, "rpm1.rpm")) require.NoError(t, err) _, err = os.Stat(filepath.Join(tmpDestDir, "rpm2.rpm")) @@ -79,11 +67,14 @@ func TestCopyRPMs(t *testing.T) { func TestGetRPMFileNamesNoRPMs(t *testing.T) { // Setup - _, rpmSourceDir, teardown := setupRPMSourceDir(t, false) - defer teardown() + tmpDir, err := os.MkdirTemp("", "eib-rpm-") + require.NoError(t, err) + defer func() { + assert.NoError(t, os.RemoveAll(tmpDir)) + }() // Test - rpmFileNames, err := getRPMFileNames(rpmSourceDir) + rpmFileNames, err := getRPMFileNames(tmpDir) // Verify require.ErrorContains(t, err, "no RPMs found") @@ -93,11 +84,14 @@ func TestGetRPMFileNamesNoRPMs(t *testing.T) { func TestCopyRPMsNoRPMDestDir(t *testing.T) { // Setup - _, rpmSourceDir, teardown := setupRPMSourceDir(t, true) - defer teardown() + tmpDir, err := os.MkdirTemp("", "eib-rpm-") + require.NoError(t, err) + defer func() { + assert.NoError(t, os.RemoveAll(tmpDir)) + }() // Test - err := copyRPMs(rpmSourceDir, "", []string{"rpm1.rpm", "rpm2.rpm"}) + err = copyRPMs(tmpDir, "", []string{"rpm1.rpm", "rpm2.rpm"}) // Verify require.ErrorContains(t, err, "RPM destination directory cannot be empty") @@ -105,11 +99,14 @@ func TestCopyRPMsNoRPMDestDir(t *testing.T) { func TestCopyRPMsNoRPMSrcDir(t *testing.T) { // Setup - tmpDestDir, _, teardown := setupRPMSourceDir(t, true) - defer teardown() + tmpDir, err := os.MkdirTemp("", "eib-rpm-") + require.NoError(t, err) + defer func() { + assert.NoError(t, os.RemoveAll(tmpDir)) + }() // Test - err := copyRPMs("", tmpDestDir, []string{"rpm1.rpm", "rpm2.rpm"}) + err = copyRPMs("", tmpDir, []string{"rpm1.rpm", "rpm2.rpm"}) // Verify require.ErrorContains(t, err, "opening source file") @@ -117,11 +114,8 @@ func TestCopyRPMsNoRPMSrcDir(t *testing.T) { func TestWriteRPMScript(t *testing.T) { // Setup - ctx, err := context.NewContext("", "", true) - require.NoError(t, err) - defer func() { - assert.NoError(t, context.CleanUpBuildDir(ctx)) - }() + ctx, teardown := setupContext(t) + defer teardown() // Test script, err := writeRPMScript(ctx, []string{"rpm1.rpm", "rpm2.rpm"}) @@ -144,17 +138,11 @@ func TestWriteRPMScript(t *testing.T) { assert.Contains(t, foundContents, "rpm2.rpm") } -func TestProcessRPMs(t *testing.T) { +func TestConfigureRPMs(t *testing.T) { // Setup - tmpDir, _, teardown := setupRPMSourceDir(t, true) + ctx, _, teardown := setupRPMSourceDir(t) defer teardown() - ctx, err := context.NewContext(tmpDir, "", true) - require.NoError(t, err) - defer func() { - assert.NoError(t, context.CleanUpBuildDir(ctx)) - }() - // Test scripts, err := configureRPMs(ctx) @@ -181,13 +169,13 @@ func TestProcessRPMs(t *testing.T) { func TestGenerateRPMPath(t *testing.T) { // Setup - tmpDir, expectedPath, teardown := setupRPMSourceDir(t, false) + ctx, teardown := setupContext(t) defer teardown() - ctx, err := context.NewContext(tmpDir, "", true) - require.NoError(t, err) + rpmDir := filepath.Join(ctx.ImageConfigDir, "rpms") + require.NoError(t, os.Mkdir(rpmDir, 0o755)) defer func() { - assert.NoError(t, context.CleanUpBuildDir(ctx)) + assert.NoError(t, os.RemoveAll(rpmDir)) }() // Test @@ -196,5 +184,5 @@ func TestGenerateRPMPath(t *testing.T) { // Verify require.NoError(t, err) - assert.Equal(t, expectedPath, generatedPath) + assert.Equal(t, rpmDir, generatedPath) } diff --git a/pkg/combustion/users_test.go b/pkg/combustion/users_test.go index 401c19e8..915e4edc 100644 --- a/pkg/combustion/users_test.go +++ b/pkg/combustion/users_test.go @@ -8,13 +8,15 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/suse-edge/edge-image-builder/pkg/config" - "github.com/suse-edge/edge-image-builder/pkg/context" "github.com/suse-edge/edge-image-builder/pkg/fileio" ) func TestConfigureUsers(t *testing.T) { // Setup - imageConfig := config.ImageConfig{ + ctx, teardown := setupContext(t) + defer teardown() + + ctx.ImageConfig = &config.ImageConfig{ OperatingSystem: config.OperatingSystem{ Users: []config.OperatingSystemUser{ { @@ -39,14 +41,6 @@ func TestConfigureUsers(t *testing.T) { }, } - ctx, err := context.NewContext("", "", true) - require.NoError(t, err) - defer func() { - assert.NoError(t, context.CleanUpBuildDir(ctx)) - }() - - ctx.ImageConfig = imageConfig - // Test scripts, err := configureUsers(ctx) @@ -97,11 +91,8 @@ func TestConfigureUsers(t *testing.T) { func TestConfigureUsers_NoUsers(t *testing.T) { // Setup - ctx, err := context.NewContext("", "", true) - require.NoError(t, err) - defer func() { - assert.NoError(t, context.CleanUpBuildDir(ctx)) - }() + ctx, teardown := setupContext(t) + defer teardown() // Test scripts, err := configureUsers(ctx) diff --git a/pkg/context/context.go b/pkg/context/context.go index 45b1bd48..a92bba52 100644 --- a/pkg/context/context.go +++ b/pkg/context/context.go @@ -17,11 +17,11 @@ type Context struct { CombustionDir string // DeleteBuildDir indicates whether the BuildDir should be cleaned up after the image is built. DeleteBuildDir bool - - config.ImageConfig + // ImageConfig contains the image definition properties. + *config.ImageConfig } -func NewContext(imageConfigDir, buildDir string, deleteBuildDir bool) (*Context, error) { +func NewContext(imageConfigDir, buildDir string, deleteBuildDir bool, imageConfig *config.ImageConfig) (*Context, error) { if buildDir == "" { tmpDir, err := os.MkdirTemp("", "eib-") if err != nil { @@ -40,6 +40,7 @@ func NewContext(imageConfigDir, buildDir string, deleteBuildDir bool) (*Context, BuildDir: buildDir, CombustionDir: combustionDir, DeleteBuildDir: deleteBuildDir, + ImageConfig: imageConfig, }, nil } diff --git a/pkg/context/context_test.go b/pkg/context/context_test.go index 9f47fb09..001472a2 100644 --- a/pkg/context/context_test.go +++ b/pkg/context/context_test.go @@ -10,7 +10,7 @@ import ( ) func TestContext_New(t *testing.T) { - context, err := NewContext("", "", false) + context, err := NewContext("", "", false, nil) require.NoError(t, err) defer os.RemoveAll(context.BuildDir) @@ -25,7 +25,7 @@ func TestContext_New_ExistingBuildDir(t *testing.T) { defer os.RemoveAll(tmpDir) // Test - context, err := NewContext("", tmpDir, false) + context, err := NewContext("", tmpDir, false, nil) require.NoError(t, err) // Verify