Skip to content

Commit

Permalink
Do not generate and compile blocking exe automatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmacknz committed Sep 14, 2023
1 parent fe0d427 commit 0b42ffc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
15 changes: 15 additions & 0 deletions internal/pkg/agent/install/testblocking/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package main

import (
"math"
"time"
)

// Simple program that blocks forever to ensure exes running from a directory on Windows can be removed during uninstall.
func main() {
<-time.After(time.Duration(math.MaxInt64))
}
34 changes: 4 additions & 30 deletions internal/pkg/agent/install/uninstall_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,23 @@
package install

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

"github.com/stretchr/testify/require"
)

const simpleBlockForever = `
package main
import (
"math"
"time"
)
func main() {
<-time.After(time.Duration(math.MaxInt64))
}
`

func TestRemovePath(t *testing.T) {
dir := filepath.Join(t.TempDir(), "subdir")
err := os.Mkdir(dir, 0644)
require.NoError(t, err)
const binaryName = "testblocking"
binaryPath, err := filepath.Abs(filepath.Join(binaryName, binaryName+".exe"))
require.NoErrorf(t, err, "failed abs %s", binaryPath)

src := filepath.Join(dir, "main.go")
err = os.WriteFile(src, []byte(simpleBlockForever), 0644)
require.NoError(t, err)

binary := filepath.Join(dir, "main.exe")
cmd := exec.Command("go", "build", "-o", binary, src)
_, err = cmd.CombinedOutput()
require.NoError(t, err)

cmd = exec.Command(binary)
cmd := exec.Command(binaryPath)
err = cmd.Start()
require.NoError(t, err)
defer func() {
_ = cmd.Process.Kill()
_ = cmd.Wait()
}()

err = RemovePath(dir)
require.NoError(t, err)
}
15 changes: 9 additions & 6 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,19 @@ func (Build) Clean() {
// TestBinaries build the required binaries for the test suite.
func (Build) TestBinaries() error {
wd, _ := os.Getwd()
p := filepath.Join(wd, "pkg", "component", "fake")
for _, name := range []string{"component", "shipper"} {
binary := name
testBinaryPkgs := []string{
filepath.Join(wd, "pkg", "component", "fake", "component"),
filepath.Join(wd, "pkg", "component", "fake", "shipper"),
filepath.Join(wd, "internal", "pkg", "agent", "install", "testblocking"),
}
for _, pkg := range testBinaryPkgs {
binary := filepath.Base(pkg)
if runtime.GOOS == "windows" {
binary += ".exe"
}

fakeDir := filepath.Join(p, name)
outputName := filepath.Join(fakeDir, binary)
err := RunGo("build", "-o", outputName, filepath.Join(fakeDir))
outputName := filepath.Join(pkg, binary)
err := RunGo("build", "-o", outputName, filepath.Join(pkg))
if err != nil {
return err
}
Expand Down

0 comments on commit 0b42ffc

Please sign in to comment.