@@ -2,13 +2,16 @@ package dataupload_test
22
33import (
44 "bytes"
5+ "compress/gzip"
56 "crypto/x509"
67 "encoding/json"
78 "encoding/pem"
89 "errors"
910 "fmt"
11+ "io"
1012 "net/http"
1113 "os"
14+ "path/filepath"
1215 "testing"
1316 "time"
1417
@@ -191,7 +194,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI(t *testing.T) {
191194 cyberArkClient , err := dataupload .NewCyberArkClient (nil , serviceURL , identityClient .AuthenticateRequest )
192195 require .NoError (t , err )
193196
194- dataReadings := loadDataReadings (t , "testdata/example-1/datareadings.json" )
197+ dataReadings := parseDataReadings (t , readGZIP ( t , "testdata/example-1/datareadings.json.gz" ) )
195198 err = cyberArkClient .PostDataReadingsWithOptions (
196199 ctx ,
197200 api.DataReadingsPost {
@@ -207,15 +210,13 @@ func TestCyberArkClient_PostDataReadingsWithOptions_RealAPI(t *testing.T) {
207210 require .NoError (t , err )
208211}
209212
210- func loadDataReadings (t * testing.T , path string ) []* api.DataReading {
213+ func parseDataReadings (t * testing.T , data [] byte ) []* api.DataReading {
211214 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 )
219220
220221 for _ , reading := range dataReadings {
221222 dataBytes , err := json .Marshal (reading .Data )
@@ -244,8 +245,33 @@ func loadDataReadings(t *testing.T, path string) []*api.DataReading {
244245 return dataReadings
245246}
246247
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+
247273func 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" ) )
249275 snapshot , err := dataupload .ConvertDataReadingsToCyberarkSnapshot (api.DataReadingsPost {
250276 AgentMetadata : & api.AgentMetadata {
251277 Version : "test-version" ,
@@ -258,13 +284,11 @@ func TestConvertDataReadingsToCyberarkSnapshot(t *testing.T) {
258284 actualSnapshotBytes , err := json .MarshalIndent (snapshot , "" , " " )
259285 require .NoError (t , err )
260286
261- goldenFilePath := "testdata/example-1/snapshot.json"
287+ goldenFilePath := "testdata/example-1/snapshot.json.gz "
262288 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 )
265290 } else {
266- expectedSnapshotBytes , err := os .ReadFile (goldenFilePath )
267- require .NoError (t , err )
291+ expectedSnapshotBytes := readGZIP (t , goldenFilePath )
268292 assert .JSONEq (t , string (expectedSnapshotBytes ), string (actualSnapshotBytes ))
269293 }
270294}
0 commit comments