|
| 1 | +/* |
| 2 | +Copyright 2018 Pressinfra SRL |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package clustercontroller |
| 18 | + |
| 19 | +import ( |
| 20 | + "sync" |
| 21 | + "time" |
| 22 | + |
| 23 | + "k8s.io/client-go/kubernetes/fake" |
| 24 | + "k8s.io/client-go/tools/record" |
| 25 | + |
| 26 | + . "github.com/onsi/ginkgo" |
| 27 | + . "github.com/onsi/gomega" |
| 28 | + . "github.com/onsi/gomega/gstruct" |
| 29 | + "github.com/robfig/cron" |
| 30 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | + |
| 32 | + api "github.com/presslabs/mysql-operator/pkg/apis/mysql/v1alpha1" |
| 33 | + fakeMyClient "github.com/presslabs/mysql-operator/pkg/generated/clientset/versioned/fake" |
| 34 | + "github.com/presslabs/mysql-operator/pkg/util/options" |
| 35 | + tutil "github.com/presslabs/mysql-operator/pkg/util/test" |
| 36 | +) |
| 37 | + |
| 38 | +var _ = Describe("Test cluster reconciliation queue", func() { |
| 39 | + var ( |
| 40 | + client *fake.Clientset |
| 41 | + myClient *fakeMyClient.Clientset |
| 42 | + rec *record.FakeRecorder |
| 43 | + cluster *api.MysqlCluster |
| 44 | + controller *Controller |
| 45 | + stop chan struct{} |
| 46 | + opt *options.Options |
| 47 | + ) |
| 48 | + |
| 49 | + BeforeEach(func() { |
| 50 | + opt = options.GetOptions() |
| 51 | + client = fake.NewSimpleClientset() |
| 52 | + myClient = fakeMyClient.NewSimpleClientset() |
| 53 | + rec = record.NewFakeRecorder(100) |
| 54 | + cluster = tutil.NewFakeCluster("backupscheduler") |
| 55 | + stop = make(chan struct{}) |
| 56 | + controller = newController(stop, client, myClient, rec) |
| 57 | + backupPollingTime = 10 * time.Millisecond |
| 58 | + backupWatchTimeout = time.Second |
| 59 | + |
| 60 | + }) |
| 61 | + |
| 62 | + AfterEach(func() { |
| 63 | + close(stop) |
| 64 | + }) |
| 65 | + |
| 66 | + Describe("Scheduled backups cron", func() { |
| 67 | + Context("cluster with schedule backups", func() { |
| 68 | + It("try to register multiple times", func() { |
| 69 | + cluster.Spec.BackupSchedule = "0 * * * *" |
| 70 | + _, err := myClient.MysqlV1alpha1().MysqlClusters(tutil.Namespace).Create(cluster) |
| 71 | + Expect(err).To(Succeed()) |
| 72 | + |
| 73 | + err = controller.registerClusterInBackupCron(cluster) |
| 74 | + Expect(err).To(Succeed()) |
| 75 | + |
| 76 | + Expect(controller.cron.Entries()).To(HaveLen(1)) |
| 77 | + |
| 78 | + err = controller.registerClusterInBackupCron(cluster) |
| 79 | + Expect(err).To(Succeed()) |
| 80 | + |
| 81 | + Expect(controller.cron.Entries()).To(HaveLen(1)) |
| 82 | + Expect(controller.cron.Entries()).To( |
| 83 | + ContainElement(PointTo(MatchFields(IgnoreExtras, Fields{ |
| 84 | + "Job": MatchFields(IgnoreExtras, Fields{ |
| 85 | + "Name": Equal(cluster.Name), |
| 86 | + "Namespace": Equal(cluster.Namespace), |
| 87 | + "BackupRunning": PointTo(Equal(false)), |
| 88 | + }), |
| 89 | + })))) |
| 90 | + }) |
| 91 | + It("try to register multiple times in parallel", func() { |
| 92 | + cluster2 := tutil.NewFakeCluster("bs2") |
| 93 | + |
| 94 | + cluster.Spec.BackupSchedule = "0 * * * *" |
| 95 | + cluster2.Spec.BackupSchedule = "0 * * * *" |
| 96 | + |
| 97 | + go controller.registerClusterInBackupCron(cluster) |
| 98 | + go controller.registerClusterInBackupCron(cluster2) |
| 99 | + go controller.registerClusterInBackupCron(cluster) |
| 100 | + go controller.registerClusterInBackupCron(cluster2) |
| 101 | + |
| 102 | + Eventually(func() []*cron.Entry { |
| 103 | + lockJobRegister.Lock() // avoid a data race |
| 104 | + defer lockJobRegister.Unlock() |
| 105 | + return controller.cron.Entries() |
| 106 | + }).Should(HaveLen(2)) |
| 107 | + }) |
| 108 | + |
| 109 | + It("start job to schedule a backup", func() { |
| 110 | + j := job{ |
| 111 | + Name: cluster.Name, |
| 112 | + Namespace: cluster.Namespace, |
| 113 | + myClient: myClient, |
| 114 | + BackupRunning: new(bool), |
| 115 | + lock: new(sync.Mutex), |
| 116 | + } |
| 117 | + go j.Run() |
| 118 | + go j.Run() |
| 119 | + |
| 120 | + Eventually(func() *bool { |
| 121 | + return j.BackupRunning |
| 122 | + }).Should(PointTo(Equal(true))) |
| 123 | + Eventually(func() []api.MysqlBackup { |
| 124 | + bs, _ := myClient.Mysql().MysqlBackups(j.Namespace).List(metav1.ListOptions{}) |
| 125 | + return bs.Items |
| 126 | + }).Should(HaveLen(1)) |
| 127 | + |
| 128 | + bs, _ := myClient.Mysql().MysqlBackups(j.Namespace).List(metav1.ListOptions{}) |
| 129 | + backup := bs.Items[0] |
| 130 | + backup.Status.Completed = true |
| 131 | + _, err := myClient.Mysql().MysqlBackups(j.Namespace).Update(&backup) |
| 132 | + Expect(err).To(Succeed()) |
| 133 | + |
| 134 | + Eventually(func() *bool { |
| 135 | + return j.BackupRunning |
| 136 | + }).Should(PointTo(Equal(false))) |
| 137 | + }) |
| 138 | + }) |
| 139 | + }) |
| 140 | +}) |
0 commit comments