Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: add `DataSource` related tests (#2962)

boojack02c90cff9825
diff --git a/tests/data_source_test.go b/tests/data_source_test.go
new file mode 100644
index 0000000000..ec39460c2b
--- /dev/null
+++ b/tests/data_source_test.go
@@ -0,0 +1,118 @@
+package tests
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ "go.uber.org/zap/zapcore"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common/log"
+ "github.com/bytebase/bytebase/plugin/db"
+ "github.com/bytebase/bytebase/tests/fake"
+)
+
+func TestDataSource(t *testing.T) {
+ a := require.New(t)
+ log.SetLevel(zapcore.DebugLevel)
+ ctx := context.Background()
+ serverPort := getTestPort(t.Name())
+ ctl := &controller{}
+ dataDir := t.TempDir()
+ err := ctl.StartServer(ctx, dataDir, fake.NewGitLab, serverPort)
+ a.NoError(err)
+ defer ctl.Close(ctx)
+ err = ctl.Login()
+ a.NoError(err)
+ err = ctl.setLicense()
+ a.NoError(err)
+
+ instanceRootDir := t.TempDir()
+ instanceName := "testInstance1"
+ instanceDir, err := ctl.provisionSQLiteInstance(instanceRootDir, instanceName)
+ a.NoError(err)
+
+ environments, err := ctl.getEnvironments()
+ a.NoError(err)
+ prodEnvironment, err := findEnvironment(environments, "Prod")
+ a.NoError(err)
+
+ instance, err := ctl.addInstance(api.InstanceCreate{
+ EnvironmentID: prodEnvironment.ID,
+ Name: "test",
+ Engine: db.SQLite,
+ Host: instanceDir,
+ })
+ a.NoError(err)
+ a.Equal(1, len(instance.DataSourceList))
+
+ adminDataSource := instance.DataSourceList[0]
+ a.Equal(api.AdminDataSourceName, adminDataSource.Name)
+ a.Equal(api.Admin, adminDataSource.Type)
+
+ instanceID := instance.ID
+
+ // create read-only data source.
+ err = ctl.createDataSource(api.DataSourceCreate{
+ InstanceID: instanceID,
+ DatabaseID: adminDataSource.DatabaseID,
+ Name: api.ReadOnlyDataSourceName,
+ Type: api.RO,
+ Username: "ro_ds",
+ Password: "",
+ SslCa: "",
+ SslCert: "",
+ SslKey: "",
+ })
+ a.NoError(err)
+
+ instance, err = ctl.getInstanceByID(instanceID)
+ a.NoError(err)
+ a.NotEqual(nil, instance)
+ a.Equal(2, len(instance.DataSourceList))
+
+ readOnlyDataSource := instance.DataSourceList[1]
+ a.Equal(api.ReadOnlyDataSourceName, readOnlyDataSource.Name)
+ a.Equal(api.RO, readOnlyDataSource.Type)
+
+ dataSourceNewName := "updated_ro_ds"
+ dataSourceNewPassword := "bytebase"
+ // create read-only data source.
+ err = ctl.patchDataSource(adminDataSource.DatabaseID, api.DataSourcePatch{
+ ID: readOnlyDataSource.ID,
+ Username: &dataSourceNewName,
+ Password: &dataSourceNewPassword,
+ })
+ a.NoError(err)
+
+ dataSourceNewHostOverride := "127.0.0.1"
+ dataSourceNewPortOverride := "8000"
+
+ // update read-only data source read replica fields without enterprise license.
+ err = ctl.removeLicense()
+ a.NoError(err)
+ err = ctl.patchDataSource(adminDataSource.DatabaseID, api.DataSourcePatch{
+ ID: readOnlyDataSource.ID,
+ HostOverride: &dataSourceNewHostOverride,
+ PortOverride: &dataSourceNewPortOverride,
+ })
+ a.Equal(err.Error(), "http response error code 403 body \"{\\\"message\\\":\\\"Read replica connection is a ENTERPRISE feature, please upgrade to access it.\\\"}\\n\"")
+
+ err = ctl.setLicense()
+ a.NoError(err)
+ err = ctl.patchDataSource(adminDataSource.DatabaseID, api.DataSourcePatch{
+ ID: readOnlyDataSource.ID,
+ HostOverride: &dataSourceNewHostOverride,
+ PortOverride: &dataSourceNewPortOverride,
+ })
+ a.NoError(err)
+
+ err = ctl.deleteDataSource(adminDataSource.DatabaseID, readOnlyDataSource.ID)
+ a.NoError(err)
+
+ instance, err = ctl.getInstanceByID(instanceID)
+ a.NoError(err)
+ a.NotEqual(nil, instance)
+ a.Equal(1, len(instance.DataSourceList))
+}
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 99fd256d09..142c4e613b 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -264,19 +264,6 @@ func TestSchemaAndDataUpdate(t *testing.T) {
DatabaseID: &database.ID,
})
a.NoError(err)
-
- // Test if POST /api/database/:id/data-source api is working right.
- // TODO(steven): I will add read-only data source testing to a separate test later.
- err = ctl.createDataSource(api.DataSourceCreate{
- InstanceID: instance.ID,
- DatabaseID: database.ID,
- CreatorID: project.Creator.ID,
- Name: "ADMIN data source",
- Type: "ADMIN",
- Username: "root",
- Password: "",
- })
- a.NoError(err)
}
func TestVCS(t *testing.T) {
diff --git a/tests/tests.go b/tests/tests.go
index 976acbbd68..1a1a7c7bc0 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -160,6 +160,7 @@ func getTestPort(testName string) int {
"TestSQLReviewForPostgreSQL",
"TestArchiveProject",
+ "TestDataSource",
}
port := 1234
for _, name := range tests {
@@ -618,6 +619,16 @@ func (ctl *controller) setLicense() error {
return nil
}
+func (ctl *controller) removeLicense() error {
+ err := ctl.switchPlan(&enterpriseAPI.SubscriptionPatch{
+ License: "",
+ })
+ if err != nil {
+ return errors.Wrap(err, "failed to switch plan")
+ }
+ return nil
+}
+
func (ctl *controller) switchPlan(patch *enterpriseAPI.SubscriptionPatch) error {
buf := new(bytes.Buffer)
if err := jsonapi.MarshalPayload(buf, patch); err != nil {
@@ -632,6 +643,19 @@ func (ctl *controller) switchPlan(patch *enterpriseAPI.SubscriptionPatch) error
return nil
}
+func (ctl *controller) getInstanceByID(instanceID int) (*api.Instance, error) {
+ body, err := ctl.get(fmt.Sprintf("/instance/%d", instanceID), nil)
+ if err != nil {
+ return nil, err
+ }
+
+ instance := new(api.Instance)
+ if err = jsonapi.UnmarshalPayload(body, instance); err != nil {
+ return nil, errors.Wrap(err, "fail to unmarshal get instance response")
+ }
+ return instance, nil
+}
+
// addInstance adds an instance.
func (ctl *controller) addInstance(instanceCreate api.InstanceCreate) (*api.Instance, error) {
buf := new(bytes.Buffer)
@@ -1434,6 +1458,32 @@ func (ctl *controller) createDataSource(dataSourceCreate api.DataSourceCreate) e
return nil
}
+func (ctl *controller) patchDataSource(databaseID int, dataSourcePatch api.DataSourcePatch) error {
+ buf := new(bytes.Buffer)
+ if err := jsonapi.MarshalPayload(buf, &dataSourcePatch); err != nil {
+ return errors.Wrap(err, "failed to marshal dataSourcePatch")
+ }
+
+ body, err := ctl.patch(fmt.Sprintf("/database/%d/data-source/%d", databaseID, dataSourcePatch.ID), buf)
+ if err != nil {
+ return err
+ }
+
+ dataSource := new(api.DataSource)
+ if err = jsonapi.UnmarshalPayload(body, dataSource); err != nil {
+ return errors.Wrap(err, "fail to unmarshal dataSource response")
+ }
+ return nil
+}
+
+func (ctl *controller) deleteDataSource(databaseID, dataSourceID int) error {
+ _, err := ctl.delete(fmt.Sprintf("/database/%d/data-source/%d", databaseID, dataSourceID), nil)
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
// upsertPolicy upserts the policy.
func (ctl *controller) upsertPolicy(policyUpsert api.PolicyUpsert) error {
buf := new(bytes.Buffer)