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
5 changes: 2 additions & 3 deletions cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"flag"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
Expand Down Expand Up @@ -656,7 +655,7 @@ func (c *cli) CmdEdit(args ...string) error {
out, err := yaml.Marshal(mirror)

// Open a temporary file
f, err := ioutil.TempFile(os.TempDir(), "edit")
f, err := os.CreateTemp("", "edit")
if err != nil {
log.Fatal("Cannot create temporary file:", err)
}
Expand All @@ -683,7 +682,7 @@ reopen:
}

// Read the file back
out, err = ioutil.ReadFile(f.Name())
out, err = os.ReadFile(f.Name())
if err != nil {
log.Fatal("Cannot read file", f.Name())
}
Expand Down
3 changes: 1 addition & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package config

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -141,7 +140,7 @@ func ReloadConfig() error {
}
}

content, err := ioutil.ReadFile(core.ConfigFile)
content, err := os.ReadFile(core.ConfigFile)
if err != nil {
fmt.Println("Configuration could not be found.\n\tUse -config <path>")
os.Exit(1)
Expand Down
29 changes: 5 additions & 24 deletions http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package http

import (
"errors"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -233,22 +232,13 @@ type testContext struct {
}

// Prepare a test, return the context
func prepareTest(filenames []string) (testContext, error) {
func prepareTest(t *testing.T, filenames []string) (testContext, error) {
// Create a temporary directory for test data
testDir, err := ioutil.TempDir("", "mirrorbits-tests")
if err != nil {
return testContext{}, err
}

defer func() {
if err != nil {
os.RemoveAll(testDir)
}
}()
testDir := t.TempDir()

// Create the repo directory, along with dummy files
repoDir := testDir + "/repo"
err = os.Mkdir(repoDir, 0755)
err := os.Mkdir(repoDir, 0755)
if err != nil {
return testContext{}, err
}
Expand Down Expand Up @@ -310,25 +300,17 @@ func prepareTest(filenames []string) (testContext, error) {
}, nil
}

// Cleanup after a test is done
func cleanupTest(ctx testContext) {
if ctx.TestDir != "" {
os.RemoveAll(ctx.TestDir)
}
}

// Test 4xx return codes from MirrorHandler.
//
// Those HTTP codes are triggered when the file requested doesn't even exist in
// the local repo. Mirrorbits doesn't query the database in those cases, so
// there's no need to mock redis commands.
func TestMirrorHandler4xx(t *testing.T) {
// Prepare
ctx, err := prepareTest([]string{})
ctx, err := prepareTest(t, []string{})
if err != nil {
t.Fatal(err)
}
defer cleanupTest(ctx)

noHeader := map[string]string{}

Expand Down Expand Up @@ -460,11 +442,10 @@ var mockedCmds304 = [][]mockedCmd{
// unexpected.
func TestMirrorHandler3xx(t *testing.T) {
// Prepare
ctx, err := prepareTest([]string{testFile})
ctx, err := prepareTest(t, []string{testFile})
if err != nil {
t.Fatal(err)
}
defer cleanupTest(ctx)

// Define tests
tests := map[string]struct {
Expand Down
15 changes: 5 additions & 10 deletions logs/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package logs
import (
"bytes"
"errors"
"io/ioutil"
"io"
"os"
"reflect"
"strings"
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestIsTerminal(t *testing.T) {
t.Fatalf("The current terminal is supposed to support colors")
}

f, err := ioutil.TempFile("", "mirrorbits-tests")
f, err := os.CreateTemp("", "mirrorbits-tests")
if err != nil {
t.Errorf("Unable to create a temporary file: %s", err.Error())
return
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestReloadRuntimeLogs(t *testing.T) {
}

/* */
f, err := ioutil.TempFile("", "mirrorbits-tests")
f, err := os.CreateTemp("", "mirrorbits-tests")
if err != nil {
t.Errorf("Unable to create a temporary file: %s", err.Error())
return
Expand All @@ -127,7 +127,7 @@ func TestReloadRuntimeLogs(t *testing.T) {
testString := "Testing42"
log.Error(testString)

buf, _ := ioutil.ReadAll(f)
buf, _ := io.ReadAll(f)
if !strings.Contains(string(buf), testString) {
t.Fatalf("The log doesn't contain the string %s", testString)
}
Expand All @@ -143,12 +143,7 @@ func TestReloadRuntimeLogs(t *testing.T) {
}

func TestOpenLogFile(t *testing.T) {
path, err := ioutil.TempDir("", "mirrorbits-tests")
if err != nil {
t.Errorf("Unable to create temporary directory: %s", err.Error())
return
}
defer os.RemoveAll(path)
path := t.TempDir()

f, newfile, err := openLogFile(path + "/test1.log")
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package process
import (
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -170,7 +169,7 @@ func WritePidFile() {

// Get our own PID and write it
pid := strconv.Itoa(os.Getpid())
if err := ioutil.WriteFile(p, []byte(pid), 0644); err != nil {
if err := os.WriteFile(p, []byte(pid), 0644); err != nil {
log.Errorf("Unable to write pid file: %v", err)
}
}
Expand All @@ -191,7 +190,7 @@ func RemovePidFile() {

// GetRemoteProcPid gets the pid as it appears in the pid file (maybe not ours)
func GetRemoteProcPid() int {
b, err := ioutil.ReadFile(GetPidLocation())
b, err := os.ReadFile(GetPidLocation())
if err != nil {
return -1
}
Expand Down
Loading