How to use EndExecution method of result Package

Best Testkube code snippet using result.EndExecution

tests.go

Source:tests.go Github

copy

Full Screen

1package v12import (3 "context"4 "fmt"5 "net/http"6 "sort"7 "strconv"8 "strings"9 "github.com/gofiber/fiber/v2"10 testsv3 "github.com/kubeshop/testkube-operator/apis/tests/v3"11 "github.com/kubeshop/testkube/pkg/api/v1/testkube"12 "github.com/kubeshop/testkube/pkg/crd"13 "github.com/kubeshop/testkube/pkg/executor/client"14 testsmapper "github.com/kubeshop/testkube/pkg/mapper/tests"15 "github.com/kubeshop/testkube/pkg/secret"16 "go.mongodb.org/mongo-driver/mongo"17 "k8s.io/apimachinery/pkg/api/errors"18)19// GetTestHandler is method for getting an existing test20func (s TestkubeAPI) GetTestHandler() fiber.Handler {21 return func(c *fiber.Ctx) error {22 name := c.Params("id")23 crTest, err := s.TestsClient.Get(name)24 if err != nil {25 if errors.IsNotFound(err) {26 return s.Error(c, http.StatusNotFound, err)27 }28 return s.Error(c, http.StatusBadGateway, err)29 }30 test := testsmapper.MapTestCRToAPI(*crTest)31 if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {32 if test.Content != nil && test.Content.Data != "" {33 test.Content.Data = fmt.Sprintf("%q", test.Content.Data)34 }35 if test.ExecutionRequest != nil && test.ExecutionRequest.VariablesFile != "" {36 test.ExecutionRequest.VariablesFile = fmt.Sprintf("%q", test.ExecutionRequest.VariablesFile)37 }38 data, err := crd.GenerateYAML(crd.TemplateTest, []testkube.Test{test})39 return s.getCRDs(c, data, err)40 }41 return c.JSON(test)42 }43}44// GetTestWithExecutionHandler is method for getting an existing test with execution45func (s TestkubeAPI) GetTestWithExecutionHandler() fiber.Handler {46 return func(c *fiber.Ctx) error {47 name := c.Params("id")48 crTest, err := s.TestsClient.Get(name)49 if err != nil {50 if errors.IsNotFound(err) {51 return s.Error(c, http.StatusNotFound, err)52 }53 return s.Error(c, http.StatusBadGateway, err)54 }55 test := testsmapper.MapTestCRToAPI(*crTest)56 if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {57 if test.Content != nil && test.Content.Data != "" {58 test.Content.Data = fmt.Sprintf("%q", test.Content.Data)59 }60 if test.ExecutionRequest != nil && test.ExecutionRequest.VariablesFile != "" {61 test.ExecutionRequest.VariablesFile = fmt.Sprintf("%q", test.ExecutionRequest.VariablesFile)62 }63 data, err := crd.GenerateYAML(crd.TemplateTest, []testkube.Test{test})64 return s.getCRDs(c, data, err)65 }66 ctx := c.Context()67 startExecution, startErr := s.ExecutionResults.GetLatestByTest(ctx, name, "starttime")68 if startErr != nil && startErr != mongo.ErrNoDocuments {69 return s.Error(c, http.StatusInternalServerError, startErr)70 }71 endExecution, endErr := s.ExecutionResults.GetLatestByTest(ctx, name, "endtime")72 if endErr != nil && endErr != mongo.ErrNoDocuments {73 return s.Error(c, http.StatusInternalServerError, endErr)74 }75 testWithExecution := testkube.TestWithExecution{76 Test: &test,77 }78 if startErr == nil && endErr == nil {79 if startExecution.StartTime.After(endExecution.EndTime) {80 testWithExecution.LatestExecution = &startExecution81 } else {82 testWithExecution.LatestExecution = &endExecution83 }84 } else if startErr == nil {85 testWithExecution.LatestExecution = &startExecution86 } else if endErr == nil {87 testWithExecution.LatestExecution = &endExecution88 }89 return c.JSON(testWithExecution)90 }91}92func (s TestkubeAPI) getFilteredTestList(c *fiber.Ctx) (*testsv3.TestList, error) {93 crTests, err := s.TestsClient.List(c.Query("selector"))94 if err != nil {95 return nil, err96 }97 search := c.Query("textSearch")98 if search != "" {99 // filter items array100 for i := len(crTests.Items) - 1; i >= 0; i-- {101 if !strings.Contains(crTests.Items[i].Name, search) {102 crTests.Items = append(crTests.Items[:i], crTests.Items[i+1:]...)103 }104 }105 }106 testType := c.Query("type")107 if testType != "" {108 // filter items array109 for i := len(crTests.Items) - 1; i >= 0; i-- {110 if !strings.Contains(crTests.Items[i].Spec.Type_, testType) {111 crTests.Items = append(crTests.Items[:i], crTests.Items[i+1:]...)112 }113 }114 }115 return crTests, nil116}117// ListTestsHandler is a method for getting list of all available tests118func (s TestkubeAPI) ListTestsHandler() fiber.Handler {119 return func(c *fiber.Ctx) error {120 crTests, err := s.getFilteredTestList(c)121 if err != nil {122 return s.Error(c, http.StatusBadGateway, err)123 }124 tests := testsmapper.MapTestListKubeToAPI(*crTests)125 if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {126 for i := range tests {127 if tests[i].Content != nil && tests[i].Content.Data != "" {128 tests[i].Content.Data = fmt.Sprintf("%q", tests[i].Content.Data)129 }130 if tests[i].ExecutionRequest != nil && tests[i].ExecutionRequest.VariablesFile != "" {131 tests[i].ExecutionRequest.VariablesFile = fmt.Sprintf("%q", tests[i].ExecutionRequest.VariablesFile)132 }133 }134 data, err := crd.GenerateYAML(crd.TemplateTest, tests)135 return s.getCRDs(c, data, err)136 }137 return c.JSON(tests)138 }139}140// ListTestsHandler is a method for getting list of all available tests141func (s TestkubeAPI) TestMetricsHandler() fiber.Handler {142 return func(c *fiber.Ctx) error {143 testName := c.Params("id")144 const DefaultLimit = 0145 limit, err := strconv.Atoi(c.Query("limit", strconv.Itoa(DefaultLimit)))146 if err != nil {147 limit = DefaultLimit148 }149 const DefaultLastDays = 7150 last, err := strconv.Atoi(c.Query("last", strconv.Itoa(DefaultLastDays)))151 if err != nil {152 last = DefaultLastDays153 }154 metrics, err := s.ExecutionResults.GetTestMetrics(context.Background(), testName, limit, last)155 if err != nil {156 return s.Error(c, http.StatusBadGateway, err)157 }158 return c.JSON(metrics)159 }160}161// getLatestExecutions return latest executions either by starttime or endtime for tests162func (s TestkubeAPI) getLatestExecutions(ctx context.Context, testNames []string) (map[string]testkube.Execution, error) {163 executions, err := s.ExecutionResults.GetLatestByTests(ctx, testNames, "starttime")164 if err != nil && err != mongo.ErrNoDocuments {165 return nil, err166 }167 startExecutionMap := make(map[string]testkube.Execution, len(executions))168 for i := range executions {169 startExecutionMap[executions[i].TestName] = executions[i]170 }171 executions, err = s.ExecutionResults.GetLatestByTests(ctx, testNames, "endtime")172 if err != nil && err != mongo.ErrNoDocuments {173 return nil, err174 }175 endExecutionMap := make(map[string]testkube.Execution, len(executions))176 for i := range executions {177 endExecutionMap[executions[i].TestName] = executions[i]178 }179 executionMap := make(map[string]testkube.Execution)180 for _, testName := range testNames {181 startExecution, okStart := startExecutionMap[testName]182 endExecution, okEnd := endExecutionMap[testName]183 if !okStart && !okEnd {184 continue185 }186 if okStart && !okEnd {187 executionMap[testName] = startExecution188 continue189 }190 if !okStart && okEnd {191 executionMap[testName] = endExecution192 continue193 }194 if startExecution.StartTime.After(endExecution.EndTime) {195 executionMap[testName] = startExecution196 } else {197 executionMap[testName] = endExecution198 }199 }200 return executionMap, nil201}202// ListTestWithExecutionsHandler is a method for getting list of all available test with latest executions203func (s TestkubeAPI) ListTestWithExecutionsHandler() fiber.Handler {204 return func(c *fiber.Ctx) error {205 crTests, err := s.getFilteredTestList(c)206 if err != nil {207 return s.Error(c, http.StatusBadGateway, err)208 }209 tests := testsmapper.MapTestListKubeToAPI(*crTests)210 if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {211 for i := range tests {212 if tests[i].Content != nil && tests[i].Content.Data != "" {213 tests[i].Content.Data = fmt.Sprintf("%q", tests[i].Content.Data)214 }215 if tests[i].ExecutionRequest != nil && tests[i].ExecutionRequest.VariablesFile != "" {216 tests[i].ExecutionRequest.VariablesFile = fmt.Sprintf("%q", tests[i].ExecutionRequest.VariablesFile)217 }218 }219 data, err := crd.GenerateYAML(crd.TemplateTest, tests)220 return s.getCRDs(c, data, err)221 }222 ctx := c.Context()223 results := make([]testkube.TestWithExecution, 0, len(tests))224 testNames := make([]string, len(tests))225 for i := range tests {226 testNames[i] = tests[i].Name227 }228 executionMap, err := s.getLatestExecutions(ctx, testNames)229 if err != nil {230 return s.Error(c, http.StatusInternalServerError, err)231 }232 for i := range tests {233 if execution, ok := executionMap[tests[i].Name]; ok {234 results = append(results, testkube.TestWithExecution{235 Test: &tests[i],236 LatestExecution: &execution,237 })238 } else {239 results = append(results, testkube.TestWithExecution{240 Test: &tests[i],241 })242 }243 }244 sort.Slice(results, func(i, j int) bool {245 iTime := results[i].Test.Created246 if results[i].LatestExecution != nil {247 iTime = results[i].LatestExecution.EndTime248 if results[i].LatestExecution.StartTime.After(results[i].LatestExecution.EndTime) {249 iTime = results[i].LatestExecution.StartTime250 }251 }252 jTime := results[j].Test.Created253 if results[j].LatestExecution != nil {254 jTime = results[j].LatestExecution.EndTime255 if results[j].LatestExecution.StartTime.After(results[j].LatestExecution.EndTime) {256 jTime = results[j].LatestExecution.StartTime257 }258 }259 return iTime.After(jTime)260 })261 status := c.Query("status")262 if status != "" {263 statusList, err := testkube.ParseExecutionStatusList(status, ",")264 if err != nil {265 return s.Error(c, http.StatusBadRequest, fmt.Errorf("execution status filter invalid: %w", err))266 }267 statusMap := statusList.ToMap()268 // filter items array269 for i := len(results) - 1; i >= 0; i-- {270 if results[i].LatestExecution != nil && results[i].LatestExecution.ExecutionResult != nil &&271 results[i].LatestExecution.ExecutionResult.Status != nil {272 if _, ok := statusMap[*results[i].LatestExecution.ExecutionResult.Status]; ok {273 continue274 }275 }276 results = append(results[:i], results[i+1:]...)277 }278 }279 return c.JSON(results)280 }281}282// CreateTestHandler creates new test CR based on test content283func (s TestkubeAPI) CreateTestHandler() fiber.Handler {284 return func(c *fiber.Ctx) error {285 var request testkube.TestUpsertRequest286 err := c.BodyParser(&request)287 if err != nil {288 return s.Error(c, http.StatusBadRequest, err)289 }290 if c.Accepts(mediaTypeJSON, mediaTypeYAML) == mediaTypeYAML {291 if request.Content != nil && request.Content.Data != "" {292 request.Content.Data = fmt.Sprintf("%q", request.Content.Data)293 }294 if request.ExecutionRequest != nil && request.ExecutionRequest.VariablesFile != "" {295 request.ExecutionRequest.VariablesFile = fmt.Sprintf("%q", request.ExecutionRequest.VariablesFile)296 }297 data, err := crd.GenerateYAML(crd.TemplateTest, []testkube.TestUpsertRequest{request})298 return s.getCRDs(c, data, err)299 }300 s.Log.Infow("creating test", "request", request)301 testSpec := testsmapper.MapToSpec(request)302 testSpec.Namespace = s.Namespace303 test, err := s.TestsClient.Create(testSpec)304 s.Metrics.IncCreateTest(test.Spec.Type_, err)305 if err != nil {306 return s.Error(c, http.StatusBadGateway, err)307 }308 stringData := GetSecretsStringData(request.Content)309 if err = s.SecretClient.Create(secret.GetMetadataName(request.Name), test.Labels, stringData); err != nil {310 return s.Error(c, http.StatusBadGateway, err)311 }312 c.Status(http.StatusCreated)313 return c.JSON(test)314 }315}316// UpdateTestHandler updates an existing test CR based on test content317func (s TestkubeAPI) UpdateTestHandler() fiber.Handler {318 return func(c *fiber.Ctx) error {319 var request testkube.TestUpsertRequest320 err := c.BodyParser(&request)321 if err != nil {322 return s.Error(c, http.StatusBadRequest, err)323 }324 s.Log.Infow("updating test", "request", request)325 // we need to get resource first and load its metadata.ResourceVersion326 test, err := s.TestsClient.Get(request.Name)327 if err != nil {328 return s.Error(c, http.StatusBadGateway, err)329 }330 // map test but load spec only to not override metadata.ResourceVersion331 testSpec := testsmapper.MapToSpec(request)332 test.Spec = testSpec.Spec333 test.Labels = request.Labels334 test, err = s.TestsClient.Update(test)335 s.Metrics.IncUpdateTest(test.Spec.Type_, err)336 if err != nil {337 return s.Error(c, http.StatusBadGateway, err)338 }339 // update secrets for scipt340 stringData := GetSecretsStringData(request.Content)341 if err = s.SecretClient.Apply(secret.GetMetadataName(request.Name), test.Labels, stringData); err != nil {342 return s.Error(c, http.StatusBadGateway, err)343 }344 return c.JSON(test)345 }346}347// DeleteTestHandler is a method for deleting a test with id348func (s TestkubeAPI) DeleteTestHandler() fiber.Handler {349 return func(c *fiber.Ctx) error {350 name := c.Params("id")351 err := s.TestsClient.Delete(name)352 if err != nil {353 if errors.IsNotFound(err) {354 return s.Warn(c, http.StatusNotFound, err)355 }356 return s.Error(c, http.StatusBadGateway, err)357 }358 // delete secrets for test359 if err = s.SecretClient.Delete(secret.GetMetadataName(name)); err != nil {360 if !errors.IsNotFound(err) {361 return s.Error(c, http.StatusBadGateway, err)362 }363 }364 // delete executions for test365 if err = s.ExecutionResults.DeleteByTest(c.Context(), name); err != nil {366 return s.Error(c, http.StatusBadGateway, err)367 }368 return c.SendStatus(http.StatusNoContent)369 }370}371// DeleteTestsHandler for deleting all tests372func (s TestkubeAPI) DeleteTestsHandler() fiber.Handler {373 return func(c *fiber.Ctx) error {374 var err error375 var testNames []string376 selector := c.Query("selector")377 if selector == "" {378 err = s.TestsClient.DeleteAll()379 } else {380 testList, err := s.TestsClient.List(selector)381 if err != nil {382 if !errors.IsNotFound(err) {383 return s.Error(c, http.StatusBadGateway, err)384 }385 } else {386 for _, item := range testList.Items {387 testNames = append(testNames, item.Name)388 }389 }390 err = s.TestsClient.DeleteByLabels(selector)391 }392 if err != nil {393 if errors.IsNotFound(err) {394 return s.Warn(c, http.StatusNotFound, err)395 }396 return s.Error(c, http.StatusBadGateway, err)397 }398 // delete all secrets for tests399 if err = s.SecretClient.DeleteAll(selector); err != nil {400 if !errors.IsNotFound(err) {401 return s.Error(c, http.StatusBadGateway, err)402 }403 }404 // delete all executions for tests405 if selector == "" {406 err = s.ExecutionResults.DeleteAll(c.Context())407 } else {408 err = s.ExecutionResults.DeleteByTests(c.Context(), testNames)409 }410 if err != nil {411 return s.Error(c, http.StatusBadGateway, err)412 }413 return c.SendStatus(http.StatusNoContent)414 }415}416func GetSecretsStringData(content *testkube.TestContent) map[string]string {417 // create secrets for test418 stringData := map[string]string{client.GitUsernameSecretName: "", client.GitTokenSecretName: ""}419 if content != nil && content.Repository != nil {420 stringData[client.GitUsernameSecretName] = content.Repository.Username421 stringData[client.GitTokenSecretName] = content.Repository.Token422 }423 return stringData424}...

