github.com/Axway/agent-sdk@v1.1.101/pkg/config/logconfig_test.go (about)

     1  package config
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/Axway/agent-sdk/pkg/cmd/properties"
     7  	"github.com/Axway/agent-sdk/pkg/util/log"
     8  	"github.com/spf13/cobra"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  const (
    13  	defLevel          = "info"
    14  	defFormat         = "json"
    15  	defOutput         = "stdout"
    16  	defMaskedVals     = ""
    17  	defPath           = "logs"
    18  	defMaxSize        = 10485760
    19  	defMaxAge         = 0
    20  	defMaxFiles       = 7
    21  	defMetricName     = "metrics.log"
    22  	defMetricMaxSize  = 10485760
    23  	defMetricMaxAge   = 0
    24  	defMetricMaxFiles = 0
    25  	defUsageName      = "usage.log"
    26  	defUsageMaxSize   = 10485760
    27  	defUsageMaxAge    = 365
    28  	defUsageMaxFiles  = 0
    29  )
    30  
    31  func TestDefaultLogConfig(t *testing.T) {
    32  	testCases := map[string]struct {
    33  		logName   string
    34  		agentType AgentType
    35  	}{
    36  		"default discovery agent configuration": {
    37  			agentType: DiscoveryAgent,
    38  			logName:   "discovery_agent.log",
    39  		},
    40  		"default traceability agent configuration": {
    41  			agentType: TraceabilityAgent,
    42  			logName:   "traceability_agent.log",
    43  		},
    44  	}
    45  	for name, tc := range testCases {
    46  		t.Run(name, func(t *testing.T) {
    47  			rootCmd := &cobra.Command{}
    48  			props := properties.NewProperties(rootCmd)
    49  			AddLogConfigProperties(props, tc.logName)
    50  			AddMetricLogConfigProperties(props, tc.agentType)
    51  			AddUsageConfigProperties(props, tc.agentType)
    52  
    53  			// Parse config
    54  			_, err := ParseAndSetupLogConfig(props, tc.agentType)
    55  			assert.Nil(t, err, "Expected no error with default values")
    56  
    57  			// Validate the default
    58  			assert.Equal(t, defLevel, props.StringPropertyValue(pathLogLevel))
    59  			assert.Equal(t, defFormat, props.StringPropertyValue(pathLogFormat))
    60  			assert.Equal(t, defOutput, props.StringPropertyValue(pathLogOutput))
    61  			assert.Equal(t, defMaskedVals, props.StringPropertyValue(pathLogMaskedValues))
    62  			assert.Equal(t, tc.logName, props.StringPropertyValue(pathLogFileName))
    63  			assert.Equal(t, defPath, props.StringPropertyValue(pathLogFilePath))
    64  			assert.Equal(t, defMaxSize, props.IntPropertyValue(pathLogFileMaxSize))
    65  			assert.Equal(t, defMaxAge, props.IntPropertyValue(pathLogFileMaxAge))
    66  			assert.Equal(t, defMaxFiles, props.IntPropertyValue(pathLogFileMaxBackups))
    67  
    68  			if tc.agentType == TraceabilityAgent {
    69  				assert.Equal(t, defMetricName, props.StringPropertyValue(pathLogMetricsFileName))
    70  				assert.Equal(t, defMetricMaxSize, props.IntPropertyValue(pathLogMetricsFileMaxSize))
    71  				assert.Equal(t, defMetricMaxAge, props.IntPropertyValue(pathLogMetricsFileMaxAge))
    72  				assert.Equal(t, defMetricMaxFiles, props.IntPropertyValue(pathLogMetricsFileMaxBackups))
    73  
    74  				assert.Equal(t, defUsageName, props.StringPropertyValue(pathLogUsageFileName))
    75  				assert.Equal(t, defUsageMaxSize, props.IntPropertyValue(pathLogUsageFileMaxSize))
    76  				assert.Equal(t, defUsageMaxAge, props.IntPropertyValue(pathLogUsageFileMaxAge))
    77  				assert.Equal(t, defUsageMaxFiles, props.IntPropertyValue(pathLogUsageFileMaxBackups))
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func TestLogConfigValidations(t *testing.T) {
    84  	testCases := map[string]struct {
    85  		errInfo          string
    86  		agentType        AgentType
    87  		metricsEnabled   bool
    88  		usageEnabled     bool
    89  		level            string
    90  		format           string
    91  		output           string
    92  		maxSize          int
    93  		maxBackups       int
    94  		maxAge           int
    95  		metricMaxSize    int
    96  		metricMaxBackups int
    97  		metricMaxAge     int
    98  		usageMaxSize     int
    99  		usageMaxBackups  int
   100  		usageMaxAge      int
   101  	}{
   102  		"expect err, bad log level": {
   103  			errInfo: "log.level",
   104  			level:   "debug1",
   105  		},
   106  		"expect err, bad log format": {
   107  			errInfo: "log.format",
   108  			format:  "line1",
   109  		},
   110  		"expect err, bad log output": {
   111  			errInfo: "log.output",
   112  			output:  "unknown",
   113  		},
   114  		"expect err, bad max log size": {
   115  			errInfo: "log.file.rotateeverybytes",
   116  			maxSize: 1,
   117  		},
   118  		"expect err, bad max log backups": {
   119  			errInfo:    "log.file.keepfiles",
   120  			maxBackups: -1,
   121  		},
   122  		"expect err, bad max log age": {
   123  			errInfo: "log.file.cleanbackupsevery",
   124  			maxAge:  -1,
   125  		},
   126  		"success": {
   127  			metricsEnabled: true,
   128  			usageEnabled:   true,
   129  		},
   130  		"expect err, traceability agent, bad metric log size": {
   131  			agentType:      TraceabilityAgent,
   132  			metricsEnabled: true,
   133  			errInfo:        "log.metricfile.rotateeverybytes",
   134  			metricMaxSize:  1,
   135  		},
   136  		"expect err, traceability agent, bad metric log backups": {
   137  			agentType:        TraceabilityAgent,
   138  			metricsEnabled:   true,
   139  			errInfo:          "log.metricfile.keepfiles",
   140  			metricMaxBackups: -1,
   141  		},
   142  		"expect err, traceability agent, bad metric log age": {
   143  			agentType:      TraceabilityAgent,
   144  			metricsEnabled: true,
   145  			errInfo:        "log.metricfile.cleanbackupsevery",
   146  			metricMaxAge:   -1,
   147  		},
   148  		"expect err, traceability agent, bad usage log size": {
   149  			agentType:      TraceabilityAgent,
   150  			metricsEnabled: true,
   151  			usageEnabled:   true,
   152  			errInfo:        "log.usagefile.rotateeverybytes",
   153  			usageMaxSize:   1,
   154  		},
   155  		"expect err, traceability agent, bad usage log backups": {
   156  			agentType:       TraceabilityAgent,
   157  			usageEnabled:    true,
   158  			metricsEnabled:  true,
   159  			errInfo:         "log.usagefile.keepfiles",
   160  			usageMaxBackups: -1,
   161  		},
   162  		"expect err, traceability agent, bad usage log age": {
   163  			agentType:      TraceabilityAgent,
   164  			usageEnabled:   true,
   165  			metricsEnabled: true,
   166  			errInfo:        "log.usagefile.cleanbackupsevery",
   167  			usageMaxAge:    -1,
   168  		},
   169  	}
   170  	for name, tc := range testCases {
   171  		t.Run(name, func(t *testing.T) {
   172  			if tc.agentType == 0 {
   173  				tc.agentType = DiscoveryAgent
   174  			}
   175  			if tc.level == "" {
   176  				tc.level = defLevel
   177  			}
   178  			if tc.format == "" {
   179  				tc.format = defFormat
   180  			}
   181  			if tc.output == "" {
   182  				tc.output = defOutput
   183  			}
   184  			if tc.maxSize == 0 {
   185  				tc.maxSize = defMaxSize
   186  			}
   187  			if tc.maxBackups == 0 {
   188  				tc.maxBackups = defMaxFiles
   189  			}
   190  			if tc.maxAge == 0 {
   191  				tc.maxAge = defMaxAge
   192  			}
   193  			if tc.metricMaxSize == 0 {
   194  				tc.metricMaxSize = defMetricMaxSize
   195  			}
   196  			if tc.metricMaxBackups == 0 {
   197  				tc.metricMaxBackups = defMetricMaxFiles
   198  			}
   199  			if tc.metricMaxAge == 0 {
   200  				tc.metricMaxAge = defMetricMaxAge
   201  			}
   202  			if tc.usageMaxSize == 0 {
   203  				tc.usageMaxSize = defUsageMaxSize
   204  			}
   205  			if tc.usageMaxBackups == 0 {
   206  				tc.usageMaxBackups = defUsageMaxFiles
   207  			}
   208  			if tc.usageMaxAge == 0 {
   209  				tc.usageMaxAge = defUsageMaxAge
   210  			}
   211  
   212  			log.GlobalLoggerConfig = log.LoggerConfig{}
   213  			props := properties.NewProperties(&cobra.Command{})
   214  			props.AddStringProperty(pathLogLevel, tc.level, "")
   215  			props.AddStringProperty(pathLogFormat, tc.format, "")
   216  			props.AddStringProperty(pathLogOutput, tc.output, "")
   217  			props.AddStringProperty(pathLogFileName, "agent.log", "")
   218  			props.AddStringProperty(pathLogFilePath, defPath, "")
   219  			props.AddIntProperty(pathLogFileMaxSize, tc.maxSize, "")
   220  			props.AddIntProperty(pathLogFileMaxBackups, tc.maxBackups, "")
   221  			props.AddIntProperty(pathLogFileMaxAge, tc.maxAge, "")
   222  
   223  			if tc.agentType == TraceabilityAgent && tc.metricsEnabled {
   224  				props.AddBoolProperty(pathLogMetricsFileEnabled, true, "")
   225  				props.AddStringProperty(pathLogMetricsFileName, "metrics.log", "")
   226  				props.AddIntProperty(pathLogMetricsFileMaxSize, tc.metricMaxSize, "")
   227  				props.AddIntProperty(pathLogMetricsFileMaxBackups, tc.metricMaxBackups, "")
   228  				props.AddIntProperty(pathLogMetricsFileMaxAge, tc.metricMaxAge, "")
   229  			}
   230  
   231  			if tc.agentType == TraceabilityAgent && tc.usageEnabled {
   232  				props.AddBoolProperty(pathLogUsageFileEnabled, true, "")
   233  				props.AddStringProperty(pathLogUsageFileName, "usage.log", "")
   234  				props.AddIntProperty(pathLogUsageFileMaxSize, tc.usageMaxSize, "")
   235  				props.AddIntProperty(pathLogUsageFileMaxBackups, tc.usageMaxBackups, "")
   236  				props.AddIntProperty(pathLogUsageFileMaxAge, tc.usageMaxAge, "")
   237  			}
   238  			_, err := ParseAndSetupLogConfig(props, tc.agentType)
   239  			if tc.errInfo != "" {
   240  				if !assert.NotNil(t, err) {
   241  					return
   242  				}
   243  				assert.Contains(t, err.Error(), tc.errInfo)
   244  				return
   245  			}
   246  			assert.Nil(t, err)
   247  		})
   248  	}
   249  }