Initial commit

Signed-off-by: Andy Goldstein <andy.goldstein@gmail.com>
This commit is contained in:
Andy Goldstein
2017-08-02 13:27:17 -04:00
commit 2fe501f527
2024 changed files with 948288 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
/*
Copyright 2017 Heptio Inc.
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 test
import (
"io"
"github.com/stretchr/testify/mock"
"github.com/heptio/ark/pkg/apis/ark/v1"
)
type FakeBackupService struct {
mock.Mock
}
func (f *FakeBackupService) GetAllBackups(bucket string) ([]*v1.Backup, error) {
args := f.Called(bucket)
var backups []*v1.Backup
b := args.Get(0)
if b != nil {
backups = b.([]*v1.Backup)
}
return backups, args.Error(1)
}
func (f *FakeBackupService) UploadBackup(bucket, name string, metadata, backup io.ReadSeeker) error {
args := f.Called(bucket, name, metadata, backup)
return args.Error(0)
}
func (f *FakeBackupService) DownloadBackup(bucket, name string) (io.ReadCloser, error) {
args := f.Called(bucket, name)
return args.Get(0).(io.ReadCloser), args.Error(1)
}
func (f *FakeBackupService) DeleteBackup(bucket, backupName string) error {
args := f.Called(bucket, backupName)
return args.Error(0)
}
+66
View File
@@ -0,0 +1,66 @@
/*
Copyright 2017 Heptio Inc.
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 test
import (
"github.com/stretchr/testify/mock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
"github.com/heptio/ark/pkg/client"
)
type FakeDynamicFactory struct {
mock.Mock
}
var _ client.DynamicFactory = &FakeDynamicFactory{}
func (df *FakeDynamicFactory) ClientForGroupVersionResource(gvr schema.GroupVersionResource, resource metav1.APIResource, namespace string) (client.Dynamic, error) {
args := df.Called(gvr, resource, namespace)
return args.Get(0).(client.Dynamic), args.Error(1)
}
func (df *FakeDynamicFactory) ClientForGroupVersionKind(gvk schema.GroupVersionKind, resource metav1.APIResource, namespace string) (client.Dynamic, error) {
args := df.Called(gvk, resource, namespace)
return args.Get(0).(client.Dynamic), args.Error(1)
}
type FakeDynamicClient struct {
mock.Mock
}
var _ client.Dynamic = &FakeDynamicClient{}
func (c *FakeDynamicClient) List(options metav1.ListOptions) (runtime.Object, error) {
args := c.Called(options)
return args.Get(0).(runtime.Object), args.Error(1)
}
func (c *FakeDynamicClient) Create(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
args := c.Called(obj)
return args.Get(0).(*unstructured.Unstructured), args.Error(1)
}
func (c *FakeDynamicClient) Watch(options metav1.ListOptions) (watch.Interface, error) {
args := c.Called(options)
return args.Get(0).(watch.Interface), args.Error(1)
}
+49
View File
@@ -0,0 +1,49 @@
/*
Copyright 2017 Heptio Inc.
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 test
import (
"errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
)
type FakeMapper struct {
meta.RESTMapper
AutoReturnResource bool
Resources map[schema.GroupVersionResource]schema.GroupVersionResource
}
func (m *FakeMapper) ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error) {
if m.AutoReturnResource {
return schema.GroupVersionResource{
Group: input.Group,
Version: input.Version,
Resource: input.Resource,
}, nil
}
if m.Resources == nil {
return schema.GroupVersionResource{}, errors.New("invalid resource")
}
if gr, found := m.Resources[input]; found {
return gr, nil
}
return schema.GroupVersionResource{}, errors.New("invalid resource")
}
+81
View File
@@ -0,0 +1,81 @@
/*
Copyright 2017 Heptio Inc.
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 test
import (
"errors"
"k8s.io/apimachinery/pkg/util/sets"
api "github.com/heptio/ark/pkg/apis/ark/v1"
)
type FakeSnapshotService struct {
// SnapshotID->VolumeID
SnapshotsTaken sets.String
// VolumeID -> (SnapshotID, Type, Iops)
SnapshottableVolumes map[string]api.VolumeBackupInfo
// VolumeBackupInfo -> VolumeID
RestorableVolumes map[api.VolumeBackupInfo]string
}
func (s *FakeSnapshotService) GetAllSnapshots() ([]string, error) {
return s.SnapshotsTaken.List(), nil
}
func (s *FakeSnapshotService) CreateSnapshot(volumeID string) (string, error) {
if _, exists := s.SnapshottableVolumes[volumeID]; !exists {
return "", errors.New("snapshottable volume not found")
}
if s.SnapshotsTaken == nil {
s.SnapshotsTaken = sets.NewString()
}
s.SnapshotsTaken.Insert(s.SnapshottableVolumes[volumeID].SnapshotID)
return s.SnapshottableVolumes[volumeID].SnapshotID, nil
}
func (s *FakeSnapshotService) CreateVolumeFromSnapshot(snapshotID, volumeType string, iops *int) (string, error) {
key := api.VolumeBackupInfo{
SnapshotID: snapshotID,
Type: volumeType,
Iops: iops,
}
return s.RestorableVolumes[key], nil
}
func (s *FakeSnapshotService) DeleteSnapshot(snapshotID string) error {
if !s.SnapshotsTaken.Has(snapshotID) {
return errors.New("snapshot not found")
}
s.SnapshotsTaken.Delete(snapshotID)
return nil
}
func (s *FakeSnapshotService) GetVolumeInfo(volumeID string) (string, *int, error) {
if volumeInfo, exists := s.SnapshottableVolumes[volumeID]; !exists {
return "", nil, errors.New("VolumeID not found")
} else {
return volumeInfo.Type, volumeInfo.Iops, nil
}
}
+106
View File
@@ -0,0 +1,106 @@
/*
Copyright 2017 Heptio Inc.
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 test
import (
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/heptio/ark/pkg/apis/ark/v1"
)
type TestBackup struct {
*v1.Backup
}
func NewTestBackup() *TestBackup {
return &TestBackup{
Backup: &v1.Backup{
ObjectMeta: metav1.ObjectMeta{
Namespace: v1.DefaultNamespace,
},
},
}
}
func (b *TestBackup) WithNamespace(namespace string) *TestBackup {
b.Namespace = namespace
return b
}
func (b *TestBackup) WithName(name string) *TestBackup {
b.Name = name
return b
}
func (b *TestBackup) WithLabel(key, value string) *TestBackup {
if b.Labels == nil {
b.Labels = make(map[string]string)
}
b.Labels[key] = value
return b
}
func (b *TestBackup) WithPhase(phase v1.BackupPhase) *TestBackup {
b.Status.Phase = phase
return b
}
func (b *TestBackup) WithIncludedResources(r ...string) *TestBackup {
b.Spec.IncludedResources = r
return b
}
func (b *TestBackup) WithExcludedResources(r ...string) *TestBackup {
b.Spec.ExcludedResources = r
return b
}
func (b *TestBackup) WithIncludedNamespaces(ns ...string) *TestBackup {
b.Spec.IncludedNamespaces = ns
return b
}
func (b *TestBackup) WithExcludedNamespaces(ns ...string) *TestBackup {
b.Spec.ExcludedNamespaces = ns
return b
}
func (b *TestBackup) WithTTL(ttl time.Duration) *TestBackup {
b.Spec.TTL = metav1.Duration{Duration: ttl}
return b
}
func (b *TestBackup) WithExpiration(expiration time.Time) *TestBackup {
b.Status.Expiration = metav1.Time{Time: expiration}
return b
}
func (b *TestBackup) WithVersion(version int) *TestBackup {
b.Status.Version = version
return b
}
func (b *TestBackup) WithSnapshot(pv string, snapshot string) *TestBackup {
if b.Status.VolumeBackups == nil {
b.Status.VolumeBackups = make(map[string]*v1.VolumeBackupInfo)
}
b.Status.VolumeBackups[pv] = &v1.VolumeBackupInfo{SnapshotID: snapshot}
return b
}
+62
View File
@@ -0,0 +1,62 @@
/*
Copyright 2017 Heptio Inc.
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 test
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
api "github.com/heptio/ark/pkg/apis/ark/v1"
)
type TestRestore struct {
*api.Restore
}
func NewTestRestore(ns, name string, phase api.RestorePhase) *TestRestore {
return &TestRestore{
Restore: &api.Restore{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
},
Spec: api.RestoreSpec{},
Status: api.RestoreStatus{
Phase: phase,
},
},
}
}
func (r *TestRestore) WithRestorableNamespace(name string) *TestRestore {
r.Spec.Namespaces = append(r.Spec.Namespaces, name)
return r
}
func (r *TestRestore) WithValidationError(err string) *TestRestore {
r.Status.ValidationErrors = append(r.Status.ValidationErrors, err)
return r
}
func (r *TestRestore) WithBackup(name string) *TestRestore {
r.Spec.BackupName = name
return r
}
func (r *TestRestore) WithErrors(e api.RestoreResult) *TestRestore {
r.Status.Errors = e
return r
}
+61
View File
@@ -0,0 +1,61 @@
/*
Copyright 2017 Heptio Inc.
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 test
import (
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
api "github.com/heptio/ark/pkg/apis/ark/v1"
)
type TestSchedule struct {
*api.Schedule
}
func NewTestSchedule(namespace, name string) *TestSchedule {
return &TestSchedule{
Schedule: &api.Schedule{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
},
}
}
func (s *TestSchedule) WithPhase(phase api.SchedulePhase) *TestSchedule {
s.Status.Phase = phase
return s
}
func (s *TestSchedule) WithValidationError(msg string) *TestSchedule {
s.Status.ValidationErrors = append(s.Status.ValidationErrors, msg)
return s
}
func (s *TestSchedule) WithCronSchedule(cronExpression string) *TestSchedule {
s.Spec.Schedule = cronExpression
return s
}
func (s *TestSchedule) WithLastBackupTime(timeString string) *TestSchedule {
t, _ := time.Parse("2006-01-02 15:04:05", timeString)
s.Status.LastBackup = metav1.Time{Time: t}
return s
}