Преглед изворни кода

Merge pull request #40100 from thaJeztah/test_fixes

Some small (test) fixes/improvements
Akihiro Suda пре 5 година
родитељ
комит
4124e78d57

+ 1 - 0
daemon/container_unix_test.go

@@ -24,6 +24,7 @@ func TestContainerWarningHostAndPublishPorts(t *testing.T) {
 			"8080": []nat.PortBinding{{HostPort: "8989"}},
 			"8080": []nat.PortBinding{{HostPort: "8989"}},
 		}, warnings: []string{"Published ports are discarded when using host network mode"}},
 		}, warnings: []string{"Published ports are discarded when using host network mode"}},
 	}
 	}
+	muteLogs()
 
 
 	for _, tc := range testCases {
 	for _, tc := range testCases {
 		hostConfig := &containertypes.HostConfig{
 		hostConfig := &containertypes.HostConfig{

+ 1 - 0
daemon/daemon_unix_test.go

@@ -68,6 +68,7 @@ func TestAdjustCPUShares(t *testing.T) {
 		repository: tmp,
 		repository: tmp,
 		root:       tmp,
 		root:       tmp,
 	}
 	}
+	muteLogs()
 
 
 	hostConfig := &containertypes.HostConfig{
 	hostConfig := &containertypes.HostConfig{
 		Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
 		Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},

+ 1 - 0
daemon/health_test.go

@@ -78,6 +78,7 @@ func TestHealthStates(t *testing.T) {
 		EventsService:     e,
 		EventsService:     e,
 		containersReplica: store,
 		containersReplica: store,
 	}
 	}
+	muteLogs()
 
 
 	c.Config.Healthcheck = &containertypes.HealthConfig{
 	c.Config.Healthcheck = &containertypes.HealthConfig{
 		Retries: 1,
 		Retries: 1,

+ 3 - 2
daemon/info_unix.go

@@ -205,14 +205,15 @@ func getBackingFs(v *types.Info) string {
 //
 //
 //     tini version 0.18.0 - git.fec3683
 //     tini version 0.18.0 - git.fec3683
 func parseInitVersion(v string) (version string, commit string, err error) {
 func parseInitVersion(v string) (version string, commit string, err error) {
-	parts := strings.Split(strings.TrimSpace(v), " - ")
+	parts := strings.Split(v, " - ")
 
 
 	if len(parts) >= 2 {
 	if len(parts) >= 2 {
-		gitParts := strings.Split(parts[1], ".")
+		gitParts := strings.Split(strings.TrimSpace(parts[1]), ".")
 		if len(gitParts) == 2 && gitParts[0] == "git" {
 		if len(gitParts) == 2 && gitParts[0] == "git" {
 			commit = gitParts[1]
 			commit = gitParts[1]
 		}
 		}
 	}
 	}
+	parts[0] = strings.TrimSpace(parts[0])
 	if strings.HasPrefix(parts[0], "tini version ") {
 	if strings.HasPrefix(parts[0], "tini version ") {
 		version = strings.TrimPrefix(parts[0], "tini version ")
 		version = strings.TrimPrefix(parts[0], "tini version ")
 	}
 	}

+ 23 - 8
daemon/info_unix_test.go

@@ -26,12 +26,24 @@ func TestParseInitVersion(t *testing.T) {
 		}, {
 		}, {
 			output:  "tini version 0.13.2",
 			output:  "tini version 0.13.2",
 			version: "0.13.2",
 			version: "0.13.2",
+		}, {
+			output:  "tini version 0.13.2 - ",
+			version: "0.13.2",
+		}, {
+			output: " - git.949e6fa",
+			commit: "949e6fa",
 		}, {
 		}, {
 			output:  "tini version0.13.2",
 			output:  "tini version0.13.2",
 			invalid: true,
 			invalid: true,
+		}, {
+			output:  "version 0.13.0",
+			invalid: true,
 		}, {
 		}, {
 			output:  "",
 			output:  "",
 			invalid: true,
 			invalid: true,
+		}, {
+			output:  " - ",
+			invalid: true,
 		}, {
 		}, {
 			output:  "hello world",
 			output:  "hello world",
 			invalid: true,
 			invalid: true,
@@ -39,14 +51,17 @@ func TestParseInitVersion(t *testing.T) {
 	}
 	}
 
 
 	for _, test := range tests {
 	for _, test := range tests {
-		version, commit, err := parseInitVersion(test.output)
-		if test.invalid {
-			assert.Check(t, is.ErrorContains(err, ""))
-		} else {
-			assert.Check(t, err)
-		}
-		assert.Equal(t, test.version, version)
-		assert.Equal(t, test.commit, commit)
+		test := test
+		t.Run(test.output, func(t *testing.T) {
+			version, commit, err := parseInitVersion(test.output)
+			if test.invalid {
+				assert.Check(t, is.ErrorContains(err, ""))
+			} else {
+				assert.Check(t, err)
+			}
+			assert.Equal(t, test.version, version)
+			assert.Equal(t, test.commit, commit)
+		})
 	}
 	}
 }
 }
 
 

+ 16 - 0
daemon/reload_test.go

@@ -13,10 +13,16 @@ import (
 	_ "github.com/docker/docker/pkg/discovery/memory"
 	_ "github.com/docker/docker/pkg/discovery/memory"
 	"github.com/docker/docker/registry"
 	"github.com/docker/docker/registry"
 	"github.com/docker/libnetwork"
 	"github.com/docker/libnetwork"
+	"github.com/sirupsen/logrus"
 	"gotest.tools/assert"
 	"gotest.tools/assert"
 	is "gotest.tools/assert/cmp"
 	is "gotest.tools/assert/cmp"
 )
 )
 
 
+// muteLogs suppresses logs that are generated during the test
+func muteLogs() {
+	logrus.SetLevel(logrus.ErrorLevel)
+}
+
 func TestDaemonReloadLabels(t *testing.T) {
 func TestDaemonReloadLabels(t *testing.T) {
 	daemon := &Daemon{
 	daemon := &Daemon{
 		configStore: &config.Config{
 		configStore: &config.Config{
@@ -26,6 +32,7 @@ func TestDaemonReloadLabels(t *testing.T) {
 		},
 		},
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 	}
 	}
+	muteLogs()
 
 
 	valuesSets := make(map[string]interface{})
 	valuesSets := make(map[string]interface{})
 	valuesSets["labels"] = "foo:baz"
 	valuesSets["labels"] = "foo:baz"
@@ -51,6 +58,7 @@ func TestDaemonReloadAllowNondistributableArtifacts(t *testing.T) {
 		configStore:  &config.Config{},
 		configStore:  &config.Config{},
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 	}
 	}
+	muteLogs()
 
 
 	var err error
 	var err error
 	// Initialize daemon with some registries.
 	// Initialize daemon with some registries.
@@ -106,6 +114,8 @@ func TestDaemonReloadMirrors(t *testing.T) {
 	daemon := &Daemon{
 	daemon := &Daemon{
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 	}
 	}
+	muteLogs()
+
 	var err error
 	var err error
 	daemon.RegistryService, err = registry.NewService(registry.ServiceOptions{
 	daemon.RegistryService, err = registry.NewService(registry.ServiceOptions{
 		InsecureRegistries: []string{},
 		InsecureRegistries: []string{},
@@ -205,6 +215,8 @@ func TestDaemonReloadInsecureRegistries(t *testing.T) {
 	daemon := &Daemon{
 	daemon := &Daemon{
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 	}
 	}
+	muteLogs()
+
 	var err error
 	var err error
 	// initialize daemon with existing insecure registries: "127.0.0.0/8", "10.10.1.11:5000", "10.10.1.22:5000"
 	// initialize daemon with existing insecure registries: "127.0.0.0/8", "10.10.1.11:5000", "10.10.1.22:5000"
 	daemon.RegistryService, err = registry.NewService(registry.ServiceOptions{
 	daemon.RegistryService, err = registry.NewService(registry.ServiceOptions{
@@ -297,6 +309,8 @@ func TestDaemonReloadNotAffectOthers(t *testing.T) {
 	daemon := &Daemon{
 	daemon := &Daemon{
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 	}
 	}
+	muteLogs()
+
 	daemon.configStore = &config.Config{
 	daemon.configStore = &config.Config{
 		CommonConfig: config.CommonConfig{
 		CommonConfig: config.CommonConfig{
 			Labels: []string{"foo:bar"},
 			Labels: []string{"foo:bar"},
@@ -331,6 +345,7 @@ func TestDaemonDiscoveryReload(t *testing.T) {
 	daemon := &Daemon{
 	daemon := &Daemon{
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 	}
 	}
+	muteLogs()
 	daemon.configStore = &config.Config{
 	daemon.configStore = &config.Config{
 		CommonConfig: config.CommonConfig{
 		CommonConfig: config.CommonConfig{
 			ClusterStore:     "memory://127.0.0.1",
 			ClusterStore:     "memory://127.0.0.1",
@@ -411,6 +426,7 @@ func TestDaemonDiscoveryReloadFromEmptyDiscovery(t *testing.T) {
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 		imageService: images.NewImageService(images.ImageServiceConfig{}),
 	}
 	}
 	daemon.configStore = &config.Config{}
 	daemon.configStore = &config.Config{}
+	muteLogs()
 
 
 	valuesSet := make(map[string]interface{})
 	valuesSet := make(map[string]interface{})
 	valuesSet["cluster-store"] = "memory://127.0.0.1:2222"
 	valuesSet["cluster-store"] = "memory://127.0.0.1:2222"

+ 45 - 25
daemon/top_unix_test.go

@@ -4,6 +4,8 @@ package daemon // import "github.com/docker/docker/daemon"
 
 
 import (
 import (
 	"testing"
 	"testing"
+
+	"gotest.tools/assert"
 )
 )
 
 
 func TestContainerTopValidatePSArgs(t *testing.T) {
 func TestContainerTopValidatePSArgs(t *testing.T) {
@@ -22,14 +24,14 @@ func TestContainerTopValidatePSArgs(t *testing.T) {
 		"":                          false,
 		"":                          false,
 	}
 	}
 	for psArgs, errExpected := range tests {
 	for psArgs, errExpected := range tests {
-		err := validatePSArgs(psArgs)
-		t.Logf("tested %q, got err=%v", psArgs, err)
-		if errExpected && err == nil {
-			t.Fatalf("expected error, got %v (%q)", err, psArgs)
-		}
-		if !errExpected && err != nil {
-			t.Fatalf("expected nil, got %v (%q)", err, psArgs)
-		}
+		t.Run(psArgs, func(t *testing.T) {
+			err := validatePSArgs(psArgs)
+			if errExpected {
+				assert.ErrorContains(t, err, "", "psArgs: %q", psArgs)
+			} else {
+				assert.NilError(t, err, "psArgs: %q", psArgs)
+			}
+		})
 	}
 	}
 }
 }
 
 
@@ -39,41 +41,59 @@ func TestContainerTopParsePSOutput(t *testing.T) {
 		pids        []uint32
 		pids        []uint32
 		errExpected bool
 		errExpected bool
 	}{
 	}{
-		{[]byte(`  PID COMMAND
+		{
+			output: []byte(`  PID COMMAND
    42 foo
    42 foo
    43 bar
    43 bar
 		- -
 		- -
   100 baz
   100 baz
-`), []uint32{42, 43}, false},
-		{[]byte(`  UID COMMAND
+`),
+			pids:        []uint32{42, 43},
+			errExpected: false,
+		},
+		{
+			output: []byte(`  UID COMMAND
    42 foo
    42 foo
    43 bar
    43 bar
 		- -
 		- -
   100 baz
   100 baz
-`), []uint32{42, 43}, true},
+`),
+			pids:        []uint32{42, 43},
+			errExpected: true,
+		},
 		// unicode space (U+2003, 0xe2 0x80 0x83)
 		// unicode space (U+2003, 0xe2 0x80 0x83)
-		{[]byte(` PID COMMAND
+		{
+			output: []byte(` PID COMMAND
    42 foo
    42 foo
    43 bar
    43 bar
 		- -
 		- -
   100 baz
   100 baz
-`), []uint32{42, 43}, true},
+`),
+			pids:        []uint32{42, 43},
+			errExpected: true,
+		},
 		// the first space is U+2003, the second one is ascii.
 		// the first space is U+2003, the second one is ascii.
-		{[]byte(` PID COMMAND
+		{
+			output: []byte(` PID COMMAND
    42 foo
    42 foo
    43 bar
    43 bar
   100 baz
   100 baz
-`), []uint32{42, 43}, true},
+`),
+			pids:        []uint32{42, 43},
+			errExpected: true,
+		},
 	}
 	}
 
 
-	for _, f := range tests {
-		_, err := parsePSOutput(f.output, f.pids)
-		t.Logf("tested %q, got err=%v", string(f.output), err)
-		if f.errExpected && err == nil {
-			t.Fatalf("expected error, got %v (%q)", err, string(f.output))
-		}
-		if !f.errExpected && err != nil {
-			t.Fatalf("expected nil, got %v (%q)", err, string(f.output))
-		}
+	for _, tc := range tests {
+		tc := tc
+		t.Run(string(tc.output), func(t *testing.T) {
+			_, err := parsePSOutput(tc.output, tc.pids)
+			if tc.errExpected && err == nil {
+				t.Fatalf("expected error, got %v (%q)", err, string(tc.output))
+			}
+			if !tc.errExpected && err != nil {
+				t.Fatalf("expected nil, got %v (%q)", err, string(tc.output))
+			}
+		})
 	}
 	}
 }
 }