1 package services
2
3 import (
4 "context"
5 "fmt"
6 "net/http"
7 "net/http/httptest"
8 "testing"
9
10 "github.com/gin-gonic/gin"
11 "github.com/stretchr/testify/assert"
12 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13 "sigs.k8s.io/controller-runtime/pkg/client"
14
15 config "edge-infra.dev/pkg/edge/device-registrar/config"
16
17 api "edge-infra.dev/pkg/edge/device-registrar/api/v1alpha1"
18
19 test "edge-infra.dev/pkg/edge/device-registrar/utils/test"
20 )
21
22 func TestDeleteApplicationSuccess(t *testing.T) {
23 router, scheme := setupTest()
24 app1, app2 := getApps()
25 devBinding1, devBinding2 := getDeviceBindings()
26
27 mockClient := test.NewErrorInjectingFakeClient(scheme, true, &app1, &app2, &devBinding1, &devBinding2)
28
29 router.Use(injectMockClientMiddleware(t, mockClient))
30
31 w := serveDeleteAppRequest(t, router, "123e4567-e89b-12d3-a456-426614174000")
32
33 assert.Equal(t, http.StatusNoContent, w.Code)
34
35
36 devbList := &api.DeviceBindingList{}
37 err := mockClient.List(context.TODO(), devbList, client.InNamespace("device-registrar-clients"))
38 assert.NoError(t, err)
39
40
41 assert.Len(t, devbList.Items, 1)
42
43
44 extDev := devbList.Items[0]
45 assert.Len(t, extDev.Spec.Applications, 1)
46 }
47
48 func TestDeleteAppDeleteError(t *testing.T) {
49 router, scheme := setupTest()
50 app1, _ := getApps()
51
52 mockClient := test.NewErrorInjectingFakeClient(scheme, true, &app1)
53 mockClient.SetFailOnDelete()
54
55 router.Use(injectMockClientMiddleware(t, mockClient))
56
57 w := serveDeleteAppRequest(t, router, "123e4567-e89b-12d3-a456-426614174000")
58 assert.Equal(t, http.StatusInternalServerError, w.Code)
59 assert.Contains(t, w.Body.String(), "delete error")
60 }
61
62 func TestDeleteAppListError(t *testing.T) {
63 router, scheme := setupTest()
64 app1, _ := getApps()
65 devBinding1, _ := getDeviceBindings()
66
67 mockClient := test.NewErrorInjectingFakeClient(scheme, true, &app1, &devBinding1)
68 mockClient.SetFailOnList()
69
70 router.Use(injectMockClientMiddleware(t, mockClient))
71
72 w := serveDeleteAppRequest(t, router, "123e4567-e89b-12d3-a456-426614174000")
73 assert.Equal(t, http.StatusInternalServerError, w.Code)
74 assert.Contains(t, w.Body.String(), "unsupported list type")
75 }
76
77 func TestDeleteAppUpdateError(t *testing.T) {
78 router, scheme := setupTest()
79 app1, _ := getApps()
80 devBinding1, devBinding2 := getDeviceBindings()
81
82 mockClient := test.NewErrorInjectingFakeClient(scheme, true, &app1, &devBinding1, &devBinding2)
83 mockClient.SetFailOnUpdate()
84
85 router.Use(injectMockClientMiddleware(t, mockClient))
86
87 w := serveDeleteAppRequest(t, router, "123e4567-e89b-12d3-a456-426614174000")
88 assert.Equal(t, http.StatusInternalServerError, w.Code)
89 assert.Contains(t, w.Body.String(), "update error")
90 }
91
92 func TestDeleteApplicationNonExistentApp(t *testing.T) {
93 router, scheme := setupTest()
94
95 mockClient := test.NewErrorInjectingFakeClient(scheme, true)
96
97 router.Use(injectMockClientMiddleware(t, mockClient))
98
99 w := serveDeleteAppRequest(t, router, "123")
100 assert.Equal(t, http.StatusNotFound, w.Code)
101 assert.Contains(t, w.Body.String(), "not found")
102 }
103
104 func serveDeleteAppRequest(t *testing.T, router *gin.Engine, appID string) *httptest.ResponseRecorder {
105
106 router.POST("/applications/:appID", DeleteApplication)
107
108
109 request := fmt.Sprintf("/applications/%s", appID)
110 req, err := http.NewRequest(http.MethodPost, request, nil)
111 assert.NoError(t, err)
112 w := httptest.NewRecorder()
113 router.ServeHTTP(w, req)
114
115 return w
116 }
117
118 func getDeviceBindings() (api.DeviceBinding, api.DeviceBinding) {
119 devBinding1 := api.DeviceBinding{
120 ObjectMeta: metav1.ObjectMeta{
121 Name: "device-binding-1",
122 Namespace: config.Namespace,
123 },
124 Spec: api.DeviceBindingSpec{
125 Device: api.DeviceSubject{
126 Name: "test-device",
127 },
128 Applications: []api.ApplicationSubject{
129 {
130 ID: "123e4567-e89b-12d3-a456-426614174000",
131 },
132 {
133 ID: "123e4567-e89b-12d3-a456-426614174001",
134 },
135 },
136 },
137 }
138 devBinding2 := api.DeviceBinding{
139 ObjectMeta: metav1.ObjectMeta{
140 Name: "device-binding-2",
141 Namespace: config.Namespace,
142 },
143 Spec: api.DeviceBindingSpec{
144 Device: api.DeviceSubject{
145 Name: "test-device-2",
146 },
147 Applications: []api.ApplicationSubject{
148 {
149 ID: "123e4567-e89b-12d3-a456-426614174000",
150 },
151 },
152 },
153 }
154 return devBinding1, devBinding2
155 }
156
View as plain text