Repository navigation
Expand file tree
/
Copy pathapi_test.go
More file actions
103 lines (93 loc) · 3.46 KB
/
Copy pathapi_test.go
File metadata and controls
103 lines (93 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package api
import (
"context"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/gorilla/mux"
"github.com/rockbears/log"
"github.com/stretchr/testify/require"
"github.com/ovh/cds/engine/api/authentication/builtin"
"github.com/ovh/cds/engine/api/authentication/local"
authdrivertest "github.com/ovh/cds/engine/api/authentication/test"
"github.com/ovh/cds/engine/api/bootstrap"
apiTest "github.com/ovh/cds/engine/api/test"
"github.com/ovh/cds/engine/api/workflow"
"github.com/ovh/cds/engine/cache"
"github.com/ovh/cds/engine/service"
"github.com/ovh/cds/engine/test"
"github.com/ovh/cds/sdk"
)
func newTestAPI(t *testing.T, bootstrapFunc ...test.Bootstrapf) (*API, *test.FakeTransaction, *Router) {
log.Factory = log.NewTestingWrapper(t)
bootstrapFunc = append(bootstrapFunc, bootstrap.InitiliazeDB)
db, factory, store := apiTest.SetupPGWithFactory(t, bootstrapFunc...)
router := newRouter(mux.NewRouter(), "/"+test.GetTestName(t))
var cancel context.CancelFunc
router.Background, cancel = context.WithCancel(context.Background())
api := &API{
StartupTime: time.Now(),
Router: router,
DBConnectionFactory: factory,
Config: Configuration{},
Cache: store,
}
api.AuthenticationDrivers = make(map[sdk.AuthConsumerType]sdk.AuthDriver)
api.AuthenticationDrivers[sdk.ConsumerLocal] = local.NewDriver(context.TODO(), false, "http://localhost:8080", "")
api.AuthenticationDrivers[sdk.ConsumerBuiltin] = builtin.NewDriver()
api.AuthenticationDrivers[sdk.ConsumerTest] = authdrivertest.NewDriver(t)
api.AuthenticationDrivers[sdk.ConsumerTest2] = authdrivertest.NewDriver(t)
api.GoRoutines = sdk.NewGoRoutines(context.TODO())
api.InitRouter()
ctx, cancel := context.WithCancel(context.Background())
go workflow.Initialize(ctx, api.mustDB, api.Cache, "", "", "", 300000)
t.Cleanup(func() {
cancel()
// Clean all the pending crafting workflow runs
lockKey := cache.Key("api:workflowRunCraft")
require.NoError(t, store.DeleteAll(lockKey))
ids, _ := workflow.LoadCratingWorkflowRunIDs(api.mustDB())
for _, id := range ids {
require.NoError(t, workflow.UpdateCraftedWorkflowRun(api.mustDB(), id))
}
cancel()
})
return api, db, router
}
func newRouter(m *mux.Router, p string) *Router {
r := &Router{
Mux: m,
Prefix: p,
URL: "",
mapRouterConfigs: map[string]*service.RouterConfig{},
Background: context.Background(),
}
return r
}
func newTestServer(t *testing.T, bootstrapFunc ...test.Bootstrapf) (*API, *test.FakeTransaction, string) {
bootstrapFunc = append(bootstrapFunc, bootstrap.InitiliazeDB)
db, factory, cache := apiTest.SetupPGWithFactory(t, bootstrapFunc...)
router := newRouter(mux.NewRouter(), "")
var cancel context.CancelFunc
router.Background, cancel = context.WithCancel(context.Background())
api := &API{
StartupTime: time.Now(),
Router: router,
DBConnectionFactory: factory,
Config: Configuration{},
Cache: cache,
}
api.AuthenticationDrivers = make(map[sdk.AuthConsumerType]sdk.AuthDriver)
api.AuthenticationDrivers[sdk.ConsumerLocal] = local.NewDriver(context.TODO(), false, "http://localhost:8080", "")
api.AuthenticationDrivers[sdk.ConsumerBuiltin] = builtin.NewDriver()
api.GoRoutines = sdk.NewGoRoutines(context.TODO())
api.InitRouter()
ts := httptest.NewServer(router.Mux)
url, _ := url.Parse(ts.URL)
t.Cleanup(func() {
cancel()
ts.Close()
})
return api, db, url.String()
}