Skip to content

Commit

Permalink
Merge 41112c0 into 1e7e89b
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-carvalho committed Jul 17, 2020
2 parents 1e7e89b + 41112c0 commit 8e50ba1
Show file tree
Hide file tree
Showing 10 changed files with 543 additions and 285 deletions.
35 changes: 18 additions & 17 deletions api/scheduler_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@ autoscaling:
}
scheduler1.Version = "v1.0"

operationFinished := false
mockRedisClient.EXPECT().TxPipeline().Return(mockPipeline)
mockPipeline.EXPECT().HMSet(gomock.Any(), gomock.Any()).Do(func(_ string, m map[string]interface{}) {
Expect(m).To(HaveKeyWithValue("status", http.StatusOK))
Expand All @@ -1050,7 +1051,7 @@ autoscaling:
mockPipeline.EXPECT().Expire(gomock.Any(), 10*time.Minute)
mockPipeline.EXPECT().Del(opManager.BuildCurrOpKey())
mockPipeline.EXPECT().Exec().Do(func() {
opManager.StopLoop()
operationFinished = true
})

// Update scheduler
Expand Down Expand Up @@ -1115,7 +1116,7 @@ autoscaling:
Expect(response).To(HaveKeyWithValue("success", true))
Expect(response).To(HaveKey("operationKey"))

Eventually(opManager.IsStopped, 30*time.Second).Should(BeTrue())
Eventually(func() bool { return operationFinished }, time.Minute, time.Second).Should(BeTrue())
})

It("should asynchronously update scheduler and show error when occurred", func() {
Expand All @@ -1141,6 +1142,7 @@ autoscaling:
"not": "empty",
}, nil)).AnyTimes()

operationFinished := false
mockRedisClient.EXPECT().TxPipeline().Return(mockPipeline)
mockPipeline.EXPECT().HMSet(gomock.Any(), gomock.Any()).Do(func(_ string, m map[string]interface{}) {
Expect(m).To(HaveKeyWithValue("status", http.StatusInternalServerError))
Expand All @@ -1152,7 +1154,7 @@ autoscaling:
mockPipeline.EXPECT().Expire(gomock.Any(), 10*time.Minute)
mockPipeline.EXPECT().Del(opManager.BuildCurrOpKey())
mockPipeline.EXPECT().Exec().Do(func() {
opManager.StopLoop()
operationFinished = true
})

pods, err := clientset.CoreV1().Pods(configYaml.Name).List(metav1.ListOptions{})
Expand Down Expand Up @@ -1196,8 +1198,7 @@ autoscaling:
json.Unmarshal(recorder.Body.Bytes(), &response)
Expect(response).To(HaveKeyWithValue("success", true))
Expect(response).To(HaveKey("operationKey"))

Eventually(opManager.IsStopped, 30*time.Second).Should(BeTrue())
Eventually(func() bool { return operationFinished }, time.Minute, time.Second).Should(BeTrue())
})
})

Expand Down Expand Up @@ -2798,6 +2799,7 @@ game: game-name
"not": "empty",
}, nil)).AnyTimes()

operationFinished := false
mockRedisClient.EXPECT().TxPipeline().Return(mockPipeline)
mockPipeline.EXPECT().HMSet(gomock.Any(), gomock.Any()).Do(func(_ string, m map[string]interface{}) {
Expect(m).To(HaveKeyWithValue("status", http.StatusOK))
Expand All @@ -2807,7 +2809,7 @@ game: game-name
mockPipeline.EXPECT().Expire(gomock.Any(), 10*time.Minute)
mockPipeline.EXPECT().Del(opManager.BuildCurrOpKey())
mockPipeline.EXPECT().Exec().Do(func() {
opManager.StopLoop()
operationFinished = true
})

// Update scheduler
Expand Down Expand Up @@ -2870,8 +2872,7 @@ game: game-name
json.Unmarshal(recorder.Body.Bytes(), &response)
Expect(response).To(HaveKeyWithValue("success", true))
Expect(recorder.Code).To(Equal(http.StatusOK))

Eventually(opManager.IsStopped, 30*time.Second).Should(BeTrue())
Eventually(func() bool { return operationFinished }, time.Minute, time.Second).Should(BeTrue())
})

