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

Fix TestRemovePath being flagged as a possible virus #3411

Merged
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
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))
}
44 changes: 21 additions & 23 deletions internal/pkg/agent/install/uninstall_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,42 @@ import (
"path/filepath"
"testing"

"github.com/otiai10/copy"
"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)
var (
pkgName = "testblocking"
binaryName = pkgName + ".exe"
)

// Create a temporary directory that we can safely remove. The directory is created as a new
// sub-directory. This avoids having Microsoft Defender quarantine the file if it is exec'd from
// the default temporary directory.
destDir, err := os.MkdirTemp(pkgName, t.Name())
require.NoError(t, err)

src := filepath.Join(dir, "main.go")
err = os.WriteFile(src, []byte(simpleBlockForever), 0644)
require.NoError(t, err)
// Copy the test executable to the new temporary directory.
destpath, err := filepath.Abs(filepath.Join(destDir, binaryName))
require.NoErrorf(t, err, "failed dest abs %s + %s", destDir, binaryName)

srcPath, err := filepath.Abs(filepath.Join(pkgName, binaryName))
require.NoErrorf(t, err, "failed src abs %s + %s", pkgName, binaryName)

binary := filepath.Join(dir, "main.exe")
cmd := exec.Command("go", "build", "-o", binary, src)
_, err = cmd.CombinedOutput()
err = copy.Copy(srcPath, destpath, copy.Options{Sync: true})
require.NoError(t, err)

cmd = exec.Command(binary)
// Execute the test executable asynchronously.
cmd := exec.Command(destpath)
err = cmd.Start()
require.NoError(t, err)
defer func() {
_ = cmd.Process.Kill()
_ = cmd.Wait()
}()

err = RemovePath(dir)
// Ensure the directory containing the executable can be removed.
err = RemovePath(destDir)
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