Skip to content

Commit ca1c029

Browse files
Merge pull request #25366 from baude/artifacterrortypes
Define artifact error types
2 parents 8ba13d4 + 7030b55 commit ca1c029

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

pkg/libartifact/artifact.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package libartifact
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"strings"
87

98
"github.com/containers/image/v5/manifest"
9+
"github.com/containers/podman/v5/pkg/libartifact/types"
1010
"github.com/opencontainers/go-digest"
1111
)
1212

@@ -33,7 +33,7 @@ func (a *Artifact) GetName() (string, error) {
3333
}
3434
// We don't have a concept of None for artifacts yet, but if we do,
3535
// then we should probably not error but return `None`
36-
return "", errors.New("artifact is unnamed")
36+
return "", types.ErrArtifactUnamed
3737
}
3838

3939
// SetName is a accessor for setting the artifact name
@@ -75,5 +75,5 @@ func (al ArtifactList) GetByNameOrDigest(nameOrDigest string) (*Artifact, bool,
7575
return artifact, true, nil
7676
}
7777
}
78-
return nil, false, fmt.Errorf("no artifact found with name or digest of %s", nameOrDigest)
78+
return nil, false, fmt.Errorf("%s: %w", nameOrDigest, types.ErrArtifactNotExist)
7979
}

pkg/libartifact/store/store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (as ArtifactStore) Add(ctx context.Context, dest string, paths []string, op
196196
// error means it exists
197197
_, _, err := artifacts.GetByNameOrDigest(dest)
198198
if err == nil {
199-
return nil, fmt.Errorf("artifact %s already exists", dest)
199+
return nil, fmt.Errorf("%s: %w", dest, libartTypes.ErrArtifactAlreadyExists)
200200
}
201201
artifactManifest = specV1.Manifest{
202202
Versioned: specs.Versioned{SchemaVersion: ManifestSchemaVersion},
@@ -227,7 +227,7 @@ func (as ArtifactStore) Add(ctx context.Context, dest string, paths []string, op
227227
for _, path := range paths {
228228
fileName := filepath.Base(path)
229229
if _, ok := fileNames[fileName]; ok {
230-
return nil, fmt.Errorf("file: %q already exists in artifact", fileName)
230+
return nil, fmt.Errorf("%s: %w", fileName, libartTypes.ErrArtifactFileExists)
231231
}
232232
fileNames[fileName] = struct{}{}
233233
}

pkg/libartifact/types/errors.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package types
2+
3+
import (
4+
"errors"
5+
)
6+
7+
var (
8+
ErrArtifactUnamed = errors.New("artifact is unnamed")
9+
ErrArtifactNotExist = errors.New("artifact does not exist")
10+
ErrArtifactAlreadyExists = errors.New("artifact already exists")
11+
ErrArtifactFileExists = errors.New("file already exists in artifact")
12+
)

test/e2e/artifact_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ var _ = Describe("Podman artifact", func() {
8888
// Adding an artifact with an existing name should fail
8989
addAgain := podmanTest.Podman([]string{"artifact", "add", artifact1Name, artifact1File})
9090
addAgain.WaitWithDefaultTimeout()
91-
Expect(addAgain).Should(ExitWithError(125, fmt.Sprintf("Error: artifact %s already exists", artifact1Name)))
91+
Expect(addAgain).Should(ExitWithError(125, fmt.Sprintf("Error: %s: artifact already exists", artifact1Name)))
9292
})
9393

9494
It("podman artifact add with options", func() {
@@ -164,7 +164,7 @@ var _ = Describe("Podman artifact", func() {
164164
// Trying to remove an image that does not exist should fail
165165
rmFail := podmanTest.Podman([]string{"artifact", "rm", "foobar"})
166166
rmFail.WaitWithDefaultTimeout()
167-
Expect(rmFail).Should(ExitWithError(125, fmt.Sprintf("Error: no artifact found with name or digest of %s", "foobar")))
167+
Expect(rmFail).Should(ExitWithError(125, fmt.Sprintf("Error: %s: artifact does not exist", "foobar")))
168168

169169
// Add an artifact to remove later
170170
artifact1File, err := createArtifactFile(4192)
@@ -180,7 +180,7 @@ var _ = Describe("Podman artifact", func() {
180180
// Inspecting that the removed artifact should fail
181181
inspectArtifact := podmanTest.Podman([]string{"artifact", "inspect", artifact1Name})
182182
inspectArtifact.WaitWithDefaultTimeout()
183-
Expect(inspectArtifact).Should(ExitWithError(125, fmt.Sprintf("Error: no artifact found with name or digest of %s", artifact1Name)))
183+
Expect(inspectArtifact).Should(ExitWithError(125, fmt.Sprintf("Error: %s: artifact does not exist", artifact1Name)))
184184
})
185185

186186
It("podman artifact inspect with full or partial digest", func() {
@@ -440,7 +440,8 @@ var _ = Describe("Podman artifact", func() {
440440

441441
appendFail := podmanTest.Podman([]string{"artifact", "add", "--append", artifact1Name, artifact1File})
442442
appendFail.WaitWithDefaultTimeout()
443-
Expect(appendFail).Should(ExitWithError(125, fmt.Sprintf("Error: file: \"%s\" already exists in artifact", filepath.Base(artifact1File))))
443+
// Error: PJYjLgoU: file already exists in artifact
444+
Expect(appendFail).Should(ExitWithError(125, fmt.Sprintf("Error: %s: file already exists in artifact", filepath.Base(artifact1File))))
444445

445446
a := podmanTest.InspectArtifact(artifact1Name)
446447

@@ -456,11 +457,11 @@ var _ = Describe("Podman artifact", func() {
456457

457458
addFail := podmanTest.Podman([]string{"artifact", "add", artifact1Name, artifact1File, artifact1File})
458459
addFail.WaitWithDefaultTimeout()
459-
Expect(addFail).Should(ExitWithError(125, fmt.Sprintf("Error: file: \"%s\" already exists in artifact", filepath.Base(artifact1File))))
460+
Expect(addFail).Should(ExitWithError(125, fmt.Sprintf("Error: %s: file already exists in artifact", filepath.Base(artifact1File))))
460461

461462
inspectFail := podmanTest.Podman([]string{"artifact", "inspect", artifact1Name})
462463
inspectFail.WaitWithDefaultTimeout()
463-
Expect(inspectFail).Should(ExitWithError(125, fmt.Sprintf("Error: no artifact found with name or digest of %s", artifact1Name)))
464+
Expect(inspectFail).Should(ExitWithError(125, fmt.Sprintf("Error: %s: artifact does not exist", artifact1Name)))
464465
})
465466

466467
It("podman artifact add --append file already exists in artifact", func() {
@@ -472,7 +473,7 @@ var _ = Describe("Podman artifact", func() {
472473

473474
appendFail := podmanTest.Podman([]string{"artifact", "add", "--append", artifact1Name, artifact1File})
474475
appendFail.WaitWithDefaultTimeout()
475-
Expect(appendFail).Should(ExitWithError(125, fmt.Sprintf("Error: file: \"%s\" already exists in artifact", filepath.Base(artifact1File))))
476+
Expect(appendFail).Should(ExitWithError(125, fmt.Sprintf("Error: %s: file already exists in artifact", filepath.Base(artifact1File))))
476477
a := podmanTest.InspectArtifact(artifact1Name)
477478

478479
Expect(a.Manifest.Layers).To(HaveLen(1))

0 commit comments

Comments
 (0)