Skip to content

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

feat(test): restruct test framework library to a controller class (#479)

Danny Xu375492ffb9e7
diff --git a/bin/server/cmd/profile_dev.go b/bin/server/cmd/profile_dev.go
index ab7dae977f..4b4322dd85 100644
--- a/bin/server/cmd/profile_dev.go
+++ b/bin/server/cmd/profile_dev.go
@@ -8,12 +8,12 @@ import (
"time"
)
-func activeProfile(dataDir string, port int, isDemo bool) profile {
+func activeProfile(dataDir string, port int, isDemo bool) Profile {
dsn := fmt.Sprintf("file:%s/bytebase_dev.db", dataDir)
if isDemo {
dsn = fmt.Sprintf("file:%s/bytebase_demo.db", dataDir)
}
- return profile{
+ return Profile{
mode: "dev",
port: port,
dsn: dsn,
@@ -24,8 +24,8 @@ func activeProfile(dataDir string, port int, isDemo bool) profile {
}
// GetTestProfile will return a profile for testing.
-func GetTestProfile(dataDir string) profile {
- return profile{
+func GetTestProfile(dataDir string) Profile {
+ return Profile{
mode: "dev",
port: 1234,
dsn: fmt.Sprintf("file:%s/bytebase_test.db", dataDir),
diff --git a/bin/server/cmd/profile_release.go b/bin/server/cmd/profile_release.go
index c2d275046f..dc27653463 100644
--- a/bin/server/cmd/profile_release.go
+++ b/bin/server/cmd/profile_release.go
@@ -8,7 +8,7 @@ import (
"time"
)
-func activeProfile(dataDir string, port int, isDemo bool) profile {
+func activeProfile(dataDir string, port int, isDemo bool) Profile {
dsn := fmt.Sprintf("file:%s/bytebase.db", dataDir)
seedDir := "seed/release"
forceResetSeed := false
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go
index 7071c6d859..47d5bf9606 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -130,7 +130,9 @@ func init() {
// -----------------------------------Command Line Config END--------------------------------------
// -----------------------------------Main Entry Point---------------------------------------------
-type profile struct {
+
+// Profile is the configuration to start main server.
+type Profile struct {
// mode can be "release" or "dev"
mode string
// port is the binding port for server.
@@ -151,8 +153,9 @@ type config struct {
secret string
}
+// Main is the main server for Bytebase.
type Main struct {
- profile *profile
+ profile *Profile
l *zap.Logger
@@ -245,7 +248,8 @@ func start() {
<-ctx.Done()
}
-func NewMain(activeProfile profile, logger *zap.Logger) *Main {
+// NewMain creates a main server based on profile.
+func NewMain(activeProfile Profile, logger *zap.Logger) *Main {
fmt.Println("-----Config BEGIN-----")
fmt.Printf("mode=%s\n", activeProfile.mode)
fmt.Printf("server=%s:%d\n", host, activeProfile.port)
@@ -282,6 +286,7 @@ func initSetting(ctx context.Context, settingService api.SettingService) (*confi
return result, nil
}
+// Run will run the main server.
func (m *Main) Run(ctx context.Context) error {
db := store.NewDB(m.l, m.profile.dsn, m.profile.seedDir, m.profile.forceResetSeed, readonly, version)
if err := db.Open(); err != nil {
diff --git a/tests/start_test.go b/tests/start_test.go
index 734cc635a3..e33647f4a2 100644
--- a/tests/start_test.go
+++ b/tests/start_test.go
@@ -2,64 +2,18 @@ package tests
import (
"context"
- "fmt"
- "net/http"
- "strings"
"testing"
- "time"
-
- "github.com/bytebase/bytebase/bin/server/cmd"
)
func TestServiceStart(t *testing.T) {
ctx := context.Background()
- logger, err := cmd.GetLogger()
- if err != nil {
- panic(fmt.Errorf("failed to create logger, %w", err))
- }
- defer logger.Sync()
- profile := cmd.GetTestProfile(t.TempDir())
- m := cmd.NewMain(profile, logger)
-
- errChan := make(chan error, 1)
- go func() {
- if err := m.Run(ctx); err != nil {
- errChan <- fmt.Errorf("m.Run() error %w", err)
- }
- }()
-
- if err := waitForServerStart(m, errChan); err != nil {
- t.Fatal("failed to wait for server to start, %w", err)
+ ctl := &controller{}
+ if err := ctl.StartMain(ctx, t.TempDir()); err != nil {
+ t.Fatal(err)
}
+ defer ctl.Close()
- if err := m.Close(); err != nil {
- t.Fatal("m.Close() error %w", err)
- }
-}
-
-func waitForServerStart(m *cmd.Main, errChan <-chan error) error {
- ticker := time.NewTicker(100 * time.Millisecond)
- defer ticker.Stop()
-
- for {
- select {
- case <-ticker.C:
- if m.GetServer() == nil {
- continue
- }
- e := m.GetServer().GetEcho()
- if e == nil {
- continue
- }
- addr := e.ListenerAddr()
- if addr != nil && strings.Contains(addr.String(), ":") {
- return nil // was started
- }
- case err := <-errChan:
- if err == http.ErrServerClosed {
- return nil
- }
- return err
- }
+ if err := ctl.Login(); err != nil {
+ t.Fatal(err)
}
}
diff --git a/tests/tests.go b/tests/tests.go
new file mode 100644
index 0000000000..5745c99c8b
--- /dev/null
+++ b/tests/tests.go
@@ -0,0 +1,111 @@
+package tests
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+ "strings"
+ "time"
+
+ "github.com/bytebase/bytebase/bin/server/cmd"
+)
+
+var (
+ port = 1234
+ rootURL = fmt.Sprintf("http://localhost:%d/api", port)
+)
+
+type controller struct {
+ main *cmd.Main
+ client *http.Client
+ cookie string
+}
+
+// StartMain starts the main server.
+func (ctl *controller) StartMain(ctx context.Context, dataDir string) error {
+ // start main server.
+ logger, err := cmd.GetLogger()
+ if err != nil {
+ return fmt.Errorf("failed to get logger, error %w", err)
+ }
+ defer logger.Sync()
+ profile := cmd.GetTestProfile(dataDir)
+ ctl.main = cmd.NewMain(profile, logger)
+
+ errChan := make(chan error, 1)
+ go func() {
+ if err := ctl.main.Run(ctx); err != nil {
+ errChan <- fmt.Errorf("failed to run main server, error %w", err)
+ }
+ }()
+
+ if err := waitForServerStart(ctl.main, errChan); err != nil {
+ return fmt.Errorf("failed to wait for server to start, error %w", err)
+ }
+
+ // initialize controller clients.
+ ctl.client = &http.Client{}
+
+ return nil
+}
+
+func waitForServerStart(m *cmd.Main, errChan <-chan error) error {
+ ticker := time.NewTicker(100 * time.Millisecond)
+ defer ticker.Stop()
+
+ for {
+ select {
+ case <-ticker.C:
+ if m.GetServer() == nil {
+ continue
+ }
+ e := m.GetServer().GetEcho()
+ if e == nil {
+ continue
+ }
+ addr := e.ListenerAddr()
+ if addr != nil && strings.Contains(addr.String(), ":") {
+ return nil // was started
+ }
+ case err := <-errChan:
+ if err == http.ErrServerClosed {
+ return nil
+ }
+ return err
+ }
+ }
+}
+
+func (ctl *controller) Close() error {
+ if ctl.main != nil {
+ return ctl.main.Close()
+ }
+ return nil
+}
+
+// Login will login as user [email protected] and caches its cookie.
+func (ctl *controller) Login() error {
+ resp, err := ctl.client.Post(
+ fmt.Sprintf("%s/auth/login/BYTEBASE", rootURL),
+ "",
+ strings.NewReader(`{"data":{"type":"loginInfo","attributes":{"email":"[email protected]","password":"1024"}}}`))
+ if err != nil {
+ return fmt.Errorf("fail to post login request, error %w", err)
+ }
+
+ cookie := ""
+ h := resp.Header.Get("Set-Cookie")
+ parts := strings.Split(h, "; ")
+ for _, p := range parts {
+ if strings.HasPrefix(p, "access-token=") {
+ cookie = p
+ break
+ }
+ }
+ if cookie == "" {
+ return fmt.Errorf("unable to find access token in the login response headers")
+ }
+ ctl.cookie = cookie
+
+ return nil
+}