2017-09-12 19:54:08 +00:00
|
|
|
/*
|
2018-01-02 18:51:49 +00:00
|
|
|
Copyright 2017 the Heptio Ark contributors.
|
2017-09-12 19:54:08 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2017-12-11 22:10:52 +00:00
|
|
|
"encoding/json"
|
2017-09-12 19:54:08 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-09-14 21:27:31 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2017-09-12 19:54:08 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
core "k8s.io/client-go/testing"
|
|
|
|
|
|
|
|
"github.com/heptio/ark/pkg/apis/ark/v1"
|
2017-10-25 16:42:03 +00:00
|
|
|
"github.com/heptio/ark/pkg/generated/clientset/versioned/fake"
|
2017-09-12 19:54:08 +00:00
|
|
|
informers "github.com/heptio/ark/pkg/generated/informers/externalversions"
|
2017-12-11 22:10:52 +00:00
|
|
|
"github.com/heptio/ark/pkg/util/collections"
|
2017-12-12 23:22:46 +00:00
|
|
|
arktest "github.com/heptio/ark/pkg/util/test"
|
2017-09-12 19:54:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProcessDownloadRequest(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
key string
|
|
|
|
phase v1.DownloadRequestPhase
|
|
|
|
targetKind v1.DownloadTargetKind
|
|
|
|
targetName string
|
2018-04-02 22:34:19 +00:00
|
|
|
restore *v1.Restore
|
2017-09-12 19:54:08 +00:00
|
|
|
expectedError string
|
2018-04-02 22:34:19 +00:00
|
|
|
expectedDir string
|
2017-09-12 19:54:08 +00:00
|
|
|
expectedPhase v1.DownloadRequestPhase
|
|
|
|
expectedURL string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty key",
|
|
|
|
key: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bad key format",
|
|
|
|
key: "a/b/c",
|
2017-09-14 21:27:31 +00:00
|
|
|
expectedError: `error splitting queue key: unexpected key format: "a/b/c"`,
|
2017-09-12 19:54:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "backup log request with phase '' gets a url",
|
|
|
|
key: "heptio-ark/dr1",
|
|
|
|
phase: "",
|
|
|
|
targetKind: v1.DownloadTargetKindBackupLog,
|
|
|
|
targetName: "backup1",
|
2018-04-02 22:34:19 +00:00
|
|
|
expectedDir: "backup1",
|
2017-09-12 19:54:08 +00:00
|
|
|
expectedPhase: v1.DownloadRequestPhaseProcessed,
|
|
|
|
expectedURL: "signedURL",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "backup log request with phase 'New' gets a url",
|
|
|
|
key: "heptio-ark/dr1",
|
|
|
|
phase: v1.DownloadRequestPhaseNew,
|
|
|
|
targetKind: v1.DownloadTargetKindBackupLog,
|
|
|
|
targetName: "backup1",
|
2018-04-02 22:34:19 +00:00
|
|
|
expectedDir: "backup1",
|
2017-09-12 19:54:08 +00:00
|
|
|
expectedPhase: v1.DownloadRequestPhaseProcessed,
|
|
|
|
expectedURL: "signedURL",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "restore log request with phase '' gets a url",
|
|
|
|
key: "heptio-ark/dr1",
|
|
|
|
phase: "",
|
|
|
|
targetKind: v1.DownloadTargetKindRestoreLog,
|
|
|
|
targetName: "backup1-20170912150214",
|
2018-04-02 22:34:19 +00:00
|
|
|
restore: arktest.NewTestRestore(v1.DefaultNamespace, "backup1-20170912150214", v1.RestorePhaseCompleted).WithBackup("backup1").Restore,
|
|
|
|
expectedDir: "backup1",
|
2017-09-12 19:54:08 +00:00
|
|
|
expectedPhase: v1.DownloadRequestPhaseProcessed,
|
|
|
|
expectedURL: "signedURL",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "restore log request with phase New gets a url",
|
|
|
|
key: "heptio-ark/dr1",
|
|
|
|
phase: v1.DownloadRequestPhaseNew,
|
|
|
|
targetKind: v1.DownloadTargetKindRestoreLog,
|
|
|
|
targetName: "backup1-20170912150214",
|
2018-04-02 22:34:19 +00:00
|
|
|
restore: arktest.NewTestRestore(v1.DefaultNamespace, "backup1-20170912150214", v1.RestorePhaseCompleted).WithBackup("backup1").Restore,
|
|
|
|
expectedDir: "backup1",
|
2017-09-12 19:54:08 +00:00
|
|
|
expectedPhase: v1.DownloadRequestPhaseProcessed,
|
|
|
|
expectedURL: "signedURL",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2017-09-14 21:27:31 +00:00
|
|
|
var (
|
|
|
|
client = fake.NewSimpleClientset()
|
|
|
|
sharedInformers = informers.NewSharedInformerFactory(client, 0)
|
|
|
|
downloadRequestsInformer = sharedInformers.Ark().V1().DownloadRequests()
|
2018-04-02 22:34:19 +00:00
|
|
|
restoresInformer = sharedInformers.Ark().V1().Restores()
|
2017-12-12 23:22:46 +00:00
|
|
|
backupService = &arktest.BackupService{}
|
|
|
|
logger = arktest.NewLogger()
|
2017-09-14 21:27:31 +00:00
|
|
|
)
|
2017-09-12 19:54:08 +00:00
|
|
|
defer backupService.AssertExpectations(t)
|
|
|
|
|
|
|
|
c := NewDownloadRequestController(
|
|
|
|
client.ArkV1(),
|
|
|
|
downloadRequestsInformer,
|
2018-04-02 22:34:19 +00:00
|
|
|
restoresInformer,
|
2017-09-12 19:54:08 +00:00
|
|
|
backupService,
|
|
|
|
"bucket",
|
2017-09-14 21:27:31 +00:00
|
|
|
logger,
|
2017-09-12 19:54:08 +00:00
|
|
|
).(*downloadRequestController)
|
|
|
|
|
2017-12-11 22:10:52 +00:00
|
|
|
var downloadRequest *v1.DownloadRequest
|
|
|
|
|
2017-09-12 19:54:08 +00:00
|
|
|
if tc.expectedPhase == v1.DownloadRequestPhaseProcessed {
|
|
|
|
target := v1.DownloadTarget{
|
|
|
|
Kind: tc.targetKind,
|
|
|
|
Name: tc.targetName,
|
|
|
|
}
|
|
|
|
|
2017-12-11 22:10:52 +00:00
|
|
|
downloadRequest = &v1.DownloadRequest{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Namespace: v1.DefaultNamespace,
|
|
|
|
Name: "dr1",
|
|
|
|
},
|
|
|
|
Spec: v1.DownloadRequestSpec{
|
|
|
|
Target: target,
|
2017-09-12 19:54:08 +00:00
|
|
|
},
|
2017-12-11 22:10:52 +00:00
|
|
|
}
|
|
|
|
downloadRequestsInformer.Informer().GetStore().Add(downloadRequest)
|
2017-09-12 19:54:08 +00:00
|
|
|
|
2018-04-02 22:34:19 +00:00
|
|
|
if tc.restore != nil {
|
|
|
|
restoresInformer.Informer().GetStore().Add(tc.restore)
|
|
|
|
}
|
|
|
|
|
|
|
|
backupService.On("CreateSignedURL", target, "bucket", tc.expectedDir, 10*time.Minute).Return("signedURL", nil)
|
2017-09-12 19:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// method under test
|
|
|
|
err := c.processDownloadRequest(tc.key)
|
|
|
|
|
|
|
|
if tc.expectedError != "" {
|
|
|
|
assert.EqualError(t, err, tc.expectedError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-12-11 22:10:52 +00:00
|
|
|
actions := client.Actions()
|
|
|
|
|
|
|
|
// if we don't expect a phase update, this means
|
|
|
|
// we don't expect any actions to take place
|
|
|
|
if tc.expectedPhase == "" {
|
|
|
|
require.Equal(t, 0, len(actions))
|
|
|
|
return
|
2017-09-12 19:54:08 +00:00
|
|
|
}
|
2017-12-11 22:10:52 +00:00
|
|
|
|
|
|
|
// otherwise, we should get exactly 1 patch
|
|
|
|
require.Equal(t, 1, len(actions))
|
|
|
|
patchAction, ok := actions[0].(core.PatchAction)
|
|
|
|
require.True(t, ok, "action is not a PatchAction")
|
|
|
|
|
|
|
|
patch := make(map[string]interface{})
|
|
|
|
require.NoError(t, json.Unmarshal(patchAction.GetPatch(), &patch), "cannot unmarshal patch")
|
|
|
|
|
|
|
|
// check the URL
|
|
|
|
assert.True(t, collections.HasKeyAndVal(patch, "status.downloadURL", tc.expectedURL), "patch's status.downloadURL does not match")
|
|
|
|
|
|
|
|
// check the Phase
|
|
|
|
assert.True(t, collections.HasKeyAndVal(patch, "status.phase", string(tc.expectedPhase)), "patch's status.phase does not match")
|
|
|
|
|
|
|
|
// check that Expiration exists
|
|
|
|
// TODO pass a fake clock to the controller and verify
|
|
|
|
// the expiration value
|
|
|
|
assert.True(t, collections.Exists(patch, "status.expiration"), "patch's status.expiration does not exist")
|
|
|
|
|
|
|
|
// we expect 3 total updates.
|
|
|
|
res, _ := collections.GetMap(patch, "status")
|
|
|
|
assert.Equal(t, 3, len(res), "patch's status has the wrong number of keys")
|
2017-09-12 19:54:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|