-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient_test.go
More file actions
984 lines (810 loc) · 34.2 KB
/
client_test.go
File metadata and controls
984 lines (810 loc) · 34.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
/*
Copyright 2024.
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 prefect
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
prefectiov1 "github.com/PrefectHQ/prefect-operator/api/v1"
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/utils/ptr"
)
func TestPrefectClient(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Prefect Client Suite")
}
var _ = Describe("Prefect HTTP Client", func() {
var (
ctx context.Context
client *Client
mockServer *httptest.Server
logger logr.Logger
)
BeforeEach(func() {
ctx = context.Background()
logger = logr.Discard()
})
AfterEach(func() {
if mockServer != nil {
mockServer.Close()
}
})
Describe("Client Creation", func() {
It("Should create client with default timeout", func() {
client := NewClient("http://test.com", "test-key", logger)
Expect(client.BaseURL).To(Equal("http://test.com"))
Expect(client.APIKey).To(Equal("test-key"))
Expect(client.HTTPClient.Timeout).To(Equal(30 * time.Second))
})
})
Describe("Authentication", func() {
It("Should handle Prefect Cloud authentication", func() {
By("Setting up mock server that mimics Prefect Cloud")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
// Verify Authorization header is present and correct
authHeader := r.Header.Get("Authorization")
Expect(authHeader).To(Equal("Bearer pnu_1234567890abcdef"))
expectedFlow := Flow{
ID: "flow-cloud-12345",
Name: "cloud-flow",
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(expectedFlow)
}))
By("Creating client with Prefect Cloud API key")
client = NewClient(mockServer.URL, "pnu_1234567890abcdef", logger)
By("Calling GetFlowByName")
flow, err := client.GetFlowByName(ctx, "cloud-flow")
By("Verifying request succeeds with Prefect Cloud authentication")
Expect(err).NotTo(HaveOccurred())
Expect(flow).NotTo(BeNil())
Expect(flow.Name).To(Equal("cloud-flow"))
})
It("Should handle empty API key (open-source Prefect without auth)", func() {
By("Setting up mock server that mimics open-source Prefect")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
// Verify no Authorization header when API key is empty
Expect(r.Header.Get("Authorization")).To(Equal(""))
expectedFlow := Flow{
ID: "flow-12345",
Name: "test-flow",
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(expectedFlow)
}))
By("Creating client with empty API key")
client = NewClient(mockServer.URL, "", logger)
By("Calling GetFlowByName")
flow, err := client.GetFlowByName(ctx, "test-flow")
By("Verifying request succeeds without authentication")
Expect(err).NotTo(HaveOccurred())
Expect(flow).NotTo(BeNil())
Expect(flow.Name).To(Equal("test-flow"))
})
It("Should handle custom authentication tokens", func() {
By("Setting up mock server that mimics authenticated open-source Prefect")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
// Check for custom auth header
authHeader := r.Header.Get("Authorization")
if authHeader != "Bearer custom_token_123" {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"detail": "Authentication required"}`))
return
}
expectedFlow := Flow{
ID: "flow-selfhosted-12345",
Name: "selfhosted-flow",
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(expectedFlow)
}))
By("Creating client with custom authentication token")
client = NewClient(mockServer.URL, "custom_token_123", logger)
By("Calling GetFlowByName")
flow, err := client.GetFlowByName(ctx, "selfhosted-flow")
By("Verifying request succeeds with custom authentication")
Expect(err).NotTo(HaveOccurred())
Expect(flow).NotTo(BeNil())
Expect(flow.Name).To(Equal("selfhosted-flow"))
})
It("Should handle authentication errors", func() {
By("Setting up mock server that returns 401")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"detail": "Invalid API key"}`))
}))
By("Creating client with invalid API key")
client = NewClient(mockServer.URL, "invalid_key", logger)
By("Calling GetFlowByName")
flow, err := client.GetFlowByName(ctx, "test-flow")
By("Verifying authentication error is handled")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("API request failed with status 401"))
Expect(err.Error()).To(ContainSubstring("Invalid API key"))
Expect(flow).To(BeNil())
})
})
Describe("Error Handling", func() {
It("Should handle network errors", func() {
By("Creating client with invalid URL")
client = NewClient("http://invalid-host:99999", "test-api-key", logger)
By("Calling GetFlowByName")
flow, err := client.GetFlowByName(ctx, "test-flow")
By("Verifying network error is returned")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to make request"))
Expect(flow).To(BeNil())
})
It("Should handle context cancellation", func() {
By("Setting up mock server with slow response")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
time.Sleep(100 * time.Millisecond)
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(Flow{ID: "test", Name: "test-flow"})
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Creating cancelled context")
cancelCtx, cancel := context.WithCancel(ctx)
cancel()
By("Calling GetFlowByName with cancelled context")
flow, err := client.GetFlowByName(cancelCtx, "test-flow")
By("Verifying context cancellation error")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to make request"))
Expect(flow).To(BeNil())
})
It("Should handle invalid JSON response", func() {
By("Setting up mock server with invalid JSON")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{invalid json`))
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Calling GetFlowByName")
flow, err := client.GetFlowByName(ctx, "test-flow")
By("Verifying unmarshal error is returned")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to unmarshal response"))
Expect(flow).To(BeNil())
})
It("Should handle non-JSON response content types", func() {
By("Setting up mock server with text response")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("not json"))
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Calling GetFlowByName")
flow, err := client.GetFlowByName(ctx, "test-flow")
By("Verifying unmarshal error for non-JSON response")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to unmarshal response"))
Expect(flow).To(BeNil())
})
It("Should handle API errors", func() {
By("Setting up mock server with 500 error")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"detail": "Internal server error"}`))
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Calling GetFlowByName")
flow, err := client.GetFlowByName(ctx, "error-flow")
By("Verifying error is returned")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("API request failed with status 500"))
Expect(err.Error()).To(ContainSubstring("Internal server error"))
Expect(flow).To(BeNil())
})
})
Describe("GetFlowByName", func() {
It("Should successfully retrieve a flow by name", func() {
By("Setting up mock server with flow response")
expectedFlow := Flow{
ID: "flow-12345",
Created: time.Now(),
Updated: time.Now(),
Name: "test-flow",
Tags: []string{"test", "example"},
Labels: map[string]string{"env": "test"},
}
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
// Verify request method and path
Expect(r.Method).To(Equal("GET"))
Expect(r.URL.Path).To(Equal("/flows/name/test-flow"))
Expect(r.Header.Get("Content-Type")).To(Equal("application/json"))
// Return mock flow response
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(expectedFlow)
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Calling GetFlowByName")
flow, err := client.GetFlowByName(ctx, "test-flow")
By("Verifying the response")
Expect(err).NotTo(HaveOccurred())
Expect(flow).NotTo(BeNil())
Expect(flow.ID).To(Equal(expectedFlow.ID))
Expect(flow.Name).To(Equal(expectedFlow.Name))
Expect(flow.Tags).To(Equal(expectedFlow.Tags))
Expect(flow.Labels).To(Equal(expectedFlow.Labels))
})
It("Should return nil when flow is not found", func() {
By("Setting up mock server with 404 response")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Expect(r.Method).To(Equal("GET"))
Expect(r.URL.Path).To(Equal("/flows/name/nonexistent-flow"))
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"detail": "Flow not found"}`))
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Calling GetFlowByName for nonexistent flow")
flow, err := client.GetFlowByName(ctx, "nonexistent-flow")
By("Verifying nil is returned for 404")
Expect(err).NotTo(HaveOccurred())
Expect(flow).To(BeNil())
})
})
Describe("CreateOrGetFlow", func() {
It("Should create a new flow when it doesn't exist", func() {
By("Setting up mock server for flow creation")
expectedFlow := Flow{
ID: "new-flow-12345",
Created: time.Now(),
Updated: time.Now(),
Name: "new-flow",
Tags: []string{"new", "created"},
Labels: map[string]string{"source": "operator"},
}
callCount := 0
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
callCount++
switch r.URL.Path {
case "/flows/name/new-flow":
// First call - flow doesn't exist
Expect(r.Method).To(Equal("GET"))
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"detail": "Flow not found"}`))
case "/flows/":
// Second call - create flow
Expect(r.Method).To(Equal("POST"))
Expect(r.Header.Get("Content-Type")).To(Equal("application/json"))
// Return created flow
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(expectedFlow)
default:
Fail("Unexpected path: " + r.URL.Path)
}
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Calling CreateOrGetFlow")
flowSpec := &FlowSpec{
Name: "new-flow",
Tags: []string{"new", "created"},
Labels: map[string]string{"source": "operator"},
}
flow, err := client.CreateOrGetFlow(ctx, flowSpec)
By("Verifying flow was created")
Expect(err).NotTo(HaveOccurred())
Expect(flow).NotTo(BeNil())
Expect(flow.ID).To(Equal(expectedFlow.ID))
Expect(flow.Name).To(Equal(expectedFlow.Name))
Expect(callCount).To(Equal(2)) // GET then POST
})
It("Should return existing flow when it already exists", func() {
By("Setting up mock server for existing flow")
existingFlow := Flow{
ID: "existing-flow-12345",
Created: time.Now(),
Updated: time.Now(),
Name: "existing-flow",
Tags: []string{"existing"},
Labels: map[string]string{"source": "existing"},
}
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
// Only GET call should happen
Expect(r.Method).To(Equal("GET"))
Expect(r.URL.Path).To(Equal("/flows/name/existing-flow"))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(existingFlow)
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Calling CreateOrGetFlow")
flowSpec := &FlowSpec{
Name: "existing-flow",
Tags: []string{"new", "tag"},
Labels: map[string]string{"source": "operator"},
}
flow, err := client.CreateOrGetFlow(ctx, flowSpec)
By("Verifying existing flow was returned")
Expect(err).NotTo(HaveOccurred())
Expect(flow).NotTo(BeNil())
Expect(flow.ID).To(Equal(existingFlow.ID))
Expect(flow.Name).To(Equal(existingFlow.Name))
// Should return existing flow, not create new one
Expect(flow.Tags).To(Equal(existingFlow.Tags))
})
})
Describe("CreateOrUpdateDeployment", func() {
It("Should create a new deployment", func() {
By("Setting up mock server for deployment creation")
expectedDeployment := Deployment{
ID: "deployment-12345",
Created: time.Now(),
Updated: time.Now(),
Name: "test-deployment",
FlowID: "flow-123",
Paused: false,
Tags: []string{"test", "deployment"},
Parameters: map[string]interface{}{"param1": "value1"},
Entrypoint: ptr.To("flows.py:main_flow"),
WorkPoolName: ptr.To("kubernetes"),
}
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Expect(r.Method).To(Equal("POST"))
Expect(r.URL.Path).To(Equal("/deployments/"))
Expect(r.Header.Get("Content-Type")).To(Equal("application/json"))
// Verify request body contains deployment spec
var deploymentSpec DeploymentSpec
_ = json.NewDecoder(r.Body).Decode(&deploymentSpec)
Expect(deploymentSpec.Name).To(Equal("test-deployment"))
Expect(deploymentSpec.FlowID).To(Equal("flow-123"))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(expectedDeployment)
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Calling CreateOrUpdateDeployment")
deploymentSpec := &DeploymentSpec{
Name: "test-deployment",
FlowID: "flow-123",
Tags: []string{"test", "deployment"},
Parameters: map[string]interface{}{"param1": "value1"},
Entrypoint: ptr.To("flows.py:main_flow"),
WorkPoolName: ptr.To("kubernetes"),
}
deployment, err := client.CreateOrUpdateDeployment(ctx, deploymentSpec)
By("Verifying deployment was created")
Expect(err).NotTo(HaveOccurred())
Expect(deployment).NotTo(BeNil())
Expect(deployment.ID).To(Equal(expectedDeployment.ID))
Expect(deployment.Name).To(Equal(expectedDeployment.Name))
Expect(deployment.FlowID).To(Equal(expectedDeployment.FlowID))
})
})
Describe("GetDeployment", func() {
It("Should retrieve a deployment by ID", func() {
By("Setting up mock server for deployment retrieval")
expectedDeployment := Deployment{
ID: "deployment-12345",
Created: time.Now(),
Updated: time.Now(),
Name: "test-deployment",
FlowID: "flow-123",
Paused: false,
Status: "READY",
Tags: []string{"test"},
WorkPoolName: ptr.To("kubernetes"),
}
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Expect(r.Method).To(Equal("GET"))
Expect(r.URL.Path).To(Equal("/deployments/deployment-12345"))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(expectedDeployment)
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Calling GetDeployment")
deployment, err := client.GetDeployment(ctx, "deployment-12345")
By("Verifying deployment was retrieved")
Expect(err).NotTo(HaveOccurred())
Expect(deployment).NotTo(BeNil())
Expect(deployment.ID).To(Equal(expectedDeployment.ID))
Expect(deployment.Name).To(Equal(expectedDeployment.Name))
Expect(deployment.Status).To(Equal(expectedDeployment.Status))
})
})
Describe("UpdateDeployment", func() {
It("Should update an existing deployment", func() {
By("Setting up mock server for deployment update")
updatedDeployment := Deployment{
ID: "deployment-12345",
Created: time.Now().Add(-time.Hour),
Updated: time.Now(),
Name: "updated-deployment",
FlowID: "flow-123",
Paused: true,
Tags: []string{"updated", "test"},
Parameters: map[string]interface{}{"param1": "updated_value"},
WorkPoolName: ptr.To("kubernetes"),
}
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Expect(r.Method).To(Equal("PATCH"))
Expect(r.URL.Path).To(Equal("/deployments/deployment-12345"))
Expect(r.Header.Get("Content-Type")).To(Equal("application/json"))
// Verify request body contains updated deployment spec
var deploymentSpec DeploymentSpec
_ = json.NewDecoder(r.Body).Decode(&deploymentSpec)
Expect(deploymentSpec.Name).To(Equal("updated-deployment"))
Expect(*deploymentSpec.Paused).To(BeTrue())
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(updatedDeployment)
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Calling UpdateDeployment")
deploymentSpec := &DeploymentSpec{
Name: "updated-deployment",
FlowID: "flow-123",
Paused: ptr.To(true),
Tags: []string{"updated", "test"},
Parameters: map[string]interface{}{"param1": "updated_value"},
}
deployment, err := client.UpdateDeployment(ctx, "deployment-12345", deploymentSpec)
By("Verifying deployment was updated")
Expect(err).NotTo(HaveOccurred())
Expect(deployment).NotTo(BeNil())
Expect(deployment.ID).To(Equal("deployment-12345"))
Expect(deployment.Name).To(Equal("updated-deployment"))
Expect(deployment.Paused).To(BeTrue())
})
})
Describe("DeleteDeployment", func() {
It("Should delete a deployment", func() {
By("Setting up mock server for deployment deletion")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Expect(r.Method).To(Equal("DELETE"))
Expect(r.URL.Path).To(Equal("/deployments/deployment-12345"))
// Return 204 No Content like the real Prefect API
w.WriteHeader(http.StatusNoContent)
}))
By("Creating client with mock server URL")
client = NewClient(mockServer.URL, "test-api-key", logger)
By("Calling DeleteDeployment")
err := client.DeleteDeployment(ctx, "deployment-12345")
By("Verifying deployment was deleted")
Expect(err).NotTo(HaveOccurred())
})
It("Should handle both 200 and 204 status codes for deletion", func() {
By("Testing 200 status code first")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Expect(r.Method).To(Equal("DELETE"))
Expect(r.URL.Path).To(Equal("/deployments/deployment-200"))
// Some APIs might return 200 with a response body
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"detail": "Deployment deleted"}`))
}))
client = NewClient(mockServer.URL, "test-api-key", logger)
err := client.DeleteDeployment(ctx, "deployment-200")
Expect(err).NotTo(HaveOccurred())
mockServer.Close()
By("Testing 204 status code")
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer GinkgoRecover()
Expect(r.Method).To(Equal("DELETE"))
Expect(r.URL.Path).To(Equal("/deployments/deployment-204"))
// Standard response for successful deletion
w.WriteHeader(http.StatusNoContent)
}))
client = NewClient(mockServer.URL, "test-api-key", logger)
err = client.DeleteDeployment(ctx, "deployment-204")
Expect(err).NotTo(HaveOccurred())
})
})
Describe("GetDeploymentByName", func() {
It("Should return error for unimplemented method", func() {
By("Creating client")
client = NewClient("http://test.com", "test-api-key", logger)
By("Calling GetDeploymentByName")
deployment, err := client.GetDeploymentByName(ctx, "test-deployment", "flow-123")
By("Verifying unimplemented error")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("GetDeploymentByName not yet implemented"))
Expect(deployment).To(BeNil())
})
})
Describe("HTTP Status Code Handling", func() {
var mockServer *httptest.Server
AfterEach(func() {
if mockServer != nil {
mockServer.Close()
}
})
Context("Success status codes (2xx)", func() {
It("Should accept 200 OK", func() {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"id":"test-123","name":"test","flow_id":"flow-123","status":"READY"}`))
}))
client = NewClient(mockServer.URL, "test-api-key", logger)
deployment, err := client.GetDeployment(ctx, "test-123")
Expect(err).NotTo(HaveOccurred())
Expect(deployment).NotTo(BeNil())
Expect(deployment.ID).To(Equal("test-123"))
})
It("Should accept 201 Created for deployment creation", func() {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated) // 201 Created
_, _ = w.Write([]byte(`{"id":"new-deployment-123","name":"test","flow_id":"flow-123","status":"READY"}`))
}))
client = NewClient(mockServer.URL, "test-api-key", logger)
spec := &DeploymentSpec{
Name: "test-deployment",
FlowID: "flow-123",
}
deployment, err := client.CreateOrUpdateDeployment(ctx, spec)
Expect(err).NotTo(HaveOccurred())
Expect(deployment).NotTo(BeNil())
Expect(deployment.ID).To(Equal("new-deployment-123"))
})
It("Should accept 202 Accepted", func() {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusAccepted) // 202 Accepted
_, _ = w.Write([]byte(`{"id":"test-123","name":"test","flow_id":"flow-123","status":"READY"}`))
}))
client = NewClient(mockServer.URL, "test-api-key", logger)
deployment, err := client.GetDeployment(ctx, "test-123")
Expect(err).NotTo(HaveOccurred())
Expect(deployment).NotTo(BeNil())
})
It("Should accept 204 No Content for deletions", func() {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent) // 204 No Content
}))
client = NewClient(mockServer.URL, "test-api-key", logger)
err := client.DeleteDeployment(ctx, "test-123")
Expect(err).NotTo(HaveOccurred())
})
})
Context("Error status codes (non-2xx)", func() {
It("Should reject 400 Bad Request", func() {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest) // 400 Bad Request
_, _ = w.Write([]byte(`{"error":"Invalid request"}`))
}))
client = NewClient(mockServer.URL, "test-api-key", logger)
deployment, err := client.GetDeployment(ctx, "test-123")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("API request failed with status 400"))
Expect(deployment).To(BeNil())
})
It("Should reject 401 Unauthorized", func() {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized) // 401 Unauthorized
_, _ = w.Write([]byte(`{"error":"Unauthorized"}`))
}))
client = NewClient(mockServer.URL, "test-api-key", logger)
deployment, err := client.GetDeployment(ctx, "test-123")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("API request failed with status 401"))
Expect(deployment).To(BeNil())
})
It("Should reject 404 Not Found (when not handling explicitly)", func() {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound) // 404 Not Found
_, _ = w.Write([]byte(`{"error":"Not found"}`))
}))
client = NewClient(mockServer.URL, "test-api-key", logger)
deployment, err := client.GetDeployment(ctx, "test-123")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("API request failed with status 404"))
Expect(deployment).To(BeNil())
})
It("Should reject 422 Unprocessable Entity", func() {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnprocessableEntity) // 422 Unprocessable Entity
_, _ = w.Write([]byte(`{"error":"Validation failed"}`))
}))
client = NewClient(mockServer.URL, "test-api-key", logger)
spec := &DeploymentSpec{
Name: "invalid-deployment",
FlowID: "flow-123",
}
deployment, err := client.CreateOrUpdateDeployment(ctx, spec)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("API request failed with status 422"))
Expect(deployment).To(BeNil())
})
It("Should reject 500 Internal Server Error", func() {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError) // 500 Internal Server Error
_, _ = w.Write([]byte(`{"error":"Server error"}`))
}))
client = NewClient(mockServer.URL, "test-api-key", logger)
deployment, err := client.GetDeployment(ctx, "test-123")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("API request failed with status 500"))
Expect(deployment).To(BeNil())
})
})
})
Describe("Client Creation from ServerReference", func() {
var (
serverRef *prefectiov1.PrefectServerReference
logger logr.Logger
)
BeforeEach(func() {
logger = logr.Discard()
})
Describe("NewClientFromServerReference", func() {
Context("URL generation behavior (focus on the bug)", func() {
It("should demonstrate that function hardcodes 'default' namespace", func() {
// Testing the URL generation logic by comparing what the function would produce
// vs what it should produce if it accepted a fallback namespace parameter
serverRef := &prefectiov1.PrefectServerReference{
Name: "prefect-server",
// Namespace is empty
}
// What GetAPIURL produces with different fallback namespaces
urlWithDefault := serverRef.GetAPIURL("default")
urlWithCustom := serverRef.GetAPIURL("deployment-namespace")
Expect(urlWithDefault).To(Equal("http://prefect-server.default.svc:4200/api"))
Expect(urlWithCustom).To(Equal("http://prefect-server.deployment-namespace.svc:4200/api"))
// The bug: NewClientFromServerReference always uses "default" fallback
// instead of accepting the fallback namespace as a parameter
Expect(urlWithDefault).NotTo(Equal(urlWithCustom))
})
It("should use provided fallback namespace when server namespace is empty (AFTER FIX)", func() {
serverRef := &prefectiov1.PrefectServerReference{
Name: "prefect-server",
// Namespace is empty - should use fallback namespace parameter
}
// This test verifies the fix works for remote servers (to avoid port-forwarding issues)
serverRefWithRemote := &prefectiov1.PrefectServerReference{
RemoteAPIURL: ptr.To("https://api.prefect.cloud"),
}
client, err := NewClientFromServerReference(serverRefWithRemote, "test-key", "fallback-namespace", logger)
Expect(err).NotTo(HaveOccurred())
Expect(client).NotTo(BeNil())
// Remote URL should not be affected by namespace changes
Expect(client.BaseURL).To(Equal("https://api.prefect.cloud/api"))
// Test the namespace behavior by checking what GetAPIURL would return
expectedURL := serverRef.GetAPIURL("fallback-namespace")
Expect(expectedURL).To(Equal("http://prefect-server.fallback-namespace.svc:4200/api"))
})
})
Context("when server reference has remote server", func() {
It("should use remote API URL", func() {
serverRef = &prefectiov1.PrefectServerReference{
RemoteAPIURL: ptr.To("https://api.prefect.cloud"),
}
client, err := NewClientFromServerReference(serverRef, "test-key", "test-namespace", logger)
Expect(err).NotTo(HaveOccurred())
Expect(client).NotTo(BeNil())
Expect(client.BaseURL).To(Equal("https://api.prefect.cloud/api"))
})
})
Context("URL generation for different scenarios", func() {
It("should correctly generate in-cluster URLs when namespace is specified", func() {
serverRef := &prefectiov1.PrefectServerReference{
Name: "prefect-server",
Namespace: "prefect-system",
}
// Test the URL generation directly without creating client (avoids port-forwarding issues)
expectedURL := serverRef.GetAPIURL("fallback-namespace")
Expect(expectedURL).To(Equal("http://prefect-server.prefect-system.svc:4200/api"))
// Verify fallback namespace is ignored when server namespace is specified
fallbackURL := serverRef.GetAPIURL("different-namespace")
Expect(fallbackURL).To(Equal("http://prefect-server.prefect-system.svc:4200/api"))
Expect(expectedURL).To(Equal(fallbackURL))
})
It("should use fallback namespace when server namespace is empty", func() {
serverRef := &prefectiov1.PrefectServerReference{
Name: "prefect-server",
// Namespace is empty - should use fallback
}
// Test different fallback namespaces
url1 := serverRef.GetAPIURL("namespace-1")
url2 := serverRef.GetAPIURL("namespace-2")
Expect(url1).To(Equal("http://prefect-server.namespace-1.svc:4200/api"))
Expect(url2).To(Equal("http://prefect-server.namespace-2.svc:4200/api"))
Expect(url1).NotTo(Equal(url2))
})
})
})
Describe("NewClientFromK8s", func() {
var (
ctx context.Context
)
BeforeEach(func() {
ctx = context.Background()
})
Context("namespace fallback behavior (BUG DEMONSTRATION)", func() {
It("should demonstrate the bug: NewClientFromK8s doesn't pass fallback namespace", func() {
// Test the URL generation directly to show the bug without port-forwarding issues
serverRef := &prefectiov1.PrefectServerReference{
Name: "prefect-server",
// Namespace is empty - should use deployment's namespace
}
// Direct test of GetAPIURL method shows what SHOULD happen
expectedURL := serverRef.GetAPIURL("deployment-namespace")
Expect(expectedURL).To(Equal("http://prefect-server.deployment-namespace.svc:4200/api"))
// But the bug is in NewClientFromServerReference which ignores the fallback
// and hardcodes "default" instead of using the passed namespace
actualURL := serverRef.GetAPIURL("default") // This is what the buggy code does
Expect(actualURL).To(Equal("http://prefect-server.default.svc:4200/api"))
// These URLs should be the same but they're different due to the bug
Expect(expectedURL).NotTo(Equal(actualURL))
})
It("should correctly use fallback namespace after fix (VERIFICATION)", func() {
// Use remote server reference to test NewClientFromK8s without port-forwarding
serverRef := &prefectiov1.PrefectServerReference{
RemoteAPIURL: ptr.To("https://api.prefect.cloud"),
}
client, err := NewClientFromK8s(ctx, serverRef, nil, "deployment-namespace", logger)
Expect(err).NotTo(HaveOccurred())
Expect(client).NotTo(BeNil())
Expect(client.BaseURL).To(Equal("https://api.prefect.cloud/api"))
// For in-cluster servers, verify the namespace fallback logic would work
inClusterRef := &prefectiov1.PrefectServerReference{
Name: "prefect-server",
// Namespace is empty - should use deployment-namespace as fallback
}
expectedURL := inClusterRef.GetAPIURL("deployment-namespace")
Expect(expectedURL).To(Equal("http://prefect-server.deployment-namespace.svc:4200/api"))
})
})
Context("with remote server reference", func() {
It("should use remote API URL regardless of namespace", func() {
serverRef = &prefectiov1.PrefectServerReference{
RemoteAPIURL: ptr.To("https://api.prefect.cloud"),
}
client, err := NewClientFromK8s(ctx, serverRef, nil, "deployment-namespace", logger)
Expect(err).NotTo(HaveOccurred())
Expect(client).NotTo(BeNil())
Expect(client.BaseURL).To(Equal("https://api.prefect.cloud/api"))
})
})
})
})
})