Browse code

Fix filter on expose and publish

- Add tests to ensure it's working
- Rename variables for better clarification
- Fix validation test
- Remove wrong filter assertion based on publish filter
- Change port on test

Signed-off-by: Jaime Cepeda <jcepedavillamayor@gmail.com>

Jaime Cepeda authored on 2020/02/02 02:34:54
Showing 2 changed files
... ...
@@ -551,23 +551,19 @@ func includeContainerInList(container *container.Snapshot, ctx *listContext) ite
551 551
 		}
552 552
 	}
553 553
 
554
-	if len(ctx.publish) > 0 {
555
-		shouldSkip := true
556
-		for port := range ctx.publish {
557
-			if _, ok := container.PortBindings[port]; ok {
554
+	if len(ctx.expose) > 0 || len(ctx.publish) > 0 {
555
+		var (
556
+			shouldSkip    bool = true
557
+			publishedPort nat.Port
558
+			exposedPort   nat.Port
559
+		)
560
+		for _, port := range container.Ports {
561
+			publishedPort = nat.Port(fmt.Sprintf("%d/%s", port.PublicPort, port.Type))
562
+			exposedPort = nat.Port(fmt.Sprintf("%d/%s", port.PrivatePort, port.Type))
563
+			if ok := ctx.publish[publishedPort]; ok {
558 564
 				shouldSkip = false
559 565
 				break
560
-			}
561
-		}
562
-		if shouldSkip {
563
-			return excludeContainer
564
-		}
565
-	}
566
-
567
-	if len(ctx.expose) > 0 {
568
-		shouldSkip := true
569
-		for port := range ctx.expose {
570
-			if _, ok := container.ExposedPorts[port]; ok {
566
+			} else if ok := ctx.expose[exposedPort]; ok {
571 567
 				shouldSkip = false
572 568
 				break
573 569
 			}
... ...
@@ -816,29 +816,44 @@ func (s *DockerSuite) TestPsListContainersFilterPorts(c *testing.T) {
816 816
 	out, _ = dockerCmd(c, "run", "-d", "--expose=8080", "busybox", "top")
817 817
 	id2 := strings.TrimSpace(out)
818 818
 
819
+	out, _ = dockerCmd(c, "run", "-d", "-p", "1090:90", "busybox", "top")
820
+	id3 := strings.TrimSpace(out)
821
+
819 822
 	out, _ = dockerCmd(c, "ps", "--no-trunc", "-q")
820 823
 	assert.Assert(c, strings.Contains(strings.TrimSpace(out), id1))
821 824
 	assert.Assert(c, strings.Contains(strings.TrimSpace(out), id2))
825
+	assert.Assert(c, strings.Contains(strings.TrimSpace(out), id3))
826
+
822 827
 	out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "publish=80-8080/udp")
823 828
 	assert.Assert(c, strings.TrimSpace(out) != id1)
824 829
 	assert.Assert(c, strings.TrimSpace(out) != id2)
830
+	assert.Assert(c, strings.TrimSpace(out) != id3)
825 831
 
826 832
 	out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "expose=8081")
827 833
 	assert.Assert(c, strings.TrimSpace(out) != id1)
828 834
 	assert.Assert(c, strings.TrimSpace(out) != id2)
835
+	assert.Assert(c, strings.TrimSpace(out) != id3)
829 836
 
830 837
 	out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "publish=80-81")
831
-	assert.Equal(c, strings.TrimSpace(out), id1)
838
+	assert.Assert(c, strings.TrimSpace(out) != id1)
832 839
 	assert.Assert(c, strings.TrimSpace(out) != id2)
840
+	assert.Assert(c, strings.TrimSpace(out) != id3)
833 841
 
834 842
 	out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "expose=80/tcp")
835 843
 	assert.Equal(c, strings.TrimSpace(out), id1)
836 844
 	assert.Assert(c, strings.TrimSpace(out) != id2)
845
+	assert.Assert(c, strings.TrimSpace(out) != id3)
846
+
847
+	out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "publish=1090")
848
+	assert.Assert(c, strings.TrimSpace(out) != id1)
849
+	assert.Assert(c, strings.TrimSpace(out) != id2)
850
+	assert.Equal(c, strings.TrimSpace(out), id3)
837 851
 
838 852
 	out, _ = dockerCmd(c, "ps", "--no-trunc", "-q", "--filter", "expose=8080/tcp")
839 853
 	out = RemoveOutputForExistingElements(out, existingContainers)
840 854
 	assert.Assert(c, strings.TrimSpace(out) != id1)
841 855
 	assert.Equal(c, strings.TrimSpace(out), id2)
856
+	assert.Assert(c, strings.TrimSpace(out) != id3)
842 857
 }
843 858
 
844 859
 func (s *DockerSuite) TestPsNotShowLinknamesOfDeletedContainer(c *testing.T) {