Full Screen

Full Screen

interface.go

Source:interface.go Github

copy

Full Screen

...42 // 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}...

Full Screen

Full Screen

EndExecution

Using AI Code Generation

copy

Full Screen

1import (2type SimpleChaincode struct {3}4func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response {5 return shim.Success(nil)6}7func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {8 function, args := stub.GetFunctionAndParameters()9 if function == "invoke" {10 return t.invoke(stub, args)11 } else if function == "query" {12 return t.query(stub, args)13 }14 return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"query\"")15}16func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) peer.Response {17 if args[0] == "a" {18 fmt.Println("invoke", A, B, X)19 err := stub.PutState(A, []byte(X))20 if err != nil {21 return shim.Error(err.Error())22 }23 return shim.Success(nil)24 } else if args[0] == "b" {25 fmt.Println("invoke", A, B, X)26 err := stub.PutState(B, []byte(X))27 if err != nil {28 return shim.Error(err.Error())29 }30 return shim.Success(nil)31 }32 return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"query\"")33}34func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) peer.Response {35 if args[0] == "a" {36 fmt.Println("query", A,

Full Screen

Full Screen

EndExecution

Using AI Code Generation

copy

Full Screen

1import (2type SimpleChaincode struct {3}4func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) peer.Response {5 return shim.Success(nil)6}7func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) peer.Response {8 function, args := stub.GetFunctionAndParameters()9 if function == "invoke" {10 return t.invoke(stub, args)11 }12 return shim.Error("Invalid invoke function name. Expecting \"invoke\"")13}14func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) peer.Response {15 if len(args) != 2 {16 return shim.Error("Incorrect number of arguments. Expecting 2")17 }18 err := stub.PutState(args[0], []byte(args[1]))19 if err != nil {20 return shim.Error(fmt.Sprintf("Failed to create asset: %s", args[0]))21 }22 return shim.Success(nil)23}24func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) peer.Response {25 if len(args) != 1 {26 return shim.Error("Incorrect number of arguments. Expecting name of the person to query")27 }28 value, err := stub.GetState(args[0])29 if err != nil {30 return shim.Error("Could not locate person")31 }32 return shim.Success(value)33}34func main() {35 err := shim.Start(new(SimpleChaincode))36 if err != nil {37 fmt.Printf("Error starting Simple chaincode: %s", err)38 }39}

Full Screen

Full Screen

EndExecution

Using AI Code Generation

copy

Full Screen

1func main() {2 var result = new Result()3 result.EndExecution()4}5func main() {6 var result = new Result()7 result.EndExecution()8}9func main() {10 var result = new Result()11 result.EndExecution()12}13func main() {14 var result = new Result()15 result.EndExecution()16}17func main() {18 var result = new Result()19 result.EndExecution()20}21func main() {22 var result = new Result()23 result.EndExecution()24}25func main() {26 var result = new Result()27 result.EndExecution()28}29func main() {30 var result = new Result()31 result.EndExecution()32}33func main() {34 var result = new Result()35 result.EndExecution()36}37func main() {38 var result = new Result()39 result.EndExecution()40}41func main() {42 var result = new Result()43 result.EndExecution()44}45func main() {46 var result = new Result()47 result.EndExecution()48}49func main() {50 var result = new Result()51 result.EndExecution()52}53func main() {54 var result = new Result()55 result.EndExecution()56}57func main() {

Full Screen

Full Screen

EndExecution

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 result := NewResult()4 result.EndExecution()5 fmt.Println(reflect.ValueOf(result))6}7Result{status:END, message:execution completed successfully}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful