@@ -840,6 +840,52 @@ func TestSync_ServerSideApply(t *testing.T) {
840
840
}
841
841
}
842
842
843
+ func withForceAnnotation (un * unstructured.Unstructured ) * unstructured.Unstructured {
844
+ un .SetAnnotations (map [string ]string {synccommon .AnnotationSyncOptions : synccommon .SyncOptionForce })
845
+ return un
846
+ }
847
+
848
+ func withForceAndReplaceAnnotations (un * unstructured.Unstructured ) * unstructured.Unstructured {
849
+ un .SetAnnotations (map [string ]string {synccommon .AnnotationSyncOptions : "Force=true,Replace=true" })
850
+ return un
851
+ }
852
+
853
+ func TestSync_Force (t * testing.T ) {
854
+ testCases := []struct {
855
+ name string
856
+ target * unstructured.Unstructured
857
+ live * unstructured.Unstructured
858
+ commandUsed string
859
+ force bool
860
+ }{
861
+ {"NoAnnotation" , NewPod (), NewPod (), "apply" , false },
862
+ {"ForceApplyAnnotationIsSet" , withForceAnnotation (NewPod ()), NewPod (), "apply" , true },
863
+ {"ForceReplaceAnnotationIsSet" , withForceAndReplaceAnnotations (NewPod ()), NewPod (), "replace" , true },
864
+ {"LiveObjectMissing" , withReplaceAnnotation (NewPod ()), nil , "create" , false },
865
+ }
866
+
867
+ for _ , tc := range testCases {
868
+ t .Run (tc .name , func (t * testing.T ) {
869
+ syncCtx := newTestSyncCtx (nil )
870
+
871
+ tc .target .SetNamespace (FakeArgoCDNamespace )
872
+ if tc .live != nil {
873
+ tc .live .SetNamespace (FakeArgoCDNamespace )
874
+ }
875
+ syncCtx .resources = groupResources (ReconciliationResult {
876
+ Live : []* unstructured.Unstructured {tc .live },
877
+ Target : []* unstructured.Unstructured {tc .target },
878
+ })
879
+
880
+ syncCtx .Sync ()
881
+
882
+ resourceOps , _ := syncCtx .resourceOps .(* kubetest.MockResourceOps )
883
+ assert .Equal (t , tc .commandUsed , resourceOps .GetLastResourceCommand (kube .GetResourceKey (tc .target )))
884
+ assert .Equal (t , tc .force , resourceOps .GetLastForce ())
885
+ })
886
+ }
887
+ }
888
+
843
889
func TestSelectiveSyncOnly (t * testing.T ) {
844
890
pod1 := NewPod ()
845
891
pod1 .SetName ("pod-1" )
@@ -1771,11 +1817,11 @@ func TestWaveReorderingOfPruneTasks(t *testing.T) {
1771
1817
// no change in wave order
1772
1818
expectedWaveOrder : map [string ]int {
1773
1819
// new wave // original wave
1774
- ns .GetName (): 0 , // 0
1775
- pod1 .GetName (): 1 , // 1
1776
- pod2 .GetName (): 2 , // 2
1777
- pod3 .GetName (): 3 , // 3
1778
- pod4 .GetName (): 4 , // 4
1820
+ ns .GetName (): 0 , // 0
1821
+ pod1 .GetName (): 1 , // 1
1822
+ pod2 .GetName (): 2 , // 2
1823
+ pod3 .GetName (): 3 , // 3
1824
+ pod4 .GetName (): 4 , // 4
1779
1825
},
1780
1826
},
1781
1827
{
@@ -1785,11 +1831,11 @@ func TestWaveReorderingOfPruneTasks(t *testing.T) {
1785
1831
// change in prune wave order
1786
1832
expectedWaveOrder : map [string ]int {
1787
1833
// new wave // original wave
1788
- ns .GetName (): 4 , // 0
1789
- pod1 .GetName (): 3 , // 1
1790
- pod2 .GetName (): 2 , // 2
1791
- pod3 .GetName (): 1 , // 3
1792
- pod4 .GetName (): 0 , // 4
1834
+ ns .GetName (): 4 , // 0
1835
+ pod1 .GetName (): 3 , // 1
1836
+ pod2 .GetName (): 2 , // 2
1837
+ pod3 .GetName (): 1 , // 3
1838
+ pod4 .GetName (): 0 , // 4
1793
1839
},
1794
1840
},
1795
1841
{
@@ -1799,13 +1845,13 @@ func TestWaveReorderingOfPruneTasks(t *testing.T) {
1799
1845
// change in prune wave order
1800
1846
expectedWaveOrder : map [string ]int {
1801
1847
// new wave // original wave
1802
- pod1 .GetName (): 4 , // 1
1803
- pod3 .GetName (): 3 , // 3
1804
- pod4 .GetName (): 1 , // 4
1848
+ pod1 .GetName (): 4 , // 1
1849
+ pod3 .GetName (): 3 , // 3
1850
+ pod4 .GetName (): 1 , // 4
1805
1851
1806
1852
// no change since non prune tasks
1807
- ns .GetName (): 0 , // 0
1808
- pod2 .GetName (): 2 , // 2
1853
+ ns .GetName (): 0 , // 0
1854
+ pod2 .GetName (): 2 , // 2
1809
1855
},
1810
1856
},
1811
1857
}
@@ -1830,13 +1876,13 @@ func TestWaveReorderingOfPruneTasks(t *testing.T) {
1830
1876
// change in prune wave order
1831
1877
expectedWaveOrder : map [string ]int {
1832
1878
// new wave // original wave
1833
- pod1 .GetName (): 5 , // 1
1834
- pod2 .GetName (): 5 , // 2
1835
- pod3 .GetName (): 5 , // 3
1836
- pod4 .GetName (): 5 , // 4
1879
+ pod1 .GetName (): 5 , // 1
1880
+ pod2 .GetName (): 5 , // 2
1881
+ pod3 .GetName (): 5 , // 3
1882
+ pod4 .GetName (): 5 , // 4
1837
1883
1838
1884
// no change since non prune tasks
1839
- ns .GetName (): 0 , // 0
1885
+ ns .GetName (): 0 , // 0
1840
1886
},
1841
1887
},
1842
1888
{
@@ -1847,13 +1893,13 @@ func TestWaveReorderingOfPruneTasks(t *testing.T) {
1847
1893
// change in wave order
1848
1894
expectedWaveOrder : map [string ]int {
1849
1895
// new wave // original wave
1850
- pod1 .GetName (): 4 , // 1
1851
- pod2 .GetName (): 5 , // 2
1852
- pod3 .GetName (): 2 , // 3
1853
- pod4 .GetName (): 1 , // 4
1896
+ pod1 .GetName (): 4 , // 1
1897
+ pod2 .GetName (): 5 , // 2
1898
+ pod3 .GetName (): 2 , // 3
1899
+ pod4 .GetName (): 1 , // 4
1854
1900
1855
1901
// no change since non prune tasks
1856
- ns .GetName (): 0 , // 0
1902
+ ns .GetName (): 0 , // 0
1857
1903
},
1858
1904
},
1859
1905
}
@@ -1877,11 +1923,11 @@ func TestWaveReorderingOfPruneTasks(t *testing.T) {
1877
1923
// change in prune wave order
1878
1924
expectedWaveOrder : map [string ]int {
1879
1925
// new wave // original wave
1880
- pod1 .GetName (): 5 , // 1
1881
- pod3 .GetName (): 4 , // 3
1882
- pod4 .GetName (): 4 , // 3
1883
- pod5 .GetName (): 3 , // 4
1884
- pod7 .GetName (): 1 , // 5
1926
+ pod1 .GetName (): 5 , // 1
1927
+ pod3 .GetName (): 4 , // 3
1928
+ pod4 .GetName (): 4 , // 3
1929
+ pod5 .GetName (): 3 , // 4
1930
+ pod7 .GetName (): 1 , // 5
1885
1931
1886
1932
// no change since non prune tasks
1887
1933
ns .GetName (): - 1 , // -1
@@ -1941,8 +1987,8 @@ func TestWaitForCleanUpBeforeNextWave(t *testing.T) {
1941
1987
1942
1988
// simulate successful delete of pod3
1943
1989
syncCtx .resources = groupResources (ReconciliationResult {
1944
- Target : []* unstructured.Unstructured {nil , nil , },
1945
- Live : []* unstructured.Unstructured {pod1 , pod2 , },
1990
+ Target : []* unstructured.Unstructured {nil , nil },
1991
+ Live : []* unstructured.Unstructured {pod1 , pod2 },
1946
1992
})
1947
1993
1948
1994
// next sync should prune only pod2
@@ -1966,8 +2012,8 @@ func TestWaitForCleanUpBeforeNextWave(t *testing.T) {
1966
2012
1967
2013
// simulate successful delete of pod2
1968
2014
syncCtx .resources = groupResources (ReconciliationResult {
1969
- Target : []* unstructured.Unstructured {nil , },
1970
- Live : []* unstructured.Unstructured {pod1 , },
2015
+ Target : []* unstructured.Unstructured {nil },
2016
+ Live : []* unstructured.Unstructured {pod1 },
1971
2017
})
1972
2018
1973
2019
// next sync should proceed with next wave
0 commit comments