2017-08-02 17:27:17 +00:00
|
|
|
/*
|
2017-11-21 17:24:43 +00:00
|
|
|
Copyright 2017 the Heptio Ark contributors.
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-11-21 17:24:43 +00:00
|
|
|
package restore
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2018-10-23 14:36:11 +00:00
|
|
|
|
2019-01-25 03:33:07 +00:00
|
|
|
velerotest "github.com/heptio/velero/pkg/util/test"
|
2017-08-02 17:27:17 +00:00
|
|
|
)
|
|
|
|
|
2017-11-21 17:24:43 +00:00
|
|
|
func TestPodActionExecute(t *testing.T) {
|
2017-08-02 17:27:17 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
obj runtime.Unstructured
|
|
|
|
expectedErr bool
|
|
|
|
expectedRes runtime.Unstructured
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no spec should error",
|
|
|
|
obj: NewTestUnstructured().WithName("pod-1").Unstructured,
|
|
|
|
expectedErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nodeName (only) should be deleted from spec",
|
|
|
|
obj: NewTestUnstructured().WithName("pod-1").WithSpec("nodeName", "foo").
|
|
|
|
WithSpecField("containers", []interface{}{}).
|
|
|
|
Unstructured,
|
|
|
|
expectedErr: false,
|
|
|
|
expectedRes: NewTestUnstructured().WithName("pod-1").WithSpec("foo").
|
2018-11-21 11:49:27 +00:00
|
|
|
WithSpecField("containers", []interface{}{}).
|
|
|
|
Unstructured,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "priority (only) should be deleted from spec",
|
|
|
|
obj: NewTestUnstructured().WithName("pod-1").WithSpec("priority", "foo").
|
|
|
|
WithSpecField("containers", []interface{}{}).
|
|
|
|
Unstructured,
|
|
|
|
expectedErr: false,
|
|
|
|
expectedRes: NewTestUnstructured().WithName("pod-1").WithSpec("foo").
|
2017-08-02 17:27:17 +00:00
|
|
|
WithSpecField("containers", []interface{}{}).
|
|
|
|
Unstructured,
|
|
|
|
},
|
|
|
|
{
|
2018-10-11 19:44:02 +00:00
|
|
|
name: "volumes matching prefix <service account name>-token- should be deleted",
|
2017-08-02 17:27:17 +00:00
|
|
|
obj: NewTestUnstructured().WithName("pod-1").
|
2018-09-12 21:28:00 +00:00
|
|
|
WithSpec("serviceAccountName", "foo").
|
2017-08-02 17:27:17 +00:00
|
|
|
WithSpecField("volumes", []interface{}{
|
|
|
|
map[string]interface{}{"name": "foo"},
|
2018-09-12 21:28:00 +00:00
|
|
|
map[string]interface{}{"name": "foo-token-foo"},
|
2018-10-11 19:44:02 +00:00
|
|
|
}).
|
|
|
|
WithSpecField("containers", []interface{}{}).
|
|
|
|
Unstructured,
|
2017-08-02 17:27:17 +00:00
|
|
|
expectedErr: false,
|
|
|
|
expectedRes: NewTestUnstructured().WithName("pod-1").
|
2018-09-12 21:28:00 +00:00
|
|
|
WithSpec("serviceAccountName", "foo").
|
2017-08-02 17:27:17 +00:00
|
|
|
WithSpecField("volumes", []interface{}{
|
|
|
|
map[string]interface{}{"name": "foo"},
|
2018-10-11 19:44:02 +00:00
|
|
|
}).
|
|
|
|
WithSpecField("containers", []interface{}{}).
|
|
|
|
Unstructured,
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
|
|
|
{
|
2018-10-11 19:44:02 +00:00
|
|
|
name: "container volumeMounts matching prefix <service account name>-token- should be deleted",
|
2017-08-02 17:27:17 +00:00
|
|
|
obj: NewTestUnstructured().WithName("svc-1").
|
2018-09-12 21:28:00 +00:00
|
|
|
WithSpec("serviceAccountName", "foo").
|
2018-10-11 19:44:02 +00:00
|
|
|
WithSpecField("volumes", []interface{}{
|
|
|
|
map[string]interface{}{"name": "foo"},
|
|
|
|
map[string]interface{}{"name": "foo-token-foo"},
|
|
|
|
}).
|
2017-08-02 17:27:17 +00:00
|
|
|
WithSpecField("containers", []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"volumeMounts": []interface{}{
|
2018-10-11 19:44:02 +00:00
|
|
|
map[string]interface{}{"name": "foo"},
|
|
|
|
map[string]interface{}{"name": "foo-token-foo"},
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}).
|
|
|
|
Unstructured,
|
|
|
|
expectedErr: false,
|
|
|
|
expectedRes: NewTestUnstructured().WithName("svc-1").
|
2018-09-12 21:28:00 +00:00
|
|
|
WithSpec("serviceAccountName", "foo").
|
2018-10-11 19:44:02 +00:00
|
|
|
WithSpecField("volumes", []interface{}{
|
|
|
|
map[string]interface{}{"name": "foo"},
|
|
|
|
}).
|
2017-08-02 17:27:17 +00:00
|
|
|
WithSpecField("containers", []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"volumeMounts": []interface{}{
|
2018-10-11 19:44:02 +00:00
|
|
|
map[string]interface{}{"name": "foo"},
|
2017-08-02 17:27:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}).
|
|
|
|
Unstructured,
|
|
|
|
},
|
2018-10-05 21:49:19 +00:00
|
|
|
{
|
2018-10-11 19:44:02 +00:00
|
|
|
name: "initContainer volumeMounts matching prefix <service account name>-token- should be deleted",
|
2018-10-05 21:49:19 +00:00
|
|
|
obj: NewTestUnstructured().WithName("svc-1").
|
|
|
|
WithSpec("serviceAccountName", "foo").
|
|
|
|
WithSpecField("containers", []interface{}{}).
|
2018-10-11 19:44:02 +00:00
|
|
|
WithSpecField("volumes", []interface{}{
|
|
|
|
map[string]interface{}{"name": "foo"},
|
|
|
|
map[string]interface{}{"name": "foo-token-foo"},
|
|
|
|
}).
|
2018-10-05 21:49:19 +00:00
|
|
|
WithSpecField("initContainers", []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"volumeMounts": []interface{}{
|
2018-10-11 19:44:02 +00:00
|
|
|
map[string]interface{}{"name": "foo"},
|
|
|
|
map[string]interface{}{"name": "foo-token-foo"},
|
2018-10-05 21:49:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}).
|
|
|
|
Unstructured,
|
|
|
|
expectedErr: false,
|
|
|
|
expectedRes: NewTestUnstructured().WithName("svc-1").
|
|
|
|
WithSpec("serviceAccountName", "foo").
|
|
|
|
WithSpecField("containers", []interface{}{}).
|
2018-10-11 19:44:02 +00:00
|
|
|
WithSpecField("volumes", []interface{}{
|
|
|
|
map[string]interface{}{"name": "foo"},
|
|
|
|
}).
|
2018-10-05 21:49:19 +00:00
|
|
|
WithSpecField("initContainers", []interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"volumeMounts": []interface{}{
|
2018-10-11 19:44:02 +00:00
|
|
|
map[string]interface{}{"name": "foo"},
|
2018-10-05 21:49:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}).
|
|
|
|
Unstructured,
|
|
|
|
},
|
2018-10-11 19:44:02 +00:00
|
|
|
{
|
|
|
|
name: "containers and initContainers with no volume mounts should not error",
|
|
|
|
obj: NewTestUnstructured().WithName("pod-1").
|
|
|
|
WithSpec("serviceAccountName", "foo").
|
|
|
|
WithSpecField("volumes", []interface{}{
|
|
|
|
map[string]interface{}{"name": "foo"},
|
|
|
|
map[string]interface{}{"name": "foo-token-foo"},
|
|
|
|
}).
|
|
|
|
WithSpecField("containers", []interface{}{}).
|
|
|
|
WithSpecField("initContainers", []interface{}{}).
|
|
|
|
Unstructured,
|
|
|
|
expectedErr: false,
|
|
|
|
expectedRes: NewTestUnstructured().WithName("pod-1").
|
|
|
|
WithSpec("serviceAccountName", "foo").
|
|
|
|
WithSpecField("volumes", []interface{}{
|
|
|
|
map[string]interface{}{"name": "foo"},
|
|
|
|
}).
|
|
|
|
WithSpecField("containers", []interface{}{}).
|
|
|
|
WithSpecField("initContainers", []interface{}{}).
|
|
|
|
Unstructured,
|
|
|
|
},
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2019-01-25 03:33:07 +00:00
|
|
|
action := NewPodAction(velerotest.NewLogger())
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-10-11 19:44:02 +00:00
|
|
|
res, warning, err := action.Execute(test.obj, nil)
|
|
|
|
|
|
|
|
assert.Nil(t, warning)
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-10-11 19:44:02 +00:00
|
|
|
if test.expectedErr {
|
|
|
|
assert.NotNil(t, err, "expected an error")
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, err, "expected no error, got %v", err)
|
2017-08-02 17:27:17 +00:00
|
|
|
}
|
2018-10-11 19:44:02 +00:00
|
|
|
|
|
|
|
assert.Equal(t, test.expectedRes, res)
|
2017-08-02 17:27:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|