It("should update image asynchronously and show error when occurred", func() {
Expand Down Expand Up @@ -2902,10 +2903,11 @@ game: game-name
Expect(m).To(HaveKeyWithValue("description", "failed to update scheduler image"))
Expect(m).To(HaveKeyWithValue("error", "redis error"))
})
operationFinished := false
mockPipeline.EXPECT().Expire(gomock.Any(), 10*time.Minute)
mockPipeline.EXPECT().Del(opManager.BuildCurrOpKey())
mockPipeline.EXPECT().Exec().Do(func() {
opManager.StopLoop()
operationFinished = true
})

newImageName := "new-image"
Expand Down Expand Up @@ -2936,8 +2938,7 @@ game: game-name
Expect(response).To(HaveKeyWithValue("success", true))
Expect(response).To(HaveKey("operationKey"))
Expect(recorder.Code).To(Equal(http.StatusOK))

Eventually(opManager.IsStopped, 1*time.Minute).Should(BeTrue())
Eventually(func() bool { return operationFinished }, time.Minute, time.Second).Should(BeTrue())
})
})

Expand Down Expand Up @@ -3317,10 +3318,11 @@ game: game-name
Expect(m).To(HaveKeyWithValue("operation", "UpdateSchedulerMin"))
Expect(m).To(HaveKeyWithValue("success", true))
})
operationFinished := false
mockPipeline.EXPECT().Expire(gomock.Any(), 10*time.Minute)
mockPipeline.EXPECT().Del(opManager.BuildCurrOpKey())
mockPipeline.EXPECT().Exec().Do(func() {
opManager.StopLoop()
operationFinished = true
})

scheduler1 := models.NewScheduler(configYaml1.Name, configYaml1.Game, yamlString)
Expand Down Expand Up @@ -3373,8 +3375,7 @@ game: game-name
json.Unmarshal(recorder.Body.Bytes(), &response)
Expect(response).To(HaveKeyWithValue("success", true))
Expect(response).To(HaveKey("operationKey"))

Eventually(opManager.IsStopped, 1*time.Minute).Should(BeTrue())
Eventually(func() bool { return operationFinished }, time.Minute, time.Second).Should(BeTrue())
})

It("should update min asynchronously and show error when occurred", func() {
Expand All @@ -3394,6 +3395,7 @@ game: game-name
"not": "empty",
}, nil)).AnyTimes()

operationFinished := false
mockRedisClient.EXPECT().TxPipeline().Return(mockPipeline)
mockPipeline.EXPECT().HMSet(gomock.Any(), gomock.Any()).Do(func(_ string, m map[string]interface{}) {
Expect(m).To(HaveKeyWithValue("status", http.StatusInternalServerError))
Expand All @@ -3404,7 +3406,7 @@ game: game-name
mockPipeline.EXPECT().Expire(gomock.Any(), 10*time.Minute)
mockPipeline.EXPECT().Del(opManager.BuildCurrOpKey())
mockPipeline.EXPECT().Exec().Do(func() {
opManager.StopLoop()
operationFinished = true
})

scheduler1 := models.NewScheduler(configYaml1.Name, configYaml1.Game, yamlString)
Expand Down Expand Up @@ -3449,8 +3451,7 @@ game: game-name
json.Unmarshal(recorder.Body.Bytes(), &response)
Expect(response).To(HaveKeyWithValue("success", true))
Expect(response).To(HaveKey("operationKey"))

Eventually(opManager.IsStopped, 1*time.Minute).Should(BeTrue())
Eventually(func() bool { return operationFinished }, time.Minute, time.Second).Should(BeTrue())
})
})
})
Expand Down
25 changes: 12 additions & 13 deletions api/scheduler_rollback_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,28 @@ autoscaling:
mockPipeline.EXPECT().Set(opManager.BuildCurrOpKey(), gomock.Any(), timeoutDur)
mockPipeline.EXPECT().Exec()

