Best Testkube code snippet using v1.GetByNameAndTest
executions.go
Source:executions.go
...102 }103 request.Number = s.getNextExecutionNumber(test.Name)104 request.Name = fmt.Sprintf("%s-%d", request.Name, request.Number)105 // test name + test execution name should be unique106 execution, _ = s.ExecutionResults.GetByNameAndTest(ctx, request.Name, test.Name)107 if execution.Name == request.Name {108 return execution.Err(fmt.Errorf("test execution with name %s already exists", request.Name)), nil109 }110 secretUUID, err := s.TestsClient.GetCurrentSecretUUID(test.Name)111 if err != nil {112 return execution.Errw("can't get current secret uuid: %w", err), nil113 }114 request.TestSecretUUID = secretUUID115 // merge available data into execution options test spec, executor spec, request, test id116 options, err := s.GetExecuteOptions(test.Namespace, test.Name, request)117 if err != nil {118 return execution.Errw("can't create valid execution options: %w", err), nil119 }120 // store execution in storage, can be get from API now121 execution = newExecutionFromExecutionOptions(options)122 options.ID = execution.Id123 if err := s.createSecretsReferences(&execution); err != nil {124 return execution.Errw("can't create secret variables `Secret` references: %w", err), nil125 }126 err = s.ExecutionResults.Insert(ctx, execution)127 if err != nil {128 return execution.Errw("can't create new test execution, can't insert into storage: %w", err), nil129 }130 s.Log.Infow("calling executor with options", "options", options.Request)131 execution.Start()132 s.Events.Notify(testkube.NewEventStartTest(&execution))133 // update storage with current execution status134 err = s.ExecutionResults.StartExecution(ctx, execution.Id, execution.StartTime)135 if err != nil {136 s.Events.Notify(testkube.NewEventEndTestFailed(&execution))137 return execution.Errw("can't execute test, can't insert into storage error: %w", err), nil138 }139 options.HasSecrets = true140 if _, err = s.SecretClient.Get(secret.GetMetadataName(execution.TestName)); err != nil {141 if !errors.IsNotFound(err) {142 s.Events.Notify(testkube.NewEventEndTestFailed(&execution))143 return execution.Errw("can't get secrets: %w", err), nil144 }145 options.HasSecrets = false146 }147 var result testkube.ExecutionResult148 // sync/async test execution149 if options.Sync {150 result, err = s.Executor.ExecuteSync(&execution, options)151 } else {152 result, err = s.Executor.Execute(&execution, options)153 }154 // set execution result to one created155 execution.ExecutionResult = &result156 // update storage with current execution status157 if uerr := s.ExecutionResults.UpdateResult(ctx, execution.Id, result); uerr != nil {158 s.Events.Notify(testkube.NewEventEndTestFailed(&execution))159 return execution.Errw("update execution error: %w", uerr), nil160 }161 if err != nil {162 s.Events.Notify(testkube.NewEventEndTestFailed(&execution))163 return execution.Errw("test execution failed: %w", err), nil164 }165 s.Log.Infow("test started", "executionId", execution.Id, "status", execution.ExecutionResult.Status)166 // notify immediately onlly when sync run otherwise job results handler need notify about test finish167 if options.Sync && execution.ExecutionResult != nil && *execution.ExecutionResult.Status != testkube.RUNNING_ExecutionStatus {168 s.Events.Notify(testkube.NewEventEndTestSuccess(&execution))169 }170 return execution, nil171}172// createSecretsReferences strips secrets from text and store it inside model as reference to secret173func (s TestkubeAPI) createSecretsReferences(execution *testkube.Execution) (err error) {174 secrets := map[string]string{}175 secretName := execution.Id + "-vars"176 for k, v := range execution.Variables {177 if v.IsSecret() {178 obfuscated := execution.Variables[k]179 if v.SecretRef != nil {180 obfuscated.SecretRef = &testkube.SecretRef{181 Namespace: execution.TestNamespace,182 Name: v.SecretRef.Name,183 Key: v.SecretRef.Key,184 }185 } else {186 obfuscated.Value = ""187 obfuscated.SecretRef = &testkube.SecretRef{188 Namespace: execution.TestNamespace,189 Name: secretName,190 Key: v.Name,191 }192 secrets[v.Name] = v.Value193 }194 execution.Variables[k] = obfuscated195 }196 }197 labels := map[string]string{"executionID": execution.Id, "testName": execution.TestName}198 if len(secrets) > 0 {199 return s.SecretClient.Create(200 secretName,201 labels,202 secrets,203 )204 }205 return nil206}207// ListExecutionsHandler returns array of available test executions208func (s TestkubeAPI) ListExecutionsHandler() fiber.Handler {209 return func(c *fiber.Ctx) error {210 // TODO refactor into some Services (based on some abstraction for CRDs at least / CRUD)211 // should we split this to separate endpoint? currently this one handles212 // endpoints from /executions and from /tests/{id}/executions213 // or should id be a query string as it's some kind of filter?214 filter := getFilterFromRequest(c)215 executions, err := s.ExecutionResults.GetExecutions(c.Context(), filter)216 if err != nil {217 return s.Error(c, http.StatusInternalServerError, err)218 }219 executionTotals, err := s.ExecutionResults.GetExecutionTotals(c.Context(), false, filter)220 if err != nil {221 return s.Error(c, http.StatusInternalServerError, err)222 }223 filteredTotals, err := s.ExecutionResults.GetExecutionTotals(c.Context(), true, filter)224 if err != nil {225 return s.Error(c, http.StatusInternalServerError, err)226 }227 results := testkube.ExecutionsResult{228 Totals: &executionTotals,229 Filtered: &filteredTotals,230 Results: mapExecutionsToExecutionSummary(executions),231 }232 return c.JSON(results)233 }234}235func (s TestkubeAPI) ExecutionLogsStreamHandler() fiber.Handler {236 return websocket.New(func(c *websocket.Conn) {237 executionID := c.Params("executionID")238 l := s.Log.With("executionID", executionID)239 l.Debugw("getting pod logs and passing to websocket", "id", c.Params("id"), "locals", c.Locals, "remoteAddr", c.RemoteAddr(), "localAddr", c.LocalAddr())240 logs, err := s.Executor.Logs(executionID)241 if err != nil {242 l.Errorw("can't get pod logs", "error", err)243 c.Conn.Close()244 return245 }246 for logLine := range logs {247 l.Debugw("sending log line to websocket", "line", logLine)248 c.WriteJSON(logLine)249 }250 })251}252// ExecutionLogsHandler streams the logs from a test execution253func (s *TestkubeAPI) ExecutionLogsHandler() fiber.Handler {254 return func(c *fiber.Ctx) error {255 executionID := c.Params("executionID")256 s.Log.Debug("getting logs", "executionID", executionID)257 ctx := c.Context()258 ctx.SetContentType("text/event-stream")259 ctx.Response.Header.Set("Cache-Control", "no-cache")260 ctx.Response.Header.Set("Connection", "keep-alive")261 ctx.Response.Header.Set("Transfer-Encoding", "chunked")262 ctx.SetBodyStreamWriter(fasthttp.StreamWriter(func(w *bufio.Writer) {263 s.Log.Debug("starting stream writer")264 w.Flush()265 execution, err := s.ExecutionResults.Get(ctx, executionID)266 if err != nil {267 output.PrintError(os.Stdout, fmt.Errorf("could not get execution result for ID %s: %w", executionID, err))268 s.Log.Errorw("getting execution error", "error", err)269 w.Flush()270 return271 }272 if execution.ExecutionResult.IsCompleted() {273 err := s.streamLogsFromResult(execution.ExecutionResult, w)274 if err != nil {275 output.PrintError(os.Stdout, fmt.Errorf("could not get execution result for ID %s: %w", executionID, err))276 s.Log.Errorw("getting execution error", "error", err)277 w.Flush()278 }279 return280 }281 s.streamLogsFromJob(executionID, w)282 }))283 return nil284 }285}286// GetExecutionHandler returns test execution object for given test and execution id/name287func (s TestkubeAPI) GetExecutionHandler() fiber.Handler {288 return func(c *fiber.Ctx) error {289 ctx := c.Context()290 id := c.Params("id", "")291 executionID := c.Params("executionID")292 var execution testkube.Execution293 var err error294 if id == "" {295 execution, err = s.ExecutionResults.Get(ctx, executionID)296 if err == mongo.ErrNoDocuments {297 execution, err = s.ExecutionResults.GetByName(ctx, executionID)298 if err == mongo.ErrNoDocuments {299 return s.Error(c, http.StatusNotFound, fmt.Errorf("test with execution id/name %s not found", executionID))300 }301 }302 if err != nil {303 return s.Error(c, http.StatusInternalServerError, err)304 }305 } else {306 execution, err = s.ExecutionResults.GetByNameAndTest(ctx, executionID, id)307 if err == mongo.ErrNoDocuments {308 return s.Error(c, http.StatusNotFound, fmt.Errorf("test %s/%s not found", id, executionID))309 }310 if err != nil {311 return s.Error(c, http.StatusInternalServerError, err)312 }313 }314 execution.Duration = types.FormatDuration(execution.Duration)315 testSecretMap := make(map[string]string)316 if execution.TestSecretUUID != "" {317 testSecretMap, err = s.TestsClient.GetSecretTestVars(execution.TestName, execution.TestSecretUUID)318 if err != nil {319 return s.Error(c, http.StatusInternalServerError, err)320 }...
executions_test.go
Source:executions_test.go
...134 panic("not implemented")135 }136 return r.GetFn(ctx, name)137}138func (r MockExecutionResultsRepository) GetByNameAndTest(ctx context.Context, name, testName string) (testkube.Execution, error) {139 panic("not implemented")140}141func (r MockExecutionResultsRepository) GetLatestByTest(ctx context.Context, testName, sortField string) (testkube.Execution, error) {142 panic("not implemented")143}144func (r MockExecutionResultsRepository) GetLatestByTests(ctx context.Context, testNames []string, sortField string) (executions []testkube.Execution, err error) {145 panic("not implemented")146}147func (r MockExecutionResultsRepository) GetExecutions(ctx context.Context, filter result.Filter) ([]testkube.Execution, error) {148 panic("not implemented")149}150func (r MockExecutionResultsRepository) GetExecutionTotals(ctx context.Context, paging bool, filter ...result.Filter) (result testkube.ExecutionsTotals, err error) {151 panic("not implemented")152}...
interface.go
Source:interface.go
...29 // Get gets execution result by id30 Get(ctx context.Context, id string) (testkube.Execution, error)31 // GetByName gets execution result by name32 GetByName(ctx context.Context, id string) (testkube.Execution, error)33 // GetByNameAndTest gets execution result by name and test name34 GetByNameAndTest(ctx context.Context, name, testName string) (testkube.Execution, error)35 // GetLatestByTest gets latest execution result by test36 GetLatestByTest(ctx context.Context, testName, sortField string) (testkube.Execution, error)37 // GetLatestByTests gets latest execution results by test names38 GetLatestByTests(ctx context.Context, testNames []string, sortField string) (executions []testkube.Execution, err error)39 // GetExecutions gets executions using a filter, use filter with no data for all40 GetExecutions(ctx context.Context, filter Filter) ([]testkube.Execution, error)41 // GetExecutionTotals gets the statistics on number of executions using a filter, but without paging42 GetExecutionTotals(ctx context.Context, paging bool, filter ...Filter) (result testkube.ExecutionsTotals, err error)43 // Insert inserts new execution result44 Insert(ctx context.Context, result testkube.Execution) error45 // Update updates execution result46 Update(ctx context.Context, result testkube.Execution) error47 // UpdateExecution updates result in execution48 UpdateResult(ctx context.Context, id string, execution testkube.ExecutionResult) error...
GetByNameAndTest
Using AI Code Generation
1import (2func main() {3 v1.GetByNameAndTest("v1")4}5import "fmt"6func GetByNameAndTest(name string) {7 fmt.Println("v1", name)8}9import "fmt"10func GetByNameAndTest(name string) {11 fmt.Println("v2", name)12}13import "fmt"14func GetByNameAndTest(name string) {15 fmt.Println("v3", name)16}17import "fmt"18func GetByNameAndTest(name string) {19 fmt.Println("v4", name)20}21import "fmt"22func GetByNameAndTest(name string) {23 fmt.Println("v5", name)24}25import "fmt"26func GetByNameAndTest(name string) {27 fmt.Println("v6", name)28}29import "fmt"30func GetByNameAndTest(name string) {31 fmt.Println("v7", name)32}33import "fmt"34func GetByNameAndTest(name string) {35 fmt.Println("v8", name)36}37import "fmt"38func GetByNameAndTest(name string) {39 fmt.Println("v9", name)40}41import "fmt"42func GetByNameAndTest(name string) {43 fmt.Println("v10", name)44}45import "fmt"46func GetByNameAndTest(name string) {47 fmt.Println("v11", name)48}49import "fmt"50func GetByNameAndTest(name string) {51 fmt.Println("v12", name)52}53import "fmt"54func GetByNameAndTest(name string) {55 fmt.Println("v13", name)56}57import
GetByNameAndTest
Using AI Code Generation
1v1 v = new v1();2v.GetByNameAndTest("John");3v.GetByNameAndTest("John", "Doe");4v1 v = new v1();5v.GetByNameAndTest("John");6v.GetByNameAndTest("John", "Doe");7v1 v = new v1();8v.GetByNameAndTest("John");9v.GetByNameAndTest("John", "Doe");10v1 v = new v1();11v.GetByNameAndTest("John");12v.GetByNameAndTest("John", "Doe");13v1 v = new v1();14v.GetByNameAndTest("John");15v.GetByNameAndTest("John", "Doe");16v1 v = new v1();17v.GetByNameAndTest("John");18v.GetByNameAndTest("John", "Doe");19v1 v = new v1();20v.GetByNameAndTest("John");21v.GetByNameAndTest("John", "Doe");22v1 v = new v1();23v.GetByNameAndTest("John");24v.GetByNameAndTest("John", "Doe");25v1 v = new v1();26v.GetByNameAndTest("John");27v.GetByNameAndTest("John", "Doe");28v1 v = new v1();29v.GetByNameAndTest("John");30v.GetByNameAndTest("John", "Doe");31v1 v = new v1();
GetByNameAndTest
Using AI Code Generation
1import (2func main() {3 fmt.Println(v1.GetByNameAndTest("Chandan"))4}5func GetByNameAndTest(name string) string {6}7func GetByNameAndTest(name string) string {8}9func GetByNameAndTest(name string) string {10}
GetByNameAndTest
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello")4 v1.GetByNameAndTest("Raj")5}6import (7func GetByNameAndTest(name string) {8 fmt.Println("Hello", name)9}10 /usr/local/go/src/v1 (from $GOROOT)11 /home/raj/go/src/v1 (from $GOPATH)
GetByNameAndTest
Using AI Code Generation
1import (2func main() {3 fmt.Println("main")4 v1.GetByNameAndTest()5}6import "fmt"7func GetName() {8 fmt.Println("v1.GetName")9}10import "fmt"11func GetByNameAndTest() {12 fmt.Println("v1.GetByNameAndTest")13}14import "fmt"15func GetName() {16 fmt.Println("v2.GetName")17}18import (19func GetByNameAndTest() {20 fmt.Println("v2.GetByNameAndTest")21 v1.GetName()22}
GetByNameAndTest
Using AI Code Generation
1import (2func main() {3 fmt.Println(v1.GetByNameAndTest("Rahul"))4 fmt.Println(v2.GetByNameAndTest("Rahul"))5}6import "fmt"7func GetByNameAndTest(name string) string {8 return fmt.Sprintf("Name: %s", name)9}10import "fmt"11func GetByNameAndTest(name string) string {12 return fmt.Sprintf("Name: %s and Test: %s", name, "Test")13}14import (15func GetByNameAndTest(name string) string {16 return fmt.Sprintf("Name: %s and Test: %s", name, v2.GetTest())17}18import "fmt"19func GetByNameAndTest(name string) string {20 return fmt.Sprintf("Name: %s and Test: %s", name, "Test")21}22func GetTest() string {23}
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!!