@@ -2,8 +2,7 @@ package scalers
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
- "strconv"
5
+ "fmt"
7
6
"testing"
8
7
9
8
"github.com/go-logr/logr"
@@ -15,12 +14,10 @@ import (
15
14
)
16
15
17
16
type parseLiiklusMetadataTestData struct {
18
- metadata map [string ]string
19
- err error
20
- liiklusAddress string
21
- group string
22
- topic string
23
- threshold int64
17
+ name string
18
+ metadata map [string ]string
19
+ ExpectedErr error
20
+ ExpectedMetatada * liiklusMetadata
24
21
}
25
22
26
23
type liiklusMetricIdentifier struct {
@@ -30,12 +27,64 @@ type liiklusMetricIdentifier struct {
30
27
}
31
28
32
29
var parseLiiklusMetadataTestDataset = []parseLiiklusMetadataTestData {
33
- {map [string ]string {}, ErrLiiklusNoTopic , "" , "" , "" , 0 },
34
- {map [string ]string {"topic" : "foo" }, ErrLiiklusNoAddress , "" , "" , "" , 0 },
35
- {map [string ]string {"topic" : "foo" , "address" : "bar:6565" }, ErrLiiklusNoGroup , "" , "" , "" , 0 },
36
- {map [string ]string {"topic" : "foo" , "address" : "bar:6565" , "group" : "mygroup" }, nil , "bar:6565" , "mygroup" , "foo" , 10 },
37
- {map [string ]string {"topic" : "foo" , "address" : "bar:6565" , "group" : "mygroup" , "activationLagThreshold" : "aa" }, strconv .ErrSyntax , "bar:6565" , "mygroup" , "foo" , 10 },
38
- {map [string ]string {"topic" : "foo" , "address" : "bar:6565" , "group" : "mygroup" , "lagThreshold" : "15" }, nil , "bar:6565" , "mygroup" , "foo" , 15 },
30
+ {
31
+ name : "Empty metadata" ,
32
+ metadata : map [string ]string {},
33
+ ExpectedErr : fmt .Errorf ("error parsing liiklus metadata: " +
34
+ "missing required parameter \" address\" in [triggerMetadata]\n " +
35
+ "missing required parameter \" topic\" in [triggerMetadata]\n " +
36
+ "missing required parameter \" group\" in [triggerMetadata]" ),
37
+ ExpectedMetatada : nil ,
38
+ },
39
+ {
40
+ name : "Empty address" ,
41
+ metadata : map [string ]string {"topic" : "foo" },
42
+ ExpectedErr : fmt .Errorf ("error parsing liiklus metadata: " +
43
+ "missing required parameter \" address\" in [triggerMetadata]\n " +
44
+ "missing required parameter \" group\" in [triggerMetadata]" ),
45
+ ExpectedMetatada : nil ,
46
+ },
47
+ {
48
+ name : "Empty group" ,
49
+ metadata : map [string ]string {"topic" : "foo" , "address" : "using-mock" },
50
+ ExpectedErr : fmt .Errorf ("error parsing liiklus metadata: " +
51
+ "missing required parameter \" group\" in [triggerMetadata]" ),
52
+ ExpectedMetatada : nil ,
53
+ },
54
+ {
55
+ name : "Valid" ,
56
+ metadata : map [string ]string {"topic" : "foo" , "address" : "using-mock" , "group" : "mygroup" },
57
+ ExpectedErr : nil ,
58
+ ExpectedMetatada : & liiklusMetadata {
59
+ LagThreshold : defaultLiiklusLagThreshold ,
60
+ ActivationLagThreshold : defaultLiiklusActivationLagThreshold ,
61
+ Address : "using-mock" ,
62
+ Topic : "foo" ,
63
+ Group : "mygroup" ,
64
+ GroupVersion : 0 ,
65
+ triggerIndex : 0 ,
66
+ },
67
+ },
68
+ {
69
+ name : "Invalid activationLagThreshold" ,
70
+ metadata : map [string ]string {"topic" : "foo" , "address" : "using-mock" , "group" : "mygroup" , "activationLagThreshold" : "invalid" },
71
+ ExpectedErr : fmt .Errorf ("error parsing liiklus metadata: unable to set param \" activationLagThreshold\" value \" invalid\" : unable to unmarshal to field type int64: invalid character 'i' looking for beginning of value" ),
72
+ ExpectedMetatada : nil ,
73
+ },
74
+ {
75
+ name : "Custom lagThreshold" ,
76
+ metadata : map [string ]string {"topic" : "foo" , "address" : "using-mock" , "group" : "mygroup" , "lagThreshold" : "20" },
77
+ ExpectedErr : nil ,
78
+ ExpectedMetatada : & liiklusMetadata {
79
+ LagThreshold : 20 ,
80
+ ActivationLagThreshold : defaultLiiklusActivationLagThreshold ,
81
+ Address : "using-mock" ,
82
+ Topic : "foo" ,
83
+ Group : "mygroup" ,
84
+ GroupVersion : 0 ,
85
+ triggerIndex : 0 ,
86
+ },
87
+ },
39
88
}
40
89
41
90
var liiklusMetricIdentifiers = []liiklusMetricIdentifier {
@@ -45,38 +94,44 @@ var liiklusMetricIdentifiers = []liiklusMetricIdentifier{
45
94
46
95
func TestLiiklusParseMetadata (t * testing.T ) {
47
96
for _ , testData := range parseLiiklusMetadataTestDataset {
48
- meta , err := parseLiiklusMetadata (& scalersconfig.ScalerConfig {TriggerMetadata : testData .metadata })
49
- if err != nil && testData .err == nil {
50
- t .Error ("Expected success but got error" , err )
51
- continue
52
- }
53
- if testData .err != nil && err == nil {
54
- t .Error ("Expected error but got success" )
55
- continue
56
- }
57
- if testData .err != nil && err != nil && ! errors .Is (err , testData .err ) {
58
- t .Errorf ("Expected error %v but got %v" , testData .err , err )
59
- continue
60
- }
61
- if err != nil {
62
- continue
63
- }
64
- if testData .liiklusAddress != meta .address {
65
- t .Errorf ("Expected address %q but got %q\n " , testData .liiklusAddress , meta .address )
66
- continue
67
- }
68
- if meta .group != testData .group {
69
- t .Errorf ("Expected group %q but got %q\n " , testData .group , meta .group )
70
- continue
71
- }
72
- if meta .topic != testData .topic {
73
- t .Errorf ("Expected topic %q but got %q\n " , testData .topic , meta .topic )
74
- continue
75
- }
76
- if meta .lagThreshold != testData .threshold {
77
- t .Errorf ("Expected threshold %d but got %d\n " , testData .threshold , meta .lagThreshold )
78
- continue
79
- }
97
+ t .Run (testData .name , func (t * testing.T ) {
98
+ meta , err := parseLiiklusMetadata (& scalersconfig.ScalerConfig {TriggerMetadata : testData .metadata })
99
+
100
+ // error cases
101
+ if testData .ExpectedErr != nil {
102
+ if err == nil {
103
+ t .Errorf ("Expected error %v but got success" , testData .ExpectedErr )
104
+ } else if err .Error () != testData .ExpectedErr .Error () {
105
+ t .Errorf ("Expected error %v but got %v" , testData .ExpectedErr , err )
106
+ }
107
+ return // Skip the rest of the checks for error cases
108
+ }
109
+
110
+ // success cases
111
+ if err != nil {
112
+ t .Errorf ("Expected success but got error %v" , err )
113
+ }
114
+ if testData .ExpectedMetatada != nil {
115
+ if testData .ExpectedMetatada .Address != meta .Address {
116
+ t .Errorf ("Expected address %q but got %q" , testData .ExpectedMetatada .Address , meta .Address )
117
+ }
118
+ if meta .Group != testData .ExpectedMetatada .Group {
119
+ t .Errorf ("Expected group %q but got %q" , testData .ExpectedMetatada .Group , meta .Group )
120
+ }
121
+ if meta .Topic != testData .ExpectedMetatada .Topic {
122
+ t .Errorf ("Expected topic %q but got %q" , testData .ExpectedMetatada .Topic , meta .Topic )
123
+ }
124
+ if meta .LagThreshold != testData .ExpectedMetatada .LagThreshold {
125
+ t .Errorf ("Expected threshold %d but got %d" , testData .ExpectedMetatada .LagThreshold , meta .LagThreshold )
126
+ }
127
+ if meta .ActivationLagThreshold != testData .ExpectedMetatada .ActivationLagThreshold {
128
+ t .Errorf ("Expected activation threshold %d but got %d" , testData .ExpectedMetatada .ActivationLagThreshold , meta .ActivationLagThreshold )
129
+ }
130
+ if meta .GroupVersion != testData .ExpectedMetatada .GroupVersion {
131
+ t .Errorf ("Expected group version %d but got %d" , testData .ExpectedMetatada .GroupVersion , meta .GroupVersion )
132
+ }
133
+ }
134
+ })
80
135
}
81
136
}
82
137
@@ -172,16 +227,17 @@ func TestLiiklusScalerGetMetricsBehavior(t *testing.T) {
172
227
173
228
func TestLiiklusGetMetricSpecForScaling (t * testing.T ) {
174
229
for _ , testData := range liiklusMetricIdentifiers {
175
- meta , err := parseLiiklusMetadata (& scalersconfig.ScalerConfig {TriggerMetadata : testData .metadataTestData .metadata , TriggerIndex : testData .triggerIndex })
176
- if err != nil {
177
- t .Fatal ("Could not parse metadata:" , err )
178
- }
179
- mockLiiklusScaler := liiklusScaler {"" , meta , nil , nil , logr .Discard ()}
180
-
181
- metricSpec := mockLiiklusScaler .GetMetricSpecForScaling (context .Background ())
182
- metricName := metricSpec [0 ].External .Metric .Name
183
- if metricName != testData .name {
184
- t .Error ("Wrong External metric source name:" , metricName )
185
- }
230
+ t .Run (testData .name , func (t * testing.T ) {
231
+ meta , err := parseLiiklusMetadata (& scalersconfig.ScalerConfig {TriggerMetadata : testData .metadataTestData .metadata })
232
+ if err != nil {
233
+ t .Fatal ("Could not parse metadata:" , err )
234
+ }
235
+ meta .triggerIndex = testData .triggerIndex
236
+ mockLiiklusScaler := liiklusScaler {"" , meta , nil , nil , logr .Discard ()}
237
+ metricSpec := mockLiiklusScaler .GetMetricSpecForScaling (context .Background ())
238
+ if metricSpec [0 ].External .Metric .Name != testData .name {
239
+ t .Errorf ("Wrong External metric source name: %s" , metricSpec [0 ].External .Metric .Name )
240
+ }
241
+ })
186
242
}
187
243
}
0 commit comments