@@ -509,6 +509,129 @@ func TestGetApplicationDetails(t *testing.T) {
509
509
baseURL = tmp
510
510
}
511
511
512
+ func TestGetApplicationHistory (t * testing.T ) {
513
+ err := argoV1aplha1 .AddToScheme (scheme .Scheme )
514
+ if err != nil {
515
+ t .Fatal (err )
516
+ }
517
+
518
+ builder := fake .NewClientBuilder ()
519
+ kc := builder .Build ()
520
+
521
+ ts , _ := makeServer (t , func (router * APIRouter ) {
522
+ router .k8sClient = kc
523
+ })
524
+
525
+ // create test ArgoCD Server to handle http requests
526
+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
527
+ u := r .URL .String ()
528
+ if strings .Contains (u , "session" ) {
529
+ m := map [string ]string {
530
+ "token" : "testing" ,
531
+ }
532
+ marshalResponse (w , m )
533
+ } else if strings .Contains (u , "metadata" ) {
534
+ m := map [string ]string {
535
+ "author" : "test" ,
536
+ "message" : "testMessage" ,
537
+ }
538
+ marshalResponse (w , m )
539
+ }
540
+ }))
541
+ defer server .Close ()
542
+ tmp := baseURL
543
+ baseURL = server .URL
544
+
545
+ var createOptions []ctrlclient.CreateOption
546
+ app , _ := testArgoApplication ("testdata/application3.yaml" )
547
+ // create argocd test-app
548
+ err = kc .Create (context .TODO (), app , createOptions ... )
549
+ if err != nil {
550
+ t .Fatal (err )
551
+ }
552
+
553
+ // create argocd instance creds secret
554
+ secret := & corev1.Secret {
555
+ ObjectMeta : metav1.ObjectMeta {
556
+ Name : defaultArgoCDInstance + "-cluster" ,
557
+ Namespace : defaultArgocdNamespace ,
558
+ },
559
+ Data : map [string ][]byte {
560
+ "admin.password" : []byte ("abc" ),
561
+ },
562
+ }
563
+ err = kc .Create (context .TODO (), secret , createOptions ... )
564
+ if err != nil {
565
+ t .Fatal (err )
566
+ }
567
+
568
+ options := url.Values {
569
+ "url" : []string {"https://github.com/test-repo/gitops.git" },
570
+ }
571
+ req := makeClientRequest (t , "Bearer testing" ,
572
+ fmt .Sprintf ("%s/history/environment/%s/application/%s?%s" , ts .URL , "dev" , "app-taxi" , options .Encode ()))
573
+ res , err := ts .Client ().Do (req )
574
+ if err != nil {
575
+ t .Fatal (err )
576
+ }
577
+ fmt .Println ("history res is " , res )
578
+
579
+ want := []envHistory {
580
+ {
581
+ Author : "test" ,
582
+ Message : "testMessage" ,
583
+ Revision : "a0c7298faead28f7f60a5106afbb18882ad220a7" ,
584
+ Environment : "dev" ,
585
+ RepoUrl : "https://github.com/test-repo/gitops.git" ,
586
+ DeployedAt : time .Date (2022 , time .Month (4 ), 22 , 17 , 11 , 29 , 0 , time .UTC ).Local ().String (),
587
+ },
588
+ {
589
+ Author : "test" ,
590
+ Message : "testMessage" ,
591
+ Revision : "3f6965bd65d9294b8fec5d6e2dc3dad08e33a8fe" ,
592
+ Environment : "dev" ,
593
+ RepoUrl : "https://github.com/test-repo/gitops.git" ,
594
+ DeployedAt : time .Date (2022 , time .Month (4 ), 21 , 14 , 17 , 49 , 0 , time .UTC ).Local ().String (),
595
+ },
596
+ {
597
+ Author : "test" ,
598
+ Message : "testMessage" ,
599
+ Revision : "e5585fcf22366e2d066e0936cbd8a0508756d02d" ,
600
+ Environment : "dev" ,
601
+ RepoUrl : "https://github.com/test-repo/gitops.git" ,
602
+ DeployedAt : time .Date (2022 , time .Month (4 ), 21 , 14 , 16 , 51 , 0 , time .UTC ).Local ().String (),
603
+ },
604
+ {
605
+ Author : "test" ,
606
+ Message : "testMessage" ,
607
+ Revision : "3f6965bd65d9294b8fec5d6e2dc3dad08e33a8fe" ,
608
+ Environment : "dev" ,
609
+ RepoUrl : "https://github.com/test-repo/gitops.git" ,
610
+ DeployedAt : time .Date (2022 , time .Month (4 ), 21 , 14 , 16 , 50 , 0 , time .UTC ).Local ().String (),
611
+ },
612
+ {
613
+ Author : "test" ,
614
+ Message : "testMessage" ,
615
+ Revision : "e5585fcf22366e2d066e0936cbd8a0508756d02d" ,
616
+ Environment : "dev" ,
617
+ RepoUrl : "https://github.com/test-repo/gitops.git" ,
618
+ DeployedAt : time .Date (2022 , time .Month (4 ), 21 , 14 , 14 , 27 , 0 , time .UTC ).Local ().String (),
619
+ },
620
+ {
621
+ Author : "test" ,
622
+ Message : "testMessage" ,
623
+ Revision : "e5585fcf22366e2d066e0936cbd8a0508756d02d" ,
624
+ Environment : "dev" ,
625
+ RepoUrl : "https://github.com/test-repo/gitops.git" ,
626
+ DeployedAt : time .Date (2022 , time .Month (4 ), 19 , 18 , 19 , 52 , 0 , time .UTC ).Local ().String (),
627
+ },
628
+ }
629
+ assertJSONResponseHistory (t , res , want )
630
+
631
+ //reset BaseURL
632
+ baseURL = tmp
633
+ }
634
+
512
635
func testArgoApplication (appCr string ) (* argoV1aplha1.Application , error ) {
513
636
applicationYaml , _ := ioutil .ReadFile (appCr )
514
637
app := & argoV1aplha1.Application {}
@@ -588,7 +711,7 @@ func makeServer(t *testing.T, opts ...routerOptionFunc) (*httptest.Server, *stub
588
711
return ts , sf .client
589
712
}
590
713
591
- func assertJSONResponse (t * testing.T , res * http.Response , want map [ string ] interface {}) {
714
+ func readBody (t * testing.T , res * http.Response ) [] byte {
592
715
t .Helper ()
593
716
if res .StatusCode != http .StatusOK {
594
717
defer res .Body .Close ()
@@ -609,9 +732,26 @@ func assertJSONResponse(t *testing.T, res *http.Response, want map[string]interf
609
732
if h := res .Header .Get ("Access-Control-Allow-Origin" ); h != "*" {
610
733
t .Fatalf ("wanted '*' got %s" , h )
611
734
}
735
+ return b
736
+ }
737
+
738
+ func assertJSONResponse (t * testing.T , res * http.Response , want map [string ]interface {}) {
739
+ b := readBody (t , res )
612
740
got := map [string ]interface {}{}
613
741
614
- err = json .Unmarshal (b , & got )
742
+ err := json .Unmarshal (b , & got )
743
+ if err != nil {
744
+ t .Fatalf ("failed to parse %s: %s" , b , err )
745
+ }
746
+ if diff := cmp .Diff (want , got ); diff != "" {
747
+ t .Fatalf ("JSON response failed:\n %s" , diff )
748
+ }
749
+ }
750
+
751
+ func assertJSONResponseHistory (t * testing.T , res * http.Response , want []envHistory ) {
752
+ b := readBody (t , res )
753
+ got := make ([]envHistory , 0 )
754
+ err := json .Unmarshal (b , & got )
615
755
if err != nil {
616
756
t .Fatalf ("failed to parse %s: %s" , b , err )
617
757
}
0 commit comments