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

Use assert.ErrorContains #1495

Merged
merged 1 commit into from
Mar 16, 2022
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
3 changes: 1 addition & 2 deletions directory/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ func TestPutBlobDigestFailure(t *testing.T) {
require.NoError(t, err)
defer dest.Close()
_, err = dest.PutBlob(context.Background(), reader, types.BlobInfo{Digest: blobDigest, Size: -1}, cache, false)
assert.Error(t, err)
assert.Contains(t, digestErrorString, err.Error())
assert.ErrorContains(t, err, digestErrorString)
err = dest.Commit(context.Background(), nil) // nil unparsedToplevel is invalid, we don’t currently use the value
assert.NoError(t, err)

Expand Down
14 changes: 6 additions & 8 deletions docker/daemon/client_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package daemon

import "testing"
import (
"github.com/containers/image/v5/types"
dockerclient "github.com/docker/docker/client"
"github.com/stretchr/testify/assert"
"net/http"
"os"
"path/filepath"
"testing"

"github.com/containers/image/v5/types"
dockerclient "github.com/docker/docker/client"
"github.com/stretchr/testify/assert"
)

func TestDockerClientFromNilSystemContext(t *testing.T) {
Expand Down Expand Up @@ -45,10 +46,7 @@ func TestTlsConfigFromInvalidCertPath(t *testing.T) {
}

_, err := tlsConfig(ctx)

if assert.Error(t, err, "An error was expected") {
assert.Regexp(t, "could not read CA certificate", err.Error())
}
assert.ErrorContains(t, err, "could not read CA certificate")
}

func TestTlsConfigFromCertPath(t *testing.T) {
Expand Down
10 changes: 4 additions & 6 deletions docker/lookaside_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ func TestLoadAndMergeConfig(t *testing.T) {
[]byte("default-docker:\n sigstore: file:////tmp/different"), 0644)
require.NoError(t, err)
_, err = loadAndMergeConfig(duplicateDefault)
require.Error(t, err)
assert.Contains(t, err.Error(), "0.yaml")
assert.Contains(t, err.Error(), "1.yaml")
assert.ErrorContains(t, err, "0.yaml")
assert.ErrorContains(t, err, "1.yaml")

// Duplicate DefaultDocker
duplicateNS := filepath.Join(tmpDir, "duplicateNS")
Expand All @@ -185,9 +184,8 @@ func TestLoadAndMergeConfig(t *testing.T) {
[]byte("docker:\n example.com:\n sigstore: file:////tmp/different"), 0644)
require.NoError(t, err)
_, err = loadAndMergeConfig(duplicateNS)
assert.Error(t, err)
assert.Contains(t, err.Error(), "0.yaml")
assert.Contains(t, err.Error(), "1.yaml")
assert.ErrorContains(t, err, "0.yaml")
assert.ErrorContains(t, err, "1.yaml")

// A fully worked example, including an empty-dictionary file and a non-.yaml file
config, err = loadAndMergeConfig("fixtures/registries.d")
Expand Down
3 changes: 1 addition & 2 deletions manifest/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ func testValidManifestWithExtraFieldsIsRejected(t *testing.T, parser func([]byte
updatedManifest := []byte(string(validManifest[:end]) +
fmt.Sprintf(`,"%s":[]}`, field))
err := parser(updatedManifest)
assert.Error(t, err, field)
// Make sure it is the error from validateUnambiguousManifestFormat, not something that
// went wrong with creating updatedManifest.
assert.Contains(t, err.Error(), "rejecting ambiguous manifest")
assert.ErrorContains(t, err, "rejecting ambiguous manifest", field)
}
}

Expand Down
3 changes: 1 addition & 2 deletions oci/layout/oci_dest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func TestPutBlobDigestFailure(t *testing.T) {
require.NoError(t, err)
defer dest.Close()
_, err = dest.PutBlob(context.Background(), reader, types.BlobInfo{Digest: blobDigest, Size: -1}, cache, false)
assert.Error(t, err)
assert.Contains(t, digestErrorString, err.Error())
assert.ErrorContains(t, err, digestErrorString)
err = dest.Commit(context.Background(), nil) // nil unparsedToplevel is invalid, we don’t currently use the value
assert.NoError(t, err)

Expand Down
3 changes: 1 addition & 2 deletions oci/layout/oci_transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,5 @@ func TestReferenceBlobPathInvalid(t *testing.T) {
ociRef, ok := ref.(ociReference)
require.True(t, ok)
_, err := ociRef.blobPath(hex, "")
assert.Error(t, err)
assert.Contains(t, err.Error(), "unexpected digest reference "+hex)
assert.ErrorContains(t, err, "unexpected digest reference "+hex)
}
19 changes: 4 additions & 15 deletions pkg/docker/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/containers/image/v5/docker/reference"
Expand Down Expand Up @@ -492,13 +491,8 @@ func TestGetAuthFailsOnBadInput(t *testing.T) {
if err := ioutil.WriteFile(configPath, []byte("Json rocks! Unless it doesn't."), 0640); err != nil {
t.Fatalf("failed to write file %q: %v", configPath, err)
}
auth, err = getCredentialsWithHomeDir(nil, "index.docker.io", tmpHomeDir)
if err == nil {
t.Fatalf("got unexpected non-error: username=%q, password=%q", auth.Username, auth.Password)
}
if !strings.Contains(err.Error(), "unmarshaling JSON") {
t.Fatalf("expected JSON syntax error, not: %#+v", err)
}
_, err = getCredentialsWithHomeDir(nil, "index.docker.io", tmpHomeDir)
assert.ErrorContains(t, err, "unmarshaling JSON")

// remove the invalid config file
os.RemoveAll(configPath)
Expand All @@ -513,13 +507,8 @@ func TestGetAuthFailsOnBadInput(t *testing.T) {
if err := ioutil.WriteFile(configPath, []byte("I'm certainly not a json string."), 0640); err != nil {
t.Fatalf("failed to write file %q: %v", configPath, err)
}
auth, err = getCredentialsWithHomeDir(nil, "index.docker.io", tmpHomeDir)
if err == nil {
t.Fatalf("got unexpected non-error: username=%q, password=%q", auth.Username, auth.Password)
}
if !strings.Contains(err.Error(), "unmarshaling JSON") {
t.Fatalf("expected JSON syntax error, not: %#+v", err)
}
_, err = getCredentialsWithHomeDir(nil, "index.docker.io", tmpHomeDir)
assert.ErrorContains(t, err, "unmarshaling JSON")
}

func TestGetAllCredentials(t *testing.T) {
Expand Down
8 changes: 3 additions & 5 deletions pkg/sysregistriesv2/system_registries_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func TestParseLocation(t *testing.T) {

// invalid locations
_, err = parseLocation("https://example.com")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "invalid location 'https://example.com': URI schemes are not supported")
assert.ErrorContains(t, err, "invalid location 'https://example.com': URI schemes are not supported")

_, err = parseLocation("john.doe@example.com")
assert.Nil(t, err)
Expand Down Expand Up @@ -434,7 +433,7 @@ func TestInvalidV2Configs(t *testing.T) {
_, err := GetRegistries(&types.SystemContext{SystemRegistriesConfPath: c.path})
assert.Error(t, err, c.path)
if c.errorSubstring != "" {
assert.Contains(t, err.Error(), c.errorSubstring, c.path)
assert.ErrorContains(t, err, c.errorSubstring, c.path)
}
}
}
Expand Down Expand Up @@ -483,8 +482,7 @@ func TestMixingV1andV2(t *testing.T) {
SystemRegistriesConfPath: "testdata/mixing-v1-v2.conf",
SystemRegistriesConfDirPath: "testdata/this-does-not-exist",
})
require.Error(t, err)
assert.Contains(t, err.Error(), "mixing sysregistry v1/v2 is not supported")
assert.ErrorContains(t, err, "mixing sysregistry v1/v2 is not supported")
}

func TestConfigCache(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions signature/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func TestSignDockerManifestWithPassphrase(t *testing.T) {

// Invalid passphrase
_, err = SignDockerManifestWithOptions(manifest, TestImageSignatureReference, mech, TestKeyFingerprintWithPassphrase, &SignOptions{Passphrase: TestPassphrase + "\n"})
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid passphrase")
assert.ErrorContains(t, err, "invalid passphrase")

// Wrong passphrase
_, err = SignDockerManifestWithOptions(manifest, TestImageSignatureReference, mech, TestKeyFingerprintWithPassphrase, &SignOptions{Passphrase: "wrong"})
Expand Down