summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pkg/specgen/generate/ports.go46
-rw-r--r--test/system/030-run.bats20
-rw-r--r--test/system/055-rm.bats9
-rw-r--r--test/system/140-diff.bats6
-rw-r--r--test/system/helpers.bash8
5 files changed, 55 insertions, 34 deletions
diff --git a/pkg/specgen/generate/ports.go b/pkg/specgen/generate/ports.go
index 7dd50ac0d..5c13c95b2 100644
--- a/pkg/specgen/generate/ports.go
+++ b/pkg/specgen/generate/ports.go
@@ -25,7 +25,12 @@ const (
func parsePortMapping(portMappings []specgen.PortMapping) ([]ocicni.PortMapping, map[string]map[string]map[uint16]uint16, map[string]map[string]map[uint16]uint16, error) {
// First, we need to validate the ports passed in the specgen, and then
// convert them into CNI port mappings.
- finalMappings := []ocicni.PortMapping{}
+ type tempMapping struct {
+ mapping ocicni.PortMapping
+ startOfRange bool
+ isInRange bool
+ }
+ tempMappings := []tempMapping{}
// To validate, we need two maps: one for host ports, one for container
// ports.
@@ -153,18 +158,32 @@ func parsePortMapping(portMappings []specgen.PortMapping) ([]ocicni.PortMapping,
Protocol: p,
HostIP: port.HostIP,
}
- finalMappings = append(finalMappings, cniPort)
+ tempMappings = append(
+ tempMappings,
+ tempMapping{
+ mapping: cniPort,
+ startOfRange: port.Range > 0 && index == 0,
+ isInRange: port.Range > 0,
+ },
+ )
}
}
}
// Handle any 0 host ports now by setting random container ports.
if postAssignHostPort {
- remadeMappings := make([]ocicni.PortMapping, 0, len(finalMappings))
+ remadeMappings := make([]ocicni.PortMapping, 0, len(tempMappings))
+
+ var (
+ candidate int
+ err error
+ )
// Iterate over all
- for _, p := range finalMappings {
- if p.HostPort != 0 {
+ for _, tmp := range tempMappings {
+ p := tmp.mapping
+
+ if p.HostPort != 0 && !tmp.isInRange {
remadeMappings = append(remadeMappings, p)
continue
}
@@ -192,9 +211,15 @@ func parsePortMapping(portMappings []specgen.PortMapping) ([]ocicni.PortMapping,
// Max retries to ensure we don't loop forever.
for i := 0; i < 15; i++ {
- candidate, err := getRandomPort()
- if err != nil {
- return nil, nil, nil, errors.Wrapf(err, "error getting candidate host port for container port %d", p.ContainerPort)
+ // Only get a random candidate for single entries or the start
+ // of a range. Otherwise we just increment the candidate.
+ if !tmp.isInRange || tmp.startOfRange {
+ candidate, err = getRandomPort()
+ if err != nil {
+ return nil, nil, nil, errors.Wrapf(err, "error getting candidate host port for container port %d", p.ContainerPort)
+ }
+ } else {
+ candidate++
}
if hostPortMap[uint16(candidate)] == 0 {
@@ -213,6 +238,11 @@ func parsePortMapping(portMappings []specgen.PortMapping) ([]ocicni.PortMapping,
return remadeMappings, containerPortValidate, hostPortValidate, nil
}
+ finalMappings := []ocicni.PortMapping{}
+ for _, m := range tempMappings {
+ finalMappings = append(finalMappings, m.mapping)
+ }
+
return finalMappings, containerPortValidate, hostPortValidate, nil
}
diff --git a/test/system/030-run.bats b/test/system/030-run.bats
index f7c48da8d..28dc7c7a7 100644
--- a/test/system/030-run.bats
+++ b/test/system/030-run.bats
@@ -460,24 +460,4 @@ json-file | f
is "$output" "$expect" "podman run with --tz=local, matches host"
}
-@test "podman container exists" {
- rand=$(random_string 30)
- run_podman 1 container exists myctr
-
- run_podman create --name myctr $IMAGE /bin/true
- run_podman container exists myctr
-
- # Create a container that podman does not know about
- run buildah from $IMAGE
- cid="$output"
-
- # exists should fail
- run_podman 1 container exists $cid
-
- # exists should succeed
- run_podman container exists --external $cid
-
- run buildah rm $cid
-}
-
# vim: filetype=sh
diff --git a/test/system/055-rm.bats b/test/system/055-rm.bats
index 7176ae4b8..0107114b5 100644
--- a/test/system/055-rm.bats
+++ b/test/system/055-rm.bats
@@ -41,11 +41,14 @@ load helpers
run_podman create --name $rand $IMAGE /bin/true
# Create a container that podman does not know about
- run buildah from $IMAGE
- cid="$output"
+ external_cid=$(buildah from $IMAGE)
+
+ # Plain 'exists' should fail, but should succeed with --external
+ run_podman 1 container exists $external_cid
+ run_podman container exists --external $external_cid
# rm should succeed
- run_podman rm $rand $cid
+ run_podman rm $rand $external_cid
}
# I'm sorry! This test takes 13 seconds. There's not much I can do about it,
diff --git a/test/system/140-diff.bats b/test/system/140-diff.bats
index d0f33e438..1277f9bbe 100644
--- a/test/system/140-diff.bats
+++ b/test/system/140-diff.bats
@@ -34,8 +34,8 @@ load helpers
@test "podman diff with buildah container " {
rand_file=$(random_string 10)
- run buildah from --name buildahctr $IMAGE
- run buildah run buildahctr sh -c "touch /$rand_file;rm /etc/services"
+ buildah from --name buildahctr $IMAGE
+ buildah run buildahctr sh -c "touch /$rand_file;rm /etc/services"
run_podman diff --format json buildahctr
@@ -51,7 +51,7 @@ load helpers
is "$result" "${expect[$field]}" "$field"
done
- run buildah rm buildahctr
+ buildah rm buildahctr
}
# vim: filetype=sh
diff --git a/test/system/helpers.bash b/test/system/helpers.bash
index 4591c9015..73cf1e5b2 100644
--- a/test/system/helpers.bash
+++ b/test/system/helpers.bash
@@ -34,6 +34,14 @@ function basic_setup() {
# Clean up all containers
run_podman rm --all --force
+ # ...including external (buildah) ones
+ run_podman ps --all --external --format '{{.ID}} {{.Names}}'
+ for line in "${lines[@]}"; do
+ set $line
+ echo "# setup(): removing stray external container $1 ($2)" >&3
+ run_podman rm $1
+ done
+
# Clean up all images except those desired
found_needed_image=
run_podman images --all --format '{{.Repository}}:{{.Tag}} {{.ID}}'