|
| 1 | +package gcs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + gcs "cloud.google.com/go/storage" |
| 11 | + "github.com/docker/docker/api/types/container" |
| 12 | + "github.com/docker/go-connections/nat" |
| 13 | + "github.com/testcontainers/testcontainers-go" |
| 14 | + "github.com/testcontainers/testcontainers-go/wait" |
| 15 | + "google.golang.org/api/option" |
| 16 | + |
| 17 | + "github.com/percona/percona-backup-mongodb/pbm/storage" |
| 18 | +) |
| 19 | + |
| 20 | +func TestGCS(t *testing.T) { |
| 21 | + ctx := context.Background() |
| 22 | + |
| 23 | + req := testcontainers.ContainerRequest{ |
| 24 | + Image: "fsouza/fake-gcs-server", |
| 25 | + ExposedPorts: []string{"4443/tcp"}, |
| 26 | + Cmd: []string{"-public-host", "localhost:4443", "-scheme", "http", "-port", "4443"}, |
| 27 | + WaitingFor: wait.ForLog("server started at"), |
| 28 | + HostConfigModifier: func(hc *container.HostConfig) { |
| 29 | + hc.PortBindings = nat.PortMap{ |
| 30 | + "4443/tcp": {{HostIP: "0.0.0.0", HostPort: "4443"}}, |
| 31 | + } |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + gcsContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 36 | + ContainerRequest: req, |
| 37 | + Started: true, |
| 38 | + }) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("failed to start GCS container: %s", err) |
| 41 | + } |
| 42 | + defer func() { |
| 43 | + if err = testcontainers.TerminateContainer(gcsContainer); err != nil { |
| 44 | + t.Fatalf("failed to terminate container: %s", err) |
| 45 | + } |
| 46 | + }() |
| 47 | + |
| 48 | + host, err := gcsContainer.Host(ctx) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("failed to get container host: %s", err) |
| 51 | + } |
| 52 | + port, err := gcsContainer.MappedPort(ctx, "4443") |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("failed to get mapped port: %s", err) |
| 55 | + } |
| 56 | + endpoint := fmt.Sprintf("http://%s:%s", host, port.Port()) |
| 57 | + fmt.Println(endpoint) |
| 58 | + |
| 59 | + _ = os.Setenv("STORAGE_EMULATOR_HOST", endpoint) |
| 60 | + defer func() { |
| 61 | + _ = os.Unsetenv("STORAGE_EMULATOR_HOST") |
| 62 | + }() |
| 63 | + |
| 64 | + gcsClient, err := gcs.NewClient(ctx, option.WithoutAuthentication()) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("failed to create GCS client: %s", err) |
| 67 | + } |
| 68 | + defer func() { |
| 69 | + if err = gcsClient.Close(); err != nil { |
| 70 | + t.Fatalf("failed to close client: %s", err) |
| 71 | + } |
| 72 | + }() |
| 73 | + |
| 74 | + bucketName := "test-bucket" |
| 75 | + bucket := gcsClient.Bucket(bucketName) |
| 76 | + if err = bucket.Create(ctx, "fakeProject", nil); err != nil { |
| 77 | + t.Fatalf("failed to create bucket: %s", err) |
| 78 | + } |
| 79 | + |
| 80 | + chunkSize := 1024 |
| 81 | + |
| 82 | + opts := &Config{ |
| 83 | + Bucket: bucketName, |
| 84 | + ChunkSize: &chunkSize, |
| 85 | + Credentials: Credentials{ |
| 86 | + |
| 87 | + PrivateKey: "-----BEGIN PRIVATE KEY-----\nKey\n-----END PRIVATE KEY-----\n", |
| 88 | + }, |
| 89 | + Retryer: &Retryer{ |
| 90 | + BackoffInitial: 1, |
| 91 | + BackoffMax: 2, |
| 92 | + BackoffMultiplier: 1, |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + stg, err := New(opts, "node", nil) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("failed to create gcs storage: %s", err) |
| 99 | + } |
| 100 | + |
| 101 | + storage.RunStorageTests(t, stg, storage.GCS) |
| 102 | + |
| 103 | + t.Run("Delete fails", func(t *testing.T) { |
| 104 | + name := "not_found.txt" |
| 105 | + err := stg.Delete(name) |
| 106 | + |
| 107 | + if err == nil { |
| 108 | + t.Errorf("expected error when deleting non-existing file, got nil") |
| 109 | + } |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +func TestConfig(t *testing.T) { |
| 114 | + opts := &Config{ |
| 115 | + Bucket: "bucketName", |
| 116 | + Prefix: "prefix", |
| 117 | + Credentials: Credentials{ |
| 118 | + |
| 119 | + PrivateKey: "-----BEGIN PRIVATE KEY-----\nKey\n-----END PRIVATE KEY-----\n", |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + t.Run("Clone", func(t *testing.T) { |
| 124 | + clone := opts.Clone() |
| 125 | + if clone == opts { |
| 126 | + t.Error("expected clone to be a different pointer") |
| 127 | + } |
| 128 | + |
| 129 | + if !opts.Equal(clone) { |
| 130 | + t.Error("expected clone to be equal") |
| 131 | + } |
| 132 | + |
| 133 | + opts.Bucket = "updatedName" |
| 134 | + if opts.Equal(clone) { |
| 135 | + t.Error("expected clone to be unchanged when updating original") |
| 136 | + } |
| 137 | + }) |
| 138 | + |
| 139 | + t.Run("Equal fails", func(t *testing.T) { |
| 140 | + if opts.Equal(nil) { |
| 141 | + t.Error("expected not to be equal other nil") |
| 142 | + } |
| 143 | + |
| 144 | + clone := opts.Clone() |
| 145 | + clone.Prefix = "updatedPrefix" |
| 146 | + if opts.Equal(clone) { |
| 147 | + t.Error("expected not to be equal when updating prefix") |
| 148 | + } |
| 149 | + |
| 150 | + clone = opts.Clone() |
| 151 | + clone. Credentials. ClientEmail = "[email protected]" |
| 152 | + if opts.Equal(clone) { |
| 153 | + t.Error("expected not to be equal when updating credentials") |
| 154 | + } |
| 155 | + }) |
| 156 | +} |
| 157 | + |
| 158 | +func TestEmptyCredentialsFail(t *testing.T) { |
| 159 | + opts := &Config{ |
| 160 | + Bucket: "bucketName", |
| 161 | + } |
| 162 | + |
| 163 | + _, err := New(opts, "node", nil) |
| 164 | + |
| 165 | + if err == nil { |
| 166 | + t.Fatalf("expected error when not specifying credentials") |
| 167 | + } |
| 168 | + |
| 169 | + if !strings.Contains(err.Error(), "required for GCS credentials") { |
| 170 | + t.Errorf("expected required credentials, got %s", err) |
| 171 | + } |
| 172 | +} |
0 commit comments