@@ -1793,10 +1793,11 @@ func TestSyncWaveHook(t *testing.T) {
17931793 syncCtx .hooks = []* unstructured.Unstructured {pod3 }
17941794
17951795 called := false
1796- syncCtx .syncWaveHook = func (phase synccommon.SyncPhase , wave int , final bool ) error {
1796+ syncCtx .syncWaveHook = func (syncIdentities [] synccommon.SyncIdentity , final bool ) error {
17971797 called = true
1798- assert .Equal (t , synccommon .SyncPhaseSync , string (phase ))
1799- assert .Equal (t , - 1 , wave )
1798+ assert .Equal (t , 1 , len (syncIdentities ))
1799+ assert .Equal (t , synccommon .SyncPhaseSync , string (syncIdentities [0 ].Phase ))
1800+ assert .Equal (t , - 1 , syncIdentities [0 ].Wave )
18001801 assert .False (t , final )
18011802 return nil
18021803 }
@@ -1806,7 +1807,7 @@ func TestSyncWaveHook(t *testing.T) {
18061807 // call sync again, it should not invoke the SyncWaveHook callback since we only should be
18071808 // doing this after an apply, and not every reconciliation
18081809 called = false
1809- syncCtx .syncWaveHook = func (_ synccommon.SyncPhase , _ int , _ bool ) error {
1810+ syncCtx .syncWaveHook = func (_ [] synccommon.SyncIdentity , _ bool ) error {
18101811 called = true
18111812 return nil
18121813 }
@@ -1819,10 +1820,11 @@ func TestSyncWaveHook(t *testing.T) {
18191820 pod1Res .HookPhase = synccommon .OperationSucceeded
18201821 syncCtx .syncRes [resourceResultKey (pod1Res .ResourceKey , synccommon .SyncPhaseSync )] = pod1Res
18211822 called = false
1822- syncCtx .syncWaveHook = func (phase synccommon.SyncPhase , wave int , final bool ) error {
1823+ syncCtx .syncWaveHook = func (syncIdentities [] synccommon.SyncIdentity , final bool ) error {
18231824 called = true
1824- assert .Equal (t , synccommon .SyncPhaseSync , string (phase ))
1825- assert .Equal (t , 0 , wave )
1825+ assert .Equal (t , 1 , len (syncIdentities ))
1826+ assert .Equal (t , synccommon .SyncPhaseSync , string (syncIdentities [0 ].Phase ))
1827+ assert .Equal (t , 0 , syncIdentities [0 ].Wave )
18261828 assert .False (t , final )
18271829 return nil
18281830 }
@@ -1835,17 +1837,135 @@ func TestSyncWaveHook(t *testing.T) {
18351837 pod2Res .HookPhase = synccommon .OperationSucceeded
18361838 syncCtx .syncRes [resourceResultKey (pod2Res .ResourceKey , synccommon .SyncPhaseSync )] = pod2Res
18371839 called = false
1838- syncCtx .syncWaveHook = func (phase synccommon.SyncPhase , wave int , final bool ) error {
1840+ syncCtx .syncWaveHook = func (syncIdentities [] synccommon.SyncIdentity , final bool ) error {
18391841 called = true
1840- assert .Equal (t , synccommon .SyncPhasePostSync , string (phase ))
1841- assert .Equal (t , 0 , wave )
1842+ assert .Equal (t , 1 , len (syncIdentities ))
1843+ assert .Equal (t , synccommon .SyncPhasePostSync , string (syncIdentities [0 ].Phase ))
1844+ assert .Equal (t , 0 , syncIdentities [0 ].Wave )
18421845 assert .True (t , final )
18431846 return nil
18441847 }
18451848 syncCtx .Sync ()
18461849 assert .True (t , called )
18471850}
18481851
1852+ func TestSyncWaveGroup (t * testing.T ) {
1853+ syncCtx := newTestSyncCtx (nil , WithOperationSettings (false , false , false , false ))
1854+ pod1 := testingutils .NewPod ()
1855+ pod1 .SetName ("pod-1" )
1856+ pod1 .SetAnnotations (map [string ]string {synccommon .AnnotationSyncWave : "-1" , synccommon .AnnotationSyncWaveGroup : "0" , synccommon .AnnotationSyncWaveGroupDependencies : "" })
1857+ pod2 := testingutils .NewPod ()
1858+ pod2 .SetName ("pod-2" )
1859+ pod2 .SetAnnotations (map [string ]string {synccommon .AnnotationSyncWave : "0" , synccommon .AnnotationSyncWaveGroup : "0" , synccommon .AnnotationSyncWaveGroupDependencies : "" })
1860+ pod3 := testingutils .NewPod ()
1861+ pod3 .SetName ("pod-3" )
1862+ pod3 .SetAnnotations (map [string ]string {synccommon .AnnotationSyncWave : "3" , synccommon .AnnotationSyncWaveGroup : "1" , synccommon .AnnotationSyncWaveGroupDependencies : "" })
1863+ pod4 := testingutils .NewPod ()
1864+ pod4 .SetName ("pod-4" )
1865+ pod4 .SetAnnotations (map [string ]string {synccommon .AnnotationSyncWave : "1" , synccommon .AnnotationSyncWaveGroup : "2" , synccommon .AnnotationSyncWaveGroupDependencies : "1" })
1866+ pod5 := testingutils .NewPod ()
1867+ pod5 .SetName ("pod-5" )
1868+ pod5 .SetAnnotations (map [string ]string {synccommon .AnnotationSyncWave : "1" , synccommon .AnnotationSyncWaveGroup : "3" , synccommon .AnnotationSyncWaveGroupDependencies : "0,1" })
1869+
1870+ syncCtx .resources = groupResources (ReconciliationResult {
1871+ Live : []* unstructured.Unstructured {nil , nil , nil , nil , nil },
1872+ Target : []* unstructured.Unstructured {pod1 , pod2 , pod3 , pod4 , pod5 },
1873+ })
1874+
1875+ called := false
1876+ syncCtx .syncWaveHook = func (syncIdentities []synccommon.SyncIdentity , final bool ) error {
1877+ called = true
1878+ assert .Equal (t , 2 , len (syncIdentities ))
1879+ assert .Equal (t , synccommon .SyncPhaseSync , string (syncIdentities [0 ].Phase ))
1880+ assert .Equal (t , - 1 , syncIdentities [0 ].Wave )
1881+ assert .Equal (t , 0 , syncIdentities [0 ].WaveGroup )
1882+ assert .Equal (t , 3 , syncIdentities [1 ].Wave )
1883+ assert .Equal (t , 1 , syncIdentities [1 ].WaveGroup )
1884+ assert .False (t , final )
1885+ return nil
1886+ }
1887+
1888+ originalSyncTasks , _ := syncCtx .getSyncTasks ()
1889+ assert .Equal (t , 5 , len (originalSyncTasks ))
1890+ _ , _ , originalStates := syncCtx .GetState ()
1891+ assert .Equal (t , 0 , len (originalStates ))
1892+
1893+ // call sync. pod-1 and pod-3 should be processed first. Verify we invoke SyncWaveHook call after applying first waves
1894+
1895+ syncCtx .Sync ()
1896+ assert .True (t , called )
1897+
1898+ _ , _ , results := syncCtx .GetState ()
1899+ assert .Equal (t , 2 , len (results ))
1900+
1901+ pod1Res := results [0 ]
1902+ pod1Res .HookPhase = synccommon .OperationSucceeded
1903+ syncCtx .syncRes [resourceResultKey (pod1Res .ResourceKey , synccommon .SyncPhaseSync )] = pod1Res
1904+
1905+ pod2Res := results [1 ]
1906+ pod2Res .HookPhase = synccommon .OperationSucceeded
1907+ syncCtx .syncRes [resourceResultKey (pod2Res .ResourceKey , synccommon .SyncPhaseSync )] = pod2Res
1908+
1909+ // Here I would like to see :
1910+ // syncCtx.resources[0].Target and syncCtx.resources[2].Target not nil because pod-1 and pod-3 have been synced
1911+ // syncTasks should contain 3 elements : pod-2, pod-4 and pod-5
1912+ // but syncCtx.getSyncTasks() returns 5 elements
1913+
1914+ /*####### /!\ THIS TEST SHOULD PASS /!\ #######
1915+
1916+ syncTasks2, _ := syncCtx.getSyncTasks()
1917+ assert.Equal(t, 3, len(syncTasks2))
1918+
1919+ ######## /!\ BUT IT DOES NOT /!\ #####*/
1920+
1921+ // call sync again, pod-2 and pod-4 should be synced during this sync. Verify we invoke SyncWaveHook call after applying second waves
1922+
1923+ called = false
1924+ syncCtx .syncWaveHook = func (_ []synccommon.SyncIdentity , _ bool ) error {
1925+ called = true
1926+ return nil
1927+ }
1928+ syncCtx .Sync ()
1929+ assert .True (t , called )
1930+
1931+ _ , _ , results2 := syncCtx .GetState ()
1932+ assert .Equal (t , 4 , len (results2 ))
1933+
1934+ pod3Res := results2 [2 ]
1935+ pod3Res .HookPhase = synccommon .OperationSucceeded
1936+ syncCtx .syncRes [resourceResultKey (pod3Res .ResourceKey , synccommon .SyncPhaseSync )] = pod3Res
1937+
1938+ pod4Res := results2 [3 ]
1939+ pod4Res .HookPhase = synccommon .OperationSucceeded
1940+ syncCtx .syncRes [resourceResultKey (pod4Res .ResourceKey , synccommon .SyncPhaseSync )] = pod4Res
1941+
1942+ // complete last wave, then call Sync again. Verify we invoke another SyncWaveHook call after applying last wave
1943+ called = false
1944+ syncCtx .syncWaveHook = func (syncIdentities []synccommon.SyncIdentity , final bool ) error {
1945+ called = true
1946+ return nil
1947+ }
1948+ syncCtx .Sync ()
1949+ assert .True (t , called )
1950+
1951+ _ , _ , results3 := syncCtx .GetState ()
1952+ assert .Equal (t , 5 , len (results3 ))
1953+
1954+ pod5Res := results3 [0 ]
1955+ pod5Res .HookPhase = synccommon .OperationSucceeded
1956+ syncCtx .syncRes [resourceResultKey (pod5Res .ResourceKey , synccommon .SyncPhaseSync )] = pod5Res
1957+
1958+ // no remaining wave. Verify we don't invoke another SyncWaveHook call
1959+
1960+ called = false
1961+ syncCtx .syncWaveHook = func (syncIdentities []synccommon.SyncIdentity , final bool ) error {
1962+ called = true
1963+ return nil
1964+ }
1965+ syncCtx .Sync ()
1966+ assert .False (t , called )
1967+ }
1968+
18491969func TestSyncWaveHookFail (t * testing.T ) {
18501970 syncCtx := newTestSyncCtx (nil , WithOperationSettings (false , false , false , false ))
18511971 pod1 := testingutils .NewPod ()
@@ -1857,7 +1977,7 @@ func TestSyncWaveHookFail(t *testing.T) {
18571977 })
18581978
18591979 called := false
1860- syncCtx .syncWaveHook = func (_ synccommon.SyncPhase , _ int , _ bool ) error {
1980+ syncCtx .syncWaveHook = func (_ [] synccommon.SyncIdentity , _ bool ) error {
18611981 called = true
18621982 return errors .New ("intentional error" )
18631983 }
0 commit comments