github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/log/config_test.go (about)

     1  package log_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/pkg/log"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestConfig_Validate(t *testing.T) {
    11  	var tests = []struct {
    12  		Msg            string
    13  		ConfigProvider func() *log.Config
    14  		ExpectValid    bool
    15  	}{
    16  		{
    17  			Msg: "Default config should be valid",
    18  			ConfigProvider: func() *log.Config {
    19  				return log.DefaultConfig()
    20  			},
    21  			ExpectValid: true,
    22  		},
    23  		{
    24  			Msg: "Should return error on invalid log level",
    25  			ConfigProvider: func() *log.Config {
    26  				config := log.DefaultConfig()
    27  				config.Level = "invalid"
    28  				return config
    29  			},
    30  		},
    31  		{
    32  			Msg: "Missing Log Format should be invalid",
    33  			ConfigProvider: func() *log.Config {
    34  				config := log.DefaultConfig()
    35  				config.Format = ""
    36  				return config
    37  			},
    38  		},
    39  		{
    40  			Msg: "Should return error on unsupported log format",
    41  			ConfigProvider: func() *log.Config {
    42  				config := log.DefaultConfig()
    43  				config.Format = "invalid"
    44  				return config
    45  			},
    46  		},
    47  		{
    48  			Msg: "Missing Output should be invalid",
    49  			ConfigProvider: func() *log.Config {
    50  				config := log.DefaultConfig()
    51  				config.Output = ""
    52  				return config
    53  			},
    54  		},
    55  		{
    56  			Msg: "Should return error on unsupported output",
    57  			ConfigProvider: func() *log.Config {
    58  				config := log.DefaultConfig()
    59  				config.Output = "invalid"
    60  				return config
    61  			},
    62  		},
    63  		{
    64  			Msg: "Missing Bootstrap Correlation ID should be invalid",
    65  			ConfigProvider: func() *log.Config {
    66  				config := log.DefaultConfig()
    67  				config.BootstrapCorrelationID = ""
    68  				return config
    69  			},
    70  		},
    71  	}
    72  
    73  	for _, test := range tests {
    74  		t.Run(test.Msg, func(t *testing.T) {
    75  			err := test.ConfigProvider().Validate()
    76  			if test.ExpectValid {
    77  				require.NoError(t, err)
    78  			} else {
    79  				require.Error(t, err)
    80  			}
    81  		})
    82  	}
    83  }