Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit 7a44c0f

Browse files
author
Jakub Dzon
committed
Added tests
Signed-off-by: Jakub Dzon <[email protected]>
1 parent 8d9eb77 commit 7a44c0f

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

internal/edgeapi/backend/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Notification struct {
1313
Retry int32
1414
}
1515

16+
//go:generate mockgen -package=backend -destination=mock_heartbeat-handler.go . HeartbeatHandler
1617
type HeartbeatHandler interface {
1718
Process(ctx context.Context, notification Notification) (bool, error)
1819
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package factory_test
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestBackendFactory(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Backend Factory Suite")
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package factory_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
"go.uber.org/zap"
7+
"k8s.io/client-go/tools/record"
8+
"sigs.k8s.io/controller-runtime/pkg/client"
9+
10+
"github.com/project-flotta/flotta-operator/internal/edgeapi/backend/factory"
11+
)
12+
13+
const namespace = "some-ns"
14+
15+
var _ = Describe("Backend factory", func() {
16+
17+
It("should create k8s factory", func() {
18+
// given
19+
logger := &zap.SugaredLogger{}
20+
eventRecorder := record.NewFakeRecorder(1)
21+
var c client.Client
22+
23+
// when
24+
backend := factory.CreateBackend(namespace, c, logger, eventRecorder)
25+
26+
// then
27+
Expect(backend).ToNot(BeNil())
28+
})
29+
})

internal/edgeapi/backend/mock_heartbeat-handler.go

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package yggdrasil_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/golang/mock/gomock"
8+
. "github.com/onsi/ginkgo/v2"
9+
. "github.com/onsi/gomega"
10+
11+
"github.com/project-flotta/flotta-operator/internal/edgeapi/backend"
12+
"github.com/project-flotta/flotta-operator/internal/edgeapi/yggdrasil"
13+
)
14+
15+
var _ = Describe("Heartbeat handler", func() {
16+
17+
var (
18+
mockCtrl *gomock.Controller
19+
mockDelegate *backend.MockHeartbeatHandler
20+
handler *yggdrasil.RetryingDelegatingHandler
21+
)
22+
23+
BeforeEach(func() {
24+
mockCtrl = gomock.NewController(GinkgoT())
25+
mockDelegate = backend.NewMockHeartbeatHandler(mockCtrl)
26+
27+
handler = yggdrasil.NewRetryingDelegatingHandler(mockDelegate)
28+
})
29+
30+
AfterEach(func() {
31+
mockCtrl.Finish()
32+
})
33+
34+
It("should call delegate", func() {
35+
// given
36+
ctx := context.TODO()
37+
notification := backend.Notification{DeviceID: "1234"}
38+
39+
mockDelegate.EXPECT().
40+
Process(ctx, notification).
41+
Return(false, nil)
42+
43+
// when
44+
err := handler.Process(ctx, notification)
45+
46+
// then
47+
Expect(err).ToNot(HaveOccurred())
48+
// mock verification
49+
})
50+
51+
It("should retry calling delegate on error and succeed", func() {
52+
// given
53+
ctx := context.TODO()
54+
notification := backend.Notification{DeviceID: "1234"}
55+
retryNotification := backend.Notification{DeviceID: "1234", Retry: 1}
56+
errorCall := mockDelegate.EXPECT().
57+
Process(ctx, notification).
58+
Return(true, fmt.Errorf("boom"))
59+
mockDelegate.EXPECT().
60+
Process(ctx, retryNotification).
61+
Return(false, nil).
62+
After(errorCall)
63+
64+
// when
65+
err := handler.Process(ctx, notification)
66+
67+
// then
68+
Expect(err).ToNot(HaveOccurred())
69+
})
70+
71+
It("should retry calling delegate on error and eventually fail", func() {
72+
// given
73+
ctx := context.TODO()
74+
notification := backend.Notification{DeviceID: "1234"}
75+
76+
mockDelegate.EXPECT().
77+
Process(ctx, gomock.AssignableToTypeOf(notification)).
78+
Return(true, fmt.Errorf("boom")).
79+
Times(4)
80+
81+
// when
82+
err := handler.Process(ctx, notification)
83+
84+
// then
85+
Expect(err).To(HaveOccurred())
86+
})
87+
})

0 commit comments

Comments
 (0)