-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Database Replica Integration Tests (#1596)
This patch adds the remaining database replica integration tests. It also corrects some of the parameters for the replica promote test. Co-authored-by: Anna Lushnikova <[email protected]>
- Loading branch information
Showing
4 changed files
with
245 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httputil" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/sclevine/spec" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var _ = suite("database/replica", func(t *testing.T, when spec.G, it spec.S) { | ||
var ( | ||
expect *require.Assertions | ||
server *httptest.Server | ||
) | ||
|
||
it.Before(func() { | ||
expect = require.New(t) | ||
|
||
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
switch req.URL.Path { | ||
case "/v2/databases/1111/replicas": | ||
auth := req.Header.Get("Authorization") | ||
if auth != "Bearer some-magic-token" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
if req.Method != http.MethodPost { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
body, err := io.ReadAll(req.Body) | ||
expect.NoError(err) | ||
|
||
expect.JSONEq(databaseReplicaCreateRequest, string(body)) | ||
|
||
w.WriteHeader(http.StatusCreated) | ||
w.Write([]byte(databaseReplicaCreateResponse)) | ||
default: | ||
dump, err := httputil.DumpRequest(req, true) | ||
if err != nil { | ||
t.Fatal("failed to dump request") | ||
} | ||
|
||
t.Fatalf("received unknown request: %s", dump) | ||
} | ||
})) | ||
}) | ||
|
||
when("command is create", func() { | ||
it("create a read-only replica database", func() { | ||
cmd := exec.Command(builtBinaryPath, | ||
"-t", "some-magic-token", | ||
"-u", server.URL, | ||
"databases", | ||
"replica", | ||
"create", | ||
"1111", | ||
"2222", | ||
) | ||
|
||
output, err := cmd.CombinedOutput() | ||
expect.NoError(err, fmt.Sprintf("received error output: %s", output)) | ||
}) | ||
|
||
}) | ||
}) | ||
|
||
const ( | ||
// All of the values are default except for the name. | ||
databaseReplicaCreateRequest = `{"name":"2222","private_network_uuid":"","region":"nyc1","size":"db-s-1vcpu-1gb"}` | ||
databaseReplicaCreateResponse = `{"replica":{"name":"2222","connection":{"uri":"","database":"defaultdb","host":"","port":25060,"user":"doadmin","password":"","ssl":true},"private_connection":{"uri":"","database":"","host":"","port":25060,"user":"doadmin","password":"","ssl":true},"region":"nyc1","status":"online","created_at":"2019-01-11T18:37:36Z"}}` | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httputil" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/sclevine/spec" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var _ = suite("database/replica", func(t *testing.T, when spec.G, it spec.S) { | ||
var ( | ||
expect *require.Assertions | ||
server *httptest.Server | ||
) | ||
|
||
it.Before(func() { | ||
expect = require.New(t) | ||
|
||
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
switch req.URL.Path { | ||
case "/v2/databases/1111/replicas/2222": | ||
auth := req.Header.Get("Authorization") | ||
if auth != "Bearer some-magic-token" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
if req.Method != http.MethodDelete { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusNoContent) | ||
w.Write([]byte(``)) | ||
default: | ||
dump, err := httputil.DumpRequest(req, true) | ||
if err != nil { | ||
t.Fatal("failed to dump request") | ||
} | ||
|
||
t.Fatalf("received unknown request: %s", dump) | ||
} | ||
})) | ||
}) | ||
|
||
when("command is delete", func() { | ||
it("delete a replica database", func() { | ||
cmd := exec.Command(builtBinaryPath, | ||
"-t", "some-magic-token", | ||
"-u", server.URL, | ||
"databases", | ||
"replica", | ||
"delete", | ||
// Need to force to forego the prompt. | ||
"--force", | ||
"1111", | ||
"2222", | ||
) | ||
|
||
output, err := cmd.CombinedOutput() | ||
expect.NoError(err, fmt.Sprintf("received error output: %s", output)) | ||
}) | ||
|
||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/http/httputil" | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/sclevine/spec" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var _ = suite("database/replica", func(t *testing.T, when spec.G, it spec.S) { | ||
var ( | ||
expect *require.Assertions | ||
server *httptest.Server | ||
) | ||
|
||
it.Before(func() { | ||
expect = require.New(t) | ||
|
||
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
auth := req.Header.Get("Authorization") | ||
if auth != "Bearer some-magic-token" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
if req.Method != http.MethodGet { | ||
w.WriteHeader(http.StatusMethodNotAllowed) | ||
return | ||
} | ||
switch req.URL.Path { | ||
case "/v2/databases/1111/replicas/2222": | ||
w.Write([]byte(`{"replica":`)) | ||
w.Write([]byte(replicaMetadata)) | ||
w.Write([]byte(`}`)) | ||
case "/v2/databases/1111/replicas": | ||
w.Write([]byte(`{"replicas":[`)) | ||
w.Write([]byte(replicaMetadata)) | ||
w.Write([]byte(`]}`)) | ||
default: | ||
dump, err := httputil.DumpRequest(req, true) | ||
if err != nil { | ||
t.Fatal("failed to dump request") | ||
} | ||
|
||
t.Fatalf("received unknown request: %s", dump) | ||
} | ||
})) | ||
}) | ||
|
||
when("command is get", func() { | ||
it("return metadata about a replica database", func() { | ||
cmd := exec.Command(builtBinaryPath, | ||
"-t", "some-magic-token", | ||
"-u", server.URL, | ||
"databases", | ||
"replica", | ||
"get", | ||
"1111", | ||
"2222", | ||
) | ||
|
||
output, err := cmd.CombinedOutput() | ||
expect.NoError(err, fmt.Sprintf("received error output: %s", output)) | ||
}) | ||
|
||
}) | ||
|
||
when("command is list", func() { | ||
it("list all of the replica databases", func() { | ||
cmd := exec.Command(builtBinaryPath, | ||
"-t", "some-magic-token", | ||
"-u", server.URL, | ||
"databases", | ||
"replica", | ||
"list", | ||
"1111", | ||
) | ||
|
||
output, err := cmd.CombinedOutput() | ||
expect.NoError(err, fmt.Sprintf("received error output: %s", output)) | ||
}) | ||
|
||
}) | ||
}) | ||
|
||
const replicaMetadata = `{"name":"read-nyc3-01","connection":{"uri":"","database":"defaultdb","host":"read-nyc3-01-do-user-19081923-0.db.ondigitalocean.com","port":25060,"user":"doadmin","password":"","ssl":true},"private_connection":{"uri":"","database":"","host":"private-read-nyc3-01-do-user-19081923-0.db.ondigitalocean.com","port":25060,"user":"doadmin","password":"","ssl":true},"region":"nyc3","status":"online","created_at":"2019-01-11T18:37:36Z"}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters