Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions e2e/add_replica_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//go:build e2e_test

package e2e

import (
"testing"

"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/require"

api "github.com/pgEdge/control-plane/api/apiv1/gen/control_plane"
)

func TestAddReplica(t *testing.T) {
t.Parallel()

host1 := fixture.HostIDs()[0]
host2 := fixture.HostIDs()[1]
host3 := fixture.HostIDs()[2]

username := "admin"
password := "password"

tLog(t, "creating database")

ctx := t.Context()
db := fixture.NewDatabaseFixture(ctx, t, &api.CreateDatabaseRequest{
Spec: &api.DatabaseSpec{
DatabaseName: "add_replica",
DatabaseUsers: []*api.DatabaseUserSpec{
{
Username: username,
Password: pointerTo(password),
DbOwner: pointerTo(true),
Attributes: []string{"LOGIN", "SUPERUSER"},
},
},
Port: pointerTo(0),
PatroniPort: pointerTo(0),
Nodes: []*api.DatabaseNodeSpec{
{Name: "n1", HostIds: []api.Identifier{api.Identifier(host1)}},
{Name: "n2", HostIds: []api.Identifier{api.Identifier(host2)}},
},
},
})

tLog(t, "creating test data")

opts := ConnectionOptions{
Username: username,
Password: password,
}
db.WithConnection(ctx, opts, t, func(conn *pgx.Conn) {
_, err := conn.Exec(ctx, "CREATE TABLE foo (id int primary key, val text)")
require.NoError(t, err)
_, err = conn.Exec(ctx, "INSERT INTO foo (id, val) VALUES (1, 'foo')")
require.NoError(t, err)
})

tLog(t, "adding a replica")

err := db.Update(ctx, UpdateOptions{
Spec: &api.DatabaseSpec{
DatabaseName: "add_replica",
DatabaseUsers: []*api.DatabaseUserSpec{
{
Username: username,
DbOwner: pointerTo(true),
Attributes: []string{"LOGIN", "SUPERUSER"},
},
},
Port: pointerTo(0),
PatroniPort: pointerTo(0),
Nodes: []*api.DatabaseNodeSpec{
{
Name: "n1",
HostIds: []api.Identifier{
api.Identifier(host1),
api.Identifier(host3),
},
},
{Name: "n2", HostIds: []api.Identifier{api.Identifier(host2)}},
},
},
})
require.NoError(t, err)

tLog(t, "validating that replica exists and is populated")

opts = ConnectionOptions{
Username: username,
Password: password,
Matcher: And(WithNode("n1"), WithRole("replica")),
}
db.WithConnection(ctx, opts, t, func(conn *pgx.Conn) {
const isInRecoveryQuery = "SELECT pg_is_in_recovery()"
const valQuery = "SELECT val FROM foo WHERE id = 1"

var isInRecovery bool
require.NoError(t, conn.QueryRow(ctx, isInRecoveryQuery).Scan(&isInRecovery))
require.True(t, isInRecovery)

var val string
require.NoError(t, conn.QueryRow(ctx, valQuery).Scan(&val))
require.Equal(t, "foo", val)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,13 @@
"reason": "does_not_exist",
"diff": null
}
]
],
[
],
[
{
"type": "update",
"resource_id": "database.node::n1",
"reason": "has_diff",
"diff": [
{
"value": "n1-instance-2-id",
"op": "add",
"path": "/instance_ids/-"
}
]
"reason": "dependency_updated",
"diff": null
}
],
[
Expand Down Expand Up @@ -61,21 +53,21 @@
"reason": "dependency_updated",
"diff": null
}
],
]
],
[
[
{
"type": "create",
"resource_id": "database.switchover::n1-instance-1-id",
"resource_id": "monitor.instance::n1-instance-2-id",
"reason": "does_not_exist",
"diff": null
}
]
],
[
],
[
{
"type": "create",
"resource_id": "monitor.instance::n1-instance-2-id",
"resource_id": "database.switchover::n1-instance-1-id",
"reason": "does_not_exist",
"diff": null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
[
[
[
{
"type": "create",
"resource_id": "orchestrator.resource::n1-instance-1-dep-2-id",
"reason": "does_not_exist",
"diff": null
}
],
[
{
"type": "update",
"resource_id": "database.instance::n1-instance-1-id",
"reason": "dependency_updated",
"diff": null
}
],
[
{
"type": "update",
"resource_id": "database.node::n1",
"reason": "dependency_updated",
"diff": null
},
{
"type": "update",
"resource_id": "monitor.instance::n1-instance-1-id",
"reason": "dependency_updated",
"diff": null
}
],
[
{
"type": "update",
"resource_id": "database.postgres_database::n1:test",
"reason": "dependency_updated",
"diff": null
}
],
[
{
"type": "update",
"resource_id": "database.replication_slot::n1:n2:test",
"reason": "dependency_updated",
"diff": null
}
],
[
{
"type": "update",
"resource_id": "database.subscription::n1:n2:test",
"reason": "dependency_updated",
"diff": null
},
{
"type": "update",
"resource_id": "database.subscription::n2:n1:test",
"reason": "dependency_updated",
"diff": null
}
]
],
[
[
{
"type": "create",
"resource_id": "orchestrator.resource::n1-instance-2-dep-1-id",
"reason": "does_not_exist",
"diff": null
}
],
[
{
"type": "create",
"resource_id": "database.instance::n1-instance-2-id",
"reason": "does_not_exist",
"diff": null
}
],
[
{
"type": "update",
"resource_id": "database.node::n1",
"reason": "dependency_updated",
"diff": null
}
],
[
{
"type": "update",
"resource_id": "database.postgres_database::n1:test",
"reason": "dependency_updated",
"diff": null
}
],
[
{
"type": "update",
"resource_id": "database.replication_slot::n1:n2:test",
"reason": "dependency_updated",
"diff": null
}
],
[
{
"type": "update",
"resource_id": "database.subscription::n1:n2:test",
"reason": "dependency_updated",
"diff": null
},
{
"type": "update",
"resource_id": "database.subscription::n2:n1:test",
"reason": "dependency_updated",
"diff": null
}
]
],
[
[
{
"type": "create",
"resource_id": "monitor.instance::n1-instance-2-id",
"reason": "does_not_exist",
"diff": null
}
],
[
{
"type": "create",
"resource_id": "database.switchover::n1-instance-1-id",
"reason": "does_not_exist",
"diff": null
}
]
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,19 @@
"reason": "does_not_exist",
"diff": null
}
]
],
[
],
[
{
"type": "update",
"resource_id": "database.node::n1",
"reason": "has_diff",
"diff": [
{
"value": "n1-instance-2-id",
"op": "add",
"path": "/instance_ids/-"
}
]
"reason": "dependency_updated",
"diff": null
},
{
"type": "update",
"resource_id": "database.node::n2",
"reason": "has_diff",
"diff": [
{
"value": "n2-instance-2-id",
"op": "add",
"path": "/instance_ids/-"
}
]
"reason": "dependency_updated",
"diff": null
}
],
[
Expand Down Expand Up @@ -97,33 +83,33 @@
"reason": "dependency_updated",
"diff": null
}
],
]
],
[
[
{
"type": "create",
"resource_id": "database.switchover::n1-instance-1-id",
"resource_id": "monitor.instance::n1-instance-2-id",
"reason": "does_not_exist",
"diff": null
},
{
"type": "create",
"resource_id": "database.switchover::n2-instance-1-id",
"resource_id": "monitor.instance::n2-instance-2-id",
"reason": "does_not_exist",
"diff": null
}
]
],
[
],
[
{
"type": "create",
"resource_id": "monitor.instance::n1-instance-2-id",
"resource_id": "database.switchover::n1-instance-1-id",
"reason": "does_not_exist",
"diff": null
},
{
"type": "create",
"resource_id": "monitor.instance::n2-instance-2-id",
"resource_id": "database.switchover::n2-instance-1-id",
"reason": "does_not_exist",
"diff": null
}
Expand Down
Loading