Explorar o código

Do not send metrics for help commands

Signed-off-by: Guillaume Tardif <[email protected]>
Guillaume Tardif %!s(int64=5) %!d(string=hai) anos
pai
achega
a09983b474
Modificáronse 4 ficheiros con 24 adicións e 10 borrados
  1. 1 1
      metrics/commands.go
  2. 3 0
      metrics/metrics.go
  3. 9 9
      metrics/metrics_test.go
  4. 11 0
      tests/e2e/e2e_test.go

+ 1 - 1
metrics/commands.go

@@ -19,7 +19,7 @@ package metrics
 var commandFlags = []string{
 	//added to catch scan details
 	"--version", "--login",
-	"--help", "-h"}
+}
 
 // Generated with generatecommands/main.go
 var managementCommands = []string{

+ 3 - 0
metrics/metrics.go

@@ -51,6 +51,9 @@ func GetCommand(args []string) string {
 	result := ""
 	onlyFlags := false
 	for _, arg := range args {
+		if arg == "--help" {
+			return ""
+		}
 		if arg == "--" {
 			break
 		}

+ 9 - 9
metrics/metrics_test.go

@@ -58,6 +58,11 @@ func TestGetCommand(t *testing.T) {
 			args:     []string{"image", "ls"},
 			expected: "image ls",
 		},
+		{
+			name:     "management command with flag",
+			args:     []string{"image", "--test", "ls"},
+			expected: "image ls",
+		},
 		{
 			name:     "management subcommand with flag",
 			args:     []string{"image", "ls", "-q"},
@@ -148,7 +153,7 @@ func TestGetCommand(t *testing.T) {
 	}
 }
 
-func TestFlags(t *testing.T) {
+func TestIgnoreHelpCommands(t *testing.T) {
 	testCases := []struct {
 		name     string
 		args     []string
@@ -157,22 +162,17 @@ func TestFlags(t *testing.T) {
 		{
 			name:     "help",
 			args:     []string{"--help"},
-			expected: "--help",
+			expected: "",
 		},
 		{
 			name:     "help on run",
 			args:     []string{"run", "--help"},
-			expected: "run --help",
-		},
-		{
-			name:     "-h on run",
-			args:     []string{"run", "-h"},
-			expected: "run -h",
+			expected: "",
 		},
 		{
 			name:     "help on compose up",
 			args:     []string{"compose", "up", "--help"},
-			expected: "compose up --help",
+			expected: "",
 		},
 	}
 

+ 11 - 0
tests/e2e/e2e_test.go

@@ -164,6 +164,17 @@ func TestContextMetrics(t *testing.T) {
 	s.Start()
 	defer s.Stop()
 
+	t.Run("do not send metrics on help commands", func(t *testing.T) {
+		s.ResetUsage()
+
+		c.RunDockerCmd("--help")
+		c.RunDockerCmd("ps", "--help")
+		c.RunDockerCmd("run", "--help")
+
+		usage := s.GetUsage()
+		assert.Equal(t, 0, len(usage))
+	})
+
 	t.Run("metrics on default context", func(t *testing.T) {
 		s.ResetUsage()