github.com/Axway/agent-sdk@v1.1.101/pkg/config/metricreportingconfig_test.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "os" 6 "strconv" 7 "testing" 8 "time" 9 10 "github.com/Axway/agent-sdk/pkg/cmd/properties" 11 "github.com/Axway/agent-sdk/pkg/util/exception" 12 "github.com/spf13/cobra" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func validateMetricReporting(cfg MetricReportingConfig) (err error) { 17 exception.Block{ 18 Try: func() { 19 cfg.Validate() 20 }, 21 Catch: func(e error) { 22 err = e 23 }, 24 }.Do() 25 return 26 } 27 28 type expectedMetricConfig struct { 29 publish bool 30 schedule string 31 granularity int 32 qaVars bool 33 } 34 35 var defaultMetricConfigExpected = expectedMetricConfig{ 36 publish: true, 37 schedule: "@hourly", 38 granularity: int(time.Hour) * 1, 39 qaVars: false, 40 } 41 42 func validateMetricConfig(t *testing.T, expVals expectedMetricConfig, cfg MetricReportingConfig) { 43 assert.Equal(t, expVals.publish, cfg.CanPublish()) 44 assert.Equal(t, expVals.schedule, cfg.GetSchedule()) 45 assert.Equal(t, expVals.qaVars, cfg.UsingQAVars()) 46 } 47 48 func TestMetricReportingConfigEnvVarMigration(t *testing.T) { 49 rootCmd := &cobra.Command{ 50 Use: "test", 51 } 52 53 props := properties.NewProperties(rootCmd) 54 AddMetricReportingProperties(props) 55 56 // Test Interval old env vars 57 os.Setenv(oldUsageReportingIntervalEnvVar, "30m") 58 expected := defaultMetricConfigExpected 59 expected.schedule = "*/30 * * * *" 60 expected.granularity = int((30 * time.Minute).Milliseconds()) 61 62 cfg := ParseMetricReportingConfig(props) 63 assert.NotNil(t, cfg) 64 err := validateMetricReporting(cfg) 65 assert.Nil(t, err) 66 validateMetricConfig(t, expected, cfg) 67 68 // Test Interval in hour 69 os.Setenv(oldUsageReportingIntervalEnvVar, "2h") 70 expected.schedule = fmt.Sprintf("%d 2 * * *", time.Now().Minute()) 71 expected.granularity = int((2 * time.Hour).Milliseconds()) 72 73 cfg = ParseMetricReportingConfig(props) 74 assert.NotNil(t, cfg) 75 err = validateMetricReporting(cfg) 76 77 assert.Nil(t, err) 78 validateMetricConfig(t, expected, cfg) 79 80 // Test Interval new env vars 81 os.Setenv(newMetricReportingScheduleEnvVar, defaultExpected.schedule) 82 83 expected = defaultMetricConfigExpected 84 cfg = ParseMetricReportingConfig(props) 85 assert.NotNil(t, cfg) 86 err = validateMetricReporting(cfg) 87 assert.Nil(t, err) 88 validateMetricConfig(t, expected, cfg) 89 90 // Test Publish old env vars 91 os.Setenv(oldUsageReportingPublishMetricEnvVar, "false") 92 expected = defaultMetricConfigExpected 93 expected.publish = false 94 95 cfg = ParseMetricReportingConfig(props) 96 assert.NotNil(t, cfg) 97 err = validateMetricReporting(cfg) 98 assert.Nil(t, err) 99 validateMetricConfig(t, expected, cfg) 100 101 // Test Publish new env vars 102 os.Setenv(newMetricReportingPublishEnvVar, strconv.FormatBool(defaultExpected.publish)) 103 104 expected = defaultMetricConfigExpected 105 cfg = ParseMetricReportingConfig(props) 106 assert.NotNil(t, cfg) 107 err = validateMetricReporting(cfg) 108 assert.Nil(t, err) 109 validateMetricConfig(t, expected, cfg) 110 } 111 112 func TestMetricReportingConfigProperties(t *testing.T) { 113 rootCmd := &cobra.Command{ 114 Use: "test", 115 } 116 117 props := properties.NewProperties(rootCmd) 118 119 // Test default config 120 AddMetricReportingProperties(props) 121 122 cfg := ParseMetricReportingConfig(props) 123 assert.NotNil(t, cfg) 124 125 err := validateMetricReporting(cfg) 126 assert.Nil(t, err) 127 128 validateMetricConfig(t, defaultMetricConfigExpected, cfg) 129 130 // invalid schedule 131 currentSchedule := cfg.GetSchedule() 132 cfg.(*MetricReportingConfiguration).Schedule = "*/1511 * * * *" 133 err = validateMetricReporting(cfg) 134 assert.NotNil(t, err) 135 cfg.(*MetricReportingConfiguration).Schedule = currentSchedule 136 137 // QA schedule override 138 os.Setenv(qaMetricReportingScheduleEnvVar, "*/1 * * * *") 139 cfg.(*MetricReportingConfiguration).Schedule = "" 140 err = validateMetricReporting(cfg) 141 assert.Nil(t, err) 142 } 143 144 func TestNewMetricReporting(t *testing.T) { 145 cfg := NewMetricReporting() 146 assert.NotNil(t, cfg) 147 validateMetricConfig(t, defaultMetricConfigExpected, cfg) 148 }