mirror of
https://github.com/fleetdm/fleet
synced 2026-05-09 18:20:48 +00:00
# Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [x] Added/updated tests - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. - [x] Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)). --------- Co-authored-by: Roberto Dip <rroperzh@gmail.com>
80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
//go:build !windows
|
|
|
|
package scripts
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestExecCmdNonWindows(t *testing.T) {
|
|
zshPath := "/bin/zsh"
|
|
if runtime.GOOS == "linux" {
|
|
zshPath = "/usr/bin/zsh"
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
contents string
|
|
output string
|
|
exitCode int
|
|
error error
|
|
}{
|
|
{
|
|
name: "no shebang",
|
|
contents: "[ -z \"$ZSH_VERSION\" ] && echo 1",
|
|
output: "1",
|
|
},
|
|
{
|
|
name: "sh shebang",
|
|
contents: "#!/bin/sh\n[ -z \"$ZSH_VERSION\" ] && echo 1",
|
|
output: "1",
|
|
},
|
|
{
|
|
name: "zsh shebang",
|
|
contents: "#!" + zshPath + "\n[ -n \"$ZSH_VERSION\" ] && echo 1",
|
|
output: "1",
|
|
},
|
|
{
|
|
name: "zsh shebang with args",
|
|
contents: "#!" + zshPath + " -e\n[ -n \"$ZSH_VERSION\" ] && echo 1",
|
|
output: "1",
|
|
},
|
|
{
|
|
name: "unsupported shebang",
|
|
contents: "#!/bin/python",
|
|
error: fleet.ErrUnsupportedInterpreter,
|
|
exitCode: -1,
|
|
},
|
|
}
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if strings.HasPrefix(tc.contents, "#!"+zshPath) {
|
|
// skip if zsh is not installed
|
|
if _, err := exec.LookPath(zshPath); err != nil {
|
|
t.Skipf("zsh not installed: %s", err)
|
|
}
|
|
}
|
|
scriptPath := strings.ReplaceAll(tc.name, " ", "_") + ".sh"
|
|
scriptPath = filepath.Join(tmpDir, scriptPath)
|
|
err := os.WriteFile(scriptPath, []byte(tc.contents), os.ModePerm)
|
|
require.NoError(t, err)
|
|
|
|
output, exitCode, err := ExecCmd(context.Background(), scriptPath, nil)
|
|
require.Equal(t, tc.output, strings.TrimSpace(string(output)))
|
|
require.Equal(t, tc.exitCode, exitCode)
|
|
require.ErrorIs(t, err, tc.error)
|
|
})
|
|
}
|
|
}
|