Skip to content

Commit

Permalink
Extract Context type to context package
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 463523f commit fe58bfd
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 52 deletions.
13 changes: 7 additions & 6 deletions cmd/eib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/suse-edge/edge-image-builder/pkg/build"
"github.com/suse-edge/edge-image-builder/pkg/config"
"github.com/suse-edge/edge-image-builder/pkg/context"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand All @@ -20,7 +21,7 @@ const (
argVerbose = "verbose"
)

func processArgs() (*config.ImageConfig, *build.Context, error) {
func processArgs() (*config.ImageConfig, *context.Context, error) {
var (
configFile string
configDir string
Expand Down Expand Up @@ -49,12 +50,12 @@ func processArgs() (*config.ImageConfig, *build.Context, error) {
return nil, nil, fmt.Errorf("validating the config dir %s: %w", configDir, err)
}

context, err := build.NewContext(configDir, buildDir, deleteBuildDir)
ctx, err := context.NewContext(configDir, buildDir, deleteBuildDir)
if err != nil {
return nil, nil, fmt.Errorf("building dir structure: %w", err)
}

return imageConfig, context, err
return imageConfig, ctx, err
}

func setupLogging(verbose bool) {
Expand Down Expand Up @@ -111,17 +112,17 @@ func validateImageConfigDir(configDir string) error {
}

func main() {
imageConfig, context, err := processArgs()
imageConfig, ctx, err := processArgs()
if err != nil {
zap.L().Fatal("CLI arguments could not be parsed", zap.Error(err))
}

builder := build.New(imageConfig, context)
builder := build.New(imageConfig, ctx)
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 {
if err = context.CleanUpBuildDir(ctx); err != nil {
zap.L().Error("Failed to clean up build directory", zap.Error(err))
}
}
5 changes: 3 additions & 2 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (

"github.com/suse-edge/edge-image-builder/pkg/combustion"
"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"
)

type Builder struct {
imageConfig *config.ImageConfig
context *Context
context *context.Context
}

func New(imageConfig *config.ImageConfig, context *Context) *Builder {
func New(imageConfig *config.ImageConfig, context *context.Context) *Builder {
return &Builder{
imageConfig: imageConfig,
context: context,
Expand Down
17 changes: 9 additions & 8 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/suse-edge/edge-image-builder/pkg/context"
)

func TestGenerateCombustionDirFilename(t *testing.T) {
// Setup
context, err := NewContext("", "", true)
ctx, err := context.NewContext("", "", true)
require.NoError(t, err)
defer func() {
assert.NoError(t, CleanUpBuildDir(context))
assert.NoError(t, context.CleanUpBuildDir(ctx))
}()

builder := Builder{
context: context,
context: ctx,
}

testFilename := "combustion-file.sh"
Expand All @@ -26,20 +27,20 @@ func TestGenerateCombustionDirFilename(t *testing.T) {
filename := builder.generateCombustionDirFilename(testFilename)

// Verify
expectedFilename := filepath.Join(context.CombustionDir, testFilename)
expectedFilename := filepath.Join(ctx.CombustionDir, testFilename)
assert.Equal(t, expectedFilename, filename)
}

func TestGenerateBuildDirFilename(t *testing.T) {
// Setup
context, err := NewContext("", "", true)
ctx, err := context.NewContext("", "", true)
require.NoError(t, err)
defer func() {
assert.NoError(t, CleanUpBuildDir(context))
assert.NoError(t, context.CleanUpBuildDir(ctx))
}()

builder := Builder{
context: context,
context: ctx,
}

testFilename := "build-dir-file.sh"
Expand All @@ -48,6 +49,6 @@ func TestGenerateBuildDirFilename(t *testing.T) {
filename := builder.generateBuildDirFilename(testFilename)

// Verify
expectedFilename := filepath.Join(context.BuildDir, testFilename)
expectedFilename := filepath.Join(ctx.BuildDir, testFilename)
require.Equal(t, expectedFilename, filename)
}
13 changes: 7 additions & 6 deletions pkg/build/iso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
)

func TestDeleteNoExistingImage(t *testing.T) {
Expand All @@ -21,10 +22,10 @@ func TestDeleteNoExistingImage(t *testing.T) {
OutputImageName: "not-there",
},
}
context := Context{
ctx := context.Context{
ImageConfigDir: tmpDir,
}
builder := New(&imageConfig, &context)
builder := New(&imageConfig, &ctx)

// Test
err = builder.deleteExistingOutputIso()
Expand All @@ -44,10 +45,10 @@ func TestDeleteExistingImage(t *testing.T) {
OutputImageName: "not-there",
},
}
context := Context{
ctx := context.Context{
ImageConfigDir: tmpDir,
}
builder := New(&imageConfig, &context)
builder := New(&imageConfig, &ctx)

_, err = os.Create(builder.generateOutputImageFilename())
require.NoError(t, err)
Expand All @@ -71,11 +72,11 @@ func TestCreateXorrisoCommand(t *testing.T) {
OutputImageName: "build-image",
},
}
context := Context{
ctx := context.Context{
ImageConfigDir: "config-dir",
CombustionDir: "combustion",
}
builder := New(&imageConfig, &context)
builder := New(&imageConfig, &ctx)

// Test
cmd, logfile, err := builder.createXorrisoCommand()
Expand Down
7 changes: 4 additions & 3 deletions pkg/build/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ 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
context, err := NewContext("", "", true)
ctx, err := context.NewContext("", "", true)
require.NoError(t, err)
defer func() {
assert.NoError(t, CleanUpBuildDir(context))
assert.NoError(t, context.CleanUpBuildDir(ctx))
}()

builder := Builder{context: context}
builder := Builder{context: ctx}

// Test
script, err := builder.configureMessage()
Expand Down
11 changes: 6 additions & 5 deletions pkg/build/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
)

Expand All @@ -19,10 +20,10 @@ func TestCreateRawImageCopyCommand(t *testing.T) {
OutputImageName: "build-image",
},
}
context := Context{
ctx := context.Context{
ImageConfigDir: "config-dir",
}
builder := New(&imageConfig, &context)
builder := New(&imageConfig, &ctx)

// Test
cmd := builder.createRawImageCopyCommand()
Expand Down Expand Up @@ -53,10 +54,10 @@ func TestWriteModifyScript(t *testing.T) {
KernelArgs: []string{"alpha", "beta"},
},
}
context, err := NewContext("config-dir", tmpDir, false)
ctx, err := context.NewContext("config-dir", tmpDir, false)
require.NoError(t, err)

builder := New(&imageConfig, context)
builder := New(&imageConfig, ctx)

// Test
err = builder.writeModifyScript()
Expand All @@ -81,7 +82,7 @@ func TestWriteModifyScript(t *testing.T) {
func TestCreateModifyCommand(t *testing.T) {
// Setup
builder := Builder{
context: &Context{
context: &context.Context{
BuildDir: "build-dir",
},
}
Expand Down
19 changes: 10 additions & 9 deletions pkg/build/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/suse-edge/edge-image-builder/pkg/context"
"github.com/suse-edge/edge-image-builder/pkg/fileio"
)

Expand Down Expand Up @@ -116,13 +117,13 @@ func TestCopyRPMsNoRPMSrcDir(t *testing.T) {

func TestWriteRPMScript(t *testing.T) {
// Setup
context, err := NewContext("", "", true)
ctx, err := context.NewContext("", "", true)
require.NoError(t, err)
defer func() {
assert.NoError(t, CleanUpBuildDir(context))
assert.NoError(t, context.CleanUpBuildDir(ctx))
}()

builder := Builder{context: context}
builder := Builder{context: ctx}

// Test
script, err := builder.writeRPMScript([]string{"rpm1.rpm", "rpm2.rpm"})
Expand Down Expand Up @@ -150,13 +151,13 @@ func TestProcessRPMs(t *testing.T) {
tmpDir, _, teardown := setupRPMSourceDir(t, true)
defer teardown()

context, err := NewContext(tmpDir, "", true)
ctx, err := context.NewContext(tmpDir, "", true)
require.NoError(t, err)
defer func() {
assert.NoError(t, CleanUpBuildDir(context))
assert.NoError(t, context.CleanUpBuildDir(ctx))
}()

builder := Builder{context: context}
builder := Builder{context: ctx}

// Test
script, err := builder.processRPMs()
Expand Down Expand Up @@ -186,13 +187,13 @@ func TestGenerateRPMPath(t *testing.T) {
tmpDir, expectedPath, teardown := setupRPMSourceDir(t, false)
defer teardown()

context, err := NewContext(tmpDir, "", true)
ctx, err := context.NewContext(tmpDir, "", true)
require.NoError(t, err)
defer func() {
assert.NoError(t, CleanUpBuildDir(context))
assert.NoError(t, context.CleanUpBuildDir(ctx))
}()

builder := Builder{context: context}
builder := Builder{context: ctx}

// Test
generatedPath, err := builder.generateRPMPath()
Expand Down
7 changes: 4 additions & 3 deletions pkg/build/scripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/suse-edge/edge-image-builder/pkg/context"
"github.com/suse-edge/edge-image-builder/pkg/fileio"
)

Expand Down Expand Up @@ -34,7 +35,7 @@ func TestConfigureScripts(t *testing.T) {
defer os.RemoveAll(tmpDestDir)

builder := &Builder{
context: &Context{
context: &context.Context{
ImageConfigDir: tmpSrcDir,
CombustionDir: tmpDestDir,
},
Expand Down Expand Up @@ -71,7 +72,7 @@ func TestConfigureScriptsNoScriptsDir(t *testing.T) {
defer os.RemoveAll(tmpSrcDir)

builder := &Builder{
context: &Context{
context: &context.Context{
ImageConfigDir: tmpSrcDir,
},
}
Expand All @@ -97,7 +98,7 @@ func TestConfigureScriptsEmptyScriptsDir(t *testing.T) {
require.NoError(t, err)

builder := &Builder{
context: &Context{
context: &context.Context{
ImageConfigDir: tmpSrcDir,
},
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/build/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
)

Expand Down Expand Up @@ -38,15 +39,15 @@ func TestConfigureUsers(t *testing.T) {
},
}

context, err := NewContext("", "", true)
ctx, err := context.NewContext("", "", true)
require.NoError(t, err)
defer func() {
assert.NoError(t, CleanUpBuildDir(context))
assert.NoError(t, context.CleanUpBuildDir(ctx))
}()

builder := &Builder{
imageConfig: imageConfig,
context: context,
context: ctx,
}

// Test
Expand All @@ -57,7 +58,7 @@ func TestConfigureUsers(t *testing.T) {

assert.Equal(t, usersScriptName, script)

expectedFilename := filepath.Join(context.CombustionDir, usersScriptName)
expectedFilename := filepath.Join(ctx.CombustionDir, usersScriptName)
foundBytes, err := os.ReadFile(expectedFilename)
require.NoError(t, err)

Expand Down Expand Up @@ -98,15 +99,15 @@ func TestConfigureUsers(t *testing.T) {

func TestConfigureUsers_NoUsers(t *testing.T) {
// Setup
context, err := NewContext("", "", true)
ctx, err := context.NewContext("", "", true)
require.NoError(t, err)
defer func() {
assert.NoError(t, CleanUpBuildDir(context))
assert.NoError(t, context.CleanUpBuildDir(ctx))
}()

builder := &Builder{
imageConfig: &config.ImageConfig{},
context: context,
context: ctx,
}

// Test
Expand All @@ -117,7 +118,7 @@ func TestConfigureUsers_NoUsers(t *testing.T) {

assert.Equal(t, "", script)

expectedFilename := filepath.Join(context.CombustionDir, usersScriptName)
expectedFilename := filepath.Join(ctx.CombustionDir, usersScriptName)
_, err = os.ReadFile(expectedFilename)
require.ErrorIs(t, err, os.ErrNotExist)
}
Loading

0 comments on commit fe58bfd

Please sign in to comment.