Skip to content

Commit

Permalink
Stash v1beta1 E2E test for DaemonSet (#741)
Browse files Browse the repository at this point in the history
xref: #707

Tasks:

 [x] Workload backup and restore for DaemonSet
  • Loading branch information
suaas21 authored and tamalsaha committed Apr 17, 2019
1 parent 62960e5 commit 286792a
Show file tree
Hide file tree
Showing 12 changed files with 376 additions and 33 deletions.
4 changes: 2 additions & 2 deletions hack/deploy/rbac-list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ rules:
- "*"
verbs: ["*"]
- apiGroups:
- appcatalog.appscode.com
- appcatalog.appscode.com
resources:
- "*"
- "*"
verbs: ["*"]
- apiGroups:
- apps
Expand Down
14 changes: 13 additions & 1 deletion pkg/controller/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (c *StashController) ensureBackupSidecarDeleted(w *wapi.Workload, bc *api_v
func (c *StashController) ensureWorkloadLatestState(w *wapi.Workload) (bool, error) {
stateChanged := false

err := wait.PollImmediateInfinite(3*time.Second, func() (done bool, err error) {
err := wait.PollImmediate(3*time.Second, 5*time.Minute, func() (done bool, err error) {
r, err := metav1.LabelSelectorAsSelector(w.Spec.Selector)
if err != nil {
return false, err
Expand All @@ -274,6 +274,9 @@ func (c *StashController) ensureWorkloadLatestState(w *wapi.Workload) (bool, err
// we have to restart these pods so that it starts with latest update
var podsToRestart []core.Pod
for _, pod := range pods.Items {
if !isPodOwnedByWorkload(w, pod) {
continue
}
podSidecarState := hasStashSidecar(pod.Spec.Containers)
podInitContainerState := hasStashInitContainer(pod.Spec.InitContainers)
podBackupResourceHash := util.GetString(pod.Annotations, api_v1beta1.AppliedBackupConfigurationSpecHash)
Expand Down Expand Up @@ -305,3 +308,12 @@ func (c *StashController) ensureWorkloadLatestState(w *wapi.Workload) (bool, err

return stateChanged, nil
}

func isPodOwnedByWorkload(w *wapi.Workload, pod core.Pod) bool {
for _, ref := range pod.OwnerReferences {
if ref.Kind == w.Kind && ref.Name == w.Name {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func GetHostName(target *api_v1beta1.Target) (string, error) {
podOrdinal := podInfo[len(podInfo)-1]
return "host-" + podOrdinal, nil
case apis.KindDaemonSet:
nodeName := os.Getenv("POD_NAME")
nodeName := os.Getenv("NODE_NAME")
if nodeName == "" {
return "", fmt.Errorf("missing nodeName for %s", apis.KindDaemonSet)
}
Expand Down
3 changes: 1 addition & 2 deletions test/e2e/framework/backup_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/appscode/stash/apis/stash/v1alpha1"
"github.com/appscode/stash/apis/stash/v1beta1"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -47,5 +46,5 @@ func (f *Invocation) CreateBackupConfiguration(backupCfg v1beta1.BackupConfigura
}

func (f *Invocation) DeleteBackupConfiguration(backupCfg v1beta1.BackupConfiguration) error {
return f.StashClient.StashV1beta1().BackupConfigurations(backupCfg.Namespace).Delete(backupCfg.Name, &v1.DeleteOptions{})
return f.StashClient.StashV1beta1().BackupConfigurations(backupCfg.Namespace).Delete(backupCfg.Name, &metav1.DeleteOptions{})
}
14 changes: 7 additions & 7 deletions test/e2e/framework/backup_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (f *Framework) EventuallyBackupSessionPhase(meta metav1.ObjectMeta) GomegaA

func (f *Framework) EventuallyBackupSessionCreated(meta metav1.ObjectMeta) GomegaAsyncAssertion {
return Eventually(
func() bool {
func() bool {
backupsnlist, err := f.StashClient.StashV1beta1().BackupSessions(meta.Namespace).List(metav1.ListOptions{})
Expect(err).NotTo(HaveOccurred())
if len(backupsnlist.Items) > 0 {
Expand All @@ -34,15 +34,15 @@ func (f *Framework) EventuallyBackupSessionCreated(meta metav1.ObjectMeta) Gomeg
)
}

func (f *Framework) GetBackupSession(meta metav1.ObjectMeta) (*v1beta1.BackupSession, error){
func (f *Framework) GetBackupSession(meta metav1.ObjectMeta) (*v1beta1.BackupSession, error) {
backupsnlist, err := f.StashClient.StashV1beta1().BackupSessions(meta.Namespace).List(metav1.ListOptions{})
if err != nil{
return nil,err
if err != nil {
return nil, err
}
if len(backupsnlist.Items)>0{
return &backupsnlist.Items[0],nil
if len(backupsnlist.Items) > 0 {
return &backupsnlist.Items[0], nil
}
return nil,fmt.Errorf("no BackupSession found")
return nil, fmt.Errorf("no BackupSession found")
}

func (f *Framework) EventuallyBackupSessionTotalHost(meta metav1.ObjectMeta) GomegaAsyncAssertion {
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/framework/daemonset.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package framework

import (
"time"

"github.com/appscode/go/crypto/rand"
. "github.com/onsi/gomega"
apps "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/intstr"
)

Expand Down Expand Up @@ -54,3 +57,23 @@ func (f *Framework) EventuallyDaemonSet(meta metav1.ObjectMeta) GomegaAsyncAsser
return obj
})
}

func (f *Framework) EventuallyPodAccessible(meta metav1.ObjectMeta) GomegaAsyncAssertion {
return Eventually(func() bool {
labelSelector := fields.SelectorFromSet(meta.Labels)
podList, err := f.KubeClient.CoreV1().Pods(meta.Namespace).List(metav1.ListOptions{LabelSelector: labelSelector.String()})
Expect(err).NotTo(HaveOccurred())

for _, pod := range podList.Items {
_, err := f.ExecOnPod(&pod, "ls", "-R")
if err == nil {
return true
}
}
return false
},
time.Minute*2,
time.Second*2,
)

}
3 changes: 1 addition & 2 deletions test/e2e/framework/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func (fi *Invocation) PodTemplate(labels map[string]string, pvcName string) core
}

func (f *Framework) GetPod(meta metav1.ObjectMeta) (*core.Pod, error) {
labelSelector := fields.SelectorFromSet(meta.Labels)
podList, err := f.KubeClient.CoreV1().Pods(meta.Namespace).List(metav1.ListOptions{LabelSelector: labelSelector.String()})
podList, err := f.KubeClient.CoreV1().Pods(meta.Namespace).List(metav1.ListOptions{})
if err != nil {
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/framework/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func (f *Framework) DeleteRepositories(repositories []*api.Repository) {
Expect(err).NotTo(HaveOccurred())
}
}

func (f *Framework) DeleteRepository(repository *api.Repository) error {
err := f.StashClient.StashV1alpha1().Repositories(repository.Namespace).Delete(repository.Name, deleteInBackground())
return err
}
func (f *Framework) BrowseResticRepository(repository *api.Repository) ([]stow.Item, error) {
cfg, err := osm.NewOSMContext(f.KubeClient, repository)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/framework/restore_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func (f *Invocation) CreateRestoreSession(restoreSession v1beta1.RestoreSession)
return err
}

func (f Invocation) DeleteRestoreSession(meta metav1.ObjectMeta) error{
_,err := f.StashClient.StashV1beta1().RestoreSessions(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
func (f Invocation) DeleteRestoreSession(meta metav1.ObjectMeta) error {
err := f.StashClient.StashV1beta1().RestoreSessions(meta.Namespace).Delete(meta.Name, &metav1.DeleteOptions{})
return err
}

Expand Down
9 changes: 9 additions & 0 deletions test/e2e/framework/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package framework
import (
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
core_util "kmodules.xyz/client-go/core/v1"
)

const (
Expand Down Expand Up @@ -38,3 +39,11 @@ func (f *Framework) CreateService(obj core.Service) error {
func (f *Framework) DeleteService(meta metav1.ObjectMeta) error {
return f.KubeClient.CoreV1().Services(meta.Namespace).Delete(meta.Name, deleteInForeground())
}

func (f *Framework) CreateOrPatchService(obj core.Service) error {
_, _, err := core_util.CreateOrPatchService(f.KubeClient, obj.ObjectMeta, func(in *core.Service) *core.Service {
in.Spec = obj.Spec
return in
})
return err
}
36 changes: 32 additions & 4 deletions test/e2e/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
)

var (
files = []string{"test-data1.txt", "test-data2.txt", "test-data3.txt", "test-data4.txt", "test-data5.txt"}
)
Expand Down Expand Up @@ -340,7 +341,7 @@ func GetPathsFromResticFileGroups(restic *api.Restic) []string {

func GetPathsFromRestoreSession(restoreSession *v1beta1.RestoreSession) []string {
paths := make([]string, 0)
for i, _ := range restoreSession.Spec.Rules {
for i := range restoreSession.Spec.Rules {
for _, p := range restoreSession.Spec.Rules[i].Paths {
paths = append(paths, p)
}
Expand All @@ -353,13 +354,13 @@ func removeDuplicates(elements []string) []string {
encountered := map[string]bool{}

// Create a map of all unique elements.
for v:= range elements {
for v := range elements {
encountered[elements[v]] = true
}

// Place all keys from the map into a slice.
result := []string{}
for key, _ := range encountered {
for key := range encountered {
result = append(result, key)
}
return result
Expand Down Expand Up @@ -521,6 +522,19 @@ func WaitUntilBackupConfigurationDeleted(sc cs.Interface, meta metav1.ObjectMeta
})
}

func WaitUntilRestoreSessionDeleted(sc cs.Interface, meta metav1.ObjectMeta) error {
return wait.PollImmediate(PullInterval, WaitTimeOut, func() (done bool, err error) {
if _, err := sc.StashV1beta1().RestoreSessions(meta.Namespace).Get(meta.Name, metav1.GetOptions{}); err != nil {
if kerr.IsNotFound(err) {
return true, nil
} else {
return true, err
}
}
return false, nil
})
}

func WaitUntilRecoveryDeleted(sc cs.Interface, meta metav1.ObjectMeta) error {

return wait.PollImmediate(PullInterval, WaitTimeOut, func() (done bool, err error) {
Expand Down Expand Up @@ -555,6 +569,19 @@ func WaitUntilRepositoriesDeleted(sc cs.Interface, repositories []*api.Repositor
return false, nil
})
}
func WaitUntilRepositoryDeleted(sc cs.Interface, repository *api.Repository) error {

return wait.PollImmediate(PullInterval, WaitTimeOut, func() (done bool, err error) {
if _, err := sc.StashV1alpha1().Repositories(repository.Namespace).Get(repository.Name, metav1.GetOptions{}); err != nil {
if kerr.IsNotFound(err) {
return true, nil
} else {
return true, err
}
}
return false, nil
})
}

func (f *Framework) WaitUntilDaemonPodReady(meta metav1.ObjectMeta) error {

Expand All @@ -563,10 +590,11 @@ func (f *Framework) WaitUntilDaemonPodReady(meta metav1.ObjectMeta) error {
if err != nil {
return false, nil
}
if pod.Status.Phase == core.PodPhase(core.PodReady) {
if pod.Status.Phase == core.PodPhase(core.PodRunning) {
return true, nil
}
return false, nil

})
}

Expand Down
Loading

0 comments on commit 286792a

Please sign in to comment.