|
| 1 | +// Licensed to Elasticsearch B.V. under one or more agreements. |
| 2 | +// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +package gcs |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "encoding/json" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "log" |
| 13 | + "net" |
| 14 | + "net/http" |
| 15 | + "os" |
| 16 | + "testing" |
| 17 | + |
| 18 | + "github.com/ory/dockertest/v3" |
| 19 | + "github.com/ory/dockertest/v3/docker" |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | + "gotest.tools/assert" |
| 22 | + |
| 23 | + "github.com/elastic/stream/pkg/output" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + emulatorHost = "localhost" |
| 28 | + emulatorPort = "4443" |
| 29 | + bucket = "testbucket" |
| 30 | + objectname = "testobject" |
| 31 | +) |
| 32 | + |
| 33 | +var emulatorHostAndPort = fmt.Sprintf("http://%s", net.JoinHostPort(emulatorHost, emulatorPort)) |
| 34 | + |
| 35 | +func TestMain(m *testing.M) { |
| 36 | + pool, err := dockertest.NewPool("") |
| 37 | + if err != nil { |
| 38 | + log.Fatalf("Could not connect to docker: %s", err) |
| 39 | + } |
| 40 | + |
| 41 | + resource, err := pool.RunWithOptions(&dockertest.RunOptions{ |
| 42 | + Repository: "fsouza/fake-gcs-server", |
| 43 | + Tag: "latest", |
| 44 | + Cmd: []string{"-host=0.0.0.0", "-public-host=localhost", fmt.Sprintf("-port=%s", emulatorPort), "-scheme=http"}, |
| 45 | + PortBindings: map[docker.Port][]docker.PortBinding{ |
| 46 | + emulatorPort: {{HostIP: emulatorHost, HostPort: emulatorPort}}, |
| 47 | + }, |
| 48 | + ExposedPorts: []string{emulatorPort}, |
| 49 | + }, func(config *docker.HostConfig) { |
| 50 | + // set AutoRemove to true so that stopped container goes away by itself |
| 51 | + config.AutoRemove = true |
| 52 | + config.RestartPolicy = docker.RestartPolicy{ |
| 53 | + Name: "no", |
| 54 | + } |
| 55 | + }) |
| 56 | + if err != nil { |
| 57 | + log.Fatalf("Could not start resource: %s", err) |
| 58 | + } |
| 59 | + |
| 60 | + if err := pool.Retry(func() error { |
| 61 | + // Disable HTTP keep-alives to ensure no extra goroutines hang around. |
| 62 | + httpClient := http.Client{Transport: &http.Transport{DisableKeepAlives: true}} |
| 63 | + |
| 64 | + // Sanity check the emulator. |
| 65 | + resp, err := httpClient.Get(fmt.Sprintf("http://%s:%s/storage/v1/b", emulatorHost, emulatorPort)) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + defer resp.Body.Close() |
| 70 | + |
| 71 | + _, err = io.ReadAll(resp.Body) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + if resp.StatusCode != http.StatusOK { |
| 76 | + return fmt.Errorf("unexpected status code: %v", resp.StatusCode) |
| 77 | + } |
| 78 | + return err |
| 79 | + }); err != nil { |
| 80 | + _ = pool.Purge(resource) |
| 81 | + log.Fatalf("Could not connect to the gcs instance: %s", err) |
| 82 | + } |
| 83 | + |
| 84 | + code := m.Run() |
| 85 | + |
| 86 | + _ = pool.Purge(resource) |
| 87 | + |
| 88 | + os.Exit(code) |
| 89 | +} |
| 90 | + |
| 91 | +func TestGcs(t *testing.T) { |
| 92 | + out, err := New(&output.Options{ |
| 93 | + Addr: emulatorHostAndPort, |
| 94 | + GcsOptions: output.GcsOptions{ |
| 95 | + Bucket: bucket, |
| 96 | + Object: objectname, |
| 97 | + }, |
| 98 | + }) |
| 99 | + require.NoError(t, err) |
| 100 | + |
| 101 | + err = out.DialContext(context.Background()) |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + event := map[string]interface{}{ |
| 105 | + "message": "hello world!", |
| 106 | + } |
| 107 | + data, err := json.Marshal(event) |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + n, err := out.Write(data) |
| 111 | + require.NoError(t, err) |
| 112 | + assert.Equal(t, len(data), n) |
| 113 | + |
| 114 | + // Need to close the Writer for the Object to be created in the Bucket. |
| 115 | + out.Close() |
| 116 | + |
| 117 | + gcsClient, ctx, cancel, err := NewClient(emulatorHostAndPort) |
| 118 | + require.NoError(t, err) |
| 119 | + t.Cleanup(func() { _ = gcsClient.Close() }) |
| 120 | + t.Cleanup(cancel) |
| 121 | + |
| 122 | + o := gcsClient.Bucket(bucket).Object(objectname) |
| 123 | + r, err := o.NewReader(ctx) |
| 124 | + require.NoError(t, err) |
| 125 | + |
| 126 | + body, err := io.ReadAll(r) |
| 127 | + require.NoError(t, err) |
| 128 | + defer r.Close() |
| 129 | + |
| 130 | + assert.Equal(t, string(data), string(body)) |
| 131 | +} |
0 commit comments