Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuckal777 committed Feb 28, 2022
1 parent 88016f6 commit e995437
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
test: manifests generate fmt vet ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./controllers ./esx ./event ./kubernikus ./plugin ./plugin/impl ./state -coverprofile=coverage.out -coverpkg github.com/sapcc/maintenance-controller/...
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./common ./controllers ./esx ./event ./kubernikus ./plugin ./plugin/impl ./state -coverprofile=coverage.out -coverpkg github.com/sapcc/maintenance-controller/...

##@ Build

Expand Down
54 changes: 54 additions & 0 deletions common/weekday_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
*
* Copyright 2020 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, 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 common

import (
"testing"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestCommon(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Plugin Suite")
}

var _ = Describe("WeekdayFromString", func() {

It("should parse monday", func() {
weekday, err := WeekdayFromString("monday")
Expect(err).To(Succeed())
Expect(weekday).To(Equal(time.Monday))
})

It("should parse wed", func() {
weekday, err := WeekdayFromString("wed")
Expect(err).To(Succeed())
Expect(weekday).To(Equal(time.Wednesday))
})

It("should not parse abcde", func() {
_, err := WeekdayFromString("abcde")
Expect(err).ToNot(Succeed())
})

})
49 changes: 48 additions & 1 deletion plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var _ = Describe("Registry", func() {
Expect(instance.Name).To(Equal("test"))
})

It("loads notification plugin instances", func() {
It("loads periodic notification plugin instances", func() {
var configStr = `notify:
- type: someNotificationPlugin
name: test
Expand All @@ -114,6 +114,32 @@ var _ = Describe("Registry", func() {
Expect(instance.Name).To(Equal("test"))
})

It("loads scheduled notification plugin instances", func() {
var configStr = `notify:
- type: someNotificationPlugin
name: test
schedule:
type: scheduled
config:
instant: 11:00
weekdays: ["tue"]
config:
key: somekey
value: someval
`
registry := NewRegistry()
registry.NotificationPlugins["someNotificationPlugin"] = &successfulNotification{}
config, err := yaml.NewConfig([]byte(configStr))
Expect(err).To(Succeed())
var descriptor InstancesDescriptor
Expect(config.Unpack(&descriptor)).To(Succeed())
err = registry.LoadInstances(&descriptor)
Expect(err).To(Succeed())
Expect(registry.NotificationInstances).To(HaveLen(1))
instance := registry.NotificationInstances["test"]
Expect(instance.Name).To(Equal("test"))
})

It("loads trigger plugin instances", func() {
var configStr = `trigger:
- type: someTriggerPlugin
Expand Down Expand Up @@ -284,6 +310,27 @@ var _ = Describe("Registry", func() {
assertError(configStr)
})

It("fails to load notification plugin instances with unknown scheduler", func() {
var configStr = `notify:
- type: someNotificationPlugin
name: test
schedule:
type: unknown
config: null
config:
key: somekey
value: someval
`
registry := NewRegistry()
registry.NotificationPlugins["someNotificationPlugin"] = &successfulNotification{}
config, err := yaml.NewConfig([]byte(configStr))
Expect(err).To(Succeed())
var descriptor InstancesDescriptor
Expect(config.Unpack(&descriptor)).To(Succeed())
err = registry.LoadInstances(&descriptor)
Expect(err).ToNot(Succeed())
})

})

})
Expand Down
7 changes: 7 additions & 0 deletions state/maintenance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ var _ = Describe("InMaintenance State", func() {
Expect(trigger.Invoked).To(Equal(1))
})

It("fails to transition if target state is not defined", func() {
im := newInMaintenance(chains)
err := im.Trigger(plugin.Parameters{Log: logr.Discard()}, Required, &Data{})
Expect(err).ToNot(Succeed())
Expect(trigger.Invoked).To(Equal(0))
})

It("executes the notifications", func() {
im := newInMaintenance(chains)
err := im.Notify(plugin.Parameters{Log: logr.Discard()}, &Data{LastNotificationTimes: make(map[string]time.Time)})
Expand Down
7 changes: 7 additions & 0 deletions state/operational_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ var _ = Describe("Operational State", func() {
Expect(trigger.Invoked).To(Equal(1))
})

It("fails to transition if target state is not defined", func() {
op := newOperational(chains)
err := op.Trigger(plugin.Parameters{Log: logr.Discard()}, InMaintenance, &Data{})
Expect(err).ToNot(Succeed())
Expect(trigger.Invoked).To(Equal(0))
})

It("executes the notifications", func() {
op := newOperational(chains)
err := op.Notify(plugin.Parameters{Log: logr.Discard()}, &Data{LastNotificationTimes: make(map[string]time.Time)})
Expand Down
7 changes: 7 additions & 0 deletions state/required_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ var _ = Describe("MaintenanceRequired State", func() {
Expect(trigger.Invoked).To(Equal(1))
})

It("fails to transition if target state is not defined", func() {
mr := newMaintenanceRequired(chains)
err := mr.Trigger(plugin.Parameters{Log: logr.Discard()}, Operational, &Data{})
Expect(err).ToNot(Succeed())
Expect(trigger.Invoked).To(Equal(0))
})

It("executes the notifications", func() {
mr := newMaintenanceRequired(chains)
err := mr.Notify(plugin.Parameters{Log: logr.Discard()}, &Data{LastNotificationTimes: make(map[string]time.Time)})
Expand Down
16 changes: 16 additions & 0 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,20 @@ var _ = Describe("ParseData", func() {
Expect(data.LastNotificationTimes).ToNot(BeNil())
})

It("should fail with invalid json", func() {
var node v1.Node
node.Annotations = map[string]string{constants.DataAnnotationKey: "{{}"}
_, err := ParseData(&node)
Expect(err).ToNot(Succeed())
})

})

var _ = Describe("ValidateLabel", func() {

It("fails on invalid input", func() {
_, err := ValidateLabel("hello")
Expect(err).ToNot(Succeed())
})

})

0 comments on commit e995437

Please sign in to comment.