@@ -21,69 +21,61 @@ import (
21
21
"testing"
22
22
23
23
bundlev1 "github.com/elastic/harp/api/gen/go/harp/bundle/v1"
24
- "github.com/golang/protobuf/proto"
24
+
25
+ "github.com/stretchr/testify/assert"
25
26
)
26
27
27
28
func TestFromBundle (t * testing.T ) {
28
- type args struct {
29
- b * bundlev1.Bundle
30
- }
31
29
tests := []struct {
32
30
name string
33
- args args
31
+ bundle * bundlev1. Bundle
34
32
want * bundlev1.RuleSet
35
33
wantErr bool
36
34
}{
37
35
{
38
- name : "nil" ,
39
- args : args {
40
- b : nil ,
41
- },
36
+ name : "nil" ,
37
+ bundle : & bundlev1.Bundle {},
42
38
want : nil ,
43
39
wantErr : true ,
44
40
},
45
41
{
46
- name : "packages are nil" ,
47
- args : args {
48
- b : & bundlev1.Bundle {
49
- Labels : map [string ]string {
50
- "test" : "true" ,
51
- },
52
- Annotations : map [string ]string {
53
- "harp.elastic.co/v1/testing#bundlePurpose" : "test" ,
54
- },
55
- Packages : nil ,
42
+ name : "empty packages" ,
43
+ bundle : & bundlev1.Bundle {
44
+ Labels : map [string ]string {
45
+ "test" : "true" ,
56
46
},
47
+ Annotations : map [string ]string {
48
+ "harp.elastic.co/v1/testing#bundlePurpose" : "test" ,
49
+ },
50
+ Packages : nil ,
57
51
},
58
52
want : nil ,
59
53
wantErr : true ,
60
54
},
61
55
{
62
56
name : "secrets are nil" ,
63
- args : args {
64
- b : & bundlev1.Bundle {
65
- Labels : map [string ]string {
66
- "test" : "true" ,
67
- },
68
- Annotations : map [string ]string {
69
- "harp.elastic.co/v1/testing#bundlePurpose" : "test" ,
70
- },
71
- Packages : []* bundlev1.Package {
72
- {
73
- Labels : map [string ]string {
74
- "external" : "true" ,
75
- },
76
- Annotations : map [string ]string {
77
- "infosec.elastic.co/v1/SecretPolicy#rotationMethod" : "ci" ,
78
- "infosec.elastic.co/v1/SecretPolicy#rotationPeriod" : "90d" ,
79
- "infosec.elastic.co/v1/SecretPolicy#serviceType" : "authentication" ,
80
- "infosec.elastic.co/v1/SecretPolicy#severity" : "high" ,
81
- "infra.elastic.co/v1/CI#jobName" : "rotate-external-api-key" ,
82
- "harp.elastic.co/v1/package#encryptionKeyAlias" : "test" ,
83
- },
84
- Name : "app/production/testAccount/testService/v1.0.0/internalTestComponent/authentication/api_key" ,
85
- Secrets : nil ,
57
+ bundle : & bundlev1.Bundle {
58
+ Labels : map [string ]string {
59
+ "test" : "true" ,
60
+ },
61
+ Annotations : map [string ]string {
62
+ "harp.elastic.co/v1/testing#bundlePurpose" : "test" ,
63
+ },
64
+ Packages : []* bundlev1.Package {
65
+ {
66
+ Labels : map [string ]string {
67
+ "external" : "true" ,
86
68
},
69
+ Annotations : map [string ]string {
70
+ "infosec.elastic.co/v1/SecretPolicy#rotationMethod" : "ci" ,
71
+ "infosec.elastic.co/v1/SecretPolicy#rotationPeriod" : "90d" ,
72
+ "infosec.elastic.co/v1/SecretPolicy#serviceType" : "authentication" ,
73
+ "infosec.elastic.co/v1/SecretPolicy#severity" : "high" ,
74
+ "infra.elastic.co/v1/CI#jobName" : "rotate-external-api-key" ,
75
+ "harp.elastic.co/v1/package#encryptionKeyAlias" : "test" ,
76
+ },
77
+ Name : "app/production/testAccount/testService/v1.0.0/internalTestComponent/authentication/api_key" ,
78
+ Secrets : nil ,
87
79
},
88
80
},
89
81
},
@@ -101,31 +93,29 @@ func TestFromBundle(t *testing.T) {
101
93
},
102
94
{
103
95
name : "secret data is nil" ,
104
- args : args {
105
- b : & bundlev1.Bundle {
106
- Labels : map [string ]string {
107
- "test" : "true" ,
108
- },
109
- Annotations : map [string ]string {
110
- "harp.elastic.co/v1/testing#bundlePurpose" : "test" ,
111
- },
112
- Packages : []* bundlev1.Package {
113
- {
114
- Labels : map [string ]string {
115
- "external" : "true" ,
116
- },
117
- Annotations : map [string ]string {
118
- "infosec.elastic.co/v1/SecretPolicy#rotationMethod" : "ci" ,
119
- "infosec.elastic.co/v1/SecretPolicy#rotationPeriod" : "90d" ,
120
- "infosec.elastic.co/v1/SecretPolicy#serviceType" : "authentication" ,
121
- "infosec.elastic.co/v1/SecretPolicy#severity" : "high" ,
122
- "infra.elastic.co/v1/CI#jobName" : "rotate-external-api-key" ,
123
- "harp.elastic.co/v1/package#encryptionKeyAlias" : "test" ,
124
- },
125
- Name : "app/production/testAccount/testService/v1.0.0/internalTestComponent/authentication/api_key" ,
126
- Secrets : & bundlev1.SecretChain {
127
- Data : nil ,
128
- },
96
+ bundle : & bundlev1.Bundle {
97
+ Labels : map [string ]string {
98
+ "test" : "true" ,
99
+ },
100
+ Annotations : map [string ]string {
101
+ "harp.elastic.co/v1/testing#bundlePurpose" : "test" ,
102
+ },
103
+ Packages : []* bundlev1.Package {
104
+ {
105
+ Labels : map [string ]string {
106
+ "external" : "true" ,
107
+ },
108
+ Annotations : map [string ]string {
109
+ "infosec.elastic.co/v1/SecretPolicy#rotationMethod" : "ci" ,
110
+ "infosec.elastic.co/v1/SecretPolicy#rotationPeriod" : "90d" ,
111
+ "infosec.elastic.co/v1/SecretPolicy#serviceType" : "authentication" ,
112
+ "infosec.elastic.co/v1/SecretPolicy#severity" : "high" ,
113
+ "infra.elastic.co/v1/CI#jobName" : "rotate-external-api-key" ,
114
+ "harp.elastic.co/v1/package#encryptionKeyAlias" : "test" ,
115
+ },
116
+ Name : "app/production/testAccount/testService/v1.0.0/internalTestComponent/authentication/api_key" ,
117
+ Secrets : & bundlev1.SecretChain {
118
+ Data : nil ,
129
119
},
130
120
},
131
121
},
@@ -144,38 +134,36 @@ func TestFromBundle(t *testing.T) {
144
134
},
145
135
{
146
136
name : "package and secrets define with annotations and labels" ,
147
- args : args {
148
- b : & bundlev1.Bundle {
149
- Labels : map [string ]string {
150
- "test" : "true" ,
151
- },
152
- Annotations : map [string ]string {
153
- "harp.elastic.co/v1/testing#bundlePurpose" : "test" ,
154
- },
155
- Packages : []* bundlev1.Package {
156
- {
137
+ bundle : & bundlev1.Bundle {
138
+ Labels : map [string ]string {
139
+ "test" : "true" ,
140
+ },
141
+ Annotations : map [string ]string {
142
+ "harp.elastic.co/v1/testing#bundlePurpose" : "test" ,
143
+ },
144
+ Packages : []* bundlev1.Package {
145
+ {
146
+ Labels : map [string ]string {
147
+ "external" : "true" ,
148
+ },
149
+ Annotations : map [string ]string {
150
+ "harp.elastic.co/v1/package#encryptionKeyAlias" : "test" ,
151
+ "infra.elastic.co/v1/CI#jobName" : "rotate-external-api-key" ,
152
+ "infosec.elastic.co/v1/SecretPolicy#rotationMethod" : "ci" ,
153
+ "infosec.elastic.co/v1/SecretPolicy#rotationPeriod" : "90d" ,
154
+ "infosec.elastic.co/v1/SecretPolicy#serviceType" : "authentication" ,
155
+ "infosec.elastic.co/v1/SecretPolicy#severity" : "high" ,
156
+ },
157
+ Name : "app/production/testAccount/testService/v1.0.0/internalTestComponent/authentication/api_key" ,
158
+ Secrets : & bundlev1.SecretChain {
157
159
Labels : map [string ]string {
158
- "external" : "true" ,
159
- },
160
- Annotations : map [string ]string {
161
- "harp.elastic.co/v1/package#encryptionKeyAlias" : "test" ,
162
- "infra.elastic.co/v1/CI#jobName" : "rotate-external-api-key" ,
163
- "infosec.elastic.co/v1/SecretPolicy#rotationMethod" : "ci" ,
164
- "infosec.elastic.co/v1/SecretPolicy#rotationPeriod" : "90d" ,
165
- "infosec.elastic.co/v1/SecretPolicy#serviceType" : "authentication" ,
166
- "infosec.elastic.co/v1/SecretPolicy#severity" : "high" ,
160
+ "vendor" : "true" ,
167
161
},
168
- Name : "app/production/testAccount/testService/v1.0.0/internalTestComponent/authentication/api_key" ,
169
- Secrets : & bundlev1.SecretChain {
170
- Labels : map [string ]string {
171
- "vendor" : "true" ,
172
- },
173
- Data : []* bundlev1.KV {
174
- {
175
- Key : "API_KEY" ,
176
- Type : "string" ,
177
- Value : []byte ("3YGVuHwUqYVkjk-c6lQgfVQwFHawPG36TgAm72sPZGE=" ),
178
- },
162
+ Data : []* bundlev1.KV {
163
+ {
164
+ Key : "API_KEY" ,
165
+ Type : "string" ,
166
+ Value : []byte ("3YGVuHwUqYVkjk-c6lQgfVQwFHawPG36TgAm72sPZGE=" ),
179
167
},
180
168
},
181
169
},
@@ -213,13 +201,23 @@ func TestFromBundle(t *testing.T) {
213
201
}
214
202
for _ , tt := range tests {
215
203
t .Run (tt .name , func (t * testing.T ) {
216
- got , err := FromBundle (tt .args .b )
217
- if (err != nil ) != tt .wantErr {
218
- t .Errorf ("error = %v, wantErr %v" , err , tt .wantErr )
219
- }
204
+ got , err := FromBundle (tt .bundle )
205
+ if tt .wantErr {
206
+ assert .Error (t , err )
207
+ } else {
208
+ assert .NoError (t , err )
209
+ assert .Equal (t , tt .want .ApiVersion , got .ApiVersion )
210
+ assert .Equal (t , tt .want .Kind , got .Kind )
211
+ assert .Equal (t , tt .want .Meta , got .Meta )
212
+ assert .Equal (t , len (tt .want .Spec .Rules ), len (got .Spec .Rules ))
220
213
221
- if ! proto .Equal (got , tt .want ) {
222
- t .Errorf ("Ruleset not equal = %v, want %v" , got , tt .want )
214
+ for idx , expectedRule := range tt .want .Spec .Rules {
215
+ gotRule := got .Spec .Rules [idx ]
216
+ assert .Equal (t , expectedRule .Name , gotRule .Name )
217
+ assert .Equal (t , expectedRule .Path , gotRule .Path )
218
+ assert .Equal (t , len (expectedRule .Constraints ), len (gotRule .Constraints ))
219
+ assert .ElementsMatch (t , expectedRule .GetConstraints (), gotRule .GetConstraints ())
220
+ }
223
221
}
224
222
})
225
223
}
0 commit comments