-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move install-go from metal-images to metal-hammer
- Loading branch information
Showing
10 changed files
with
2,138 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package install | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"log/slog" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type cmdexec struct { | ||
log *slog.Logger | ||
c func(ctx context.Context, name string, arg ...string) *exec.Cmd | ||
} | ||
|
||
type cmdParams struct { | ||
name string | ||
args []string | ||
dir string | ||
timeout time.Duration | ||
combined bool | ||
stdin string | ||
env []string | ||
} | ||
|
||
func (i *cmdexec) command(p *cmdParams) (out string, err error) { | ||
var ( | ||
start = time.Now() | ||
output []byte | ||
) | ||
i.log.Info("running command", "command", strings.Join(append([]string{p.name}, p.args...), " "), "start", start.String()) | ||
|
||
ctx := context.Background() | ||
if p.timeout != 0 { | ||
var cancel context.CancelFunc | ||
ctx, cancel = context.WithTimeout(ctx, p.timeout) | ||
defer cancel() | ||
} | ||
|
||
cmd := i.c(ctx, p.name, p.args...) | ||
if p.dir != "" { | ||
cmd.Dir = "/etc/metal" | ||
} | ||
|
||
cmd.Env = append(cmd.Env, p.env...) | ||
|
||
// show stderr | ||
cmd.Stderr = os.Stderr | ||
|
||
if p.stdin != "" { | ||
stdin, err := cmd.StdinPipe() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
go func() { | ||
defer stdin.Close() | ||
_, err = io.WriteString(stdin, p.stdin) | ||
if err != nil { | ||
i.log.Error("error when writing to command's stdin", "error", err) | ||
} | ||
}() | ||
} | ||
|
||
if p.combined { | ||
output, err = cmd.CombinedOutput() | ||
} else { | ||
output, err = cmd.Output() | ||
} | ||
|
||
out = string(output) | ||
took := time.Since(start) | ||
|
||
if err != nil { | ||
i.log.Error("executed command with error", "output", out, "duration", took.String(), "error", err) | ||
return "", err | ||
} | ||
|
||
i.log.Info("executed command", "output", out, "duration", took.String()) | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package install | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// tests were inspired by this blog article: https://npf.io/2015/06/testing-exec-command/ | ||
|
||
type fakeexec struct { | ||
t *testing.T | ||
mockCount int | ||
mocks []fakeexecparams | ||
} | ||
|
||
// nolint:musttag | ||
type fakeexecparams struct { | ||
WantCmd []string `json:"want_cmd"` | ||
Output string `json:"output"` | ||
ExitCode int `json:"exit_code"` | ||
} | ||
|
||
func fakeCmd(t *testing.T, params ...fakeexecparams) func(ctx context.Context, command string, args ...string) *exec.Cmd { | ||
f := fakeexec{ | ||
t: t, | ||
mocks: params, | ||
} | ||
return f.command | ||
} | ||
|
||
func (f *fakeexec) command(ctx context.Context, command string, args ...string) *exec.Cmd { | ||
if f.mockCount >= len(f.mocks) { | ||
require.Fail(f.t, "more commands called than mocks are available") | ||
} | ||
|
||
params := f.mocks[f.mockCount] | ||
f.mockCount++ | ||
|
||
assert.Equal(f.t, params.WantCmd, append([]string{command}, args...)) | ||
|
||
j, err := json.Marshal(params) | ||
require.NoError(f.t, err) | ||
|
||
cs := []string{"-test.run=TestHelperProcess", "--", string(j)} | ||
cmd := exec.CommandContext(ctx, os.Args[0], cs...) //nolint | ||
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} | ||
return cmd | ||
} | ||
|
||
func TestHelperProcess(t *testing.T) { | ||
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { | ||
return | ||
} | ||
|
||
var f fakeexecparams | ||
err := json.Unmarshal([]byte(os.Args[3]), &f) | ||
require.NoError(t, err) | ||
|
||
fmt.Fprint(os.Stdout, f.Output) | ||
|
||
os.Exit(f.ExitCode) | ||
} |
Oops, something went wrong.