Fix unit tests and don't patch with empty finalizers
Signed-off-by: Grant Griffiths <ggriffiths@purestorage.com>
This commit is contained in:
@@ -85,7 +85,7 @@ func TestSyncContent(t *testing.T) {
|
||||
{
|
||||
name: "1-3: Basic sync content create snapshot with non-existent secret",
|
||||
initialContents: withContentAnnotations(withContentStatus(newContentArray("content1-3", "snapuid1-3", "snap1-3", "sid1-3", invalidSecretClass, "", "volume-handle-1-3", retainPolicy, nil, &defaultSize, true),
|
||||
nil), map[string]string{
|
||||
&crdv1.VolumeSnapshotContentStatus{}), map[string]string{
|
||||
utils.AnnDeletionSecretRefName: "",
|
||||
utils.AnnDeletionSecretRefNamespace: "",
|
||||
}),
|
||||
@@ -94,12 +94,12 @@ func TestSyncContent(t *testing.T) {
|
||||
SnapshotHandle: nil,
|
||||
RestoreSize: nil,
|
||||
ReadyToUse: &False,
|
||||
Error: newSnapshotError("Failed to create snapshot: failed to get input parameters to create snapshot for content content1-3: \"cannot retrieve secrets for snapshot content \\\"content1-3\\\", err: secret name or namespace not specified\""),
|
||||
Error: newSnapshotError("Failed to check and update snapshot content: failed to get input parameters to create snapshot for content content1-3: \"cannot retrieve secrets for snapshot content \\\"content1-3\\\", err: secret name or namespace not specified\""),
|
||||
}), map[string]string{
|
||||
utils.AnnDeletionSecretRefName: "",
|
||||
utils.AnnDeletionSecretRefNamespace: "",
|
||||
}), initialSecrets: []*v1.Secret{}, // no initial secret created
|
||||
expectedEvents: []string{"Warning SnapshotCreationFailed"},
|
||||
expectedEvents: []string{"Warning SnapshotContentCheckandUpdateFailed"},
|
||||
errors: noerrors,
|
||||
test: testSyncContent,
|
||||
},
|
||||
@@ -149,7 +149,7 @@ func TestSyncContent(t *testing.T) {
|
||||
{
|
||||
name: "1-5: Basic sync content create snapshot with failed secret call",
|
||||
initialContents: withContentAnnotations(withContentStatus(newContentArray("content1-5", "snapuid1-5", "snap1-5", "sid1-5", invalidSecretClass, "", "volume-handle-1-5", retainPolicy, nil, &defaultSize, true),
|
||||
nil), map[string]string{
|
||||
&crdv1.VolumeSnapshotContentStatus{}), map[string]string{
|
||||
utils.AnnDeletionSecretRefName: "secret",
|
||||
utils.AnnDeletionSecretRefNamespace: "default",
|
||||
}),
|
||||
@@ -158,12 +158,12 @@ func TestSyncContent(t *testing.T) {
|
||||
SnapshotHandle: nil,
|
||||
RestoreSize: nil,
|
||||
ReadyToUse: &False,
|
||||
Error: newSnapshotError("Failed to create snapshot: failed to get input parameters to create snapshot for content content1-5: \"cannot get credentials for snapshot content \\\"content1-5\\\"\""),
|
||||
Error: newSnapshotError(`Failed to check and update snapshot content: failed to get input parameters to create snapshot for content content1-5: "cannot get credentials for snapshot content \"content1-5\""`),
|
||||
}), map[string]string{
|
||||
utils.AnnDeletionSecretRefName: "secret",
|
||||
utils.AnnDeletionSecretRefNamespace: "default",
|
||||
}), initialSecrets: []*v1.Secret{}, // no initial secret created
|
||||
expectedEvents: []string{"Warning SnapshotCreationFailed"},
|
||||
expectedEvents: []string{"Warning SnapshotContentCheckandUpdateFailed"},
|
||||
errors: []reactorError{
|
||||
// Inject error to the first client.VolumesnapshotV1().VolumeSnapshots().Update call.
|
||||
// All other calls will succeed.
|
||||
|
@@ -15,6 +15,7 @@ package sidecar_controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
@@ -25,6 +26,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
jsonpatch "github.com/evanphx/json-patch"
|
||||
crdv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
|
||||
clientset "github.com/kubernetes-csi/external-snapshotter/client/v4/clientset/versioned"
|
||||
"github.com/kubernetes-csi/external-snapshotter/client/v4/clientset/versioned/fake"
|
||||
@@ -215,6 +217,46 @@ func (r *snapshotReactor) React(action core.Action) (handled bool, ret runtime.O
|
||||
klog.V(4).Infof("saved updated content %s", content.Name)
|
||||
return true, content, nil
|
||||
|
||||
case action.Matches("patch", "volumesnapshotcontents"):
|
||||
content := &crdv1.VolumeSnapshotContent{}
|
||||
action := action.(core.PatchAction)
|
||||
|
||||
// Check and bump object version
|
||||
storedSnapshotContent, found := r.contents[action.GetName()]
|
||||
if found {
|
||||
// Apply patch
|
||||
storedSnapshotBytes, err := json.Marshal(storedSnapshotContent)
|
||||
if err != nil {
|
||||
return true, nil, err
|
||||
}
|
||||
contentPatch, err := jsonpatch.DecodePatch(action.GetPatch())
|
||||
if err != nil {
|
||||
return true, nil, err
|
||||
}
|
||||
|
||||
modified, err := contentPatch.Apply(storedSnapshotBytes)
|
||||
if err != nil {
|
||||
return true, nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(modified, content)
|
||||
if err != nil {
|
||||
return true, nil, err
|
||||
}
|
||||
|
||||
storedVer, _ := strconv.Atoi(content.ResourceVersion)
|
||||
content.ResourceVersion = strconv.Itoa(storedVer + 1)
|
||||
} else {
|
||||
return true, nil, fmt.Errorf("cannot update snapshot content %s: snapshot content not found", action.GetName())
|
||||
}
|
||||
|
||||
// Store the updated object to appropriate places.
|
||||
r.contents[content.Name] = content
|
||||
r.changedObjects = append(r.changedObjects, content)
|
||||
r.changedSinceLastSync++
|
||||
klog.V(4).Infof("saved updated content %s", content.Name)
|
||||
return true, content, nil
|
||||
|
||||
case action.Matches("get", "volumesnapshotcontents"):
|
||||
name := action.(core.GetAction).GetName()
|
||||
content, found := r.contents[name]
|
||||
@@ -488,6 +530,7 @@ func newSnapshotReactor(kubeClient *kubefake.Clientset, client *fake.Clientset,
|
||||
|
||||
client.AddReactor("create", "volumesnapshotcontents", reactor.React)
|
||||
client.AddReactor("update", "volumesnapshotcontents", reactor.React)
|
||||
client.AddReactor("patch", "volumesnapshotcontents", reactor.React)
|
||||
client.AddReactor("get", "volumesnapshotcontents", reactor.React)
|
||||
client.AddReactor("delete", "volumesnapshotcontents", reactor.React)
|
||||
kubeClient.AddReactor("get", "secrets", reactor.React)
|
||||
|
Reference in New Issue
Block a user