mockRedisClient.EXPECT().HGetAll(gomock.Any()).Return(goredis.NewStringStringMapResult(nil, nil)).AnyTimes()
mockRedisClient.EXPECT().HGetAll(gomock.Any()).
Return(goredis.NewStringStringMapResult(map[string]string{
"description": models.OpManagerWaitingLock,
}, nil))

operationFinished := false
mockRedisClient.EXPECT().TxPipeline().Return(mockPipeline)
mockPipeline.EXPECT().HMSet(gomock.Any(), gomock.Any()).Do(func(_ string, m map[string]interface{}) {
Expect(m).To(HaveKeyWithValue("status", http.StatusOK))
Expect(m).To(HaveKeyWithValue("operation", "SchedulerRollback"))
Expect(m).To(HaveKeyWithValue("success", true))
})
mockPipeline.EXPECT().Expire(gomock.Any(), 10*time.Minute)
mockPipeline.EXPECT().Del(opManager.BuildCurrOpKey())
mockPipeline.EXPECT().Exec().Do(func() {
opManager.StopLoop()
mockPipeline.EXPECT().Del(opManager.BuildCurrOpKey()).Do(func(_ interface{}) {
operationFinished = true
})
mockPipeline.EXPECT().Exec()

version := "v1.0"
// Select version from database
MockSelectYamlWithVersion(yamlStringToRollbackTo, version, mockDb, nil)

calls := NewCalls()

configLockKey := models.GetSchedulerConfigLockKey(config.GetString("watcher.lockKey"), scheduler1.Name)

// Get config lock
Expand All @@ -238,17 +240,14 @@ autoscaling:

MockUpdateVersionsTable(mockDb, nil)

calls.Finish()

app.Router.ServeHTTP(recorder, request)
Expect(recorder.Code).To(Equal(http.StatusOK))

var response map[string]interface{}
json.Unmarshal(recorder.Body.Bytes(), &response)
Expect(response).To(HaveKeyWithValue("success", true))
Expect(response).To(HaveKey("operationKey"))

Eventually(opManager.IsStopped, 10*time.Second).Should(BeTrue())
Eventually(func() bool { return operationFinished }, time.Minute, time.Second).Should(BeTrue())
})

It("should save error on redis if failed", func() {
Expand All @@ -275,6 +274,7 @@ autoscaling:

mockRedisClient.EXPECT().HGetAll(gomock.Any()).Return(goredis.NewStringStringMapResult(nil, nil)).AnyTimes()

operationFinished := false
mockRedisClient.EXPECT().TxPipeline().Return(mockPipeline)
mockPipeline.EXPECT().HMSet(gomock.Any(), gomock.Any()).Do(func(_ string, m map[string]interface{}) {
Expect(m).To(HaveKeyWithValue("status", http.StatusBadRequest))
Expand All @@ -286,7 +286,7 @@ autoscaling:
mockPipeline.EXPECT().Expire(gomock.Any(), 10*time.Minute)
mockPipeline.EXPECT().Del(opManager.BuildCurrOpKey())
mockPipeline.EXPECT().Exec().Do(func() {
opManager.StopLoop()
operationFinished = true
})

version := "v1.0"
Expand All @@ -306,8 +306,7 @@ autoscaling:
json.Unmarshal(recorder.Body.Bytes(), &response)
Expect(response).To(HaveKeyWithValue("success", true))
Expect(response).To(HaveKey("operationKey"))

Eventually(opManager.IsStopped, 10*time.Second).Should(BeTrue())
Eventually(func() bool { return operationFinished }, time.Minute, time.Second).Should(BeTrue())
})
})
})
Loading

0 comments on commit 8e50ba1

Please sign in to comment.