From a09983b47455c8e9ce9a9f1aec68aeef6d61f0cb Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Thu, 8 Oct 2020 14:36:40 +0200 Subject: [PATCH] Do not send metrics for help commands Signed-off-by: Guillaume Tardif --- metrics/commands.go | 2 +- metrics/metrics.go | 3 +++ metrics/metrics_test.go | 18 +++++++++--------- tests/e2e/e2e_test.go | 11 +++++++++++ 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/metrics/commands.go b/metrics/commands.go index b9a13a08b..e07c02419 100644 --- a/metrics/commands.go +++ b/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{ diff --git a/metrics/metrics.go b/metrics/metrics.go index edc048fc7..3f6c3931d 100644 --- a/metrics/metrics.go +++ b/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 } diff --git a/metrics/metrics_test.go b/metrics/metrics_test.go index 12846baf1..cd66e18a4 100644 --- a/metrics/metrics_test.go +++ b/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: "", }, } diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 051eb1343..97cdee5eb 100644 --- a/tests/e2e/e2e_test.go +++ b/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()