|  | 
|  | 1 | +package configmap | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"testing" | 
|  | 5 | + | 
|  | 6 | +	"github.com/konveyor/openshift-velero-plugin/velero-plugins/common" | 
|  | 7 | +	"github.com/konveyor/openshift-velero-plugin/velero-plugins/util/test" | 
|  | 8 | +	"github.com/stretchr/testify/assert" | 
|  | 9 | +	"github.com/stretchr/testify/require" | 
|  | 10 | +	"github.com/vmware-tanzu/velero/pkg/plugin/velero" | 
|  | 11 | +	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | 
|  | 12 | +) | 
|  | 13 | + | 
|  | 14 | +func TestRestorePluginAppliesTo(t *testing.T) { | 
|  | 15 | +	restorePlugin := &RestorePlugin{Log: test.NewLogger()} | 
|  | 16 | +	actual, err := restorePlugin.AppliesTo() | 
|  | 17 | +	require.NoError(t, err) | 
|  | 18 | +	assert.Equal(t, velero.ResourceSelector{IncludedResources: []string{"configmaps"}}, actual) | 
|  | 19 | +} | 
|  | 20 | + | 
|  | 21 | +func TestExecute(t *testing.T) { | 
|  | 22 | +	restorePlugin := &RestorePlugin{Log: test.NewLogger()} | 
|  | 23 | + | 
|  | 24 | +	tests := []struct { | 
|  | 25 | +		name              string | 
|  | 26 | +		annotations       map[string]string | 
|  | 27 | +		expectSkipRestore bool | 
|  | 28 | +	}{ | 
|  | 29 | +		{ | 
|  | 30 | +			name:              "ConfigMap with no annotations", | 
|  | 31 | +			annotations:       nil, | 
|  | 32 | +			expectSkipRestore: false, | 
|  | 33 | +		}, | 
|  | 34 | +		{ | 
|  | 35 | +			name:              "ConfigMap with empty annotations", | 
|  | 36 | +			annotations:       map[string]string{}, | 
|  | 37 | +			expectSkipRestore: false, | 
|  | 38 | +		}, | 
|  | 39 | +		{ | 
|  | 40 | +			name: "ConfigMap with skip annotation set to true", | 
|  | 41 | +			annotations: map[string]string{ | 
|  | 42 | +				common.SkipBuildConfigConfigMapRestore: "true", | 
|  | 43 | +			}, | 
|  | 44 | +			expectSkipRestore: true, | 
|  | 45 | +		}, | 
|  | 46 | +		{ | 
|  | 47 | +			name: "ConfigMap with skip annotation set to false", | 
|  | 48 | +			annotations: map[string]string{ | 
|  | 49 | +				common.SkipBuildConfigConfigMapRestore: "false", | 
|  | 50 | +			}, | 
|  | 51 | +			expectSkipRestore: false, | 
|  | 52 | +		}, | 
|  | 53 | +		{ | 
|  | 54 | +			name: "ConfigMap with skip annotation set to invalid value", | 
|  | 55 | +			annotations: map[string]string{ | 
|  | 56 | +				common.SkipBuildConfigConfigMapRestore: "invalid", | 
|  | 57 | +			}, | 
|  | 58 | +			expectSkipRestore: false, | 
|  | 59 | +		}, | 
|  | 60 | +		{ | 
|  | 61 | +			name: "ConfigMap with other annotations", | 
|  | 62 | +			annotations: map[string]string{ | 
|  | 63 | +				"some-other-annotation": "value", | 
|  | 64 | +			}, | 
|  | 65 | +			expectSkipRestore: false, | 
|  | 66 | +		}, | 
|  | 67 | +	} | 
|  | 68 | + | 
|  | 69 | +	for _, tt := range tests { | 
|  | 70 | +		t.Run(tt.name, func(t *testing.T) { | 
|  | 71 | +			item := &unstructured.Unstructured{} | 
|  | 72 | +			item.SetAPIVersion("v1") | 
|  | 73 | +			item.SetKind("ConfigMap") | 
|  | 74 | +			item.SetNamespace("test-ns") | 
|  | 75 | +			item.SetName("test-configmap") | 
|  | 76 | +			if tt.annotations != nil { | 
|  | 77 | +				item.SetAnnotations(tt.annotations) | 
|  | 78 | +			} | 
|  | 79 | + | 
|  | 80 | +			input := &velero.RestoreItemActionExecuteInput{ | 
|  | 81 | +				Item: item, | 
|  | 82 | +			} | 
|  | 83 | + | 
|  | 84 | +			output, err := restorePlugin.Execute(input) | 
|  | 85 | +			require.NoError(t, err) | 
|  | 86 | +			assert.Equal(t, tt.expectSkipRestore, output.SkipRestore) | 
|  | 87 | +		}) | 
|  | 88 | +	} | 
|  | 89 | +} | 
0 commit comments