Best Venom code snippet using venom.randomString
workflow_queue_test.go
Source:workflow_queue_test.go
1package api2import (3 "bytes"4 "context"5 "encoding/base64"6 "encoding/json"7 "fmt"8 "io/ioutil"9 "net/http"10 "net/http/httptest"11 "net/url"12 "os"13 "path"14 "testing"15 "time"16 "github.com/ovh/cds/engine/cdn"17 "github.com/ovh/venom"18 "github.com/sguiheux/go-coverage"19 "github.com/stretchr/testify/assert"20 "github.com/stretchr/testify/require"21 "github.com/ovh/cds/engine/api/application"22 "github.com/ovh/cds/engine/api/authentication"23 "github.com/ovh/cds/engine/api/environment"24 "github.com/ovh/cds/engine/api/group"25 "github.com/ovh/cds/engine/api/keys"26 "github.com/ovh/cds/engine/api/objectstore"27 "github.com/ovh/cds/engine/api/pipeline"28 "github.com/ovh/cds/engine/api/project"29 "github.com/ovh/cds/engine/api/purge"30 "github.com/ovh/cds/engine/api/repositoriesmanager"31 "github.com/ovh/cds/engine/api/services"32 "github.com/ovh/cds/engine/api/test"33 "github.com/ovh/cds/engine/api/test/assets"34 "github.com/ovh/cds/engine/api/worker"35 "github.com/ovh/cds/engine/api/workflow"36 "github.com/ovh/cds/sdk"37 "github.com/ovh/cds/sdk/log"38)39type testRunWorkflowCtx struct {40 user *sdk.AuthentifiedUser41 password string42 project *sdk.Project43 workflow *sdk.Workflow44 run *sdk.WorkflowRun45 job *sdk.WorkflowNodeJobRun46 worker *sdk.Worker47 workerToken string48 hatchery *sdk.Service49 hatcheryToken string50 model *sdk.Model51}52func testRunWorkflow(t *testing.T, api *API, router *Router) testRunWorkflowCtx {53 u, pass := assets.InsertLambdaUser(t, api.mustDB())54 key := "proj-" + sdk.RandomString(10)55 proj := assets.InsertTestProject(t, api.mustDB(), api.Cache, key, key)56 require.NoError(t, group.InsertLinkGroupUser(context.TODO(), api.mustDB(), &group.LinkGroupUser{57 GroupID: proj.ProjectGroups[0].Group.ID,58 AuthentifiedUserID: u.ID,59 Admin: true,60 }))61 u.Groups = append(u.Groups, proj.ProjectGroups[0].Group)62 vcsServer := sdk.ProjectVCSServerLink{63 ProjectID: proj.ID,64 Name: "github",65 }66 vcsServer.Set("token", "foo")67 vcsServer.Set("secret", "bar")68 assert.NoError(t, repositoriesmanager.InsertProjectVCSServerLink(context.TODO(), api.mustDB(), &vcsServer))69 //First pipeline70 pip := sdk.Pipeline{71 ProjectID: proj.ID,72 ProjectKey: proj.Key,73 Name: "pip-" + sdk.RandomString(10),74 }75 require.NoError(t, pipeline.InsertPipeline(api.mustDB(), &pip))76 script := assets.GetBuiltinOrPluginActionByName(t, api.mustDB(), sdk.ScriptAction)77 s := sdk.NewStage("stage-" + sdk.RandomString(10))78 s.Enabled = true79 s.PipelineID = pip.ID80 pipeline.InsertStage(api.mustDB(), s)81 j := &sdk.Job{82 Enabled: true,83 Action: sdk.Action{84 Enabled: true,85 Actions: []sdk.Action{86 assets.NewAction(script.ID, sdk.Parameter{Name: "script", Value: "echo lol"}),87 },88 },89 }90 pipeline.InsertJob(api.mustDB(), j, s.ID, &pip)91 s.Jobs = append(s.Jobs, *j)92 pip.Stages = append(pip.Stages, *s)93 // Insert Application94 app := &sdk.Application{95 Name: "app-" + sdk.RandomString(10),96 }97 if err := application.Insert(api.mustDB(), *proj, app); err != nil {98 t.Fatal(err)99 }100 k := &sdk.ApplicationKey{101 Name: "my-app-key",102 Type: "pgp",103 ApplicationID: app.ID,104 }105 pgpK, err := keys.GeneratePGPKeyPair(k.Name)106 if err != nil {107 t.Fatal(err)108 }109 k.Public = pgpK.Public110 k.Private = pgpK.Private111 k.KeyID = pgpK.KeyID112 if err := application.InsertKey(api.mustDB(), k); err != nil {113 t.Fatal(err)114 }115 //Insert Application116 env := &sdk.Environment{117 Name: "env-" + sdk.RandomString(10),118 ProjectID: proj.ID,119 }120 if err := environment.InsertEnvironment(api.mustDB(), env); err != nil {121 t.Fatal(err)122 }123 envk := &sdk.EnvironmentKey{124 Name: "my-env-key",125 Type: "pgp",126 EnvironmentID: env.ID,127 }128 kpgp, err := keys.GeneratePGPKeyPair(envk.Name)129 if err != nil {130 t.Fatal(err)131 }132 envk.Public = kpgp.Public133 envk.Private = kpgp.Private134 envk.KeyID = kpgp.KeyID135 if err := environment.InsertKey(api.mustDB(), envk); err != nil {136 t.Fatal(err)137 }138 w := sdk.Workflow{139 Name: "wkf-" + sdk.RandomString(10),140 ProjectID: proj.ID,141 ProjectKey: proj.Key,142 WorkflowData: sdk.WorkflowData{143 Node: sdk.Node{144 Name: "node-1",145 Ref: "node-1",146 Type: sdk.NodeTypePipeline,147 Context: &sdk.NodeContext{148 PipelineID: pip.ID,149 ApplicationID: app.ID,150 EnvironmentID: env.ID,151 },152 },153 },154 }155 proj2, errP := project.Load(context.TODO(), api.mustDB(), proj.Key, project.LoadOptions.WithPipelines, project.LoadOptions.WithGroups)156 require.NoError(t, errP)157 require.NoError(t, workflow.Insert(context.TODO(), api.mustDB(), api.Cache, *proj2, &w))158 w1, err := workflow.Load(context.TODO(), api.mustDB(), api.Cache, *proj, w.Name, workflow.LoadOptions{})159 require.NoError(t, err)160 log.Debug("workflow %d groups: %+v", w1.ID, w1.Groups)161 //Prepare request162 vars := map[string]string{163 "key": proj.Key,164 "permWorkflowName": w1.Name,165 }166 uri := router.GetRoute("POST", api.postWorkflowRunHandler, vars)167 test.NotEmpty(t, uri)168 opts := &sdk.WorkflowRunPostHandlerOption{}169 req := assets.NewAuthentifiedRequest(t, u, pass, "POST", uri, opts)170 //Do the request171 rec := httptest.NewRecorder()172 router.Mux.ServeHTTP(rec, req)173 require.Equal(t, 202, rec.Code)174 wr := &sdk.WorkflowRun{}175 require.NoError(t, json.Unmarshal(rec.Body.Bytes(), wr))176 require.Equal(t, int64(1), wr.Number)177 if t.Failed() {178 t.FailNow()179 }180 // Wait building status181 cpt := 0182 for {183 varsGet := map[string]string{184 "key": proj.Key,185 "permWorkflowName": w1.Name,186 "number": fmt.Sprintf("%d", wr.Number),187 }188 uriGet := router.GetRoute("GET", api.getWorkflowRunHandler, varsGet)189 test.NotEmpty(t, uriGet)190 reqGet := assets.NewAuthentifiedRequest(t, u, pass, "GET", uriGet, nil)191 //Do the request192 recGet := httptest.NewRecorder()193 router.Mux.ServeHTTP(recGet, reqGet)194 require.Equal(t, 200, recGet.Code)195 wrGet := &sdk.WorkflowRun{}196 require.NoError(t, json.Unmarshal(recGet.Body.Bytes(), wrGet))197 if wrGet.Status != sdk.StatusPending {198 wr = wrGet199 break200 }201 cpt++202 if cpt == 20 {203 t.Errorf("Workflow still in checking status: %s", wrGet.Status)204 t.FailNow()205 }206 time.Sleep(500 * time.Millisecond)207 }208 return testRunWorkflowCtx{209 user: u,210 password: pass,211 project: proj,212 workflow: w1,213 run: wr,214 }215}216func testCountGetWorkflowJob(t *testing.T, api *API, router *Router, ctx *testRunWorkflowCtx) {217 uri := router.GetRoute("GET", api.countWorkflowJobQueueHandler, nil)218 test.NotEmpty(t, uri)219 req := assets.NewAuthentifiedRequest(t, ctx.user, ctx.password, "GET", uri, nil)220 rec := httptest.NewRecorder()221 router.Mux.ServeHTTP(rec, req)222 require.Equal(t, 200, rec.Code)223 count := sdk.WorkflowNodeJobRunCount{}224 require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &count))225 assert.True(t, count.Count > 0)226 if t.Failed() {227 t.FailNow()228 }229}230func testGetWorkflowJobAsRegularUser(t *testing.T, api *API, router *Router, jwt string, ctx *testRunWorkflowCtx) {231 uri := router.GetRoute("GET", api.getWorkflowJobQueueHandler, nil)232 test.NotEmpty(t, uri)233 req := assets.NewJWTAuthentifiedRequest(t, jwt, "GET", uri, nil)234 rec := httptest.NewRecorder()235 router.Mux.ServeHTTP(rec, req)236 require.Equal(t, 200, rec.Code)237 jobs := []sdk.WorkflowNodeJobRun{}238 require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &jobs))239 require.True(t, len(jobs) >= 1)240 if t.Failed() {241 t.FailNow()242 }243 ctx.job = &jobs[len(jobs)-1]244}245func testGetWorkflowJobAsWorker(t *testing.T, api *API, router *Router, ctx *testRunWorkflowCtx) {246 testRegisterWorker(t, api, router, ctx)247 uri := router.GetRoute("GET", api.getWorkflowJobQueueHandler, nil)248 test.NotEmpty(t, uri)249 req := assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "GET", uri, nil)250 rec := httptest.NewRecorder()251 router.Mux.ServeHTTP(rec, req)252 require.Equal(t, 200, rec.Code)253 jobs := []sdk.WorkflowNodeJobRun{}254 require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &jobs))255 require.Len(t, jobs, 1)256 if t.Failed() {257 t.FailNow()258 }259 ctx.job = &jobs[0]260}261func testGetWorkflowJobAsHatchery(t *testing.T, api *API, router *Router, ctx *testRunWorkflowCtx) {262 uri := router.GetRoute("GET", api.getWorkflowJobQueueHandler, nil)263 test.NotEmpty(t, uri)264 //Register the worker265 testRegisterHatchery(t, api, router, ctx)266 req := assets.NewJWTAuthentifiedRequest(t, ctx.hatcheryToken, "GET", uri, nil)267 rec := httptest.NewRecorder()268 router.Mux.ServeHTTP(rec, req)269 require.Equal(t, 200, rec.Code)270 jobs := []sdk.WorkflowNodeJobRun{}271 require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &jobs))272 require.Len(t, jobs, 1)273 if t.Failed() {274 t.FailNow()275 }276 ctx.job = &jobs[0]277}278func testRegisterWorker(t *testing.T, api *API, router *Router, ctx *testRunWorkflowCtx) {279 g, err := group.LoadByID(context.TODO(), api.mustDB(), ctx.user.Groups[0].ID)280 if err != nil {281 t.Fatalf("Error getting group: %+v", err)282 }283 model := LoadOrCreateWorkerModel(t, api, g.ID, "Test1")284 var jobID int64285 if ctx.job != nil {286 jobID = ctx.job.ID287 }288 w, workerJWT := RegisterWorker(t, api, g.ID, model.Name, jobID, jobID == 0)289 ctx.workerToken = workerJWT290 ctx.worker = w291 ctx.model = model292}293func testRegisterHatchery(t *testing.T, api *API, router *Router, ctx *testRunWorkflowCtx) {294 h, _, _, jwt := assets.InsertHatchery(t, api.mustDB(), ctx.user.Groups[0])295 ctx.hatchery = h296 ctx.hatcheryToken = jwt297}298func TestGetWorkflowJobQueueHandler(t *testing.T) {299 api, db, router := newTestAPI(t)300 // delete all existing workers301 workers, err := worker.LoadAll(context.TODO(), db)302 test.NoError(t, err)303 for _, w := range workers {304 worker.Delete(db, w.ID)305 }306 // remove all jobs in queue307 filterClean := workflow.NewQueueFilter()308 nrj, _ := workflow.LoadNodeJobRunQueue(context.TODO(), db, api.Cache, filterClean)309 for _, j := range nrj {310 _ = workflow.DeleteNodeJobRuns(db, j.WorkflowNodeRunID)311 }312 _, jwt := assets.InsertAdminUser(t, api.mustDB())313 t.Log("checkin as a user")314 ctx := testRunWorkflow(t, api, router)315 testGetWorkflowJobAsRegularUser(t, api, router, jwt, &ctx)316 assert.NotNil(t, ctx.job)317 t.Logf("checkin as a worker jobId:%d", ctx.job.ID)318 testGetWorkflowJobAsWorker(t, api, router, &ctx)319 assert.NotNil(t, ctx.job)320 // count job in queue321 testCountGetWorkflowJob(t, api, router, &ctx)322 // Get workflow run number323 //Prepare request324 vars := map[string]string{325 "key": ctx.project.Key,326 "permWorkflowName": ctx.workflow.Name,327 }328 uri := router.GetRoute("GET", api.getWorkflowRunNumHandler, vars)329 test.NotEmpty(t, uri)330 req := assets.NewAuthentifiedRequest(t, ctx.user, ctx.password, "GET", uri, nil)331 rec := httptest.NewRecorder()332 router.Mux.ServeHTTP(rec, req)333 require.Equal(t, 200, rec.Code)334 var n struct {335 Num int `json:"num"`336 }337 require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &n))338 require.Equal(t, 1, n.Num)339 // Update workflow run number340 //Prepare request341 uri = router.GetRoute("POST", api.postWorkflowRunNumHandler, vars)342 test.NotEmpty(t, uri)343 n.Num = 10344 req = assets.NewAuthentifiedRequest(t, ctx.user, ctx.password, "POST", uri, n)345 rec = httptest.NewRecorder()346 router.Mux.ServeHTTP(rec, req)347 require.Equal(t, 200, rec.Code)348 uri = router.GetRoute("GET", api.getWorkflowRunNumHandler, vars)349 test.NotEmpty(t, uri)350 req = assets.NewJWTAuthentifiedRequest(t, ctx.password, "GET", uri, nil)351 rec = httptest.NewRecorder()352 router.Mux.ServeHTTP(rec, req)353 require.Equal(t, 200, rec.Code)354 require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &n))355 require.Equal(t, 10, n.Num)356}357func Test_postTakeWorkflowJobHandler(t *testing.T) {358 api, _, router := newTestAPI(t)359 ctx := testRunWorkflow(t, api, router)360 testGetWorkflowJobAsWorker(t, api, router, &ctx)361 require.NotNil(t, ctx.job)362 //Prepare request363 vars := map[string]string{364 "key": ctx.project.Key,365 "permWorkflowName": ctx.workflow.Name,366 "id": fmt.Sprintf("%d", ctx.job.ID),367 }368 //Register the worker369 testRegisterWorker(t, api, router, &ctx)370 // Add cdn config371 api.Config.CDN = cdn.Configuration{372 PublicTCP: "cdn.net:4545",373 TCP: sdk.TCPServer{374 Port: 8090,375 Addr: "localhost",376 },377 }378 uri := router.GetRoute("POST", api.postTakeWorkflowJobHandler, vars)379 require.NotEmpty(t, uri)380 //This will check the needWorker() auth381 req := assets.NewJWTAuthentifiedRequest(t, ctx.password, "POST", uri, nil)382 rec := httptest.NewRecorder()383 router.Mux.ServeHTTP(rec, req)384 require.Equal(t, 403, rec.Code)385 //This call must work386 req = assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, nil)387 rec = httptest.NewRecorder()388 router.Mux.ServeHTTP(rec, req)389 require.Equal(t, 200, rec.Code)390 pbji := &sdk.WorkflowNodeJobRunData{}391 require.NoError(t, json.Unmarshal(rec.Body.Bytes(), pbji))392 require.Len(t, pbji.Secrets, 3)393 for _, s := range pbji.Secrets {394 switch s.Name {395 case "cds.key.my-app-key.priv":396 assert.NotEmpty(t, s.Value)397 assert.Equal(t, "pgp", s.Type)398 case "cds.key.my-env-key.priv":399 assert.NotEmpty(t, s.Value)400 assert.Equal(t, "pgp", s.Type)401 case "git.http.password":402 default:403 t.Errorf("unexpected secrets: %s", s.Name)404 }405 }406 assert.Equal(t, "cdn.net:4545", pbji.GelfServiceAddr)407 run, err := workflow.LoadNodeJobRun(context.TODO(), api.mustDB(), api.Cache, ctx.job.ID)408 require.NoError(t, err)409 assert.Equal(t, "Building", run.Status)410 assert.Equal(t, ctx.model.Name, run.Model)411 assert.Equal(t, ctx.worker.Name, run.WorkerName)412 assert.NotEmpty(t, run.HatcheryName)413 wkrDB, err := worker.LoadWorkerByIDWithDecryptKey(context.TODO(), api.mustDB(), ctx.worker.ID)414 assert.NoError(t, err)415 assert.Len(t, wkrDB.PrivateKey, 32)416}417func Test_postTakeWorkflowInvalidJobHandler(t *testing.T) {418 api, _, router := newTestAPI(t)419 ctx := testRunWorkflow(t, api, router)420 testGetWorkflowJobAsWorker(t, api, router, &ctx)421 require.NotNil(t, ctx.job)422 //Prepare request423 vars := map[string]string{424 "key": ctx.project.Key,425 "permWorkflowName": ctx.workflow.Name,426 "id": fmt.Sprintf("%d", ctx.job.ID+1), // invalid job427 }428 //Register the worker429 testRegisterWorker(t, api, router, &ctx)430 uri := router.GetRoute("POST", api.postTakeWorkflowJobHandler, vars)431 require.NotEmpty(t, uri)432 //this call must failed, we try to take a jobID not reserved at worker's registration433 req := assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, nil)434 rec := httptest.NewRecorder()435 router.Mux.ServeHTTP(rec, req)436 require.Equal(t, 403, rec.Code)437 //This must be ok, take the jobID reserved438 vars2 := map[string]string{439 "key": ctx.project.Key,440 "permWorkflowName": ctx.workflow.Name,441 "id": fmt.Sprintf("%d", ctx.job.ID),442 }443 uri2 := router.GetRoute("POST", api.postTakeWorkflowJobHandler, vars2)444 require.NotEmpty(t, uri2)445 req2 := assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri2, nil)446 rec2 := httptest.NewRecorder()447 router.Mux.ServeHTTP(rec2, req2)448 require.Equal(t, 200, rec2.Code)449}450func Test_postBookWorkflowJobHandler(t *testing.T) {451 api, _, router := newTestAPI(t)452 ctx := testRunWorkflow(t, api, router)453 testGetWorkflowJobAsHatchery(t, api, router, &ctx)454 assert.NotNil(t, ctx.job)455 //Register the hatchery456 testRegisterHatchery(t, api, router, &ctx)457 //TakeBook458 uri := router.GetRoute("POST", api.postBookWorkflowJobHandler, map[string]string{459 "permJobID": fmt.Sprintf("%d", ctx.job.ID),460 })461 test.NotEmpty(t, uri)462 req := assets.NewJWTAuthentifiedRequest(t, ctx.hatcheryToken, "POST", uri, nil)463 rec := httptest.NewRecorder()464 router.Mux.ServeHTTP(rec, req)465 require.Equal(t, 200, rec.Code)466}467func Test_postWorkflowJobResultHandler(t *testing.T) {468 api, _, router := newTestAPI(t)469 ctx := testRunWorkflow(t, api, router)470 testGetWorkflowJobAsWorker(t, api, router, &ctx)471 assert.NotNil(t, ctx.job)472 //Register the worker473 testRegisterWorker(t, api, router, &ctx)474 //Take475 uri := router.GetRoute("POST", api.postTakeWorkflowJobHandler, map[string]string{476 "id": fmt.Sprintf("%d", ctx.job.ID),477 })478 test.NotEmpty(t, uri)479 req := assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, nil)480 rec := httptest.NewRecorder()481 router.Mux.ServeHTTP(rec, req)482 require.Equal(t, 200, rec.Code)483 //Send logs484 logs := sdk.Log{485 Val: "This is a log",486 }487 uri = router.Prefix + fmt.Sprintf("/queue/workflows/%d/log", ctx.job.ID)488 req = assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, logs)489 rec = httptest.NewRecorder()490 router.Mux.ServeHTTP(rec, req)491 require.Equal(t, 204, rec.Code)492 //Send result493 res := sdk.Result{494 Duration: "10",495 Status: sdk.StatusSuccess,496 RemoteTime: time.Now(),497 BuildID: ctx.job.ID,498 NewVariables: []sdk.Variable{499 {500 Name: "cds.build.newVar",501 Value: "newVal",502 },503 },504 }505 uri = router.GetRoute("POST", api.postWorkflowJobResultHandler, map[string]string{506 "permJobID": fmt.Sprintf("%d", ctx.job.ID),507 })508 test.NotEmpty(t, uri)509 req = assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, res)510 rec = httptest.NewRecorder()511 router.Mux.ServeHTTP(rec, req)512 require.Equal(t, 204, rec.Code)513 uri = router.GetRoute("GET", api.getWorkflowRunHandler, map[string]string{514 "key": ctx.project.Key,515 "permWorkflowName": ctx.workflow.Name,516 "number": fmt.Sprintf("%d", ctx.run.Number),517 })518 req = assets.NewJWTAuthentifiedRequest(t, ctx.password, "GET", uri+"?withDetails=true", res)519 rec = httptest.NewRecorder()520 router.Mux.ServeHTTP(rec, req)521 require.Equal(t, 200, rec.Code)522 btes := rec.Body.Bytes()523 require.NoError(t, json.Unmarshal(btes, ctx.run))524 assert.Contains(t, ctx.run.RootRun().BuildParameters, sdk.Parameter{Name: "cds.build.newVar", Type: sdk.StringParameter, Value: "newVal"})525 vars := map[string]string{526 "key": ctx.project.Key,527 "permWorkflowName": ctx.workflow.Name,528 "number": fmt.Sprintf("%d", ctx.run.Number),529 "nodeRunID": fmt.Sprintf("%d", ctx.run.RootRun().ID),530 }531 uri = router.GetRoute("GET", api.getWorkflowNodeRunHandler, vars)532 req = assets.NewJWTAuthentifiedRequest(t, ctx.password, "GET", uri, res)533 rec = httptest.NewRecorder()534 router.Mux.ServeHTTP(rec, req)535 require.Equal(t, 200, rec.Code)536 btes = rec.Body.Bytes()537 var rootRun sdk.WorkflowNodeRun538 require.NoError(t, json.Unmarshal(btes, &rootRun))539 assert.Contains(t, rootRun.Stages[0].RunJobs[0].Parameters, sdk.Parameter{Name: "cds.build.newVar", Type: sdk.StringParameter, Value: "newVal"})540 assert.Contains(t, rootRun.BuildParameters, sdk.Parameter{Name: "cds.build.newVar", Type: sdk.StringParameter, Value: "newVal"})541}542func Test_postWorkflowJobTestsResultsHandler(t *testing.T) {543 api, _, router := newTestAPI(t)544 ctx := testRunWorkflow(t, api, router)545 testGetWorkflowJobAsWorker(t, api, router, &ctx)546 assert.NotNil(t, ctx.job)547 // Register the worker548 testRegisterWorker(t, api, router, &ctx)549 // Register the hatchery550 testRegisterHatchery(t, api, router, &ctx)551 // Send spawninfo552 info := []sdk.SpawnInfo{}553 uri := fmt.Sprintf("%s/queue/workflows/%d/spawn/infos", router.Prefix, ctx.job.ID)554 test.NotEmpty(t, uri)555 req := assets.NewJWTAuthentifiedRequest(t, ctx.hatcheryToken, "POST", uri, info)556 rec := httptest.NewRecorder()557 router.Mux.ServeHTTP(rec, req)558 require.Equal(t, 204, rec.Code)559 //spawn560 uri = router.GetRoute("POST", api.postTakeWorkflowJobHandler, map[string]string{561 "key": ctx.project.Key,562 "permWorkflowName": ctx.workflow.Name,563 "id": fmt.Sprintf("%d", ctx.job.ID),564 })565 test.NotEmpty(t, uri)566 req = assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, nil)567 rec = httptest.NewRecorder()568 router.Mux.ServeHTTP(rec, req)569 require.Equal(t, 200, rec.Code)570 //Send test571 tests := venom.Tests{572 Total: 2,573 TotalKO: 1,574 TotalOK: 1,575 TotalSkipped: 0,576 TestSuites: []venom.TestSuite{577 {578 Total: 1,579 Name: "TestSuite1",580 TestCases: []venom.TestCase{581 {582 Name: "TestCase1",583 Status: "OK",584 },585 },586 },587 {588 Total: 1,589 Name: "TestSuite2",590 TestCases: []venom.TestCase{591 {592 Name: "TestCase1",593 Status: "KO",594 Failures: []venom.Failure{595 {596 Value: "Fail",597 Type: "Assertion error",598 Message: "Error occurred",599 },600 },601 },602 },603 },604 },605 }606 uri = router.GetRoute("POST", api.postWorkflowJobTestsResultsHandler, map[string]string{607 "permJobID": fmt.Sprintf("%d", ctx.job.ID),608 })609 test.NotEmpty(t, uri)610 req = assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, tests)611 rec = httptest.NewRecorder()612 router.Mux.ServeHTTP(rec, req)613 require.Equal(t, 204, rec.Code)614 step := sdk.StepStatus{615 Status: sdk.StatusSuccess,616 StepOrder: 0,617 }618 uri = router.GetRoute("POST", api.postWorkflowJobStepStatusHandler, map[string]string{619 "permJobID": fmt.Sprintf("%d", ctx.job.ID),620 })621 test.NotEmpty(t, uri)622 req = assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, step)623 rec = httptest.NewRecorder()624 router.Mux.ServeHTTP(rec, req)625 require.Equal(t, 204, rec.Code)626 wNodeJobRun, errJ := workflow.LoadNodeJobRun(context.TODO(), api.mustDB(), api.Cache, ctx.job.ID)627 require.NoError(t, errJ)628 nodeRun, errN := workflow.LoadNodeRunByID(api.mustDB(), wNodeJobRun.WorkflowNodeRunID, workflow.LoadRunOptions{WithArtifacts: true, WithTests: true})629 require.NoError(t, errN)630 assert.NotNil(t, nodeRun.Tests)631 require.Equal(t, 2, nodeRun.Tests.Total)632}633func Test_postWorkflowJobArtifactHandler(t *testing.T) {634 api, db, router := newTestAPI(t)635 ctx := testRunWorkflow(t, api, router)636 testGetWorkflowJobAsWorker(t, api, router, &ctx)637 assert.NotNil(t, ctx.job)638 // Init store639 cfg := objectstore.Config{640 Kind: objectstore.Filesystem,641 Options: objectstore.ConfigOptions{642 Filesystem: objectstore.ConfigOptionsFilesystem{643 Basedir: path.Join(os.TempDir(), "store"),644 },645 },646 }647 storage, errO := objectstore.Init(context.Background(), cfg)648 require.NoError(t, errO)649 api.SharedStorage = storage650 //Prepare request651 vars := map[string]string{652 "key": ctx.project.Key,653 "permWorkflowName": ctx.workflow.Name,654 "id": fmt.Sprintf("%d", ctx.job.ID),655 }656 //Register the worker657 testRegisterWorker(t, api, router, &ctx)658 //Take659 uri := router.GetRoute("POST", api.postTakeWorkflowJobHandler, vars)660 test.NotEmpty(t, uri)661 req := assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, nil)662 rec := httptest.NewRecorder()663 router.Mux.ServeHTTP(rec, req)664 require.Equal(t, 200, rec.Code)665 vars = map[string]string{666 "ref": base64.RawURLEncoding.EncodeToString([]byte("latest")),667 "integrationName": sdk.DefaultStorageIntegrationName,668 "permProjectKey": ctx.project.Key,669 }670 uri = router.GetRoute("POST", api.postWorkflowJobArtifactHandler, vars)671 test.NotEmpty(t, uri)672 myartifact, errF := os.Create(path.Join(os.TempDir(), "myartifact"))673 defer os.RemoveAll(path.Join(os.TempDir(), "myartifact"))674 require.NoError(t, errF)675 _, errW := myartifact.Write([]byte("Hi, I am foo"))676 require.NoError(t, errW)677 errClose := myartifact.Close()678 require.NoError(t, errClose)679 params := map[string]string{}680 params["size"] = "12"681 params["perm"] = "7"682 params["md5sum"] = "123"683 params["sha512sum"] = "1234"684 params["nodeJobRunID"] = fmt.Sprintf("%d", ctx.job.ID)685 req = assets.NewJWTAuthentifiedMultipartRequest(t, ctx.workerToken, "POST", uri, path.Join(os.TempDir(), "myartifact"), "myartifact", params)686 rec = httptest.NewRecorder()687 router.Mux.ServeHTTP(rec, req)688 require.Equal(t, 204, rec.Code)689 time.Sleep(1 * time.Second)690 wNodeJobRun, errJ := workflow.LoadNodeJobRun(context.TODO(), api.mustDB(), api.Cache, ctx.job.ID)691 require.NoError(t, errJ)692 updatedNodeRun, errN2 := workflow.LoadNodeRunByID(api.mustDB(), wNodeJobRun.WorkflowNodeRunID, workflow.LoadRunOptions{WithArtifacts: true})693 require.NoError(t, errN2)694 assert.NotNil(t, updatedNodeRun.Artifacts)695 require.Equal(t, 1, len(updatedNodeRun.Artifacts))696 //Prepare request697 vars = map[string]string{698 "key": ctx.project.Key,699 "permWorkflowName": ctx.workflow.Name,700 "number": fmt.Sprintf("%d", updatedNodeRun.Number),701 }702 uri = router.GetRoute("GET", api.getWorkflowRunArtifactsHandler, vars)703 test.NotEmpty(t, uri)704 req = assets.NewJWTAuthentifiedRequest(t, ctx.password, "GET", uri, nil)705 rec = httptest.NewRecorder()706 router.Mux.ServeHTTP(rec, req)707 require.Equal(t, 200, rec.Code)708 var arts []sdk.WorkflowNodeRunArtifact709 require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &arts))710 require.Equal(t, 1, len(arts))711 require.Equal(t, "myartifact", arts[0].Name)712 // Download artifact713 //Prepare request714 vars = map[string]string{715 "key": ctx.project.Key,716 "permWorkflowName": ctx.workflow.Name,717 "artifactId": fmt.Sprintf("%d", arts[0].ID),718 }719 uri = router.GetRoute("GET", api.getDownloadArtifactHandler, vars)720 test.NotEmpty(t, uri)721 req = assets.NewJWTAuthentifiedRequest(t, ctx.password, "GET", uri, nil)722 rec = httptest.NewRecorder()723 router.Mux.ServeHTTP(rec, req)724 resp := rec.Result()725 body, _ := ioutil.ReadAll(resp.Body)726 require.Equal(t, 200, rec.Code)727 require.Equal(t, "Hi, I am foo", string(body))728 // check if file is stored locally729 containerPath := path.Join(os.TempDir(), "store", fmt.Sprintf("%d-%d-%v", ctx.run.ID, wNodeJobRun.WorkflowNodeRunID, arts[0].Ref))730 artifactPath := path.Join(containerPath, "myartifact")731 exists := fileExists(artifactPath)732 assert.Equal(t, true, exists)733 // then purge run to delete artifact734 require.NoError(t, purge.DeleteArtifacts(router.Background, db, api.Cache, api.SharedStorage, ctx.run.ID))735 // check if file is deleted736 exists = fileExists(artifactPath)737 assert.Equal(t, false, exists)738 if _, err := os.Stat(containerPath); !os.IsNotExist(err) {739 t.FailNow()740 }741}742func fileExists(filename string) bool {743 info, err := os.Stat(filename)744 if os.IsNotExist(err) {745 return false746 }747 return !info.IsDir()748}749func Test_postWorkflowJobStaticFilesHandler(t *testing.T) {750 api, _, router := newTestAPI(t)751 ctx := testRunWorkflow(t, api, router)752 testGetWorkflowJobAsWorker(t, api, router, &ctx)753 require.NotNil(t, ctx.job)754 // Init store755 cfg := objectstore.Config{756 Kind: objectstore.Filesystem,757 Options: objectstore.ConfigOptions{758 Filesystem: objectstore.ConfigOptionsFilesystem{759 Basedir: path.Join(os.TempDir(), "store"),760 },761 },762 }763 storage, errO := objectstore.Init(context.Background(), cfg)764 require.NoError(t, errO)765 api.SharedStorage = storage766 //Prepare request767 vars := map[string]string{768 "key": ctx.project.Key,769 "permWorkflowName": ctx.workflow.Name,770 "id": fmt.Sprintf("%d", ctx.job.ID),771 }772 //Register the worker773 testRegisterWorker(t, api, router, &ctx)774 //Take775 uri := router.GetRoute("POST", api.postTakeWorkflowJobHandler, vars)776 test.NotEmpty(t, uri)777 req := assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, nil)778 rec := httptest.NewRecorder()779 router.Mux.ServeHTTP(rec, req)780 require.Equal(t, 200, rec.Code)781 vars = map[string]string{782 "name": url.PathEscape("mywebsite"),783 "integrationName": sdk.DefaultStorageIntegrationName,784 "permProjectKey": ctx.project.Key,785 }786 uri = router.GetRoute("POST", api.postWorkflowJobStaticFilesHandler, vars)787 test.NotEmpty(t, uri)788 mystaticfile, errF := os.Create(path.Join(os.TempDir(), "mystaticfile"))789 defer os.RemoveAll(path.Join(os.TempDir(), "mystaticfile"))790 require.NoError(t, errF)791 _, errW := mystaticfile.Write([]byte("<html>Hi, I am foo</html>"))792 require.NoError(t, errW)793 errClose := mystaticfile.Close()794 require.NoError(t, errClose)795 params := map[string]string{796 "entrypoint": "index.html",797 "nodeJobRunID": fmt.Sprintf("%d", ctx.job.ID),798 }799 req = assets.NewJWTAuthentifiedMultipartRequest(t, ctx.workerToken, "POST", uri, path.Join(os.TempDir(), "mystaticfile"), "mystaticfile", params)800 rec = httptest.NewRecorder()801 router.Mux.ServeHTTP(rec, req)802 require.Equal(t, http.StatusNotImplemented, rec.Code)803}804func TestWorkerPrivateKey(t *testing.T) {805 api, db, router := newTestAPI(t)806 // Create user807 u, pass := assets.InsertAdminUser(t, api.mustDB())808 consumer, _ := authentication.LoadConsumerByTypeAndUserID(context.TODO(), db, sdk.ConsumerLocal, u.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)809 // Create project810 key := sdk.RandomString(10)811 proj := assets.InsertTestProject(t, db, api.Cache, key, key)812 // add group813 require.NoError(t, group.InsertLinkGroupUser(context.TODO(), api.mustDB(), &group.LinkGroupUser{814 GroupID: proj.ProjectGroups[0].Group.ID,815 AuthentifiedUserID: u.ID,816 Admin: true,817 }))818 u.Groups = append(u.Groups, proj.ProjectGroups[0].Group)819 // Create pipeline820 pip := &sdk.Pipeline{821 ProjectID: proj.ID,822 Name: sdk.RandomString(10),823 }824 assert.NoError(t, pipeline.InsertPipeline(db, pip))825 s := sdk.Stage{826 PipelineID: pip.ID,827 Name: "foo",828 Enabled: true,829 }830 assert.NoError(t, pipeline.InsertStage(db, &s))831 // get script action832 script := assets.GetBuiltinOrPluginActionByName(t, db, sdk.ScriptAction)833 j := sdk.Job{834 Enabled: true,835 PipelineStageID: s.ID,836 Action: sdk.Action{837 Name: "script",838 Actions: []sdk.Action{839 assets.NewAction(script.ID, sdk.Parameter{Name: "script", Value: "echo lol"}),840 },841 },842 }843 assert.NoError(t, pipeline.InsertJob(db, &j, s.ID, pip))844 var errPip error845 pip, errPip = pipeline.LoadPipelineByID(context.TODO(), db, pip.ID, true)846 assert.NoError(t, errPip)847 // Create application848 app := sdk.Application{849 ProjectID: proj.ID,850 Name: sdk.RandomString(10),851 }852 assert.NoError(t, application.Insert(db, *proj, &app))853 // Create workflow854 w := sdk.Workflow{855 Name: sdk.RandomString(10),856 ProjectID: proj.ID,857 ProjectKey: proj.Key,858 WorkflowData: sdk.WorkflowData{859 Node: sdk.Node{860 Name: "node1",861 Ref: "node1",862 Type: sdk.NodeTypePipeline,863 Context: &sdk.NodeContext{864 PipelineID: pip.ID,865 ApplicationID: app.ID,866 },867 },868 },869 }870 p, err := project.Load(context.TODO(), db, proj.Key, project.LoadOptions.WithPipelines, project.LoadOptions.WithApplications)871 assert.NoError(t, err)872 assert.NoError(t, workflow.Insert(context.TODO(), db, api.Cache, *p, &w))873 workflowDeepPipeline, err := workflow.LoadByID(context.TODO(), db, api.Cache, *p, w.ID, workflow.LoadOptions{DeepPipeline: true})874 assert.NoError(t, err)875 wrDB, errwr := workflow.CreateRun(db, workflowDeepPipeline, nil, u)876 assert.NoError(t, errwr)877 wrDB.Workflow = *workflowDeepPipeline878 _, errmr := workflow.StartWorkflowRun(context.Background(), db, api.Cache, *p, wrDB,879 &sdk.WorkflowRunPostHandlerOption{880 Manual: &sdk.WorkflowNodeRunManual{Username: u.Username},881 },882 consumer, nil)883 assert.NoError(t, errmr)884 ctx := testRunWorkflowCtx{885 user: u,886 password: pass,887 project: proj,888 workflow: &w,889 run: wrDB,890 }891 testRegisterWorker(t, api, router, &ctx)892 ctx.worker.JobRunID = &wrDB.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].Stages[0].RunJobs[0].ID893 assert.NoError(t, worker.SetToBuilding(context.TODO(), db, ctx.worker.ID, *ctx.worker.JobRunID, []byte("mysecret")))894 wkFromDB, err := worker.LoadWorkerByName(context.TODO(), db, ctx.worker.Name)895 require.NoError(t, err)896 require.NotEqual(t, "mysecret", string(wkFromDB.PrivateKey))897 wkFromDB, err = worker.LoadWorkerByIDWithDecryptKey(context.TODO(), db, ctx.worker.ID)898 require.NoError(t, err)899 require.Equal(t, "mysecret", string(wkFromDB.PrivateKey))900}901func TestPostVulnerabilityReportHandler(t *testing.T) {902 api, db, router := newTestAPI(t)903 // Create user904 u, pass := assets.InsertAdminUser(t, api.mustDB())905 consumer, _ := authentication.LoadConsumerByTypeAndUserID(context.TODO(), db, sdk.ConsumerLocal, u.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)906 // Create project907 key := sdk.RandomString(10)908 proj := assets.InsertTestProject(t, db, api.Cache, key, key)909 // add group910 require.NoError(t, group.InsertLinkGroupUser(context.TODO(), api.mustDB(), &group.LinkGroupUser{911 GroupID: proj.ProjectGroups[0].Group.ID,912 AuthentifiedUserID: u.ID,913 Admin: true,914 }))915 u.Groups = append(u.Groups, proj.ProjectGroups[0].Group)916 // Create pipeline917 pip := &sdk.Pipeline{918 ProjectID: proj.ID,919 Name: sdk.RandomString(10),920 }921 assert.NoError(t, pipeline.InsertPipeline(db, pip))922 s := sdk.Stage{923 PipelineID: pip.ID,924 Name: "foo",925 Enabled: true,926 }927 assert.NoError(t, pipeline.InsertStage(db, &s))928 // get script action929 script := assets.GetBuiltinOrPluginActionByName(t, db, sdk.ScriptAction)930 j := sdk.Job{931 Enabled: true,932 PipelineStageID: s.ID,933 Action: sdk.Action{934 Name: "script",935 Actions: []sdk.Action{936 assets.NewAction(script.ID, sdk.Parameter{Name: "script", Value: "echo lol"}),937 },938 },939 }940 assert.NoError(t, pipeline.InsertJob(db, &j, s.ID, pip))941 var errPip error942 pip, errPip = pipeline.LoadPipelineByID(context.TODO(), db, pip.ID, true)943 assert.NoError(t, errPip)944 // Create application945 app := sdk.Application{946 ProjectID: proj.ID,947 Name: sdk.RandomString(10),948 }949 assert.NoError(t, application.Insert(db, *proj, &app))950 // Create workflow951 w := sdk.Workflow{952 Name: sdk.RandomString(10),953 ProjectID: proj.ID,954 ProjectKey: proj.Key,955 WorkflowData: sdk.WorkflowData{956 Node: sdk.Node{957 Name: "node1",958 Ref: "node1",959 Type: sdk.NodeTypePipeline,960 Context: &sdk.NodeContext{961 PipelineID: pip.ID,962 ApplicationID: app.ID,963 },964 },965 },966 }967 p, err := project.Load(context.TODO(), db, proj.Key, project.LoadOptions.WithPipelines, project.LoadOptions.WithApplications)968 assert.NoError(t, err)969 assert.NoError(t, workflow.Insert(context.TODO(), db, api.Cache, *p, &w))970 workflowDeepPipeline, err := workflow.LoadByID(context.TODO(), db, api.Cache, *p, w.ID, workflow.LoadOptions{DeepPipeline: true})971 assert.NoError(t, err)972 wrDB, errwr := workflow.CreateRun(db, workflowDeepPipeline, nil, u)973 assert.NoError(t, errwr)974 wrDB.Workflow = *workflowDeepPipeline975 _, errmr := workflow.StartWorkflowRun(context.Background(), db, api.Cache, *p, wrDB,976 &sdk.WorkflowRunPostHandlerOption{977 Manual: &sdk.WorkflowNodeRunManual{Username: u.Username},978 },979 consumer, nil)980 assert.NoError(t, errmr)981 log.Debug("%+v", wrDB.WorkflowNodeRuns)982 // Call post coverage report handler983 // Prepare request984 vars := map[string]string{985 "permJobID": fmt.Sprintf("%d", wrDB.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].Stages[0].RunJobs[0].ID),986 }987 ctx := testRunWorkflowCtx{988 user: u,989 password: pass,990 project: proj,991 workflow: &w,992 run: wrDB,993 }994 testRegisterWorker(t, api, router, &ctx)995 ctx.worker.JobRunID = &wrDB.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].Stages[0].RunJobs[0].ID996 assert.NoError(t, worker.SetToBuilding(context.TODO(), db, ctx.worker.ID, *ctx.worker.JobRunID, nil))997 request := sdk.VulnerabilityWorkerReport{998 Vulnerabilities: []sdk.Vulnerability{999 {1000 Version: "1.0.0",1001 Title: "lodash",1002 Severity: "high",1003 Origin: "parsejson>lodash",1004 Link: "",1005 FixIn: "",1006 Description: "",1007 CVE: "",1008 Component: "",1009 Ignored: false,1010 },1011 },1012 }1013 uri := router.GetRoute("POST", api.postVulnerabilityReportHandler, vars)1014 test.NotEmpty(t, uri)1015 req := assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, request)1016 rec := httptest.NewRecorder()1017 router.Mux.ServeHTTP(rec, req)1018 require.Equal(t, 204, rec.Code)1019}1020func TestInsertNewCodeCoverageReport(t *testing.T) {1021 api, db, router := newTestAPI(t)1022 // Create user1023 u, pass := assets.InsertAdminUser(t, api.mustDB())1024 // Create project1025 key := sdk.RandomString(10)1026 proj := assets.InsertTestProject(t, db, api.Cache, key, key)1027 // add group1028 require.NoError(t, group.InsertLinkGroupUser(context.TODO(), api.mustDB(), &group.LinkGroupUser{1029 GroupID: proj.ProjectGroups[0].Group.ID,1030 AuthentifiedUserID: u.ID,1031 Admin: true,1032 }))1033 u.Groups = append(u.Groups, proj.ProjectGroups[0].Group)1034 // Add repo manager1035 proj.VCSServers = make([]sdk.ProjectVCSServerLink, 0, 1)1036 proj.VCSServers = append(proj.VCSServers)1037 vcsServer := sdk.ProjectVCSServerLink{1038 ProjectID: proj.ID,1039 Name: "repoManServ",1040 }1041 vcsServer.Set("token", "foo")1042 vcsServer.Set("secret", "bar")1043 assert.NoError(t, repositoriesmanager.InsertProjectVCSServerLink(context.TODO(), db, &vcsServer))1044 // Create pipeline1045 pip := &sdk.Pipeline{1046 ProjectID: proj.ID,1047 Name: sdk.RandomString(10),1048 }1049 assert.NoError(t, pipeline.InsertPipeline(db, pip))1050 s := sdk.Stage{1051 PipelineID: pip.ID,1052 Name: "foo",1053 Enabled: true,1054 }1055 assert.NoError(t, pipeline.InsertStage(db, &s))1056 // get script action1057 script := assets.GetBuiltinOrPluginActionByName(t, db, sdk.ScriptAction)1058 j := sdk.Job{1059 Enabled: true,1060 PipelineStageID: s.ID,1061 Action: sdk.Action{1062 Name: "script",1063 Actions: []sdk.Action{1064 assets.NewAction(script.ID, sdk.Parameter{Name: "script", Value: "echo lol"}),1065 },1066 },1067 }1068 assert.NoError(t, pipeline.InsertJob(db, &j, s.ID, pip))1069 var errPip error1070 pip, errPip = pipeline.LoadPipelineByID(context.TODO(), db, pip.ID, true)1071 assert.NoError(t, errPip)1072 // Create application1073 app := sdk.Application{1074 ProjectID: proj.ID,1075 Name: sdk.RandomString(10),1076 RepositoryFullname: "foo/bar",1077 VCSServer: "repoManServ",1078 }1079 assert.NoError(t, application.Insert(db, *proj, &app))1080 assert.NoError(t, repositoriesmanager.InsertForApplication(db, &app))1081 // Create workflow1082 w := sdk.Workflow{1083 Name: sdk.RandomString(10),1084 ProjectID: proj.ID,1085 ProjectKey: proj.Key,1086 WorkflowData: sdk.WorkflowData{1087 Node: sdk.Node{1088 Name: "node1",1089 Ref: "node1",1090 Type: sdk.NodeTypePipeline,1091 Context: &sdk.NodeContext{1092 PipelineID: pip.ID,1093 ApplicationID: app.ID,1094 },1095 },1096 },1097 }1098 p, err := project.Load(context.TODO(), db, proj.Key, project.LoadOptions.WithPipelines, project.LoadOptions.WithApplications)1099 require.NoError(t, err)1100 require.NoError(t, workflow.Insert(context.TODO(), db, api.Cache, *p, &w))1101 allSrv, err := services.LoadAll(context.TODO(), db)1102 for _, s := range allSrv {1103 if err := services.Delete(db, &s); err != nil {1104 t.Fatalf("unable to delete service: %v", err)1105 }1106 }1107 a, _ := assets.InsertService(t, db, "TestInsertNewCodeCoverageReport", services.TypeVCS)1108 defer func() {1109 _ = services.Delete(db, a)1110 }()1111 //This is a mock for the repositories service1112 services.HTTPClient = mock(1113 func(r *http.Request) (*http.Response, error) {1114 body := new(bytes.Buffer)1115 wri := new(http.Response)1116 enc := json.NewEncoder(body)1117 wri.Body = ioutil.NopCloser(body)1118 switch r.URL.String() {1119 case "/vcs/repoManServ/repos/foo/bar":1120 repo := sdk.VCSRepo{1121 ID: "1",1122 Name: "bar",1123 URL: "url",1124 Fullname: "foo/bar",1125 HTTPCloneURL: "",1126 Slug: "",1127 SSHCloneURL: "",1128 }1129 if err := enc.Encode(repo); err != nil {1130 return writeError(wri, err)1131 }1132 case "/vcs/repoManServ/repos/foo/bar/branches":1133 bs := []sdk.VCSBranch{}1134 b := sdk.VCSBranch{1135 DisplayID: "master",1136 Default: true,1137 }1138 bs = append(bs, b)1139 b2 := sdk.VCSBranch{1140 DisplayID: "my-branch",1141 Default: false,1142 }1143 bs = append(bs, b2)1144 if err := enc.Encode(bs); err != nil {1145 return writeError(wri, err)1146 }1147 case "/vcs/repoManServ/repos/foo/bar/branches/?branch=master":1148 b := sdk.VCSBranch{1149 DisplayID: "master",1150 Default: true,1151 }1152 if err := enc.Encode(b); err != nil {1153 return writeError(wri, err)1154 }1155 case "/vcs/repoManServ/repos/foo/bar/commits/":1156 c := sdk.VCSCommit{1157 URL: "url",1158 Message: "Msg",1159 Timestamp: time.Now().Unix(),1160 Hash: "123",1161 }1162 if err := enc.Encode(c); err != nil {1163 return writeError(wri, err)1164 }1165 case "/vcs/repoManServ/repos/foo/bar/branches/?branch=my-branch":1166 b := sdk.VCSBranch{1167 DisplayID: "my-branch",1168 Default: true,1169 }1170 if err := enc.Encode(b); err != nil {1171 return writeError(wri, err)1172 }1173 wri.StatusCode = http.StatusCreated1174 }1175 return wri, nil1176 },1177 )1178 // Create previous run on default branch1179 wrDB, errwr := workflow.CreateRun(db, &w, nil, u)1180 assert.NoError(t, errwr)1181 workflowWithDeepPipeline, err := workflow.LoadByID(context.TODO(), db, api.Cache, *proj, w.ID, workflow.LoadOptions{DeepPipeline: true})1182 assert.NoError(t, err)1183 consumer, _ := authentication.LoadConsumerByTypeAndUserID(context.TODO(), db, sdk.ConsumerLocal, u.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)1184 wrDB.Workflow = *workflowWithDeepPipeline1185 _, errmr := workflow.StartWorkflowRun(context.Background(), db, api.Cache, *p, wrDB, &sdk.WorkflowRunPostHandlerOption{1186 Manual: &sdk.WorkflowNodeRunManual{1187 Username: u.Username,1188 Payload: map[string]string{1189 "git.branch": "master",1190 },1191 },1192 }, consumer, nil)1193 assert.NoError(t, errmr)1194 // Create previous run on a branch1195 wrCB, errwr2 := workflow.CreateRun(db, &w, nil, u)1196 assert.NoError(t, errwr2)1197 wrCB.Workflow = w1198 _, errmr = workflow.StartWorkflowRun(context.Background(), db, api.Cache, *p, wrCB, &sdk.WorkflowRunPostHandlerOption{1199 Manual: &sdk.WorkflowNodeRunManual{1200 Username: u.Username,1201 Payload: map[string]string{1202 "git.branch": "my-branch",1203 },1204 },1205 }, consumer, nil)1206 assert.NoError(t, errmr)1207 // Add a coverage report on default branch node run1208 coverateReportDefaultBranch := sdk.WorkflowNodeRunCoverage{1209 WorkflowID: w.ID,1210 WorkflowRunID: wrDB.ID,1211 WorkflowNodeRunID: wrDB.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].ID,1212 ApplicationID: app.ID,1213 Num: wrDB.Number,1214 Branch: wrDB.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].VCSBranch,1215 Repository: wrDB.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].VCSRepository,1216 Report: coverage.Report{1217 CoveredBranches: 20,1218 TotalBranches: 30,1219 CoveredLines: 20,1220 TotalLines: 23,1221 TotalFunctions: 25,1222 CoveredFunctions: 30,1223 },1224 }1225 assert.NoError(t, workflow.InsertCoverage(db, coverateReportDefaultBranch))1226 // Add a coverage report on current branch node run1227 coverateReportCurrentBranch := sdk.WorkflowNodeRunCoverage{1228 WorkflowID: w.ID,1229 WorkflowRunID: wrCB.ID,1230 WorkflowNodeRunID: wrCB.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].ID,1231 ApplicationID: app.ID,1232 Num: wrCB.Number,1233 Branch: wrCB.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].VCSBranch,1234 Repository: wrCB.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].VCSRepository,1235 Report: coverage.Report{1236 CoveredBranches: 0,1237 TotalBranches: 30,1238 CoveredLines: 0,1239 TotalLines: 23,1240 TotalFunctions: 25,1241 CoveredFunctions: 0,1242 },1243 }1244 assert.NoError(t, workflow.InsertCoverage(db, coverateReportCurrentBranch))1245 // Run test1246 // Create a workflow run1247 wrToTest, errwr3 := workflow.CreateRun(db, &w, nil, u)1248 assert.NoError(t, errwr3)1249 wrToTest.Workflow = *workflowWithDeepPipeline1250 _, errT := workflow.StartWorkflowRun(context.Background(), db, api.Cache, *p, wrToTest, &sdk.WorkflowRunPostHandlerOption{1251 Manual: &sdk.WorkflowNodeRunManual{1252 Username: u.Username,1253 Payload: map[string]string{1254 "git.branch": "my-branch",1255 },1256 },1257 }, consumer, nil)1258 assert.NoError(t, errT)1259 wrr, err := workflow.LoadRunByID(db, wrToTest.ID, workflow.LoadRunOptions{})1260 assert.NoError(t, err)1261 log.Warning(context.Background(), "%s", wrr.Status)1262 // Call post coverage report handler1263 // Prepare request1264 vars := map[string]string{1265 "permJobID": fmt.Sprintf("%d", wrr.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].Stages[0].RunJobs[0].ID),1266 }1267 request := coverage.Report{1268 CoveredBranches: 1,1269 TotalBranches: 30,1270 CoveredLines: 1,1271 TotalLines: 23,1272 TotalFunctions: 25,1273 CoveredFunctions: 1,1274 }1275 ctx := testRunWorkflowCtx{1276 user: u,1277 password: pass,1278 project: proj,1279 workflow: &w,1280 run: wrr,1281 }1282 testRegisterWorker(t, api, router, &ctx)1283 ctx.worker.JobRunID = &wrr.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].Stages[0].RunJobs[0].ID1284 assert.NoError(t, worker.SetToBuilding(context.TODO(), db, ctx.worker.ID, *ctx.worker.JobRunID, nil))1285 uri := router.GetRoute("POST", api.postWorkflowJobCoverageResultsHandler, vars)1286 test.NotEmpty(t, uri)1287 req := assets.NewJWTAuthentifiedRequest(t, ctx.workerToken, "POST", uri, request)1288 rec := httptest.NewRecorder()1289 router.Mux.ServeHTTP(rec, req)1290 require.Equal(t, 204, rec.Code)1291 covDB, errL := workflow.LoadCoverageReport(db, wrToTest.WorkflowNodeRuns[w.WorkflowData.Node.ID][0].ID)1292 assert.NoError(t, errL)1293 require.Equal(t, coverateReportDefaultBranch.Report.CoveredBranches, covDB.Trend.DefaultBranch.CoveredBranches)1294}...
process_files_test.go
Source:process_files_test.go
...11 "github.com/stretchr/testify/require"12)13func tempDir(t *testing.T) (string, error) {14 dir := os.TempDir()15 name := path.Join(dir, randomString(5))16 if err := os.MkdirAll(name, os.FileMode(0744)); err != nil {17 return "", err18 }19 t.Logf("Creating directory %s", name)20 return name, nil21}22func randomString(n int) string {23 var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")24 b := make([]rune, n)25 for i := range b {26 b[i] = letter[rand.Intn(len(letter))]27 }28 return string(b)29}30func Test_getFilesPath(t *testing.T) {31 InitTestLogger(t)32 rand.Seed(time.Now().UnixNano())33 log.SetLevel(log.DebugLevel)34 tests := []struct {35 init func(t *testing.T) ([]string, error)36 name string37 want []string38 wantErr bool39 }{40 {41 name: "Check an empty directory",42 init: func(t *testing.T) ([]string, error) {43 dir, err := tempDir(t)44 return []string{dir}, err45 },46 wantErr: true,47 },48 {49 name: "Check an directory with one yaml file",50 init: func(t *testing.T) ([]string, error) {51 dir, err := tempDir(t)52 if err != nil {53 return nil, err54 }55 d1 := []byte("hello")56 err = os.WriteFile(path.Join(dir, "d1.yml"), d1, 0644)57 return []string{dir}, err58 },59 want: []string{"d1.yml"},60 wantErr: false,61 },62 {63 name: "Check an directory with one yaml file and a subdirectory with another file",64 init: func(t *testing.T) ([]string, error) {65 dir1, err := tempDir(t)66 if err != nil {67 return nil, err68 }69 d1 := []byte("hello")70 if err = os.WriteFile(path.Join(dir1, "d1.yml"), d1, 0644); err != nil {71 return nil, err72 }73 dir2 := path.Join(dir1, randomString(10))74 t.Logf("Creating directory %s", dir2)75 if err := os.Mkdir(dir2, 0744); err != nil {76 return nil, err77 }78 d2 := []byte("hello")79 if err = os.WriteFile(path.Join(dir2, "d2.yml"), d2, 0644); err != nil {80 return nil, err81 }82 return []string{dir1, dir2}, err83 },84 want: []string{"d1.yml", "d2.yml"},85 wantErr: false,86 },87 {88 name: "Check globstars",89 init: func(t *testing.T) ([]string, error) {90 dir1, err := tempDir(t)91 if err != nil {92 return nil, err93 }94 d1 := []byte("hello")95 if err = os.WriteFile(path.Join(dir1, "d1.yml"), d1, 0644); err != nil {96 return nil, err97 }98 dir2 := path.Join(dir1, randomString(10))99 t.Logf("Creating directory %s", dir2)100 if err := os.Mkdir(dir2, 0744); err != nil {101 return nil, err102 }103 d2 := []byte("hello")104 if err = os.WriteFile(path.Join(dir2, "d2.yml"), d2, 0644); err != nil {105 return nil, err106 }107 dir3 := path.Join(dir2, randomString(10))108 t.Logf("Creating directory %s", dir3)109 if err := os.Mkdir(dir3, 0744); err != nil {110 return nil, err111 }112 d3 := []byte("hello")113 if err = os.WriteFile(path.Join(dir2, "d3.yml"), d3, 0644); err != nil {114 return nil, err115 }116 dir4 := path.Join(dir3, randomString(10))117 t.Logf("Creating directory %s", dir3)118 if err := os.Mkdir(dir4, 0744); err != nil {119 return nil, err120 }121 d4 := []byte("hello")122 if err = os.WriteFile(path.Join(dir4, "d4.yml"), d4, 0644); err != nil {123 return nil, err124 }125 return []string{fmt.Sprintf("%s/**/*.yml", dir1)}, err126 },127 want: []string{"d1.yml", "d2.yml", "d3.yml", "d4.yml"},128 wantErr: false,129 },130 {131 name: "Check globstars with duplicate files",132 init: func(t *testing.T) ([]string, error) {133 dir1, err := tempDir(t)134 if err != nil {135 return nil, err136 }137 d1 := []byte("hello")138 if err = os.WriteFile(path.Join(dir1, "d1.yml"), d1, 0644); err != nil {139 return nil, err140 }141 dir2 := path.Join(dir1, randomString(10))142 t.Logf("Creating directory %s", dir2)143 if err := os.Mkdir(dir2, 0744); err != nil {144 return nil, err145 }146 d2 := []byte("hello")147 if err = os.WriteFile(path.Join(dir2, "d2.yml"), d2, 0644); err != nil {148 return nil, err149 }150 dir3 := path.Join(dir2, randomString(10))151 t.Logf("Creating directory %s", dir3)152 if err := os.Mkdir(dir3, 0744); err != nil {153 return nil, err154 }155 d3 := []byte("hello")156 if err = os.WriteFile(path.Join(dir2, "d3.yml"), d3, 0644); err != nil {157 return nil, err158 }159 dir4 := path.Join(dir3, randomString(10))160 t.Logf("Creating directory %s", dir3)161 if err := os.Mkdir(dir4, 0744); err != nil {162 return nil, err163 }164 d4 := []byte("hello")165 if err = os.WriteFile(path.Join(dir4, "d4.yml"), d4, 0644); err != nil {166 return nil, err167 }168 return []string{dir2, dir3, fmt.Sprintf("%s/**/*.yml", dir1)}, err169 },170 want: []string{"d1.yml", "d2.yml", "d3.yml", "d4.yml"},171 wantErr: false,172 },173 }...
randomString
Using AI Code Generation
1import (2func main() {3 fmt.Println(venom.RandomString(10))4}5import (6func main() {7 fmt.Println(venom.RandomString(10))8}9import (10func main() {11 fmt.Println(venom.RandomString(10))12}13import (14func main() {15 fmt.Println(venom.RandomString(10))16}17import (18func main() {19 fmt.Println(venom.RandomString(10))20}21import (22func main() {23 fmt.Println(venom.RandomString(10))24}25import (26func main() {27 fmt.Println(venom.RandomString(10))28}29import (30func main() {31 fmt.Println(venom.RandomString(10))32}33import (34func main() {35 fmt.Println(venom.RandomString(10))36}37import (38func main() {39 fmt.Println(venom.RandomString(10))40}41import (42func main() {43 fmt.Println(venom.RandomString(10))
randomString
Using AI Code Generation
1import (2func main() {3 fmt.Println(venom.RandomString(10))4}5import (6func main() {7 fmt.Println(venom.RandomString(10))8}9import (10func main() {11 fmt.Println(venom.RandomString(10))12}13import (14func main() {15 fmt.Println(venom.RandomString(10))16}17import (18func main() {19 fmt.Println(venom.RandomString(10))20}21import (22func main() {23 fmt.Println(venom.RandomString(10))24}25import (26func main() {27 fmt.Println(venom.RandomString(10))28}29import (30func main() {31 fmt.Println(venom.RandomString(10))32}33import (34func main() {35 fmt.Println(venom.RandomString(10))36}
randomString
Using AI Code Generation
1import (2func main() {3 fmt.Println(venom.RandomString(10))4}5import (6func RandomString(length int) string {7 rand.Seed(time.Now().UnixNano())8 bytes := make([]byte, length)9 for i := 0; i < length; i++ {10 bytes[i] = byte(randInt(65, 90))11 }12 return string(bytes)13}14func randInt(min int, max int) int {15 return min + rand.Intn(max-min)16}
randomString
Using AI Code Generation
1import "fmt"2import "github.com/venom"3func main() {4 fmt.Println(venom.randomString(10))5}6import "fmt"7import "github.com/venom"8func main() {9 fmt.Println(venom.randomString(10))10}11import "fmt"12import "github.com/venom"13func main() {14 fmt.Println(venom.randomString(10))15}16import "fmt"17import "github.com/venom"18func main() {19 fmt.Println(venom.randomString(10))20}21import "fmt"22import "github.com/venom"23func main() {24 fmt.Println(venom.randomString(10))25}26import "fmt"27import "github.com/venom"28func main() {29 fmt.Println(venom.randomString(10))30}31import "fmt"32import "github.com/venom"33func main() {34 fmt.Println(venom.randomString(10))35}36import "fmt"37import "github.com/venom"38func main() {39 fmt.Println(venom.randomString(10))40}41import "fmt"42import "github.com/venom"43func main() {44 fmt.Println(venom.randomString(10))45}46import "fmt"47import "github.com/venom"48func main() {49 fmt.Println(venom.randomString(10))50}51import "fmt"52import
randomString
Using AI Code Generation
1import "fmt"2import "github.com/venom"3func main() {4 fmt.Println(venom.randomString())5}6import "fmt"7import "github.com/venom"8func main() {9 fmt.Println(venom.randomString())10}11import "fmt"12import "github.com/venom"13func main() {14 fmt.Println(venom.randomString())15}16import "fmt"17import "github.com/venom"18func main() {19 fmt.Println(venom.randomString())20}21import "fmt"22import "github.com/venom"23func main() {24 fmt.Println(venom.randomString())25}26import "fmt"27import "github.com/venom"28func main() {29 fmt.Println(venom.randomString())30}31import "fmt"32import "github.com/venom"33func main() {34 fmt.Println(venom.randomString())35}36import "fmt"37import "github.com/venom"38func main() {39 fmt.Println(venom.randomString())40}41import "fmt"42import "github.com/venom"43func main() {44 fmt.Println(venom.randomString())45}46import "fmt"47import "github.com/venom"48func main() {49 fmt.Println(venom.randomString())50}51import "fmt"52import "github.com/venom"53func main() {54 fmt.Println(venom.randomString())55}56import "fmt"57import "github.com/venom"58func main() {59 fmt.Println(venom.randomString())60}61import "fmt"62import "github.com/venom"63func main() {64 fmt.Println(venom.randomString())65}66import "fmt"67import "github.com/venom"68func main() {69 fmt.Println(venom.randomString())70}
randomString
Using AI Code Generation
1import "fmt"2import "github.com/venom/venom"3func main() {4 fmt.Println(venom.randomString(10))5}6import "fmt"7import "github.com/venom/venom"8func main() {9 fmt.Println(venom.randomString(10))10}11import "fmt"12import "github.com/venom/venom"13func main() {14 fmt.Println(venom.randomString(10))15}16import "fmt"17import "github.com/venom/venom"18func main() {19 fmt.Println(venom.randomString(10))20}21import "fmt"22import "github.com/venom/venom"23func main() {24 fmt.Println(venom.randomString(10))25}26import "fmt"27import "github.com/venom/venom"28func main() {29 fmt.Println(venom.randomString(10))30}31import "fmt"32import "github.com/venom/venom"33func main() {34 fmt.Println(venom.randomString(10))35}36import "fmt"37import "github.com/venom/venom"38func main() {39 fmt.Println(venom.randomString(10))40}41import "fmt"42import "github.com/venom/venom"43func main() {44 fmt.Println(venom.randomString(10))45}46import "fmt"47import "github.com/venom/venom"48func main() {49 fmt.Println(venom.randomString
randomString
Using AI Code Generation
1import (2func main() {3 fmt.Println(venom.RandomString(20))4}5import (6type Venom struct {7}8func init() {9 rand.Seed(time.Now().UTC().UnixNano())10}11func RandomString(n int) string {12 var bytes = make([]byte, n)13 for i, _ := range bytes {14 bytes[i] = alphanum[rand.Intn(len(alphanum))]15 }16 return string(bytes)17}18To use the package in another file, you just need to import it. You can do this by using the following code:19import "venom"
randomString
Using AI Code Generation
1import (2func main() {3 venom := venom.New()4 fmt.Println(venom.RandomString(6))5}6import (7func main() {8 venom := venom.New()9 fmt.Println(venom.RandomString(6))10}11import (12func main() {13 venom := venom.New()14 fmt.Println(venom.RandomString(6))15}16import (17func main() {18 venom := venom.New()19 fmt.Println(venom.RandomString(6))20}21import (22func main() {23 venom := venom.New()24 fmt.Println(venom.RandomString(6))25}26import (27func main() {28 venom := venom.New()29 fmt.Println(venom.RandomString(6))30}31import (32func main() {33 venom := venom.New()34 fmt.Println(venom.RandomString(6))35}36import (37func main() {38 venom := venom.New()39 fmt.Println(venom.RandomString(6))40}41import (42func main() {43 venom := venom.New()44 fmt.Println(venom.RandomString(6))45}
randomString
Using AI Code Generation
1import "fmt"2import "github.com/venom/venom/venom"3func main(){4 fmt.Println(venom.RandomString(10))5}6import "fmt"7import "github.com/venom/venom/venom"8func main(){9 fmt.Println(venom.RandomString(10))10}11import "fmt"12import "github.com/venom/venom/venom"13func main(){14 fmt.Println(venom.RandomString(10))15}16import "fmt"17import "github.com/venom/venom/venom"18func main(){19 fmt.Println(venom.RandomString(10))20}21import "fmt"22import "github.com/venom/venom/venom"23func main(){24 fmt.Println(venom.RandomString(10))25}26import "fmt"27import "github.com/venom/venom/venom"28func main(){29 fmt.Println(venom.RandomString(10))30}31import "fmt"32import "github.com/venom/venom/venom"33func main(){34 fmt.Println(venom.RandomString(10))35}36import "fmt"37import "github.com/venom/venom/venom"38func main(){39 fmt.Println(venom.RandomString(10))40}41import "fmt"42import "github.com/venom/venom/venom"43func main(){44 fmt.Println(venom.RandomString(10))45}46import "fmt"47import "github.com/
randomString
Using AI Code Generation
1import (2func main() {3 fmt.Println(venom.RandomString(10))4}5import "math/rand"6func RandomString(n int) string {7 b := make([]byte, n)8 for i := range b {9 b[i] = letterBytes[rand.Intn(len(letterBytes))]10 }11 return string(b)12}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!