@@ -468,3 +468,110 @@ func TestPipelineCheckPeriodicSchedules(t *testing.T) {
468
468
}
469
469
})
470
470
}
471
+
472
+ func TestPipelineNameAvailable (t * testing.T ) {
473
+ tmp , _ := ioutil .TempDir ("" , "TestPipelineNameAvailable" )
474
+ dataDir := tmp
475
+
476
+ gaia .Cfg = & gaia.Config {
477
+ Logger : hclog .NewNullLogger (),
478
+ HomePath : dataDir ,
479
+ DataPath : dataDir ,
480
+ PipelinePath : dataDir ,
481
+ }
482
+
483
+ // Initialize store
484
+ dataStore , _ := services .StorageService ()
485
+ dataStore .Init ()
486
+ defer func () { services .MockStorageService (nil ) }()
487
+
488
+ // Initialize global active pipelines
489
+ ap := pipeline .NewActivePipelines ()
490
+ pipeline .GlobalActivePipelines = ap
491
+
492
+ // Initialize echo
493
+ e := echo .New ()
494
+ InitHandlers (e )
495
+
496
+ p := gaia.Pipeline {
497
+ ID : 1 ,
498
+ Name : "Pipeline A" ,
499
+ Type : gaia .PTypeGolang ,
500
+ Created : time .Now (),
501
+ }
502
+
503
+ // Add to store
504
+ err := dataStore .PipelinePut (& p )
505
+ if err != nil {
506
+ t .Fatal (err )
507
+ }
508
+ // Add to active pipelines
509
+ ap .Append (p )
510
+
511
+ t .Run ("fails for pipeline name already in use" , func (t * testing.T ) {
512
+ req := httptest .NewRequest (echo .GET , "/" , nil )
513
+ req .Header .Set ("Content-Type" , "application/json" )
514
+ rec := httptest .NewRecorder ()
515
+ c := e .NewContext (req , rec )
516
+ c .SetPath ("/api/" + gaia .APIVersion + "/pipeline/name" )
517
+ q := req .URL .Query ()
518
+ q .Add ("name" , "pipeline a" )
519
+ req .URL .RawQuery = q .Encode ()
520
+
521
+ PipelineNameAvailable (c )
522
+
523
+ if rec .Code != http .StatusBadRequest {
524
+ t .Fatalf ("expected response code %v got %v" , http .StatusBadRequest , rec .Code )
525
+ }
526
+ bodyBytes , err := ioutil .ReadAll (rec .Body )
527
+ if err != nil {
528
+ t .Fatalf ("cannot read response body: %s" , err .Error ())
529
+ }
530
+ nameAlreadyInUseMessage := "pipeline name is already in use"
531
+ if string (bodyBytes [:]) != nameAlreadyInUseMessage {
532
+ t .Fatalf ("error message should be '%s' but was '%s'" , nameAlreadyInUseMessage , string (bodyBytes [:]))
533
+ }
534
+ })
535
+
536
+ t .Run ("fails for pipeline name is too long" , func (t * testing.T ) {
537
+ req := httptest .NewRequest (echo .GET , "/" , nil )
538
+ req .Header .Set ("Content-Type" , "application/json" )
539
+ rec := httptest .NewRecorder ()
540
+ c := e .NewContext (req , rec )
541
+ c .SetPath ("/api/" + gaia .APIVersion + "/pipeline/name" )
542
+ q := req .URL .Query ()
543
+ q .Add ("name" , "pipeline a pipeline a pipeline a pipeline a pipeline a pipeline a pipeline a pipeline a pipeline a pipeline a" )
544
+ req .URL .RawQuery = q .Encode ()
545
+
546
+ PipelineNameAvailable (c )
547
+
548
+ if rec .Code != http .StatusBadRequest {
549
+ t .Fatalf ("expected response code %v got %v" , http .StatusBadRequest , rec .Code )
550
+ }
551
+ bodyBytes , err := ioutil .ReadAll (rec .Body )
552
+ if err != nil {
553
+ t .Fatalf ("cannot read response body: %s" , err .Error ())
554
+ }
555
+ nameTooLongMessage := "name of pipeline is empty or one of the path elements length exceeds 50 characters"
556
+ if string (bodyBytes [:]) != nameTooLongMessage {
557
+ t .Fatalf ("error message should be '%s' but was '%s'" , nameTooLongMessage , string (bodyBytes [:]))
558
+ }
559
+ })
560
+
561
+ t .Run ("works for pipeline with different name" , func (t * testing.T ) {
562
+ req := httptest .NewRequest (echo .GET , "/" , nil )
563
+ req .Header .Set ("Content-Type" , "application/json" )
564
+ rec := httptest .NewRecorder ()
565
+ c := e .NewContext (req , rec )
566
+ c .SetPath ("/api/" + gaia .APIVersion + "/pipeline/name" )
567
+ q := req .URL .Query ()
568
+ q .Add ("name" , "pipeline b" )
569
+ req .URL .RawQuery = q .Encode ()
570
+
571
+ PipelineNameAvailable (c )
572
+
573
+ if rec .Code != http .StatusOK {
574
+ t .Fatalf ("expected response code %v got %v" , http .StatusOK , rec .Code )
575
+ }
576
+ })
577
+ }
0 commit comments