-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix spec comparison in applyWork (#89)
Signed-off-by: Jian Qiu <jqiu@redhat.com>
- Loading branch information
Showing
4 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package agentdeploy | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"open-cluster-management.io/addon-framework/pkg/addonmanager/addontesting" | ||
fakework "open-cluster-management.io/api/client/work/clientset/versioned/fake" | ||
workinformers "open-cluster-management.io/api/client/work/informers/externalversions" | ||
workapiv1 "open-cluster-management.io/api/work/v1" | ||
) | ||
|
||
func TestApplyWork(t *testing.T) { | ||
cache := newWorkCache() | ||
fakeWorkClient := fakework.NewSimpleClientset() | ||
workInformerFactory := workinformers.NewSharedInformerFactory(fakeWorkClient, 10*time.Minute) | ||
syncContext := addontesting.NewFakeSyncContext(t, "test") | ||
|
||
work, _, _ := buildManifestWorkFromObject("cluster1", "addon1", []runtime.Object{addontesting.NewUnstructured("batch/v1", "Job", "default", "test")}) | ||
|
||
_, err := applyWork(context.TODO(), fakeWorkClient, workInformerFactory.Work().V1().ManifestWorks().Lister(), cache, syncContext.Recorder(), work) | ||
if err != nil { | ||
t.Errorf("failed to apply work with err %v", err) | ||
} | ||
|
||
addontesting.AssertActions(t, fakeWorkClient.Actions(), "create") | ||
|
||
// IF work is not changed, we should not update | ||
newWorkCopy := work.DeepCopy() | ||
fakeWorkClient.ClearActions() | ||
workInformerFactory.Work().V1().ManifestWorks().Informer().GetStore().Add(work) | ||
_, err = applyWork(context.TODO(), fakeWorkClient, workInformerFactory.Work().V1().ManifestWorks().Lister(), cache, syncContext.Recorder(), newWorkCopy) | ||
if err != nil { | ||
t.Errorf("failed to apply work with err %v", err) | ||
} | ||
addontesting.AssertNoActions(t, fakeWorkClient.Actions()) | ||
|
||
// Update work spec to update it | ||
newWork, _, _ := buildManifestWorkFromObject("cluster1", "addon1", []runtime.Object{addontesting.NewUnstructured("batch/v1", "Job", "default", "test")}) | ||
newWork.Spec.DeleteOption = &workapiv1.DeleteOption{PropagationPolicy: workapiv1.DeletePropagationPolicyTypeOrphan} | ||
fakeWorkClient.ClearActions() | ||
_, err = applyWork(context.TODO(), fakeWorkClient, workInformerFactory.Work().V1().ManifestWorks().Lister(), cache, syncContext.Recorder(), newWork) | ||
if err != nil { | ||
t.Errorf("failed to apply work with err %v", err) | ||
} | ||
addontesting.AssertActions(t, fakeWorkClient.Actions(), "update") | ||
|
||
// Do not update if generation is not changed | ||
work.Spec.DeleteOption = &workapiv1.DeleteOption{PropagationPolicy: workapiv1.DeletePropagationPolicyTypeForeground} | ||
workInformerFactory.Work().V1().ManifestWorks().Informer().GetStore().Update(work) | ||
|
||
fakeWorkClient.ClearActions() | ||
workInformerFactory.Work().V1().ManifestWorks().Informer().GetStore().Update(work) | ||
_, err = applyWork(context.TODO(), fakeWorkClient, workInformerFactory.Work().V1().ManifestWorks().Lister(), cache, syncContext.Recorder(), newWork) | ||
if err != nil { | ||
t.Errorf("failed to apply work with err %v", err) | ||
} | ||
addontesting.AssertNoActions(t, fakeWorkClient.Actions()) | ||
|
||
// change generation will cause update | ||
work.Generation = 1 | ||
workInformerFactory.Work().V1().ManifestWorks().Informer().GetStore().Update(work) | ||
|
||
fakeWorkClient.ClearActions() | ||
workInformerFactory.Work().V1().ManifestWorks().Informer().GetStore().Update(work) | ||
_, err = applyWork(context.TODO(), fakeWorkClient, workInformerFactory.Work().V1().ManifestWorks().Lister(), cache, syncContext.Recorder(), newWork) | ||
if err != nil { | ||
t.Errorf("failed to apply work with err %v", err) | ||
} | ||
addontesting.AssertActions(t, fakeWorkClient.Actions(), "update") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters