@@ -2,13 +2,16 @@ package dataupload_test
2
2
3
3
import (
4
4
"bytes"
5
+ "compress/gzip"
5
6
"crypto/x509"
6
7
"encoding/json"
7
8
"encoding/pem"
8
9
"errors"
9
10
"fmt"
11
+ "io"
10
12
"net/http"
11
13
"os"
14
+ "path/filepath"
12
15
"testing"
13
16
"time"
14
17
@@ -191,7 +194,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI(t *testing.T) {
191
194
cyberArkClient , err := dataupload .NewCyberArkClient (nil , serviceURL , identityClient .AuthenticateRequest )
192
195
require .NoError (t , err )
193
196
194
- dataReadings := loadDataReadings (t , "testdata/example-1/datareadings.json" )
197
+ dataReadings := parseDataReadings (t , readGZIP ( t , "testdata/example-1/datareadings.json.gz" ) )
195
198
err = cyberArkClient .PostDataReadingsWithOptions (
196
199
ctx ,
197
200
api.DataReadingsPost {
@@ -207,15 +210,13 @@ func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI(t *testing.T) {
207
210
require .NoError (t , err )
208
211
}
209
212
210
- func loadDataReadings (t * testing.T , path string ) []* api.DataReading {
213
+ func parseDataReadings (t * testing.T , data [] byte ) []* api.DataReading {
211
214
var dataReadings []* api.DataReading
212
- {
213
- f , err := os .Open (path )
214
- require .NoError (t , err )
215
- err = json .NewDecoder (f ).Decode (& dataReadings )
216
- require .NoError (t , f .Close ())
217
- require .NoError (t , err )
218
- }
215
+
216
+ decoder := json .NewDecoder (bytes .NewReader (data ))
217
+ decoder .DisallowUnknownFields ()
218
+ err := decoder .Decode (& dataReadings )
219
+ require .NoError (t , err )
219
220
220
221
for _ , reading := range dataReadings {
221
222
dataBytes , err := json .Marshal (reading .Data )
@@ -244,8 +245,33 @@ func loadDataReadings(t *testing.T, path string) []*api.DataReading {
244
245
return dataReadings
245
246
}
246
247
248
+ func readGZIP (t * testing.T , path string ) []byte {
249
+ f , err := os .Open (path )
250
+ require .NoError (t , err )
251
+ defer func () { require .NoError (t , f .Close ()) }()
252
+ gzr , err := gzip .NewReader (f )
253
+ require .NoError (t , err )
254
+ defer func () { require .NoError (t , gzr .Close ()) }()
255
+ bytes , err := io .ReadAll (gzr )
256
+ require .NoError (t , err )
257
+ return bytes
258
+ }
259
+
260
+ func writeGZIP (t * testing.T , path string , data []byte ) {
261
+ tmp , err := os .CreateTemp (filepath .Dir (path ), filepath .Base (path )+ ".*" )
262
+ require .NoError (t , err )
263
+ gzw := gzip .NewWriter (tmp )
264
+ _ , err = io .Copy (gzw , bytes .NewReader (data ))
265
+ require .NoError (t , gzw .Flush ())
266
+ require .NoError (t , gzw .Close ())
267
+ require .NoError (t , tmp .Close ())
268
+ require .NoError (t , err )
269
+ err = os .Rename (tmp .Name (), path )
270
+ require .NoError (t , err )
271
+ }
272
+
247
273
func TestConvertDataReadingsToCyberarkSnapshot (t * testing.T ) {
248
- dataReadings := loadDataReadings (t , "testdata/example-1/datareadings.json" )
274
+ dataReadings := parseDataReadings (t , readGZIP ( t , "testdata/example-1/datareadings.json.gz" ) )
249
275
snapshot , err := dataupload .ConvertDataReadingsToCyberarkSnapshot (api.DataReadingsPost {
250
276
AgentMetadata : & api.AgentMetadata {
251
277
Version : "test-version" ,
@@ -258,13 +284,11 @@ func TestConvertDataReadingsToCyberarkSnapshot(t *testing.T) {
258
284
actualSnapshotBytes , err := json .MarshalIndent (snapshot , "" , " " )
259
285
require .NoError (t , err )
260
286
261
- goldenFilePath := "testdata/example-1/snapshot.json"
287
+ goldenFilePath := "testdata/example-1/snapshot.json.gz "
262
288
if _ , update := os .LookupEnv ("UPDATE_GOLDEN_FILES" ); update {
263
- err := os .WriteFile (goldenFilePath , actualSnapshotBytes , 0o0644 )
264
- require .NoError (t , err )
289
+ writeGZIP (t , goldenFilePath , actualSnapshotBytes )
265
290
} else {
266
- expectedSnapshotBytes , err := os .ReadFile (goldenFilePath )
267
- require .NoError (t , err )
291
+ expectedSnapshotBytes := readGZIP (t , goldenFilePath )
268
292
assert .JSONEq (t , string (expectedSnapshotBytes ), string (actualSnapshotBytes ))
269
293
}
270
294
}
0 commit comments