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 1 commit
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))
}
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these lines still be there? Isn't RemovePath the function being tested?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes they should whoops

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this far too quickly the first time, thanks for catching this.

}
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