|
| 1 | +**HTTP Replay** |
| 2 | +===== |
| 3 | + |
| 4 | +Synopsis |
| 5 | +----- |
| 6 | + |
| 7 | +``` |
| 8 | + // install the hook for HTTP replaying: |
| 9 | + |
| 10 | + if h, ok := client.HTTPClient.(*http.Client); ok { |
| 11 | + _, err := httpreplay.InstallRecorder(h) |
| 12 | + if err != nil { |
| 13 | + return err |
| 14 | + } |
| 15 | + } |
| 16 | +
|
| 17 | +func TestMyServiceResource_basic(t *testing.T) { |
| 18 | + // In a unit test, tell the recorder what test we are running |
| 19 | + httpreplay.SetScenario("TestMyServiceResource_basic") |
| 20 | + defer httpreplay.SaveScenario() |
| 21 | + ... testing happens ... |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +Description |
| 26 | +----- |
| 27 | + |
| 28 | +This library provides a recording mechanism for the OCI-GO-SDK. It hooks into |
| 29 | +the 'Transport' layer of the HTTP request, calling through to the real |
| 30 | +network as needed. |
| 31 | + |
| 32 | +* bypass (default): Do nothing |
| 33 | +* record: Store the Interaction |
| 34 | +* replay: Load the Interaction file and send back the response |
| 35 | + |
| 36 | +Select the record or replay mode by specifying a build tag to go: `-tags <mode>` |
| 37 | + |
| 38 | + |
| 39 | +Functions |
| 40 | +----- |
| 41 | + |
| 42 | +InstallRecorder |
| 43 | +* Install hooks into the `http.Client` to allow the record/replay library to |
| 44 | + intercept HTTP calls. `InstallRecorder` tries to be safe if called multiple |
| 45 | + times, but it is possible to fool it. Best is to only call it once per |
| 46 | + `http.Client`. |
| 47 | + |
| 48 | +SetScenario |
| 49 | +* Name the scenario that is about to run. |
| 50 | + |
| 51 | + Currently, this specifies |
| 52 | + the filename that the scenario data will be saved into, |
| 53 | + with a `.yaml` extension. |
| 54 | + - If `-tags record` is specified, the |
| 55 | + requests are written to this file in the `SaveScenario` call. The Interaction file |
| 56 | + will be stored in directory `~<prj_path>/record/` with the name passed into. |
| 57 | + - If `-tags replay` is specified, then a file by that name is immediately |
| 58 | + read and used for generating replies to network requests. |
| 59 | + |
| 60 | +SaveScenario |
| 61 | +* Save the scenario data. |
| 62 | + |
| 63 | + Currently, if `-tags record` is specified, this writes all the |
| 64 | + recorded requests to the file named in `SetScenario`. |
| 65 | + |
| 66 | + |
| 67 | +Record Storage |
| 68 | +----- |
| 69 | + |
| 70 | +* In record mode: After running the test case, the record file will be stored under "oci/record/". |
| 71 | +* In replay mode: Look for the record file under "oci/record/" and throw error if it is not found. |
| 72 | + |
| 73 | + |
| 74 | +Example usage |
| 75 | +----- |
| 76 | +* To run normally: `go test` |
| 77 | +* Or run 1 specific test case: `go test -run <testname>` |
| 78 | +---- |
| 79 | +* To record interactions: `go test -tags record` |
| 80 | +* Or to record 1 specific test case: `go test -run <testname> -tags record` |
| 81 | +---- |
| 82 | +* To replay interactions: `go test -tags replay` |
| 83 | +* Or to replay 1 specific test case: `go test -run <testname> -tags replay` |
| 84 | + |
| 85 | +### Example Output |
| 86 | + |
| 87 | +Run with recording turned on, the test portion takes 2411 seconds: |
| 88 | + |
| 89 | + > go test -v -timeout 120m -run TestResourceCoreImageTestSuite -tags record |
| 90 | + === RUN TestResourceCoreImageTestSuite |
| 91 | + === RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_basic |
| 92 | + === RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageTuple |
| 93 | + === RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageUri |
| 94 | + --- PASS: TestResourceCoreImageTestSuite (2411.14s) |
| 95 | + --- PASS: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_basic (2410.58s) |
| 96 | + --- SKIP: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageTuple (0.00s) |
| 97 | + core_image_resource_test.go:191: Long running test, requires per tenancy namespace + bucket + image export object to run |
| 98 | + --- SKIP: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageUri (0.00s) |
| 99 | + core_image_resource_test.go:155: Long running test, requires exported image available via public url |
| 100 | + PASS |
| 101 | + |
| 102 | + |
| 103 | +Now that we have a recording, run in replay mode, note that it is only 4.09 seconds: |
| 104 | + |
| 105 | + > go test -v -run TestResourceCoreImageTestSuite -tags replay |
| 106 | + === RUN TestResourceCoreImageTestSuite |
| 107 | + === RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_basic |
| 108 | + === RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageTuple |
| 109 | + === RUN TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageUri |
| 110 | + --- PASS: TestResourceCoreImageTestSuite (4.09s) |
| 111 | + --- PASS: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_basic (3.60s) |
| 112 | + --- SKIP: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageTuple (0.00s) |
| 113 | + core_image_resource_test.go:191: Long running test, requires per tenancy namespace + bucket + image export object to run |
| 114 | + --- SKIP: TestResourceCoreImageTestSuite/TestAccResourceCoreImage_createFromExport_objectStorageUri (0.00s) |
| 115 | + core_image_resource_test.go:155: Long running test, requires exported image available via public url |
| 116 | + PASS |
| 117 | + |
0 commit comments