Skip to content

Commit

Permalink
Upgrade dependencies to latest versions
Browse files Browse the repository at this point in the history
- apply major upgrades for gopkg.in/yaml, gotest.tools and github.com/cenkalti/backoff
- get rid of obsolete github.com/pkg/errors
- replace github.com/Flaque/filet with standard library
- make error ignores explicit where possible
  • Loading branch information
prskr committed Dec 6, 2021
1 parent 1517941 commit 0e02a18
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 88 deletions.
6 changes: 3 additions & 3 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"gopkg.in/yaml.v3"

"github.com/testcontainers/testcontainers-go/wait"
"gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -77,7 +78,7 @@ func NewLocalDockerCompose(filePaths []string, identifier string) *LocalDockerCo
dc.absComposeFilePaths[i] = abs
}

dc.validate()
_ = dc.validate()

dc.Identifier = strings.ToLower(identifier)
dc.waitStrategySupplied = false
Expand Down Expand Up @@ -106,7 +107,6 @@ func (dc *LocalDockerCompose) getDockerComposeEnvironment() map[string]string {
}

func (dc *LocalDockerCompose) applyStrategyToRunningContainer() error {

cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/google/uuid"
"github.com/stretchr/testify/assert"

"github.com/testcontainers/testcontainers-go/wait"
)

Expand Down
5 changes: 3 additions & 2 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package testcontainers

import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
Expand All @@ -11,7 +13,6 @@ import (

"github.com/docker/docker/pkg/archive"
"github.com/docker/go-connections/nat"
"github.com/pkg/errors"

"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down Expand Up @@ -120,7 +121,7 @@ func (t ProviderType) GetProvider() (GenericProvider, error) {
case ProviderDocker:
provider, err := NewDockerProvider()
if err != nil {
return nil, errors.Wrap(err, "failed to create Docker provider")
return nil, fmt.Errorf("%w, failed to create Docker provider", err)
}
return provider, nil
}
Expand Down
2 changes: 1 addition & 1 deletion container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"archive/tar"
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"strings"
"testing"
"time"

"github.com/pkg/errors"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand Down
29 changes: 14 additions & 15 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"

