@@ -23,17 +23,15 @@ import (
23
23
"io"
24
24
"io/ioutil"
25
25
"net/http"
26
- "net/http/httputil"
27
26
"net/url"
28
27
"os"
29
28
"strings"
30
29
"testing"
31
30
32
31
"cloud.google.com/go/internal/uid"
33
32
storage_v1_tests "cloud.google.com/go/storage/internal/test/conformance"
33
+ "github.com/googleapis/gax-go/v2/callctx"
34
34
"google.golang.org/api/iterator"
35
- "google.golang.org/api/option"
36
- htransport "google.golang.org/api/transport/http"
37
35
)
38
36
39
37
var (
@@ -521,34 +519,43 @@ func TestRetryConformance(t *testing.T) {
521
519
t .Logf ("No tests for operation %v" , methodName )
522
520
}
523
521
for i , fn := range methods [methodName ] {
524
- testName := fmt .Sprintf ("%v-%v-%v-%v" , retryTest .Id , instructions .Instructions , methodName , i )
525
- t .Run (testName , func (t * testing.T ) {
526
-
527
- // Create the retry subtest
528
- subtest := & emulatorTest {T : t , name : testName , host : endpoint }
529
- subtest .create (map [string ][]string {
530
- method .Name : instructions .Instructions ,
531
- })
532
-
533
- // Create necessary test resources in the emulator
534
- subtest .populateResources (ctx , client , method .Resources )
535
-
536
- // Test
537
- err = fn (ctx , subtest .wrappedClient , & subtest .resources , retryTest .PreconditionProvided )
538
- if retryTest .ExpectSuccess && err != nil {
539
- t .Errorf ("want success, got %v" , err )
540
- }
541
- if ! retryTest .ExpectSuccess && err == nil {
542
- t .Errorf ("want failure, got success" )
522
+ transports := []string {"http" , "grpc" }
523
+ for _ , transport := range transports {
524
+ if transport == "grpc" && method .Name == "storage.objects.insert" {
525
+ t .Skip ("Implementation in testbench pending: https://github.com/googleapis/storage-testbench/issues/568" )
543
526
}
544
-
545
- // Verify that all instructions were used up during the test
546
- // (indicates that the client sent the correct requests).
547
- subtest .check ()
548
-
549
- // Close out test in emulator.
550
- subtest .delete ()
551
- })
527
+ testName := fmt .Sprintf ("%v-%v-%v-%v-%v" , transport , retryTest .Id , instructions .Instructions , methodName , i )
528
+ t .Run (testName , func (t * testing.T ) {
529
+
530
+ // Create the retry subtest
531
+ subtest := & emulatorTest {T : t , name : testName , host : endpoint }
532
+ subtest .create (map [string ][]string {
533
+ method .Name : instructions .Instructions ,
534
+ }, transport )
535
+
536
+ // Create necessary test resources in the emulator
537
+ subtest .populateResources (ctx , client , method .Resources )
538
+
539
+ // Test
540
+ // Set retry test id through headers per test call
541
+ ctx := context .Background ()
542
+ ctx = callctx .SetHeaders (ctx , "x-retry-test-id" , subtest .id )
543
+ err = fn (ctx , subtest .transportClient , & subtest .resources , retryTest .PreconditionProvided )
544
+ if retryTest .ExpectSuccess && err != nil {
545
+ t .Errorf ("want success, got %v" , err )
546
+ }
547
+ if ! retryTest .ExpectSuccess && err == nil {
548
+ t .Errorf ("want failure, got success" )
549
+ }
550
+
551
+ // Verify that all instructions were used up during the test
552
+ // (indicates that the client sent the correct requests).
553
+ subtest .check ()
554
+
555
+ // Close out test in emulator.
556
+ subtest .delete ()
557
+ })
558
+ }
552
559
}
553
560
}
554
561
}
@@ -558,11 +565,11 @@ func TestRetryConformance(t *testing.T) {
558
565
559
566
type emulatorTest struct {
560
567
* testing.T
561
- name string
562
- id string // ID to pass as a header in the test execution
563
- resources resources
564
- host * url.URL // set the path when using; path is not guaranteed between calls
565
- wrappedClient * Client
568
+ name string
569
+ id string // ID to pass as a header in the test execution
570
+ resources resources
571
+ host * url.URL // set the path when using; path is not guaranteed between calls
572
+ transportClient * Client
566
573
}
567
574
568
575
// Holds the resources for a particular test case. Only the necessary fields will
@@ -651,12 +658,14 @@ func uploadTestObject(bucketName, objName string, n []byte) error {
651
658
}
652
659
653
660
// Creates a retry test resource in the emulator
654
- func (et * emulatorTest ) create (instructions map [string ][]string ) {
661
+ func (et * emulatorTest ) create (instructions map [string ][]string , transport string ) {
655
662
c := http .DefaultClient
656
663
data := struct {
657
664
Instructions map [string ][]string `json:"instructions"`
665
+ Transport string `json:"transport"`
658
666
}{
659
667
Instructions : instructions ,
668
+ Transport : transport ,
660
669
}
661
670
662
671
buf := new (bytes.Buffer )
@@ -666,6 +675,9 @@ func (et *emulatorTest) create(instructions map[string][]string) {
666
675
667
676
et .host .Path = "retry_test"
668
677
resp , err := c .Post (et .host .String (), "application/json" , buf )
678
+ if resp .StatusCode == 501 {
679
+ et .T .Skip ("This retry test case is not yet supported in the testbench." )
680
+ }
669
681
if err != nil || resp .StatusCode != 200 {
670
682
et .Fatalf ("creating retry test: err: %v, resp: %+v" , err , resp )
671
683
}
@@ -683,14 +695,21 @@ func (et *emulatorTest) create(instructions map[string][]string) {
683
695
}
684
696
685
697
et .id = testRes .TestID
686
-
687
- // Create wrapped client which will send emulator instructions
688
698
et .host .Path = ""
689
- client , err := wrappedClient (et .T , et .id )
699
+
700
+ // Create transportClient for http or grpc
701
+ ctx := context .Background ()
702
+ transportClient , err := NewClient (ctx )
690
703
if err != nil {
691
- et .Fatalf ("creating wrapped client: %v" , err )
704
+ et .Fatalf ("HTTP transportClient: %v" , err )
705
+ }
706
+ if transport == "grpc" {
707
+ transportClient , err = NewGRPCClient (ctx )
708
+ if err != nil {
709
+ et .Fatalf ("GRPC transportClient: %v" , err )
710
+ }
692
711
}
693
- et .wrappedClient = client
712
+ et .transportClient = transportClient
694
713
}
695
714
696
715
// Verifies that all instructions for a given retry testID have been used up
@@ -732,47 +751,3 @@ func (et *emulatorTest) delete() {
732
751
et .Errorf ("deleting test: err: %v, resp: %+v" , err , resp )
733
752
}
734
753
}
735
-
736
- // retryTestRoundTripper sends the retry test ID to the emulator with each request
737
- type retryTestRoundTripper struct {
738
- * testing.T
739
- rt http.RoundTripper
740
- testID string
741
- }
742
-
743
- func (wt * retryTestRoundTripper ) RoundTrip (r * http.Request ) (* http.Response , error ) {
744
- r .Header .Set ("x-retry-test-id" , wt .testID )
745
-
746
- requestDump , err := httputil .DumpRequest (r , false )
747
- if err != nil {
748
- wt .Logf ("error creating request dump: %v" , err )
749
- }
750
-
751
- resp , err := wt .rt .RoundTrip (r )
752
- if err != nil {
753
- wt .Logf ("roundtrip error (may be expected): %v\n request: %s" , err , requestDump )
754
- }
755
- return resp , err
756
- }
757
-
758
- // Create custom client that sends instructions to the storage testbench Retry Test API
759
- func wrappedClient (t * testing.T , testID string ) (* Client , error ) {
760
- ctx := context .Background ()
761
- base := http .DefaultTransport
762
-
763
- trans , err := htransport .NewTransport (ctx , base , option .WithoutAuthentication (), option .WithUserAgent ("custom-user-agent" ))
764
- if err != nil {
765
- return nil , fmt .Errorf ("failed to create http client: %v" , err )
766
- }
767
-
768
- c := http.Client {Transport : trans }
769
-
770
- // Add RoundTripper to the created HTTP client
771
- wrappedTrans := & retryTestRoundTripper {rt : c .Transport , testID : testID , T : t }
772
- c .Transport = wrappedTrans
773
-
774
- // Supply this client to storage.NewClient
775
- // STORAGE_EMULATOR_HOST takes care of setting the correct endpoint
776
- client , err := NewClient (ctx , option .WithHTTPClient (& c ))
777
- return client , err
778
- }
0 commit comments