Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue where the modify script for raw images could not be executed #31

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ func (b *Builder) generateCombustionScript() error {
return nil
}

func (b *Builder) writeBuildDirFile(filename string, contents string, templateData any) error {
func (b *Builder) writeBuildDirFile(filename string, contents string, templateData any) (string, error) {
destFilename := filepath.Join(b.eibBuildDir, filename)
return b.writeFile(destFilename, contents, templateData)
return destFilename, b.writeFile(destFilename, contents, templateData)
}

func (b *Builder) writeCombustionFile(filename string, contents string, templateData any) error {
func (b *Builder) writeCombustionFile(filename string, contents string, templateData any) (string, error) {
destFilename := filepath.Join(b.combustionDir, filename)
return b.writeFile(destFilename, contents, templateData)
return destFilename, b.writeFile(destFilename, contents, templateData)
}

func (b *Builder) writeFile(filename string, contents string, templateData any) error {
Expand Down
6 changes: 4 additions & 2 deletions pkg/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,15 @@ func TestWriteCombustionFile(t *testing.T) {
testFilename := "combustion-file.sh"

// Test
err = builder.writeCombustionFile(testFilename, testData, nil)
writtenFilename, err := builder.writeCombustionFile(testFilename, testData, nil)

// Verify
require.NoError(t, err)

expectedFilename := filepath.Join(builder.combustionDir, testFilename)
foundData, err := os.ReadFile(expectedFilename)
require.NoError(t, err)
assert.Equal(t, expectedFilename, writtenFilename)
assert.Equal(t, testData, string(foundData))

// Make sure the file isn't automatically added to the combustion scripts list
Expand All @@ -147,12 +148,13 @@ func TestWriteBuildDirFile(t *testing.T) {
testFilename := "build-dir-file.sh"

// Test
err = builder.writeBuildDirFile(testFilename, testData, nil)
writtenFilename, err := builder.writeBuildDirFile(testFilename, testData, nil)

// Verify
require.NoError(t, err)

expectedFilename := filepath.Join(builder.eibBuildDir, testFilename)
require.Equal(t, expectedFilename, writtenFilename)
foundData, err := os.ReadFile(expectedFilename)
require.NoError(t, err)
assert.Equal(t, testData, string(foundData))
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
var messageScript string

func (b *Builder) configureMessage() error {
err := b.writeCombustionFile(messageScriptName, messageScript, nil)
_, err := b.writeCombustionFile(messageScriptName, messageScript, nil)
if err != nil {
return fmt.Errorf("copying script %s: %w", messageScriptName, err)
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/build/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package build
import (
_ "embed"
"fmt"
"os"
"os/exec"
"path/filepath"
)

const (
copyExec = "/bin/cp"
modifyScriptName = "modify-raw-image.sh"
modifyScriptMode = 0o744
)

//go:embed scripts/modify-raw-image.sh.tpl
Expand Down Expand Up @@ -54,10 +56,14 @@ func (b *Builder) writeModifyScript() error {
CombustionDir: b.combustionDir,
}

err := b.writeBuildDirFile(modifyScriptName, modifyRawImageScript, &values)
writtenFilename, err := b.writeBuildDirFile(modifyScriptName, modifyRawImageScript, &values)
if err != nil {
return fmt.Errorf("writing modification script %s: %w", modifyScriptName, err)
}
err = os.Chmod(writtenFilename, modifyScriptMode)
if err != nil {
return fmt.Errorf("changing permissions on the modification script %s: %w", modifyScriptName, err)
}

return nil
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/build/raw_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package build

import (
"io/fs"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -66,6 +67,10 @@ func TestWriteModifyScript(t *testing.T) {
foundBytes, err := os.ReadFile(expectedFilename)
require.NoError(t, err)

stats, err := os.Stat(expectedFilename)
require.NoError(t, err)
assert.Equal(t, fs.FileMode(modifyScriptMode), stats.Mode())

foundContents := string(foundBytes)
assert.Contains(t, foundContents, "guestfish --rw -a config-dir/output-image")
assert.Contains(t, foundContents, "copy-in "+builder.combustionDir)
Expand Down