gitlab.com/jfprevost/gitlab-runner-notlscheck@v11.11.4+incompatible/log/configuration_test.go (about) 1 package log 2 3 import ( 4 "testing" 5 6 "github.com/sirupsen/logrus" 7 "github.com/sirupsen/logrus/hooks/test" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 "github.com/urfave/cli" 11 12 "gitlab.com/gitlab-org/gitlab-runner/helpers" 13 ) 14 15 func prepareFakeConfiguration(logger *logrus.Logger) func() { 16 oldConfiguration := configuration 17 configuration = NewConfig(logger) 18 19 return func() { 20 configuration = oldConfiguration 21 configuration.ReloadConfiguration() 22 } 23 } 24 25 func testCommandRun(args ...string) { 26 app := cli.NewApp() 27 app.Commands = []cli.Command{ 28 { 29 Name: "logtest", 30 Action: func(cliCtx *cli.Context) {}, 31 }, 32 } 33 34 ConfigureLogging(app) 35 36 args = append([]string{"binary"}, args...) 37 args = append(args, "logtest") 38 39 app.Run(args) 40 } 41 42 type handleCliCtxTestCase struct { 43 args []string 44 expectedError string 45 expectedLevel logrus.Level 46 expectedFormatter logrus.Formatter 47 expectedLevelSetWithCli bool 48 expectedFormatSetWithCli bool 49 goroutinesDumpStopChExists bool 50 } 51 52 func TestHandleCliCtx(t *testing.T) { 53 tests := map[string]handleCliCtxTestCase{ 54 "no configuration specified": { 55 expectedLevel: logrus.InfoLevel, 56 expectedFormatter: new(RunnerTextFormatter), 57 }, 58 "--log-level specified": { 59 args: []string{"--log-level", "error"}, 60 expectedLevel: logrus.ErrorLevel, 61 expectedFormatter: new(RunnerTextFormatter), 62 expectedLevelSetWithCli: true, 63 }, 64 "--debug specified": { 65 args: []string{"--debug"}, 66 expectedLevel: logrus.DebugLevel, 67 expectedFormatter: new(RunnerTextFormatter), 68 expectedLevelSetWithCli: true, 69 goroutinesDumpStopChExists: true, 70 }, 71 "--log-level and --debug specified": { 72 args: []string{"--log-level", "error", "--debug"}, 73 expectedLevel: logrus.DebugLevel, 74 expectedFormatter: new(RunnerTextFormatter), 75 expectedLevelSetWithCli: true, 76 goroutinesDumpStopChExists: true, 77 }, 78 "invalid --log-level specified": { 79 args: []string{"--log-level", "test"}, 80 expectedError: "failed to parse log level", 81 }, 82 "--log-format specified": { 83 args: []string{"--log-format", "json"}, 84 expectedLevel: logrus.InfoLevel, 85 expectedFormatter: new(logrus.JSONFormatter), 86 expectedFormatSetWithCli: true, 87 }, 88 "invalid --log-format specified": { 89 args: []string{"--log-format", "test"}, 90 expectedError: "unknown log format", 91 }, 92 } 93 94 for name, testCase := range tests { 95 t.Run(name, func(t *testing.T) { 96 logger, _ := test.NewNullLogger() 97 98 defer prepareFakeConfiguration(logger)() 99 defer helpers.MakeFatalToPanic()() 100 101 testFunc := func() { 102 testCommandRun(testCase.args...) 103 if testCase.expectedError == "" { 104 assert.Equal(t, testCase.expectedLevel, Configuration().level) 105 assert.Equal(t, testCase.expectedFormatter, Configuration().format) 106 assert.Equal(t, testCase.expectedLevelSetWithCli, Configuration().IsLevelSetWithCli()) 107 assert.Equal(t, testCase.expectedFormatSetWithCli, Configuration().IsFormatSetWithCli()) 108 109 if testCase.goroutinesDumpStopChExists { 110 assert.NotNil(t, Configuration().goroutinesDumpStopCh) 111 } else { 112 assert.Nil(t, Configuration().goroutinesDumpStopCh) 113 } 114 } 115 } 116 117 if testCase.expectedError != "" { 118 var message *logrus.Entry 119 var ok bool 120 121 func() { 122 defer func() { 123 message, ok = recover().(*logrus.Entry) 124 }() 125 126 testFunc() 127 }() 128 129 require.True(t, ok) 130 131 panicMessage, err := message.String() 132 require.NoError(t, err) 133 134 assert.Contains(t, panicMessage, "Error while setting up logging configuration") 135 assert.Contains(t, panicMessage, testCase.expectedError) 136 137 } else { 138 assert.NotPanics(t, testFunc) 139 } 140 }) 141 } 142 } 143 144 func TestGoroutinesDumpDisabling(t *testing.T) { 145 logger, _ := test.NewNullLogger() 146 147 config := NewConfig(logger) 148 config.level = logrus.DebugLevel 149 config.ReloadConfiguration() 150 config.ReloadConfiguration() 151 152 assert.NotNil(t, config.goroutinesDumpStopCh) 153 154 config.level = logrus.InfoLevel 155 config.ReloadConfiguration() 156 config.ReloadConfiguration() 157 158 assert.Nil(t, config.goroutinesDumpStopCh) 159 }