11package cmd
22
33import (
4+ "bytes"
5+ "encoding/json"
46 "os"
57 "path"
68 "runtime"
9+ "slices"
710 "testing"
811
12+ "github.com/google/go-cmp/cmp"
13+ "github.com/google/go-cmp/cmp/cmpopts"
914 "github.com/google/uuid"
1015 "github.com/stretchr/testify/assert"
1116 "github.com/stretchr/testify/require"
1217)
1318
1419func TestSync (t * testing.T ) {
1520 configs := []struct {
16- name string
17- config string
18- err string
21+ name string
22+ config string
23+ err string
24+ summary []syncSummary
1925 }{
2026 {
2127 name : "sync_success_sourcev1_destv0" ,
@@ -24,18 +30,159 @@ func TestSync(t *testing.T) {
2430 {
2531 name : "multiple_sources" ,
2632 config : "multiple-sources.yml" ,
33+ summary : []syncSummary {
34+ {
35+ CliVersion : "development" ,
36+ DestinationErrors : 0 ,
37+ DestinationName : "test" ,
38+ DestinationPath : "cloudquery/test" ,
39+ DestinationVersion : "v2.2.14" ,
40+ Resources : 12 ,
41+ SourceName : "test" ,
42+ SourcePath : "cloudquery/test" ,
43+ SourceVersion : "v3.1.15" ,
44+ },
45+ {
46+ CliVersion : "development" ,
47+ DestinationErrors : 0 ,
48+ DestinationName : "test" ,
49+ DestinationPath : "cloudquery/test" ,
50+ DestinationVersion : "v2.2.14" ,
51+ Resources : 12 ,
52+ SourceName : "test2" ,
53+ SourcePath : "cloudquery/test" ,
54+ SourceVersion : "v3.1.15" ,
55+ },
56+ },
2757 },
2858 {
2959 name : "multiple_destinations" ,
3060 config : "multiple-destinations.yml" ,
61+ summary : []syncSummary {
62+
63+ {
64+ CliVersion : "development" ,
65+ DestinationName : "test" ,
66+ DestinationPath : "cloudquery/test" ,
67+ DestinationVersion : "v2.2.14" ,
68+ Resources : 12 ,
69+ SourceName : "test" ,
70+ SourcePath : "cloudquery/test" ,
71+ SourceVersion : "v3.1.15" ,
72+ },
73+ {
74+ CliVersion : "development" ,
75+ DestinationName : "test" ,
76+ DestinationPath : "cloudquery/test" ,
77+ DestinationVersion : "v2.2.14" ,
78+ Resources : 12 ,
79+ SourceName : "test2" ,
80+ SourcePath : "cloudquery/test" ,
81+ SourceVersion : "v3.1.15" ,
82+ },
83+ },
3184 },
3285 {
3386 name : "multiple_sources_destinations" ,
3487 config : "multiple-sources-destinations.yml" ,
88+ summary : []syncSummary {
89+ {
90+ CliVersion : "development" ,
91+ DestinationName : "test" ,
92+ DestinationPath : "cloudquery/test" ,
93+ DestinationVersion : "v2.2.14" ,
94+ Resources : 12 ,
95+ SourceName : "test" ,
96+ SourcePath : "cloudquery/test" ,
97+ SourceVersion : "v3.1.15" ,
98+ },
99+ {
100+ CliVersion : "development" ,
101+ DestinationName : "test" ,
102+ DestinationPath : "cloudquery/test" ,
103+ DestinationVersion : "v2.2.14" ,
104+ Resources : 12 ,
105+ SourceName : "test2" ,
106+ SourcePath : "cloudquery/test" ,
107+ SourceVersion : "v3.1.15" ,
108+ },
109+ {
110+ CliVersion : "development" ,
111+ DestinationName : "test-1" ,
112+ DestinationPath : "cloudquery/test" ,
113+ DestinationVersion : "v2.2.14" ,
114+ Resources : 12 ,
115+ SourceName : "test-1" ,
116+ SourcePath : "cloudquery/test" ,
117+ SourceVersion : "v3.1.15" ,
118+ },
119+ {
120+ CliVersion : "development" ,
121+ DestinationName : "test-2" ,
122+ DestinationPath : "cloudquery/test" ,
123+ DestinationVersion : "v2.2.14" ,
124+ Resources : 12 ,
125+ SourceName : "test-2" ,
126+ SourcePath : "cloudquery/test" ,
127+ SourceVersion : "v3.1.15" ,
128+ },
129+ },
35130 },
36131 {
37132 name : "different_backend_from_destination" ,
38133 config : "different-backend-from-destination.yml" ,
134+ summary : []syncSummary {
135+ {
136+ CliVersion : "development" ,
137+ DestinationName : "test" ,
138+ DestinationPath : "cloudquery/test" ,
139+ DestinationVersion : "v2.2.14" ,
140+ Resources : 12 ,
141+ SourceName : "test" ,
142+ SourcePath : "cloudquery/test" ,
143+ SourceVersion : "v3.1.15" ,
144+ },
145+ {
146+ CliVersion : "development" ,
147+ DestinationName : "test" ,
148+ DestinationPath : "cloudquery/test" ,
149+ DestinationVersion : "v2.2.14" ,
150+ Resources : 12 ,
151+ SourceName : "test2" ,
152+ SourcePath : "cloudquery/test" ,
153+ SourceVersion : "v3.1.15" ,
154+ },
155+ {
156+ CliVersion : "development" ,
157+ DestinationName : "test-1" ,
158+ DestinationPath : "cloudquery/test" ,
159+ DestinationVersion : "v2.2.14" ,
160+ Resources : 12 ,
161+ SourceName : "test-1" ,
162+ SourcePath : "cloudquery/test" ,
163+ SourceVersion : "v3.1.15" ,
164+ },
165+ {
166+ CliVersion : "development" ,
167+ DestinationName : "test-2" ,
168+ DestinationPath : "cloudquery/test" ,
169+ DestinationVersion : "v2.2.14" ,
170+ Resources : 12 ,
171+ SourceName : "test-2" ,
172+ SourcePath : "cloudquery/test" ,
173+ SourceVersion : "v3.1.15" ,
174+ },
175+ {
176+ CliVersion : "development" ,
177+ DestinationName : "test1" ,
178+ DestinationPath : "cloudquery/test" ,
179+ DestinationVersion : "v2.2.14" ,
180+ Resources : 12 ,
181+ SourceName : "test" ,
182+ SourcePath : "cloudquery/test" ,
183+ SourceVersion : "v3.1.15" ,
184+ },
185+ },
39186 },
40187 {
41188 name : "should fail with missing path error when path is missing" ,
@@ -54,14 +201,31 @@ func TestSync(t *testing.T) {
54201 testConfig := path .Join (currentDir , "testdata" , tc .config )
55202 logFileName := path .Join (cqDir , "cloudquery.log" )
56203 cmd := NewCmdRoot ()
57- cmd .SetArgs ([]string {"sync" , testConfig , "--cq-dir" , cqDir , "--log-file-name" , logFileName })
204+
205+ argList := []string {"sync" , testConfig , "--cq-dir" , cqDir , "--log-file-name" , logFileName }
206+ summaryPath := ""
207+ if len (tc .summary ) > 0 {
208+ summaryPath = path .Join (cqDir , "/test/cloudquery-summary.jsonl" )
209+ argList = append (argList , "--summary-location" , summaryPath )
210+ }
211+
212+ cmd .SetArgs (argList )
58213 err := cmd .Execute ()
59214 if tc .err != "" {
60215 assert .Contains (t , err .Error (), tc .err )
61216 } else {
62217 assert .NoError (t , err )
63218 }
64219
220+ if len (tc .summary ) > 0 {
221+ summaries := readSummaries (t , summaryPath )
222+ // have to ignore SyncID because it's random
223+ diff := cmp .Diff (tc .summary , summaries , cmpopts .IgnoreFields (syncSummary {}, "SyncID" ))
224+ if diff != "" {
225+ t .Errorf ("unexpected summaries: %v" , diff )
226+ }
227+ }
228+
65229 // check that log was written and contains some lines from the plugin
66230 b , logFileError := os .ReadFile (path .Join (cqDir , "cloudquery.log" ))
67231 logContent := string (b )
@@ -165,3 +329,21 @@ func TestSync_IsolatedPluginEnvironmentsInCloud(t *testing.T) {
165329 })
166330 }
167331}
332+
333+ func readSummaries (t * testing.T , filename string ) []syncSummary {
334+ p , err := os .ReadFile (filename )
335+ assert .NoError (t , err )
336+
337+ lines := bytes .Split (p , []byte {'\n' })
338+ summaries := make ([]syncSummary , len (lines ))
339+ for i , line := range lines {
340+ if len (line ) == 0 {
341+ summaries = slices .Delete (summaries , i , i + 1 )
342+ continue
343+ }
344+ var v syncSummary
345+ assert .NoError (t , json .Unmarshal (line , & v ))
346+ summaries [i ] = v
347+ }
348+ return summaries
349+ }
0 commit comments