Skip to content
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
26 changes: 4 additions & 22 deletions cmd/ncproxy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package main

import (
"context"
"io/ioutil"
"net"
"os"
"path/filepath"
"testing"
"time"
Expand All @@ -24,11 +22,7 @@ func TestRegisterComputeAgent(t *testing.T) {
ctx := context.Background()

// setup test database
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

db, err := bolt.Open(filepath.Join(tempDir, "networkproxy.db.test"), 0600, nil)
if err != nil {
Expand Down Expand Up @@ -73,11 +67,7 @@ func TestConfigureNetworking(t *testing.T) {
ctx := context.Background()

// setup test database
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

db, err := bolt.Open(filepath.Join(tempDir, "networkproxy.db.test"), 0600, nil)
if err != nil {
Expand Down Expand Up @@ -155,11 +145,7 @@ func TestReconnectComputeAgents_Success(t *testing.T) {
ctx := context.Background()

// setup test database
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

db, err := bolt.Open(filepath.Join(tempDir, "networkproxy.db.test"), 0600, nil)
if err != nil {
Expand Down Expand Up @@ -204,11 +190,7 @@ func TestReconnectComputeAgents_Failure(t *testing.T) {
ctx := context.Background()

// setup test database
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

db, err := bolt.Open(filepath.Join(tempDir, "networkproxy.db.test"), 0600, nil)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions internal/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package exec
import (
"context"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -39,11 +38,7 @@ func TestExec(t *testing.T) {

func TestExecWithDir(t *testing.T) {
// Test that the working directory is successfully set to whatever was passed in.
dir, err := ioutil.TempDir("", "exec-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
dir := t.TempDir()

e, err := New(
`C:\Windows\System32\cmd.exe`,
Expand Down
25 changes: 6 additions & 19 deletions internal/guest/storage/utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package storage

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -15,14 +14,10 @@ import (
func Test_WaitForFileMatchingPattern_Success(t *testing.T) {
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)

testDir, err := ioutil.TempDir("", "vmbus_test")
if err != nil {
t.Fatalf("unexpected error creating temp dir %v", err)
}
defer os.RemoveAll(testDir)
testDir := t.TempDir()

actualPath := filepath.Join(testDir, "path1")
err = os.Mkdir(actualPath, 0777)
err := os.Mkdir(actualPath, 0777)
if err != nil {
t.Fatalf("unexpected error creating test path: %v", err)
}
Expand All @@ -43,11 +38,7 @@ func Test_WaitForFileMatchingPattern_Success(t *testing.T) {
func Test_WaitForFileMatchingPattern_Multiple_Matches(t *testing.T) {
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)

testDir, err := ioutil.TempDir("", "vmbus_test")
if err != nil {
t.Fatalf("unexpected error creating temp dir %v", err)
}
defer os.RemoveAll(testDir)
testDir := t.TempDir()

actualPaths := []string{"path1", "path2"}
for _, p := range actualPaths {
Expand All @@ -59,7 +50,7 @@ func Test_WaitForFileMatchingPattern_Multiple_Matches(t *testing.T) {
}

pathPattern := filepath.Join(testDir, "path*")
_, err = WaitForFileMatchingPattern(ctx, pathPattern)
_, err := WaitForFileMatchingPattern(ctx, pathPattern)
if err == nil {
t.Fatalf("expected to fail due to multiple matching files")
}
Expand All @@ -68,14 +59,10 @@ func Test_WaitForFileMatchingPattern_Multiple_Matches(t *testing.T) {
func Test_WaitForFileMatchingPattern_No_Matches(t *testing.T) {
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)

testDir, err := ioutil.TempDir("", "vmbus_test")
if err != nil {
t.Fatalf("unexpected error creating temp dir %v", err)
}
defer os.RemoveAll(testDir)
testDir := t.TempDir()

actualPath := filepath.Join(testDir, "path1")
err = os.Mkdir(actualPath, 0777)
err := os.Mkdir(actualPath, 0777)
if err != nil {
t.Fatalf("unexpected error creating test path: %v", err)
}
Expand Down
16 changes: 7 additions & 9 deletions internal/jobcontainers/path_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jobcontainers

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -128,30 +127,29 @@ func TestGetApplicationNamePing(t *testing.T) {
}

func TestGetApplicationNameRandomBinary(t *testing.T) {
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

// Create fake executables in a temporary directory to use for the below tests.
testExe := filepath.Join(tempDir, "test.exe")
_, err = os.Create(testExe)
f1, err := os.Create(testExe)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = f1.Close() })

test2Exe := filepath.Join(tempDir, "test 2.exe")
_, err = os.Create(test2Exe)
f2, err := os.Create(test2Exe)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = f2.Close() })

exeWithSpace := filepath.Join(tempDir, "exe with space.exe")
_, err = os.Create(exeWithSpace)
f3, err := os.Create(exeWithSpace)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = f3.Close() })

tests := []*config{
// See if we can successfully find "exe with space.exe" with no quoting, it should first try "exe.exe", then "exe with.exe" and then finally
Expand Down
38 changes: 6 additions & 32 deletions internal/ncproxy/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package store

import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand All @@ -13,11 +11,7 @@ import (

func TestComputeAgentStore(t *testing.T) {
ctx := context.Background()
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

db, err := bolt.Open(filepath.Join(tempDir, "networkproxy.db.test"), 0600, nil)
if err != nil {
Expand Down Expand Up @@ -54,11 +48,7 @@ func TestComputeAgentStore(t *testing.T) {

func TestComputeAgentStore_GetComputeAgents(t *testing.T) {
ctx := context.Background()
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

db, err := bolt.Open(filepath.Join(tempDir, "networkproxy.db.test"), 0600, nil)
if err != nil {
Expand Down Expand Up @@ -93,11 +83,7 @@ func TestComputeAgentStore_GetComputeAgents(t *testing.T) {

func TestEndpointStore(t *testing.T) {
ctx := context.Background()
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

db, err := bolt.Open(filepath.Join(tempDir, "networkproxy.db.test"), 0600, nil)
if err != nil {
Expand Down Expand Up @@ -143,11 +129,7 @@ func TestEndpointStore(t *testing.T) {

func TestEndpointStore_GetAll(t *testing.T) {
ctx := context.Background()
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

db, err := bolt.Open(filepath.Join(tempDir, "networkproxy.db.test"), 0600, nil)
if err != nil {
Expand Down Expand Up @@ -192,11 +174,7 @@ func TestEndpointStore_GetAll(t *testing.T) {

func TestNetworkStore(t *testing.T) {
ctx := context.Background()
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

db, err := bolt.Open(filepath.Join(tempDir, "networkproxy.db.test"), 0600, nil)
if err != nil {
Expand Down Expand Up @@ -236,11 +214,7 @@ func TestNetworkStore(t *testing.T) {

func TestNetworkStore_GetAll(t *testing.T) {
ctx := context.Background()
tempDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

db, err := bolt.Open(filepath.Join(tempDir, "networkproxy.db.test"), 0600, nil)
if err != nil {
Expand Down
8 changes: 2 additions & 6 deletions internal/safefile/safeopen_admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@ import (
)

func TestOpenRelative(t *testing.T) {
badroot, err := tempRoot()
badroot, err := tempRoot(t)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(badroot.Name())
defer badroot.Close()

root, err := tempRoot()
root, err := tempRoot(t)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(root.Name())
defer root.Close()

// Create a file
f, err := OpenRelative("foo", root, 0, syscall.FILE_SHARE_READ, winapi.FILE_CREATE, 0)
Expand Down
20 changes: 9 additions & 11 deletions internal/safefile/safeopen_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package safefile

import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
Expand All @@ -10,26 +9,25 @@ import (
winio "github.com/Microsoft/go-winio"
)

func tempRoot() (*os.File, error) {
name, err := ioutil.TempDir("", "hcsshim-test")
if err != nil {
return nil, err
}
func tempRoot(t *testing.T) (*os.File, error) {
name := t.TempDir()
f, err := OpenRoot(name)
if err != nil {
os.Remove(name)
return nil, err
}
return f, nil

t.Cleanup(func() {
_ = f.Close()
})

return f, err
}

func TestRemoveRelativeReadOnly(t *testing.T) {
root, err := tempRoot()
root, err := tempRoot(t)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(root.Name())
defer root.Close()

p := filepath.Join(root.Name(), "foo")
f, err := os.Create(p)
Expand Down
10 changes: 2 additions & 8 deletions test/containerd-shim-runhcs-v1/delete_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//go:build functional
// +build functional

package main

import (
"io/ioutil"
"os"
"testing"
"time"
Expand Down Expand Up @@ -71,13 +71,7 @@ func Test_Delete_No_Bundle_Path(t *testing.T) {
}

func Test_Delete_HcsSystem_NotFound(t *testing.T) {
dir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal("failed to create tmpdir")
}
defer func() {
os.RemoveAll(dir)
}()
dir := t.TempDir()

before := time.Now()
stdout, stderr, err := runGlobalCommand(
Expand Down
7 changes: 2 additions & 5 deletions test/containerd-shim-runhcs-v1/start_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build functional
// +build functional

package main
Expand All @@ -7,7 +8,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -27,10 +27,7 @@ func createStartCommand(t *testing.T) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer)
}

func createStartCommandWithID(t *testing.T, id string) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer) {
bundleDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatalf("failed to create bundle with: %v", err)
}
bundleDir := t.TempDir()
wd, err := os.Getwd()
if err != nil {
t.Fatalf("failed os.Getwd() with: %v", err)
Expand Down
Loading