Best Testkube code snippet using testresult.StartExecution
testsuites.go
Source:testsuites.go
1package v12import (3 "context"4 "fmt"5 "net/http"6 "sort"7 "strconv"8 "strings"9 "sync"10 "time"11 "github.com/gofiber/fiber/v2"12 "go.mongodb.org/mongo-driver/mongo"13 "k8s.io/apimachinery/pkg/api/errors"14 testsuitesv2 "github.com/kubeshop/testkube-operator/apis/testsuite/v2"15 "github.com/kubeshop/testkube/internal/pkg/api/datefilter"16 "github.com/kubeshop/testkube/internal/pkg/api/repository/testresult"17 "github.com/kubeshop/testkube/pkg/api/v1/testkube"18 "github.com/kubeshop/testkube/pkg/crd"19 testsmapper "github.com/kubeshop/testkube/pkg/mapper/tests"20 testsuitesmapper "github.com/kubeshop/testkube/pkg/mapper/testsuites"21 "github.com/kubeshop/testkube/pkg/types"22 "github.com/kubeshop/testkube/pkg/workerpool"23)24// GetTestSuiteHandler for getting test object25func (s TestkubeAPI) CreateTestSuiteHandler() fiber.Handler {26 return func(c *fiber.Ctx) error {27 var request testkube.TestSuiteUpsertRequest28 err := c.BodyParser(&request)29 if err != nil {30 return s.Error(c, http.StatusBadRequest, err)31 }32 if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {33 if request.Description != "" {34 request.Description = fmt.Sprintf("%q", request.Description)35 }36 data, err := crd.GenerateYAML(crd.TemplateTestSuite, []testkube.TestSuiteUpsertRequest{request})37 return s.getCRDs(c, data, err)38 }39 testSuite := testsuitesmapper.MapTestSuiteUpsertRequestToTestCRD(request)40 testSuite.Namespace = s.Namespace41 s.Log.Infow("creating test suite", "testSuite", testSuite)42 created, err := s.TestsSuitesClient.Create(&testSuite)43 s.Metrics.IncCreateTestSuite(err)44 if err != nil {45 return s.Error(c, http.StatusBadRequest, err)46 }47 c.Status(http.StatusCreated)48 return c.JSON(created)49 }50}51// UpdateTestSuiteHandler updates an existing TestSuite CR based on TestSuite content52func (s TestkubeAPI) UpdateTestSuiteHandler() fiber.Handler {53 return func(c *fiber.Ctx) error {54 var request testkube.TestSuiteUpsertRequest55 err := c.BodyParser(&request)56 if err != nil {57 return s.Error(c, http.StatusBadRequest, err)58 }59 // we need to get resource first and load its metadata.ResourceVersion60 testSuite, err := s.TestsSuitesClient.Get(request.Name)61 if err != nil {62 return s.Error(c, http.StatusBadGateway, err)63 }64 // map TestSuite but load spec only to not override metadata.ResourceVersion65 testSuiteSpec := testsuitesmapper.MapTestSuiteUpsertRequestToTestCRD(request)66 testSuite.Spec = testSuiteSpec.Spec67 testSuite.Labels = request.Labels68 testSuite, err = s.TestsSuitesClient.Update(testSuite)69 s.Metrics.IncUpdateTestSuite(err)70 if err != nil {71 return s.Error(c, http.StatusBadGateway, err)72 }73 return c.JSON(testSuite)74 }75}76// GetTestSuiteHandler for getting TestSuite object77func (s TestkubeAPI) GetTestSuiteHandler() fiber.Handler {78 return func(c *fiber.Ctx) error {79 name := c.Params("id")80 crTestSuite, err := s.TestsSuitesClient.Get(name)81 if err != nil {82 if errors.IsNotFound(err) {83 return s.Warn(c, http.StatusNotFound, err)84 }85 return s.Error(c, http.StatusBadGateway, err)86 }87 testSuite := testsuitesmapper.MapCRToAPI(*crTestSuite)88 if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {89 if testSuite.Description != "" {90 testSuite.Description = fmt.Sprintf("%q", testSuite.Description)91 }92 data, err := crd.GenerateYAML(crd.TemplateTestSuite, []testkube.TestSuite{testSuite})93 return s.getCRDs(c, data, err)94 }95 return c.JSON(testSuite)96 }97}98// GetTestSuiteWithExecutionHandler for getting TestSuite object with execution99func (s TestkubeAPI) GetTestSuiteWithExecutionHandler() fiber.Handler {100 return func(c *fiber.Ctx) error {101 name := c.Params("id")102 crTestSuite, err := s.TestsSuitesClient.Get(name)103 if err != nil {104 if errors.IsNotFound(err) {105 return s.Warn(c, http.StatusNotFound, err)106 }107 return s.Error(c, http.StatusBadGateway, err)108 }109 testSuite := testsuitesmapper.MapCRToAPI(*crTestSuite)110 if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {111 if testSuite.Description != "" {112 testSuite.Description = fmt.Sprintf("%q", testSuite.Description)113 }114 data, err := crd.GenerateYAML(crd.TemplateTestSuite, []testkube.TestSuite{testSuite})115 return s.getCRDs(c, data, err)116 }117 ctx := c.Context()118 startExecution, startErr := s.TestExecutionResults.GetLatestByTestSuite(ctx, name, "starttime")119 if startErr != nil && startErr != mongo.ErrNoDocuments {120 return s.Error(c, http.StatusInternalServerError, startErr)121 }122 endExecution, endErr := s.TestExecutionResults.GetLatestByTestSuite(ctx, name, "endtime")123 if endErr != nil && endErr != mongo.ErrNoDocuments {124 return s.Error(c, http.StatusInternalServerError, endErr)125 }126 testSuiteWithExecution := testkube.TestSuiteWithExecution{127 TestSuite: &testSuite,128 }129 if startErr == nil && endErr == nil {130 if startExecution.StartTime.After(endExecution.EndTime) {131 testSuiteWithExecution.LatestExecution = &startExecution132 } else {133 testSuiteWithExecution.LatestExecution = &endExecution134 }135 } else if startErr == nil {136 testSuiteWithExecution.LatestExecution = &startExecution137 } else if endErr == nil {138 testSuiteWithExecution.LatestExecution = &endExecution139 }140 return c.JSON(testSuiteWithExecution)141 }142}143// DeleteTestSuiteHandler for deleting a TestSuite with id144func (s TestkubeAPI) DeleteTestSuiteHandler() fiber.Handler {145 return func(c *fiber.Ctx) error {146 name := c.Params("id")147 err := s.TestsSuitesClient.Delete(name)148 if err != nil {149 if errors.IsNotFound(err) {150 return s.Warn(c, http.StatusNotFound, err)151 }152 return s.Error(c, http.StatusBadGateway, err)153 }154 // delete executions for test155 if err = s.ExecutionResults.DeleteByTestSuite(c.Context(), name); err != nil {156 return s.Error(c, http.StatusBadGateway, err)157 }158 // delete executions for test suite159 if err = s.TestExecutionResults.DeleteByTestSuite(c.Context(), name); err != nil {160 return s.Error(c, http.StatusBadGateway, err)161 }162 return c.SendStatus(http.StatusNoContent)163 }164}165// DeleteTestSuitesHandler for deleting all TestSuites166func (s TestkubeAPI) DeleteTestSuitesHandler() fiber.Handler {167 return func(c *fiber.Ctx) error {168 var err error169 var testSuiteNames []string170 selector := c.Query("selector")171 if selector == "" {172 err = s.TestsSuitesClient.DeleteAll()173 } else {174 testSuiteList, err := s.TestsSuitesClient.List(selector)175 if err != nil {176 if !errors.IsNotFound(err) {177 return s.Error(c, http.StatusBadGateway, err)178 }179 } else {180 for _, item := range testSuiteList.Items {181 testSuiteNames = append(testSuiteNames, item.Name)182 }183 }184 err = s.TestsSuitesClient.DeleteByLabels(selector)185 }186 if err != nil {187 if errors.IsNotFound(err) {188 return s.Warn(c, http.StatusNotFound, err)189 }190 return s.Error(c, http.StatusBadGateway, err)191 }192 // delete all executions for tests193 if selector == "" {194 err = s.ExecutionResults.DeleteForAllTestSuites(c.Context())195 } else {196 err = s.ExecutionResults.DeleteByTestSuites(c.Context(), testSuiteNames)197 }198 if err != nil {199 return s.Error(c, http.StatusBadGateway, err)200 }201 // delete all executions for test suites202 if selector == "" {203 err = s.TestExecutionResults.DeleteAll(c.Context())204 } else {205 err = s.TestExecutionResults.DeleteByTestSuites(c.Context(), testSuiteNames)206 }207 if err != nil {208 return s.Error(c, http.StatusBadGateway, err)209 }210 return c.SendStatus(http.StatusNoContent)211 }212}213func (s TestkubeAPI) getFilteredTestSuitesList(c *fiber.Ctx) (*testsuitesv2.TestSuiteList, error) {214 crTestSuites, err := s.TestsSuitesClient.List(c.Query("selector"))215 if err != nil {216 return nil, err217 }218 search := c.Query("textSearch")219 if search != "" {220 // filter items array221 for i := len(crTestSuites.Items) - 1; i >= 0; i-- {222 if !strings.Contains(crTestSuites.Items[i].Name, search) {223 crTestSuites.Items = append(crTestSuites.Items[:i], crTestSuites.Items[i+1:]...)224 }225 }226 }227 return crTestSuites, nil228}229// ListTestSuitesHandler for getting list of all available TestSuites230func (s TestkubeAPI) ListTestSuitesHandler() fiber.Handler {231 return func(c *fiber.Ctx) error {232 crTestSuites, err := s.getFilteredTestSuitesList(c)233 if err != nil {234 return s.Error(c, http.StatusInternalServerError, err)235 }236 testSuites := testsuitesmapper.MapTestSuiteListKubeToAPI(*crTestSuites)237 if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {238 for i := range testSuites {239 if testSuites[i].Description != "" {240 testSuites[i].Description = fmt.Sprintf("%q", testSuites[i].Description)241 }242 }243 data, err := crd.GenerateYAML(crd.TemplateTestSuite, testSuites)244 return s.getCRDs(c, data, err)245 }246 return c.JSON(testSuites)247 }248}249// TestSuiteMetricsHandler returns basic metrics for given testsuite250func (s TestkubeAPI) TestSuiteMetricsHandler() fiber.Handler {251 return func(c *fiber.Ctx) error {252 const (253 DefaultLastDays = 0254 DefaultLimit = 0255 )256 testSuiteName := c.Params("id")257 limit, err := strconv.Atoi(c.Query("limit", strconv.Itoa(DefaultLimit)))258 if err != nil {259 limit = DefaultLimit260 }261 last, err := strconv.Atoi(c.Query("last", strconv.Itoa(DefaultLastDays)))262 if err != nil {263 last = DefaultLastDays264 }265 metrics, err := s.TestExecutionResults.GetTestSuiteMetrics(context.Background(), testSuiteName, limit, last)266 if err != nil {267 return s.Error(c, http.StatusBadGateway, err)268 }269 return c.JSON(metrics)270 }271}272// getLatestTestSuiteExecutions return latest test suite executions either by starttime or endtine for tests273func (s TestkubeAPI) getLatestTestSuiteExecutions(ctx context.Context, testSuiteNames []string) (map[string]testkube.TestSuiteExecution, error) {274 executions, err := s.TestExecutionResults.GetLatestByTestSuites(ctx, testSuiteNames, "starttime")275 if err != nil && err != mongo.ErrNoDocuments {276 return nil, err277 }278 startExecutionMap := make(map[string]testkube.TestSuiteExecution, len(executions))279 for i := range executions {280 if executions[i].TestSuite == nil {281 continue282 }283 startExecutionMap[executions[i].TestSuite.Name] = executions[i]284 }285 executions, err = s.TestExecutionResults.GetLatestByTestSuites(ctx, testSuiteNames, "endtime")286 if err != nil && err != mongo.ErrNoDocuments {287 return nil, err288 }289 endExecutionMap := make(map[string]testkube.TestSuiteExecution, len(executions))290 for i := range executions {291 if executions[i].TestSuite == nil {292 continue293 }294 endExecutionMap[executions[i].TestSuite.Name] = executions[i]295 }296 executionMap := make(map[string]testkube.TestSuiteExecution)297 for _, testSuiteName := range testSuiteNames {298 startExecution, okStart := startExecutionMap[testSuiteName]299 endExecution, okEnd := endExecutionMap[testSuiteName]300 if !okStart && !okEnd {301 continue302 }303 if okStart && !okEnd {304 executionMap[testSuiteName] = startExecution305 continue306 }307 if !okStart && okEnd {308 executionMap[testSuiteName] = endExecution309 continue310 }311 if startExecution.StartTime.After(endExecution.EndTime) {312 executionMap[testSuiteName] = startExecution313 } else {314 executionMap[testSuiteName] = endExecution315 }316 }317 return executionMap, nil318}319// ListTestSuiteWithExecutionsHandler for getting list of all available TestSuite with latest executions320func (s TestkubeAPI) ListTestSuiteWithExecutionsHandler() fiber.Handler {321 return func(c *fiber.Ctx) error {322 crTestSuites, err := s.getFilteredTestSuitesList(c)323 if err != nil {324 return s.Error(c, http.StatusInternalServerError, err)325 }326 testSuites := testsuitesmapper.MapTestSuiteListKubeToAPI(*crTestSuites)327 if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {328 for i := range testSuites {329 if testSuites[i].Description != "" {330 testSuites[i].Description = fmt.Sprintf("%q", testSuites[i].Description)331 }332 }333 data, err := crd.GenerateYAML(crd.TemplateTestSuite, testSuites)334 return s.getCRDs(c, data, err)335 }336 ctx := c.Context()337 results := make([]testkube.TestSuiteWithExecution, 0, len(testSuites))338 testSuiteNames := make([]string, len(testSuites))339 for i := range testSuites {340 testSuiteNames[i] = testSuites[i].Name341 }342 executionMap, err := s.getLatestTestSuiteExecutions(ctx, testSuiteNames)343 if err != nil {344 return s.Error(c, http.StatusInternalServerError, err)345 }346 for i := range testSuites {347 if execution, ok := executionMap[testSuites[i].Name]; ok {348 results = append(results, testkube.TestSuiteWithExecution{349 TestSuite: &testSuites[i],350 LatestExecution: &execution,351 })352 } else {353 results = append(results, testkube.TestSuiteWithExecution{354 TestSuite: &testSuites[i],355 })356 }357 }358 sort.Slice(results, func(i, j int) bool {359 iTime := results[i].TestSuite.Created360 if results[i].LatestExecution != nil {361 iTime = results[i].LatestExecution.EndTime362 if results[i].LatestExecution.StartTime.After(results[i].LatestExecution.EndTime) {363 iTime = results[i].LatestExecution.StartTime364 }365 }366 jTime := results[j].TestSuite.Created367 if results[j].LatestExecution != nil {368 jTime = results[j].LatestExecution.EndTime369 if results[j].LatestExecution.StartTime.After(results[j].LatestExecution.EndTime) {370 jTime = results[j].LatestExecution.StartTime371 }372 }373 return iTime.After(jTime)374 })375 status := c.Query("status")376 if status != "" {377 statusList, err := testkube.ParseTestSuiteExecutionStatusList(status, ",")378 if err != nil {379 return s.Error(c, http.StatusBadRequest, fmt.Errorf("test suite execution status filter invalid: %w", err))380 }381 statusMap := statusList.ToMap()382 // filter items array383 for i := len(results) - 1; i >= 0; i-- {384 if results[i].LatestExecution != nil && results[i].LatestExecution.Status != nil {385 if _, ok := statusMap[*results[i].LatestExecution.Status]; ok {386 continue387 }388 }389 results = append(results[:i], results[i+1:]...)390 }391 }392 return c.JSON(results)393 }394}395func (s TestkubeAPI) ExecuteTestSuitesHandler() fiber.Handler {396 return func(c *fiber.Ctx) error {397 ctx := context.Background()398 var request testkube.TestSuiteExecutionRequest399 err := c.BodyParser(&request)400 if err != nil {401 return s.Error(c, http.StatusBadRequest, fmt.Errorf("test execution request body invalid: %w", err))402 }403 name := c.Params("id")404 selector := c.Query("selector")405 s.Log.Debugw("getting test suite", "name", name, "selector", selector)406 var testSuites []testsuitesv2.TestSuite407 if name != "" {408 testSuite, err := s.TestsSuitesClient.Get(name)409 if err != nil {410 if errors.IsNotFound(err) {411 return s.Warn(c, http.StatusNotFound, err)412 }413 return s.Error(c, http.StatusBadGateway, err)414 }415 testSuites = append(testSuites, *testSuite)416 } else {417 testSuiteList, err := s.TestsSuitesClient.List(selector)418 if err != nil {419 return s.Error(c, http.StatusInternalServerError, fmt.Errorf("can't get test suites: %w", err))420 }421 testSuites = append(testSuites, testSuiteList.Items...)422 }423 var results []testkube.TestSuiteExecution424 if len(testSuites) != 0 {425 concurrencyLevel, err := strconv.Atoi(c.Query("concurrency", defaultConcurrencyLevel))426 if err != nil {427 return s.Error(c, http.StatusBadRequest, fmt.Errorf("can't detect concurrency level: %w", err))428 }429 workerpoolService := workerpool.New[testkube.TestSuite, testkube.TestSuiteExecutionRequest, testkube.TestSuiteExecution](concurrencyLevel)430 go workerpoolService.SendRequests(s.prepareTestSuiteRequests(testSuites, request))431 go workerpoolService.Run(ctx)432 for r := range workerpoolService.GetResponses() {433 results = append(results, r.Result)434 }435 }436 s.Log.Debugw("executing test", "name", name, "selector", selector)437 if name != "" && len(results) != 0 {438 if results[0].IsFailed() {439 return s.Error(c, http.StatusInternalServerError, fmt.Errorf("Test suite failed %v", name))440 }441 c.Status(http.StatusCreated)442 return c.JSON(results[0])443 }444 c.Status(http.StatusCreated)445 return c.JSON(results)446 }447}448func (s TestkubeAPI) prepareTestSuiteRequests(work []testsuitesv2.TestSuite, request testkube.TestSuiteExecutionRequest) []workerpool.Request[449 testkube.TestSuite, testkube.TestSuiteExecutionRequest, testkube.TestSuiteExecution] {450 requests := make([]workerpool.Request[testkube.TestSuite, testkube.TestSuiteExecutionRequest, testkube.TestSuiteExecution], len(work))451 for i := range work {452 requests[i] = workerpool.Request[testkube.TestSuite, testkube.TestSuiteExecutionRequest, testkube.TestSuiteExecution]{453 Object: testsuitesmapper.MapCRToAPI(work[i]),454 Options: request,455 ExecFn: s.executeTestSuite,456 }457 }458 return requests459}460func (s TestkubeAPI) ListTestSuiteExecutionsHandler() fiber.Handler {461 return func(c *fiber.Ctx) error {462 ctx := context.Background()463 filter := getExecutionsFilterFromRequest(c)464 executionsTotals, err := s.TestExecutionResults.GetExecutionsTotals(ctx, filter)465 if err != nil {466 return s.Error(c, http.StatusBadRequest, err)467 }468 allExecutionsTotals, err := s.TestExecutionResults.GetExecutionsTotals(ctx)469 if err != nil {470 return s.Error(c, http.StatusBadRequest, err)471 }472 executions, err := s.TestExecutionResults.GetExecutions(ctx, filter)473 if err != nil {474 return s.Error(c, http.StatusBadRequest, err)475 }476 return c.JSON(testkube.TestSuiteExecutionsResult{477 Totals: &allExecutionsTotals,478 Filtered: &executionsTotals,479 Results: testsuitesmapper.MapToTestExecutionSummary(executions),480 })481 }482}483func (s TestkubeAPI) GetTestSuiteExecutionHandler() fiber.Handler {484 return func(c *fiber.Ctx) error {485 ctx := context.Background()486 id := c.Params("executionID")487 execution, err := s.TestExecutionResults.Get(ctx, id)488 if err == mongo.ErrNoDocuments {489 execution, err = s.TestExecutionResults.GetByName(ctx, id)490 if err == mongo.ErrNoDocuments {491 return s.Error(c, http.StatusNotFound, fmt.Errorf("test suite with execution id/name %s not found", id))492 }493 }494 if err != nil {495 return s.Error(c, http.StatusBadRequest, err)496 }497 execution.Duration = types.FormatDuration(execution.Duration)498 secretMap := make(map[string]string)499 if execution.SecretUUID != "" && execution.TestSuite != nil {500 secretMap, err = s.TestsSuitesClient.GetSecretTestSuiteVars(execution.TestSuite.Name, execution.SecretUUID)501 if err != nil {502 return s.Error(c, http.StatusInternalServerError, err)503 }504 }505 for key, value := range secretMap {506 if variable, ok := execution.Variables[key]; ok && value != "" {507 variable.Value = string(value)508 variable.SecretRef = nil509 execution.Variables[key] = variable510 }511 }512 return c.JSON(execution)513 }514}515// ListTestSuiteTestsHandler for getting list of all available Tests for TestSuites516func (s TestkubeAPI) ListTestSuiteTestsHandler() fiber.Handler {517 return func(c *fiber.Ctx) error {518 name := c.Params("id")519 crTestSuite, err := s.TestsSuitesClient.Get(name)520 if err != nil {521 if errors.IsNotFound(err) {522 return s.Warn(c, http.StatusNotFound, err)523 }524 return s.Error(c, http.StatusBadGateway, err)525 }526 testSuite := testsuitesmapper.MapCRToAPI(*crTestSuite)527 crTests, err := s.TestsClient.ListByNames(testSuite.GetTestNames())528 if err != nil {529 if errors.IsNotFound(err) {530 return s.Warn(c, http.StatusNotFound, err)531 }532 return s.Error(c, http.StatusBadGateway, err)533 }534 return c.JSON(testsmapper.MapTestArrayKubeToAPI(crTests))535 }536}537func (s TestkubeAPI) executeTestSuite(ctx context.Context, testSuite testkube.TestSuite, request testkube.TestSuiteExecutionRequest) (538 testsuiteExecution testkube.TestSuiteExecution, err error) {539 s.Log.Debugw("Got test to execute", "test", testSuite)540 secretUUID, err := s.TestsSuitesClient.GetCurrentSecretUUID(testSuite.Name)541 if err != nil {542 return testsuiteExecution, err543 }544 request.SecretUUID = secretUUID545 if testSuite.ExecutionRequest != nil {546 if request.Name == "" && testSuite.ExecutionRequest.Name != "" {547 request.Name = testSuite.ExecutionRequest.Name548 }549 if request.HttpProxy == "" && testSuite.ExecutionRequest.HttpProxy != "" {550 request.HttpProxy = testSuite.ExecutionRequest.HttpProxy551 }552 if request.HttpsProxy == "" && testSuite.ExecutionRequest.HttpsProxy != "" {553 request.HttpsProxy = testSuite.ExecutionRequest.HttpsProxy554 }555 }556 request.Number = s.getNextExecutionNumber("ts-" + testSuite.Name)557 if request.Name == "" {558 request.Name = fmt.Sprintf("ts-%s-%d", testSuite.Name, request.Number)559 }560 testsuiteExecution = testkube.NewStartedTestSuiteExecution(testSuite, request)561 err = s.TestExecutionResults.Insert(ctx, testsuiteExecution)562 if err != nil {563 s.Log.Infow("Inserting test execution", "error", err)564 }565 s.Events.Notify(testkube.NewEventStartTestSuite(&testsuiteExecution))566 var wg sync.WaitGroup567 wg.Add(1)568 go func(testsuiteExecution *testkube.TestSuiteExecution, request testkube.TestSuiteExecutionRequest) {569 defer func(testExecution *testkube.TestSuiteExecution) {570 duration := testExecution.CalculateDuration()571 testExecution.EndTime = time.Now()572 testExecution.Duration = duration.String()573 err = s.TestExecutionResults.EndExecution(ctx, testExecution.Id, testExecution.EndTime, duration)574 if err != nil {575 s.Log.Errorw("error setting end time", "error", err.Error())576 }577 wg.Done()578 }(testsuiteExecution)579 hasFailedSteps := false580 cancellSteps := false581 for i := range testsuiteExecution.StepResults {582 if cancellSteps {583 testsuiteExecution.StepResults[i].Execution.ExecutionResult.Cancel()584 continue585 }586 // start execution of given step587 testsuiteExecution.StepResults[i].Execution.ExecutionResult.InProgress()588 err = s.TestExecutionResults.Update(ctx, *testsuiteExecution)589 if err != nil {590 s.Log.Infow("Updating test execution", "error", err)591 }592 s.executeTestStep(ctx, *testsuiteExecution, request, &testsuiteExecution.StepResults[i])593 err := s.TestExecutionResults.Update(ctx, *testsuiteExecution)594 if err != nil {595 hasFailedSteps = true596 s.Log.Errorw("saving test suite execution results error", "error", err)597 continue598 }599 if testsuiteExecution.StepResults[i].IsFailed() {600 hasFailedSteps = true601 if testsuiteExecution.StepResults[i].Step.StopTestOnFailure {602 cancellSteps = true603 continue604 }605 }606 }607 testsuiteExecution.Status = testkube.TestSuiteExecutionStatusPassed608 if hasFailedSteps {609 testsuiteExecution.Status = testkube.TestSuiteExecutionStatusFailed610 s.Events.Notify(testkube.NewEventEndTestSuiteFailed(testsuiteExecution))611 } else {612 s.Events.Notify(testkube.NewEventEndTestSuiteSuccess(testsuiteExecution))613 }614 s.Metrics.IncExecuteTestSuite(*testsuiteExecution)615 err := s.TestExecutionResults.Update(ctx, *testsuiteExecution)616 if err != nil {617 s.Log.Errorw("saving final test suite execution result error", "error", err)618 }619 }(&testsuiteExecution, request)620 // wait for sync test suite execution621 if request.Sync {622 wg.Wait()623 }624 return testsuiteExecution, nil625}626func (s TestkubeAPI) executeTestStep(ctx context.Context, testsuiteExecution testkube.TestSuiteExecution,627 request testkube.TestSuiteExecutionRequest, result *testkube.TestSuiteStepExecutionResult) {628 var testSuiteName string629 if testsuiteExecution.TestSuite != nil {630 testSuiteName = testsuiteExecution.TestSuite.Name631 }632 step := result.Step633 l := s.Log.With("type", step.Type(), "testSuiteName", testSuiteName, "name", step.FullName())634 switch step.Type() {635 case testkube.TestSuiteStepTypeExecuteTest:636 executeTestStep := step.Execute637 request := testkube.ExecutionRequest{638 Name: fmt.Sprintf("%s-%s", testSuiteName, executeTestStep.Name),639 TestSuiteName: testSuiteName,640 Namespace: executeTestStep.Namespace,641 Variables: testsuiteExecution.Variables,642 TestSuiteSecretUUID: request.SecretUUID,643 Sync: true,644 HttpProxy: request.HttpProxy,645 HttpsProxy: request.HttpsProxy,646 ExecutionLabels: request.ExecutionLabels,647 }648 l.Info("executing test", "variables", testsuiteExecution.Variables, "request", request)649 execution, err := s.executeTest(ctx, testkube.Test{Name: executeTestStep.Name}, request)650 if err != nil {651 result.Err(err)652 return653 }654 result.Execution = &execution655 case testkube.TestSuiteStepTypeDelay:656 l.Debug("delaying execution")657 time.Sleep(time.Millisecond * time.Duration(step.Delay.Duration))658 result.Execution.ExecutionResult.Success()659 default:660 result.Err(fmt.Errorf("can't find handler for execution step type: '%v'", step.Type()))661 }662}663func getExecutionsFilterFromRequest(c *fiber.Ctx) testresult.Filter {664 filter := testresult.NewExecutionsFilter()665 name := c.Query("id", "")666 if name != "" {667 filter = filter.WithName(name)668 }669 textSearch := c.Query("textSearch", "")670 if textSearch != "" {671 filter = filter.WithTextSearch(textSearch)672 }673 page, err := strconv.Atoi(c.Query("page", ""))674 if err == nil {675 filter = filter.WithPage(page)676 }677 pageSize, err := strconv.Atoi(c.Query("pageSize", ""))678 if err == nil && pageSize != 0 {679 filter = filter.WithPageSize(pageSize)680 }681 status := c.Query("status", "")682 if status != "" {683 filter = filter.WithStatus(status)684 }685 last, err := strconv.Atoi(c.Query("last", "0"))686 if err == nil && last != 0 {687 filter = filter.WithLastNDays(last)688 }689 dFilter := datefilter.NewDateFilter(c.Query("startDate", ""), c.Query("endDate", ""))690 if dFilter.IsStartValid {691 filter = filter.WithStartDate(dFilter.Start)692 }693 if dFilter.IsEndValid {694 filter = filter.WithEndDate(dFilter.End)695 }696 selector := c.Query("selector")697 if selector != "" {698 filter = filter.WithSelector(selector)699 }700 return filter701}...
mongo.go
Source:mongo.go
...149func (r *MongoRepository) Update(ctx context.Context, result testkube.TestSuiteExecution) (err error) {150 _, err = r.Coll.ReplaceOne(ctx, bson.M{"id": result.Id}, result)151 return152}153// StartExecution updates execution start time154func (r *MongoRepository) StartExecution(ctx context.Context, id string, startTime time.Time) (err error) {155 _, err = r.Coll.UpdateOne(ctx, bson.M{"id": id}, bson.M{"$set": bson.M{"starttime": startTime}})156 return157}158// EndExecution updates execution end time159func (r *MongoRepository) EndExecution(ctx context.Context, id string, endTime time.Time, duration time.Duration) (err error) {160 _, err = r.Coll.UpdateOne(ctx, bson.M{"id": id}, bson.M{"$set": bson.M{"endtime": endTime, "duration": duration.String()}})161 return162}163func composeQueryAndOpts(filter Filter) (bson.M, *options.FindOptions) {164 query := bson.M{}165 opts := options.Find()166 startTimeQuery := bson.M{}167 if filter.NameDefined() {168 query["testsuite.name"] = filter.Name()...
interface.go
Source:interface.go
...40 // Insert inserts new execution result41 Insert(ctx context.Context, result testkube.TestSuiteExecution) error42 // Update updates execution result43 Update(ctx context.Context, result testkube.TestSuiteExecution) error44 // StartExecution updates execution start time45 StartExecution(ctx context.Context, id string, startTime time.Time) error46 // EndExecution updates execution end time47 EndExecution(ctx context.Context, id string, endTime time.Time, duration time.Duration) error48 // DeleteByTestSuite deletes execution results by test suite49 DeleteByTestSuite(ctx context.Context, testSuiteName string) error50 // DeleteAll deletes all execution results51 DeleteAll(ctx context.Context) error52 // DeleteByTestSuites deletes execution results by test suites53 DeleteByTestSuites(ctx context.Context, testSuiteNames []string) (err error)54 GetTestSuiteMetrics(ctx context.Context, name string, limit, last int) (metrics testkube.ExecutionsMetrics, err error)55}...
StartExecution
Using AI Code Generation
1func main() {2 testresult.StartExecution()3}4func main() {5 testresult.EndExecution()6}7func main() {8 testresult.StartTest()9}10func main() {11 testresult.EndTest()12}13func main() {14 testresult.LogResult()15}16func main() {17 testresult.LogResult()18}19func main() {20 testresult.LogResult()21}22func main() {23 testresult.LogResult()24}25func main() {26 testresult.LogResult()27}28func main() {29 testresult.LogResult()30}31func main() {32 testresult.LogResult()33}34func main() {35 testresult.LogResult()36}37func main() {38 testresult.LogResult()39}
StartExecution
Using AI Code Generation
1import (2func main() {3 tr.StartExecution()4}5import (6func main() {7 tr.EndExecution()8}9import (10func main() {11 tr.AddTestResult("Test1", "Pass", "Test1 executed successfully")12}13import (14func main() {15 tr.UpdateTestResult("Test1", "Fail", "Test1 failed")16}17import (18func main() {19 tr.GetTestResult("Test1")20}21import (22func main() {23 tr.GetTestResult("Test1")24}25import (26func main() {27 tr.GetTestResult("Test1")28}29import (30func main() {31 tr.GetTestResult("Test1")32}33import (34func main() {35 tr.GetTestResult("Test1")36}37import (38func main() {
StartExecution
Using AI Code Generation
1import (2func main() {3 t.StartExecution()4}5import (6type TestResult struct {7}8func (t TestResult) StartExecution() {9 fmt.Println("Test execution started")10}11./1.go:10: t.StartExecution undefined (type TestResult has no field or method StartExecution)12./1.go:10: t.StartExecution undefined (type TestResult has no field or method StartExecution)13import (14type TestResult struct {15}16func (t TestResult) StartExecution() {17 fmt.Println("Test execution started")18}
StartExecution
Using AI Code Generation
1import (2func main() {3 testresult.StartExecution()4 fmt.Println("Hello, playground")5 testresult.EndExecution()6}7import (8func StartExecution() {9 startTime = time.Now()10 fmt.Println("Execution Started at ", startTime)11}12func EndExecution() {13 endTime := time.Now()14 fmt.Println("Execution Ended at ", endTime)15 fmt.Println("Total Time Taken ", endTime.Sub(startTime))16}
StartExecution
Using AI Code Generation
1import (2func main() {3 testresult := testresult.NewTestResult()4 testresult.StartExecution()5 time.Sleep(5 * time.Second)6 testresult.EndExecution()7 executionId := testresult.GetExecutionId()8 fmt.Println("Execution ID : ", executionId)9 executionUrl := testresult.GetExecutionUrl()10 fmt.Println("Execution URL : ", executionUrl)11}12import (13func main() {14 testresult := testresult.NewTestResult()15 testresult.StartExecution()16 testresult.StartTest("Test 1")17 time.Sleep(5 * time.Second)18 testresult.EndTest()19 testresult.EndExecution()20 executionId := testresult.GetExecutionId()21 fmt.Println("Execution ID : ", executionId)22 executionUrl := testresult.GetExecutionUrl()23 fmt.Println("Execution URL : ", executionUrl)24}25import (26func main() {27 testresult := testresult.NewTestResult()28 testresult.StartExecution()29 testresult.StartTest("Test 1")30 testresult.StartStep("Step 1")
StartExecution
Using AI Code Generation
1import (2func main() {3 c := cron.New()4 c.AddFunc("*/5 * * * * *", func() {5 fmt.Println("cron running")6 testresult.StartExecution()7 })8 c.Start()9 time.Sleep(5 * time.Second)10}11import (12func StartExecution() {13 cmd := exec.Command("java", "-cp", ".", "TestResult")14 cmd.Env = append(cmd.Env, "CLASSPATH=.:postgresql-9.4.1212.jar")15 err := cmd.Run()16 if err != nil {17 fmt.Println("Error in execution")18 }19}20import (21func main() {22 c := cron.New()23 c.AddFunc("*/5 * * * * *", func() {24 fmt.Println("cron running")25 testresult.StartExecution()26 })27 c.Start()28 time.Sleep(5 * time.Second)29}
StartExecution
Using AI Code Generation
1using Microsoft.VisualStudio.TestTools.UnitTesting;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 public void TestMethod()10 {11 var testResult = new TestResult();12 testResult.StartExecution();13 testResult.AddStep("step 1", "step 1 description", TestStatus.Pass);14 testResult.AddStep("step 2", "step 2 description", TestStatus.Pass);15 testResult.EndExecution();16 testResult.AddStep("step 3", "step 3 description", TestStatus.Pass);17 }18 }19}
StartExecution
Using AI Code Generation
1import "fmt"2func main() {3fmt.Println("Hello, playground")4}5import "fmt"6func StartExecution() {7fmt.Println("Hello, playground")8}9/usr/local/go/src/github.com/rohitkumar2492/testresult (from $GOROOT)10/home/rohit/go/src/github.com/rohitkumar2492/testresult (from $GOPATH)11Click to share on Telegram (Opens in new window)
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!!