"github.com/cenkalti/backoff"
"github.com/cenkalti/backoff/v4"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
Expand All @@ -28,7 +28,6 @@ import (
"github.com/google/uuid"
"github.com/magiconair/properties"
"github.com/moby/term"
"github.com/pkg/errors"

"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down Expand Up @@ -453,7 +452,7 @@ func (c *DockerContainer) StartLogProducer(ctx context.Context) error {
}
logType := h[0]
if logType > 2 {
fmt.Fprintf(os.Stderr, fmt.Sprintf("received invalid log type: %d", logType))
_, _ = fmt.Fprintf(os.Stderr, "received invalid log type: %d", logType)
// sometimes docker returns logType = 3 which is an undocumented log type, so treat it as stdout
logType = 1
}
Expand All @@ -465,7 +464,7 @@ func (c *DockerContainer) StartLogProducer(ctx context.Context) error {
_, err = r.Read(b)
if err != nil {
// TODO: add-logger: use logger to log out this error
fmt.Fprintf(os.Stderr, "error occurred reading log with known length %s", err.Error())
_, _ = fmt.Fprintf(os.Stderr, "error occurred reading log with known length %s", err.Error())
continue
}
for _, c := range c.consumers {
Expand Down Expand Up @@ -534,9 +533,9 @@ func NewDockerProvider() (*DockerProvider, error) {

// for further informacion, read https://docs.docker.com/engine/security/protect-access/
if tcConfig.TLSVerify == 1 {
cacertPath := path.Join(tcConfig.CertPath, "ca.pem")
certPath := path.Join(tcConfig.CertPath, "cert.pem")
keyPath := path.Join(tcConfig.CertPath, "key.pem")
cacertPath := filepath.Join(tcConfig.CertPath, "ca.pem")
certPath := filepath.Join(tcConfig.CertPath, "cert.pem")
keyPath := filepath.Join(tcConfig.CertPath, "key.pem")

opts = append(opts, client.WithTLSClientConfig(cacertPath, certPath, keyPath))
}
Expand Down Expand Up @@ -570,7 +569,7 @@ func readTCPropsFile() TestContainersConfig {
return TestContainersConfig{}
}

tcProp := path.Join(home, ".testcontainers.properties")
tcProp := filepath.Join(home, ".testcontainers.properties")
// init from a file
properties, err := properties.LoadFile(tcProp, properties.UTF8)
if err != nil {
Expand Down Expand Up @@ -626,7 +625,7 @@ func (p *DockerProvider) BuildImage(ctx context.Context, img ImageBuildInfo) (st
return "", err
}

resp.Body.Close()
_ = resp.Body.Close()

return repoTag, nil
}
Expand Down Expand Up @@ -678,11 +677,11 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque
if !req.SkipReaper {
r, err := NewReaper(ctx, sessionID.String(), p, req.ReaperImage)
if err != nil {
return nil, errors.Wrap(err, "creating reaper failed")
return nil, fmt.Errorf("%w: creating reaper failed", err)
}
termSignal, err = r.Connect()
if err != nil {
return nil, errors.Wrap(err, "connecting to reaper failed")
return nil, fmt.Errorf("%w: connecting to reaper failed", err)
}
for k, v := range r.Labels() {
if _, ok := req.Labels[k]; !ok {
Expand Down Expand Up @@ -871,7 +870,7 @@ func (p *DockerProvider) RunContainer(ctx context.Context, req ContainerRequest)
}

if err := c.Start(ctx); err != nil {
return c, errors.Wrap(err, "could not start container")
return c, fmt.Errorf("%w: could not start container", err)
}

return c, nil
Expand Down Expand Up @@ -948,11 +947,11 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)
if !req.SkipReaper {
r, err := NewReaper(ctx, sessionID.String(), p, req.ReaperImage)
if err != nil {
return nil, errors.Wrap(err, "creating network reaper failed")
return nil, fmt.Errorf("%w: creating network reaper failed", err)
}
termSignal, err = r.Connect()
if err != nil {
return nil, errors.Wrap(err, "connecting to network reaper failed")
return nil, fmt.Errorf("%w: connecting to network reaper failed", err)
}
for k, v := range r.Labels() {
if _, ok := req.Labels[k]; !ok {
Expand Down
52 changes: 20 additions & 32 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testcontainers

import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
Expand All @@ -10,21 +11,20 @@ import (
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"testing"
"time"

"github.com/Flaque/filet"
"github.com/stretchr/testify/assert"
"gotest.tools/v3/env"
"gotest.tools/v3/fs"

"github.com/docker/docker/errdefs"

"github.com/docker/docker/api/types/volume"

"database/sql"
// Import mysql into the scope of this package (required)
_ "github.com/go-sql-driver/mysql"

Expand All @@ -33,6 +33,7 @@ import (
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/go-redis/redis"

"github.com/testcontainers/testcontainers-go/wait"
)

Expand Down Expand Up @@ -995,7 +996,7 @@ func Test_BuildContainerFromDockerfileWithBuildLog(t *testing.T) {
t.Log("terminated container")
}()

w.Close()
_ = w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout
temp := strings.Split(string(out), "\n")
Expand Down Expand Up @@ -1191,34 +1192,24 @@ func TestEntrypoint(t *testing.T) {

func TestReadTCPropsFile(t *testing.T) {
t.Run("HOME is not set", func(t *testing.T) {
oldHome := os.Getenv("HOME")
os.Unsetenv("HOME")
defer func() {
os.Setenv("HOME", oldHome)
}()
tmpDir := fs.NewDir(t, os.TempDir())
env.Patch(t, "HOME", tmpDir.Path())

config := readTCPropsFile()

assert.Empty(t, config, "TC props file should not exist")
})

t.Run("HOME does not contain TC props file", func(t *testing.T) {
oldHome := os.Getenv("HOME")
tmpDir := filet.TmpDir(t, "")
os.Setenv("HOME", tmpDir)
defer func() {
os.Setenv("HOME", oldHome)
filet.CleanUp(t)
}()
tmpDir := fs.NewDir(t, os.TempDir())
env.Patch(t, "HOME", tmpDir.Path())

config := readTCPropsFile()

assert.Empty(t, config, "TC props file should not exist")
})

t.Run("HOME contains TC properties file", func(t *testing.T) {
oldHome := os.Getenv("HOME")

tests := []struct {
content string
expectedHost string
Expand Down Expand Up @@ -1291,15 +1282,12 @@ func TestReadTCPropsFile(t *testing.T) {
},
}
for _, tt := range tests {
tmpDir := filet.TmpDir(t, "")
os.Setenv("HOME", tmpDir)

defer func() {
os.Setenv("HOME", oldHome)
filet.CleanUp(t)
}()

_ = filet.File(t, path.Join(tmpDir, ".testcontainers.properties"), tt.content)
tmpDir := fs.NewDir(t, os.TempDir())
env.Patch(t, "HOME", tmpDir.Path())
if err := os.WriteFile(tmpDir.Join(".testcontainers.properties"), []byte(tt.content), 0o600); err != nil {
t.Errorf("Failed to create the file: %v", err)
return
}

config := readTCPropsFile()

Expand Down Expand Up @@ -1351,7 +1339,7 @@ func ExampleContainer_Start() {
ContainerRequest: req,
})
defer nginxC.Terminate(ctx)
nginxC.Start(ctx)
_ = nginxC.Start(ctx)
}

func ExampleContainer_MappedPort() {
Expand All @@ -1368,7 +1356,7 @@ func ExampleContainer_MappedPort() {
defer nginxC.Terminate(ctx)
ip, _ := nginxC.Host(ctx)
port, _ := nginxC.MappedPort(ctx, "80")
http.Get(fmt.Sprintf("http://%s:%s", ip, port.Port()))
_, _ = http.Get(fmt.Sprintf("http://%s:%s", ip, port.Port()))
}

func TestContainerCreationWithBindAndVolume(t *testing.T) {
Expand Down Expand Up @@ -1570,7 +1558,7 @@ func TestDockerContainerCopyFileToContainer(t *testing.T) {
defer nginxC.Terminate(ctx)

copiedFileName := "hello_copy.sh"
nginxC.CopyFileToContainer(ctx, "./testresources/hello.sh", "/"+copiedFileName, 700)
_ = nginxC.CopyFileToContainer(ctx, "./testresources/hello.sh", "/"+copiedFileName, 700)
c, err := nginxC.Exec(ctx, []string{"bash", copiedFileName})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1598,7 +1586,7 @@ func TestDockerContainerCopyFileFromContainer(t *testing.T) {
defer nginxC.Terminate(ctx)

copiedFileName := "hello_copy.sh"
nginxC.CopyFileToContainer(ctx, "./testresources/hello.sh", "/"+copiedFileName, 700)
_ = nginxC.CopyFileToContainer(ctx, "./testresources/hello.sh", "/"+copiedFileName, 700)
c, err := nginxC.Exec(ctx, []string{"bash", copiedFileName})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1633,7 +1621,7 @@ func TestDockerContainerCopyEmptyFileFromContainer(t *testing.T) {
defer nginxC.Terminate(ctx)

copiedFileName := "hello_copy.sh"
nginxC.CopyFileToContainer(ctx, "./testresources/empty.sh", "/"+copiedFileName, 700)
_ = nginxC.CopyFileToContainer(ctx, "./testresources/empty.sh", "/"+copiedFileName, 700)
c, err := nginxC.Exec(ctx, []string{"bash", copiedFileName})
if err != nil {
t.Fatal(err)
Expand Down
9 changes: 4 additions & 5 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package testcontainers

import (
"context"

"github.com/pkg/errors"
"fmt"
)

// GenericContainerRequest represents parameters to a generic container
Expand All @@ -27,7 +26,7 @@ func GenericNetwork(ctx context.Context, req GenericNetworkRequest) (Network, er
}
network, err := provider.CreateNetwork(ctx, req.NetworkRequest)
if err != nil {
return nil, errors.Wrap(err, "failed to create network")
return nil, fmt.Errorf("%w: failed to create network", err)
}

return network, nil
Expand All @@ -42,12 +41,12 @@ func GenericContainer(ctx context.Context, req GenericContainerRequest) (Contain

c, err := provider.CreateContainer(ctx, req.ContainerRequest)
if err != nil {
return nil, errors.Wrap(err, "failed to create container")
return nil, fmt.Errorf("%w: failed to create container", err)
}

if req.Started {
if err := c.Start(ctx); err != nil {
return c, errors.Wrap(err, "failed to start container")
return c, fmt.Errorf("%w: failed to start container", err)
}
}

Expand Down
Loading

0 comments on commit 0e02a18

Please sign in to comment.