ConnectLocalServer spins up a local server from the config and returns a client connected to it
(t *testing.T, clientConfig *LocalClientConfig)
| 133 | |
| 134 | // ConnectLocalServer spins up a local server from the config and returns a client connected to it |
| 135 | func ConnectLocalServer(t *testing.T, clientConfig *LocalClientConfig) *DevlakeClient { |
| 136 | t.Helper() |
| 137 | fmt.Printf("Using test temp directory: %s\n", throwawayDir) |
| 138 | logger := logruslog.Global.Nested("test") |
| 139 | cfg := config.GetConfig() |
| 140 | cfg.Set("DB_URL", clientConfig.DbURL) |
| 141 | db, err := runner.NewGormDb(cfg, logger) |
| 142 | require.NoError(t, err) |
| 143 | t.Cleanup(func() { |
| 144 | d, err := db.DB() |
| 145 | require.NoError(t, err) |
| 146 | require.NoError(t, d.Close()) |
| 147 | }) |
| 148 | addr := fmt.Sprintf("http://localhost:%d", clientConfig.ServerPort) |
| 149 | d := &DevlakeClient{ |
| 150 | Endpoint: addr, |
| 151 | db: db, |
| 152 | log: logger, |
| 153 | cfg: cfg, |
| 154 | basicRes: contextimpl.NewDefaultBasicRes(cfg, logger, dalgorm.NewDalgorm(db)), |
| 155 | testCtx: t, |
| 156 | timeout: clientConfig.Timeout, |
| 157 | pipelineTimeout: clientConfig.PipelineTimeout, |
| 158 | } |
| 159 | if d.timeout == 0 { |
| 160 | d.timeout = 10 * time.Second |
| 161 | } |
| 162 | if d.pipelineTimeout == 0 { |
| 163 | d.pipelineTimeout = 30 * time.Second |
| 164 | } |
| 165 | if clientConfig.CreateServer { |
| 166 | d.configureEncryption() |
| 167 | d.initPlugins(clientConfig) |
| 168 | if clientConfig.DropDb || clientConfig.TruncateDb { |
| 169 | d.prepareDB(clientConfig) |
| 170 | } |
| 171 | cfg.Set("PORT", clientConfig.ServerPort) |
| 172 | cfg.Set("PLUGIN_DIR", throwawayDir) |
| 173 | cfg.Set("LOGGING_DIR", throwawayDir) |
| 174 | go func() { |
| 175 | initService.Do(func() { |
| 176 | api.CreateAndRunApiServer() |
| 177 | }) |
| 178 | }() |
| 179 | req, err2 := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/proceed-db-migration", addr), nil) |
| 180 | require.NoError(t, err2) |
| 181 | d.forceSendHttpRequest(100, req, func(err errors.Error) bool { |
| 182 | e := err.Unwrap() |
| 183 | return goerror.Is(e, syscall.ECONNREFUSED) |
| 184 | }) |
| 185 | logger.Info("New DevLake server initialized") |
| 186 | } |
| 187 | return d |
| 188 | } |
| 189 | |
| 190 | // SetTimeout override the timeout of api requests |
| 191 | func (d *DevlakeClient) SetTimeout(timeout time.Duration) { |
no test coverage detected