Skip to content

Commit

Permalink
Initialize ImageConfig field in Context
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov committed Nov 21, 2023
1 parent f1d7c53 commit 0dabc8b
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 92 deletions.
2 changes: 1 addition & 1 deletion cmd/eib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package build

import (
"os"
"path/filepath"
"testing"

Expand All @@ -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"
Expand All @@ -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)
}
8 changes: 4 additions & 4 deletions pkg/build/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -59,7 +56,10 @@ func TestWriteModifyScript(t *testing.T) {
KernelArgs: []string{"alpha", "beta"},
},
},
context: ctx,
context: &context.Context{
ImageConfigDir: "config-dir",
BuildDir: tmpDir,
},
}

// Test
Expand Down
34 changes: 34 additions & 0 deletions pkg/combustion/combustion_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
8 changes: 2 additions & 6 deletions pkg/combustion/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
102 changes: 45 additions & 57 deletions pkg/combustion/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
Expand All @@ -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")
Expand All @@ -93,35 +84,38 @@ 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")
}

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")
}

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"})
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -196,5 +184,5 @@ func TestGenerateRPMPath(t *testing.T) {
// Verify
require.NoError(t, err)

assert.Equal(t, expectedPath, generatedPath)
assert.Equal(t, rpmDir, generatedPath)
}
21 changes: 6 additions & 15 deletions pkg/combustion/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -40,6 +40,7 @@ func NewContext(imageConfigDir, buildDir string, deleteBuildDir bool) (*Context,
BuildDir: buildDir,
CombustionDir: combustionDir,
DeleteBuildDir: deleteBuildDir,
ImageConfig: imageConfig,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit 0dabc8b

Please sign in to comment.