1
1
package sync
2
2
3
3
import (
4
+ "encoding/json"
4
5
"errors"
5
6
"fmt"
6
7
"reflect"
@@ -18,6 +19,7 @@ import (
18
19
testcore "k8s.io/client-go/testing"
19
20
"k8s.io/klog/v2/klogr"
20
21
22
+ "github.com/argoproj/gitops-engine/pkg/diff"
21
23
"github.com/argoproj/gitops-engine/pkg/health"
22
24
synccommon "github.com/argoproj/gitops-engine/pkg/sync/common"
23
25
"github.com/argoproj/gitops-engine/pkg/utils/kube"
@@ -1056,3 +1058,78 @@ func TestPruneLast(t *testing.T) {
1056
1058
assert .Equal (t , 3 , tasks .lastWave ())
1057
1059
})
1058
1060
}
1061
+
1062
+ func diffResultList () * diff.DiffResultList {
1063
+ pod1 := NewPod ()
1064
+ pod1 .SetName ("pod-1" )
1065
+ pod2 := NewPod ()
1066
+ pod2 .SetName ("pod-2" )
1067
+ pod3 := NewPod ()
1068
+ pod3 .SetName ("pod-3" )
1069
+
1070
+ diffResultList := diff.DiffResultList {
1071
+ Modified : true ,
1072
+ Diffs : []diff.DiffResult {},
1073
+ }
1074
+
1075
+ podBytes , _ := json .Marshal (pod1 )
1076
+ diffResultList .Diffs = append (diffResultList .Diffs , diff.DiffResult {NormalizedLive : []byte ("null" ), PredictedLive : podBytes , Modified : true })
1077
+
1078
+ podBytes , _ = json .Marshal (pod2 )
1079
+ diffResultList .Diffs = append (diffResultList .Diffs , diff.DiffResult {NormalizedLive : podBytes , PredictedLive : []byte ("null" ), Modified : true })
1080
+
1081
+ podBytes , _ = json .Marshal (pod3 )
1082
+ diffResultList .Diffs = append (diffResultList .Diffs , diff.DiffResult {NormalizedLive : podBytes , PredictedLive : podBytes , Modified : false })
1083
+
1084
+ return & diffResultList
1085
+ }
1086
+
1087
+ func TestApplyOutOfSyncOnly (t * testing.T ) {
1088
+ pod1 := NewPod ()
1089
+ pod1 .SetName ("pod-1" )
1090
+ pod2 := NewPod ()
1091
+ pod2 .SetName ("pod-2" )
1092
+ pod3 := NewPod ()
1093
+ pod3 .SetName ("pod-3" )
1094
+ syncCtx := newTestSyncCtx ()
1095
+
1096
+ t .Run ("applyOutOfSyncOnly=false" , func (t * testing.T ) {
1097
+ syncCtx .applyOutOfSyncOnly = true
1098
+ syncCtx .modificationResult = nil
1099
+ syncCtx .resources = groupResources (ReconciliationResult {
1100
+ Live : []* unstructured.Unstructured {nil , pod2 , pod3 },
1101
+ Target : []* unstructured.Unstructured {pod1 , nil , pod3 },
1102
+ })
1103
+ tasks , successful := syncCtx .getSyncTasks ()
1104
+
1105
+ assert .True (t , successful )
1106
+ assert .Len (t , tasks , 3 )
1107
+ })
1108
+
1109
+ syncCtx = newTestSyncCtx (WithResourceModificationChecker (true , diffResultList ()))
1110
+ t .Run ("applyOutOfSyncOnly=true" , func (t * testing.T ) {
1111
+ syncCtx .applyOutOfSyncOnly = true
1112
+ syncCtx .resources = groupResources (ReconciliationResult {
1113
+ Live : []* unstructured.Unstructured {nil , pod2 , pod3 },
1114
+ Target : []* unstructured.Unstructured {pod1 , nil , pod3 },
1115
+ })
1116
+ tasks , successful := syncCtx .getSyncTasks ()
1117
+
1118
+ assert .True (t , successful )
1119
+ assert .Len (t , tasks , 2 )
1120
+ })
1121
+
1122
+ pod4 := NewPod ()
1123
+ pod4 .SetName ("pod-4" )
1124
+ t .Run ("applyOutOfSyncOnly=true and missing resource key" , func (t * testing.T ) {
1125
+ syncCtx .applyOutOfSyncOnly = true
1126
+ syncCtx .resources = groupResources (ReconciliationResult {
1127
+ Live : []* unstructured.Unstructured {nil , pod2 , pod3 , pod4 },
1128
+ Target : []* unstructured.Unstructured {pod1 , nil , pod3 , pod4 },
1129
+ })
1130
+ tasks , successful := syncCtx .getSyncTasks ()
1131
+
1132
+ assert .True (t , successful )
1133
+ assert .Len (t , tasks , 3 )
1134
+ })
1135
+ }
0 commit comments