From 9594304d803aa15f6798944fa9075b640f996cd4 Mon Sep 17 00:00:00 2001 From: Dimitar Petrov Date: Mon, 8 Jul 2019 10:15:20 +0300 Subject: [PATCH] Fix tests --- config/config_test.go | 55 +++++++++++++++++++++ pkg/health/ping.go | 2 +- pkg/health/settings_test.go | 99 ------------------------------------- 3 files changed, 56 insertions(+), 100 deletions(-) delete mode 100644 pkg/health/settings_test.go diff --git a/config/config_test.go b/config/config_test.go index ac4a24a7e..1ebe67b18 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -18,6 +18,7 @@ package config_test import ( "fmt" + "github.com/Peripli/service-manager/pkg/health" "testing" "time" @@ -44,11 +45,25 @@ var _ = Describe("config", func() { ) Describe("Validate", func() { + var fatal bool + var failuresTreshold int64 + var interval time.Duration + assertErrorDuringValidate := func() { err = config.Validate() Expect(err).To(HaveOccurred()) } + registerIndicatorSettings := func() { + indicatorSettings := &health.IndicatorSettings{ + Fatal: fatal, + FailuresTreshold: failuresTreshold, + Interval: interval, + } + + config.Health.IndicatorsSettings["test"] = indicatorSettings + } + BeforeEach(func() { config = cfg.DefaultSettings() config.Storage.URI = "postgres://postgres:postgres@localhost:5555/postgres?sslmode=disable" @@ -56,6 +71,46 @@ var _ = Describe("config", func() { config.API.ClientID = "sm" config.API.SkipSSLValidation = true config.Storage.EncryptionKey = "ejHjRNHbS0NaqARSRvnweVV9zcmhQEa8" + + fatal = false + failuresTreshold = 1 + interval = 30 * time.Second + }) + + Context("health indicator with negative treshold", func() { + It("should be considered invalid", func() { + failuresTreshold = -1 + registerIndicatorSettings() + assertErrorDuringValidate() + }) + }) + + Context("health indicator with 0 treshold", func() { + It("should be considered invalid", func() { + failuresTreshold = 0 + registerIndicatorSettings() + assertErrorDuringValidate() + }) + }) + + Context("health indicator with interval less than 30", func() { + It("should be considered invalid", func() { + interval = 15 * time.Second + registerIndicatorSettings() + assertErrorDuringValidate() + }) + }) + + Context("health indicator with positive treshold and interval >= 30", func() { + It("should be considered valid", func() { + interval = 30 * time.Second + failuresTreshold = 3 + registerIndicatorSettings() + + err := config.Validate() + + Expect(err).ShouldNot(HaveOccurred()) + }) }) Context("when config is valid", func() { diff --git a/pkg/health/ping.go b/pkg/health/ping.go index ce55fbcaf..f1e169b22 100644 --- a/pkg/health/ping.go +++ b/pkg/health/ping.go @@ -27,7 +27,7 @@ func (*pingIndicator) Name() string { } func (*pingIndicator) Interval() time.Duration { - return 30 + return 30 * time.Second } func (*pingIndicator) FailuresTreshold() int64 { diff --git a/pkg/health/settings_test.go b/pkg/health/settings_test.go deleted file mode 100644 index 4436f3d90..000000000 --- a/pkg/health/settings_test.go +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2018 The Service Manager Authors - * - * 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 health - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "time" -) - -var _ = Describe("Healthcheck Settings", func() { - - var settings *Settings - var fatal bool - var failuresTreshold int64 - var interval time.Duration - - BeforeEach(func() { - settings = DefaultSettings() - - fatal = false - failuresTreshold = 1 - interval = 30 - }) - - var registerIndicatorSettings = func() { - indicatorSettings := &IndicatorSettings{ - Fatal: fatal, - FailuresTreshold: failuresTreshold, - Interval: interval, - } - - settings.IndicatorsSettings["test"] = indicatorSettings - } - - var assertValidationErrorOccured = func(err error) { - Expect(err).Should(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("validate Settings")) - } - - When("Indicator with negative treshold", func() { - It("Should be invalid", func() { - failuresTreshold = -1 - registerIndicatorSettings() - - err := settings.Validate() - - assertValidationErrorOccured(err) - }) - }) - - When("Indicator with 0 treshold", func() { - It("Should be invalid", func() { - failuresTreshold = 0 - registerIndicatorSettings() - - err := settings.Validate() - - assertValidationErrorOccured(err) - }) - }) - - When("Indicator with interval less than 30", func() { - It("Should be invalid", func() { - interval = 15 - registerIndicatorSettings() - - err := settings.Validate() - - assertValidationErrorOccured(err) - }) - }) - - When("Indicator with positive treshold and interval > 30", func() { - It("Should be considered valid", func() { - interval = 30 - failuresTreshold = 3 - registerIndicatorSettings() - - err := settings.Validate() - - Expect(err).ShouldNot(HaveOccurred()) - }) - }) -})