mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 18:59:09 +00:00
Implement mock for Netbird API and client (#184)
This makes testing of the operator a lot simpler by enabling a quick way to implement the crud endpoints for all the resources used. Signed-off-by: Philip Laine <philip.laine@gmail.com>
This commit is contained in:
@@ -2,16 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
||||
netbird "github.com/netbirdio/netbird/shared/management/client/rest"
|
||||
"github.com/netbirdio/netbird/shared/management/http/api"
|
||||
"github.com/netbirdio/netbird/shared/management/http/util"
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
kerrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -20,65 +11,13 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
nbv1alpha1 "github.com/netbirdio/kubernetes-operator/api/v1alpha1"
|
||||
"github.com/netbirdio/kubernetes-operator/internal/netbirdmock"
|
||||
)
|
||||
|
||||
var _ = Describe("Group Controller", func() {
|
||||
Context("When reconciling a resource", func() {
|
||||
ctx := context.Background()
|
||||
|
||||
r := rand.New(rand.NewSource(GinkgoRandomSeed()))
|
||||
groupStore := map[string]*api.Group{}
|
||||
mux := &http.ServeMux{}
|
||||
mux.Handle("POST /api/groups", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
b, err := io.ReadAll(req.Body)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
groupReq := api.GroupRequest{}
|
||||
err = json.Unmarshal(b, &groupReq)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
groupResp := &api.Group{
|
||||
Id: fmt.Sprintf("id-%d", r.Int63()),
|
||||
Name: groupReq.Name,
|
||||
}
|
||||
groupStore[groupResp.Id] = groupResp
|
||||
b, err = json.Marshal(groupResp)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
_, err = rw.Write(b)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}))
|
||||
mux.Handle("PUT /api/groups/{id}", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
id := req.PathValue("id")
|
||||
groupResp, ok := groupStore[id]
|
||||
if !ok {
|
||||
util.WriteErrorResponse("Not Found", http.StatusNotFound, rw)
|
||||
return
|
||||
}
|
||||
|
||||
b, err := io.ReadAll(req.Body)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
groupReq := api.GroupRequest{}
|
||||
err = json.Unmarshal(b, &groupReq)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
groupResp.Name = groupReq.Name
|
||||
|
||||
b, err = json.Marshal(groupResp)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
_, err = rw.Write(b)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}))
|
||||
mux.Handle("DELETE /api/groups/{id}", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
id := req.PathValue("id")
|
||||
_, ok := groupStore[id]
|
||||
if !ok {
|
||||
util.WriteErrorResponse("Not Found", http.StatusNotFound, rw)
|
||||
return
|
||||
}
|
||||
delete(groupStore, id)
|
||||
}))
|
||||
server := httptest.NewServer(mux)
|
||||
nbClient := netbird.New(server.URL, "ABC")
|
||||
|
||||
var controllerReconciler *GroupReconciler
|
||||
nn := client.ObjectKey{
|
||||
Name: "test-resource",
|
||||
@@ -88,13 +27,11 @@ var _ = Describe("Group Controller", func() {
|
||||
BeforeEach(func() {
|
||||
controllerReconciler = &GroupReconciler{
|
||||
Client: k8sClient,
|
||||
Netbird: nbClient,
|
||||
Netbird: netbirdmock.Client(),
|
||||
}
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
groupStore = map[string]*api.Group{}
|
||||
|
||||
group := &nbv1alpha1.Group{}
|
||||
err := k8sClient.Get(ctx, nn, group)
|
||||
if kerrors.IsNotFound(err) {
|
||||
@@ -126,7 +63,8 @@ var _ = Describe("Group Controller", func() {
|
||||
Expect(*group.Status.GroupID).NotTo(BeEmpty())
|
||||
|
||||
By("crerating a new group when deleted from API")
|
||||
delete(groupStore, *group.Status.GroupID)
|
||||
err = controllerReconciler.Netbird.Groups.Delete(ctx, *group.Status.GroupID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: nn})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
newGroup := &nbv1alpha1.Group{}
|
||||
|
||||
Reference in New Issue
Block a user