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

Removing a non existing container API should return 404 #9681

Merged
merged 1 commit into from
Mar 10, 2021
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
6 changes: 3 additions & 3 deletions libpod/boltdb_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ func (s *BoltState) ContainerInUse(ctr *Container) ([]string, error) {
ctrDB := ctrBucket.Bucket([]byte(ctr.ID()))
if ctrDB == nil {
ctr.valid = false
return errors.Wrapf(define.ErrNoSuchCtr, "no container with ID %s found in DB", ctr.ID())
return errors.Wrapf(define.ErrNoSuchCtr, "no container with ID %q found in DB", ctr.ID())
}

dependsBkt := ctrDB.Bucket(dependenciesBkt)
Expand Down Expand Up @@ -1669,7 +1669,7 @@ func (s *BoltState) RewriteContainerConfig(ctr *Container, newCfg *ContainerConf
ctrDB := ctrBkt.Bucket([]byte(ctr.ID()))
if ctrDB == nil {
ctr.valid = false
return errors.Wrapf(define.ErrNoSuchCtr, "no container with ID %s found in DB", ctr.ID())
return errors.Wrapf(define.ErrNoSuchCtr, "no container with ID %q found in DB", ctr.ID())
}

if err := ctrDB.Put(configKey, newCfgJSON); err != nil {
Expand Down Expand Up @@ -1767,7 +1767,7 @@ func (s *BoltState) SafeRewriteContainerConfig(ctr *Container, oldName, newName
ctrDB := ctrBkt.Bucket([]byte(ctr.ID()))
if ctrDB == nil {
ctr.valid = false
return errors.Wrapf(define.ErrNoSuchCtr, "no container with ID %s found in DB", ctr.ID())
return errors.Wrapf(define.ErrNoSuchCtr, "no container with ID %q found in DB", ctr.ID())
}

if err := ctrDB.Put(configKey, newCfgJSON); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions libpod/boltdb_state_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1055,9 +1055,9 @@ func (s *BoltState) lookupContainerID(idOrName string, ctrBucket, namesBucket, n
return nil, err
} else if !exists {
if isPod {
return nil, errors.Wrapf(define.ErrNoSuchCtr, "%s is a pod, not a container", idOrName)
return nil, errors.Wrapf(define.ErrNoSuchCtr, "%q is a pod, not a container", idOrName)
}
return nil, errors.Wrapf(define.ErrNoSuchCtr, "no container with name or ID %s found", idOrName)
return nil, errors.Wrapf(define.ErrNoSuchCtr, "no container with name or ID %q found", idOrName)
}
return id, nil
}
4 changes: 2 additions & 2 deletions libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ func (r *Runtime) evictContainer(ctx context.Context, idOrName string, removeVol

id, err := r.state.LookupContainerID(idOrName)
if err != nil {
return "", errors.Wrapf(err, "failed to find container %q in state", idOrName)
return "", err
}

// Begin by trying a normal removal. Valid containers will be removed normally.
Expand Down Expand Up @@ -744,7 +744,7 @@ func (r *Runtime) evictContainer(ctx context.Context, idOrName string, removeVol
return id, err
}
if !exists {
return id, errors.Wrapf(err, "failed to find container ID %q for eviction", id)
return id, err
}

// Re-create a container struct for removal purposes
Expand Down
7 changes: 6 additions & 1 deletion pkg/api/handlers/compat/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ func RemoveContainer(w http.ResponseWriter, r *http.Request) {
return
}
if len(report) > 0 && report[0].Err != nil {
utils.InternalServerError(w, report[0].Err)
err = report[0].Err
if errors.Cause(err) == define.ErrNoSuchCtr {
utils.ContainerNotFound(w, name, err)
return
}
utils.InternalServerError(w, err)
return
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/domain/infra/abi/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,14 @@ func (ic *ContainerEngine) ContainerRm(ctx context.Context, namesOrIds []string,
for _, ctr := range names {
logrus.Debugf("Evicting container %q", ctr)
report := entities.RmReport{Id: ctr}
id, err := ic.Libpod.EvictContainer(ctx, ctr, options.Volumes)
_, err := ic.Libpod.EvictContainer(ctx, ctr, options.Volumes)
if err != nil {
if options.Ignore && errors.Cause(err) == define.ErrNoSuchCtr {
logrus.Debugf("Ignoring error (--allow-missing): %v", err)
reports = append(reports, &report)
continue
}
report.Err = errors.Wrapf(err, "failed to evict container: %q", id)
report.Err = err
reports = append(reports, &report)
continue
}
Expand Down
1 change: 1 addition & 0 deletions test/apiv2/20-containers.at
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ t DELETE images/localhost/newrepo:v1?force=true 200
t DELETE images/localhost/newrepo:v2?force=true 200
t DELETE libpod/containers/$cid 204
t DELETE libpod/containers/myctr 204
t DELETE libpod/containers/bogus 404


# test apiv2 create container with correct entrypoint and cmd
Expand Down
2 changes: 1 addition & 1 deletion test/system/050-stop.bats
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ load helpers
name=thiscontainerdoesnotexist
run_podman 125 stop $name
is "$output" \
"Error: no container with name or ID $name found: no such container" \
"Error: no container with name or ID \"$name\" found: no such container" \
"podman stop nonexistent container"

run_podman stop --ignore $name
Expand Down