Best Testkube code snippet using testresult.DeleteAll
schedule.go
Source:schedule.go
1package cmd2import (3 "context"4 "errors"5 "fmt"6 "net"7 "os/exec"8 log "github.com/sirupsen/logrus"9)10type networkStage string11const (12 networkAdd networkStage = "Add"13 networkReady networkStage = "Ready"14 networkBusy networkStage = "Busy"15 networkError networkStage = "Error"16)17type networkState struct {18 network virterNet19 isAccess bool20 stage networkStage21}22type imageStage string23const (24 imageNone imageStage = "None"25 imageProvision imageStage = "Provision"26 imageReady imageStage = "Ready"27 imageError imageStage = "Error"28)29type runStage string30const (31 runNew runStage = "New"32 runExec runStage = "Exec"33 runDone runStage = "Done"34)35type suiteState struct {36 networks map[string]*networkState37 imageStage map[string]imageStage38 runStage map[string]runStage39 runResults map[string]testResult40 freeIDs map[int]bool41 freeNets *networkList42 errors []error43}44type action interface {45 name() string46 // updatePre updates the state before the action starts.47 updatePre(state *suiteState)48 // exec carries out the action. It should block until the action is49 // finished.50 exec(ctx context.Context, suiteRun *testSuiteRun)51 // updatePost updates the state with the results of the action.52 updatePost(state *suiteState)53}54func runScheduler(ctx context.Context, suiteRun *testSuiteRun) map[string]testResult {55 state := initializeState(suiteRun)56 defer tearDown(suiteRun, state)57 scheduleLoop(ctx, suiteRun, state)58 nErrs := len(state.errors)59 if nErrs == 0 {60 log.Infoln("STATUS: All tests succeeded!")61 } else {62 log.Warnln("ERROR: Printing all errors")63 for i, err := range state.errors {64 log.Warnf("ERROR %d: %s", i, err)65 if suiteRun.printErrorDetails {66 unwrapStderr(err)67 }68 }69 }70 return state.runResults71}72func initializeState(suiteRun *testSuiteRun) *suiteState {73 netlist := NewNetworkList(suiteRun.firstNet)74 state := suiteState{75 networks: make(map[string]*networkState),76 imageStage: make(map[string]imageStage),77 runStage: make(map[string]runStage),78 runResults: make(map[string]testResult),79 freeIDs: make(map[int]bool),80 freeNets: netlist,81 }82 for _, run := range suiteRun.testRuns {83 state.runStage[run.testID] = runNew84 }85 initialImageStage := imageReady86 if suiteRun.vmSpec.ProvisionFile != "" {87 initialImageStage = imageNone88 }89 for _, v := range suiteRun.vmSpec.VMs {90 state.imageStage[v.ID()] = initialImageStage91 }92 for i := 0; i < suiteRun.nrVMs; i++ {93 state.freeIDs[suiteRun.startVM+i] = true94 }95 return &state96}97func scheduleLoop(ctx context.Context, suiteRun *testSuiteRun, state *suiteState) {98 ctx, cancel := context.WithCancel(ctx)99 defer cancel()100 results := make(chan action)101 activeActions := 0102 for {103 for {104 if runStopping(suiteRun, state) || ctx.Err() != nil {105 break106 }107 nextAction := chooseNextAction(suiteRun, state)108 if nextAction == nil {109 break110 }111 log.Debugln("SCHEDULE: Perform action:", nextAction.name())112 nextAction.updatePre(state)113 activeActions++114 go func(a action) {115 a.exec(ctx, suiteRun)116 results <- a117 }(nextAction)118 }119 if activeActions == 0 {120 for _, run := range suiteRun.testRuns {121 if state.runStage[run.testID] != runDone {122 state.errors = append(state.errors, fmt.Errorf("Skipped test run: %s", run.testID))123 }124 }125 break126 }127 log.Debugln("SCHEDULE: Wait for result")128 r := <-results129 activeActions--130 log.Debugln("SCHEDULE: Apply result for:", r.name())131 r.updatePost(state)132 if runStopping(suiteRun, state) {133 cancel()134 }135 }136}137func tearDown(suiteRun *testSuiteRun, state *suiteState) {138 for networkName := range state.networks {139 err := removeNetwork(suiteRun.outDir, networkName)140 if err != nil {141 state.errors = append(state.errors, err)142 }143 }144}145func runStopping(suiteRun *testSuiteRun, state *suiteState) bool {146 if suiteRun.failTest && state.errors != nil {147 return true148 }149 for _, netState := range state.networks {150 if netState.stage == networkError {151 return true152 }153 }154 for _, v := range suiteRun.vmSpec.VMs {155 if state.imageStage[v.ID()] == imageError {156 return true157 }158 }159 return false160}161func chooseNextAction(suiteRun *testSuiteRun, state *suiteState) action {162 // Ignore IDs which are being used for provisioning when deciding which163 // test to work towards. This is necessary to ensure that larger tests164 // are preferred for efficient use of the available IDs.165 nonTestIDs := countNonTestIDs(suiteRun, state)166 var bestRun *testRun167 for i, run := range suiteRun.testRuns {168 if state.runStage[run.testID] != runNew {169 continue170 }171 if nonTestIDs < len(run.vms) {172 continue173 }174 if runBetter(state, bestRun, run) {175 bestRun = &suiteRun.testRuns[i]176 }177 }178 if bestRun != nil {179 action := nextActionRun(suiteRun, state, bestRun)180 if action != nil {181 return action182 }183 }184 if len(state.freeIDs) < 1 {185 return nil186 }187 for i, v := range suiteRun.vmSpec.VMs {188 if state.imageStage[v.ID()] == imageNone {189 return nextActionProvision(suiteRun, state, &suiteRun.vmSpec.VMs[i])190 }191 }192 return nil193}194// runBetter returns whether b is better than (potentially nil) run a.195func runBetter(state *suiteState, a *testRun, b testRun) bool {196 if a == nil {197 return true198 }199 // Prefer runs that use more VMs because that will generally200 // use the available IDs more efficiently201 if len(b.vms) < len(a.vms) {202 return false203 }204 if len(b.vms) > len(a.vms) {205 return true206 }207 if allImagesReady(state, a) && allNetworksReady(state, a) {208 return false209 }210 if allImagesReady(state, &b) && allNetworksReady(state, &b) {211 return true212 }213 return false214}215func allImagesReady(state *suiteState, run *testRun) bool {216 for _, v := range run.vms {217 if state.imageStage[v.ID()] != imageReady {218 return false219 }220 }221 return true222}223func allNetworksReady(state *suiteState, run *testRun) bool {224 networkName := findReadyNetwork(state, nil, accessNetwork(), true)225 if networkName == "" {226 return false227 }228 _, remainingNetworks := findExtraNetworks(state, run)229 return len(remainingNetworks) == 0230}231// findExtraNetworks returns the names of the ready networks and the networks which are not yet ready232func findExtraNetworks(state *suiteState, run *testRun) ([]string, []virterNet) {233 networkNames := []string{}234 remainingNetworks := []virterNet{}235 usedNetworkNames := map[string]bool{}236 for _, network := range run.networks {237 networkName := findReadyNetwork(state, usedNetworkNames, network, false)238 if networkName == "" {239 remainingNetworks = append(remainingNetworks, network)240 }241 networkNames = append(networkNames, networkName)242 usedNetworkNames[networkName] = true243 }244 return networkNames, remainingNetworks245}246func findReadyNetwork(state *suiteState, exclude map[string]bool, network virterNet, access bool) string {247 for i := 0; i < len(state.networks); i++ {248 networkName := generateNetworkName(i, access)249 ns, ok := state.networks[networkName]250 if !ok {251 continue252 }253 if ns.stage != networkReady {254 continue255 }256 if exclude[networkName] {257 continue258 }259 if ns.network.ForwardMode != network.ForwardMode ||260 ns.network.DHCP != network.DHCP ||261 ns.network.Domain != network.Domain {262 continue263 }264 if ns.isAccess != access {265 continue266 }267 return networkName268 }269 return ""270}271func countNonTestIDs(suiteRun *testSuiteRun, state *suiteState) int {272 nonTestIDs := suiteRun.nrVMs273 for _, run := range suiteRun.testRuns {274 if state.runStage[run.testID] == runExec {275 nonTestIDs -= len(run.vms)276 }277 }278 return nonTestIDs279}280func nextActionRun(suiteRun *testSuiteRun, state *suiteState, run *testRun) action {281 if len(state.freeIDs) < len(run.vms) {282 return nil283 }284 if !allImagesReady(state, run) {285 return nil286 }287 network := accessNetwork()288 networkName := findReadyNetwork(state, nil, network, true)289 if networkName == "" {290 return makeAddNetworkAction(state, network, true)291 }292 networkNames, remainingNetworks := findExtraNetworks(state, run)293 if len(remainingNetworks) > 0 {294 return makeAddNetworkAction(state, remainingNetworks[0], false)295 }296 ids := getIDs(suiteRun, state, len(run.vms))297 return &performTestAction{298 run: run,299 ids: ids,300 networkNames: append([]string{networkName}, networkNames...),301 }302}303func getIDs(suiteRun *testSuiteRun, state *suiteState, n int) []int {304 ids := make([]int, 0, n)305 for i := 0; i < suiteRun.nrVMs; i++ {306 id := suiteRun.startVM + i307 if state.freeIDs[id] {308 ids = append(ids, id)309 if len(ids) == n {310 break311 }312 }313 }314 return ids315}316func nextActionProvision(suiteRun *testSuiteRun, state *suiteState, v *vm) action {317 network := accessNetwork()318 networkName := findReadyNetwork(state, nil, network, true)319 if networkName == "" {320 return makeAddNetworkAction(state, network, true)321 }322 ids := getIDs(suiteRun, state, 1)323 return &provisionImageAction{v: v, id: ids[0], networkName: networkName}324}325func makeAddNetworkAction(state *suiteState, network virterNet, access bool) action {326 // Due to https://gitlab.com/libvirt/libvirt/-/issues/78 only one addNetworkAction should run at a time.327 // Basically, libvirt could potentially generate the same bridge name twice, which results in unusable networks.328 for _, ns := range state.networks {329 if ns.stage == networkAdd {330 return nil331 }332 }333 return &addNetworkAction{334 networkName: generateNetworkName(len(state.networks), access),335 network: network,336 access: access,337 }338}339func generateNetworkName(id int, access bool) string {340 networkType := "extra"341 if access {342 networkType = "access"343 }344 return fmt.Sprintf("vmshed-%d-%s", id, networkType)345}346func deleteAll(m map[int]bool, ints []int) {347 for _, index := range ints {348 delete(m, index)349 }350}351type performTestAction struct {352 run *testRun353 ids []int354 networkNames []string355 report string356 res testResult357}358func (a *performTestAction) name() string {359 return fmt.Sprintf("Test %s with IDs %v", a.run.testID, a.ids)360}361func (a *performTestAction) updatePre(state *suiteState) {362 state.runStage[a.run.testID] = runExec363 deleteAll(state.freeIDs, a.ids)364 for _, networkName := range a.networkNames {365 state.networks[networkName].stage = networkBusy366 }367}368func (a *performTestAction) exec(ctx context.Context, suiteRun *testSuiteRun) {369 a.report, a.res = performTest(ctx, suiteRun, a.run, a.ids, a.networkNames)370}371func (a *performTestAction) updatePost(state *suiteState) {372 if log.GetLevel() < log.DebugLevel {373 log.Infof("RESULT: %s - %s", a.run.testID, a.res.status)374 } else {375 fmt.Fprint(log.StandardLogger().Out, a.report)376 }377 state.runStage[a.run.testID] = runDone378 state.runResults[a.run.testID] = a.res379 if a.res.err != nil {380 state.errors = append(state.errors,381 fmt.Errorf("%s: %w", a.run.testID, a.res.err))382 }383 for _, networkName := range a.networkNames {384 state.networks[networkName].stage = networkReady385 }386 for _, id := range a.ids {387 state.freeIDs[id] = true388 }389}390type provisionImageAction struct {391 v *vm392 id int393 networkName string394 err error395}396func (a *provisionImageAction) name() string {397 return fmt.Sprintf("Provision image %s with ID %d", a.v.ID(), a.id)398}399func (a *provisionImageAction) updatePre(state *suiteState) {400 state.imageStage[a.v.ID()] = imageProvision401 delete(state.freeIDs, a.id)402 state.networks[a.networkName].stage = networkBusy403}404func (a *provisionImageAction) exec(ctx context.Context, suiteRun *testSuiteRun) {405 a.err = provisionImage(ctx, suiteRun, a.id, a.v, a.networkName)406}407func (a *provisionImageAction) updatePost(state *suiteState) {408 state.networks[a.networkName].stage = networkReady409 state.freeIDs[a.id] = true410 if a.err == nil {411 log.Infof("STATUS: Successfully provisioned %s", a.v.ID())412 state.imageStage[a.v.ID()] = imageReady413 } else {414 state.imageStage[a.v.ID()] = imageError415 state.errors = append(state.errors,416 fmt.Errorf("provision %s: %w", a.v.ID(), a.err))417 }418}419type addNetworkAction struct {420 networkName string421 network virterNet422 access bool423 ipNet *net.IPNet424 err error425}426func (a *addNetworkAction) name() string {427 return fmt.Sprintf("Add network %s", a.networkName)428}429func (a *addNetworkAction) updatePre(state *suiteState) {430 state.networks[a.networkName] = &networkState{431 network: a.network,432 isAccess: a.access,433 stage: networkAdd,434 }435 if a.network.DHCP {436 a.ipNet = state.freeNets.ReserveNext()437 }438}439func (a *addNetworkAction) exec(ctx context.Context, suiteRun *testSuiteRun) {440 dhcpCount := 0441 if a.access {442 dhcpCount = suiteRun.nrVMs443 }444 a.err = addNetwork(ctx, suiteRun.outDir, a.networkName, a.network, a.ipNet, suiteRun.startVM, dhcpCount)445}446func (a *addNetworkAction) updatePost(state *suiteState) {447 if a.err != nil {448 state.errors = append(state.errors,449 fmt.Errorf("add network %s: %w", a.networkName, a.err))450 state.networks[a.networkName].stage = networkError451 return452 }453 state.networks[a.networkName].stage = networkReady454}455func unwrapStderr(err error) {456 for wrappedErr := err; wrappedErr != nil; wrappedErr = errors.Unwrap(wrappedErr) {457 if exitErr, ok := wrappedErr.(*exec.ExitError); ok {458 log.Warnf("ERROR DETAILS: stderr:")459 fmt.Fprint(log.StandardLogger().Out, string(exitErr.Stderr))460 }461 }462}...
mongo.go
Source:mongo.go
...214func (r *MongoRepository) DeleteByTestSuite(ctx context.Context, testSuiteName string) (err error) {215 _, err = r.Coll.DeleteMany(ctx, bson.M{"testsuite.name": testSuiteName})216 return217}218// DeleteAll deletes all execution results219func (r *MongoRepository) DeleteAll(ctx context.Context) (err error) {220 _, err = r.Coll.DeleteMany(ctx, bson.M{})221 return222}223// DeleteByTestSuites deletes execution results by test suites224func (r *MongoRepository) DeleteByTestSuites(ctx context.Context, testSuiteNames []string) (err error) {225 if len(testSuiteNames) == 0 {226 return nil227 }228 var filter bson.M229 if len(testSuiteNames) > 1 {230 conditions := bson.A{}231 for _, testSuiteName := range testSuiteNames {232 conditions = append(conditions, bson.M{"testsuite.name": testSuiteName})233 }...
interface.go
Source:interface.go
...46 // 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}...
DeleteAll
Using AI Code Generation
1func main() {2 tr := testresult.New()3 tr.DeleteAll()4}5func main() {6 tr := testresult.New()7 tr.DeleteAll()8}9func main() {10 tr := testresult.New()11 tr.DeleteAll()12}13func main() {14 tr := testresult.New()15 tr.DeleteAll()16}17func main() {18 tr := testresult.New()19 tr.DeleteAll()20}21func main() {22 tr := testresult.New()23 tr.DeleteAll()24}25func main() {26 tr := testresult.New()27 tr.DeleteAll()28}29func main() {30 tr := testresult.New()31 tr.DeleteAll()32}33func main() {34 tr := testresult.New()35 tr.DeleteAll()36}37func main() {38 tr := testresult.New()39 tr.DeleteAll()40}41func main() {42 tr := testresult.New()43 tr.DeleteAll()44}45func main() {46 tr := testresult.New()47 tr.DeleteAll()48}49func main() {50 tr := testresult.New()51 tr.DeleteAll()52}53func main() {54 tr := testresult.New()55 tr.DeleteAll()56}
DeleteAll
Using AI Code Generation
1import (2func main() {3 testresult := new(TestResult)4 testresult.DeleteAll()5}6import (7func main() {8 testresult := new(TestResult)9 testresult.DeleteAll()10}11import (12func main() {13 testresult := new(TestResult)14 testresult.DeleteAll()15}16import (17func main() {18 testresult := new(TestResult)19 testresult.DeleteAll()20}21import (22func main() {23 testresult := new(TestResult)24 testresult.DeleteAll()25}26import (27func main() {28 testresult := new(TestResult)29 testresult.DeleteAll()30}31import (32func main() {33 testresult := new(TestResult)34 testresult.DeleteAll()35}36import (37func main() {38 testresult := new(TestResult)39 testresult.DeleteAll()40}41import (42func main() {43 testresult := new(TestResult)44 testresult.DeleteAll()45}46import (47func main() {48 testresult := new(TestResult)49 testresult.DeleteAll()50}51import (52func main() {53 testresult := new(TestResult)54 testresult.DeleteAll()55}
DeleteAll
Using AI Code Generation
1import (2func main() {3 obj.DeleteAll()4 fmt.Println("All records deleted")5}6import (7func main() {8 obj.DeleteAll()9 fmt.Println("All records deleted")10}11import (12func main() {13 obj.DeleteAll()14 fmt.Println("All records deleted")15}16import (17func main() {18 obj.DeleteAll()19 fmt.Println("All records deleted")20}21import (22func main() {23 obj.DeleteAll()24 fmt.Println("All records deleted")25}26import (27func main() {28 obj.DeleteAll()29 fmt.Println("All records deleted")30}31import (32func main() {33 obj.DeleteAll()34 fmt.Println("All records deleted")35}36import (37func main() {38 obj.DeleteAll()39 fmt.Println("All records deleted")40}41import (42func main() {43 obj.DeleteAll()44 fmt.Println("All records deleted")45}
DeleteAll
Using AI Code Generation
1import (2func main() {3 fmt.Println("Deleting all test results")4 testresult.DeleteAll()5 fmt.Println("All test results deleted")6}7import (8func main() {9 fmt.Println("Deleting test result with id 1")10 testresult.Delete(1)11 fmt.Println("Test result with id 1 deleted")12}13import (14func main() {15 fmt.Println("Finding all test results")16 testresult.FindAll()17 fmt.Println("All test results found")18}19import (20func main() {21 fmt.Println("Finding test result with id 1")22 testresult.Find(1)23 fmt.Println("Test result with id 1 found")24}25import (26func main() {27 fmt.Println("Updating all test results")28 testresult.UpdateAll()29 fmt.Println("All test results updated")30}31import (32func main() {33 fmt.Println("Updating test result with id 1")34 testresult.Update(1)35 fmt.Println("Test result with id 1 updated")36}37import (38type TestResult struct {
DeleteAll
Using AI Code Generation
1import (2func main() {3 var result = testresult.Result{Marks: 50}4 result.DeleteAll()5 fmt.Println(result)6}7{ 0}
DeleteAll
Using AI Code Generation
1import (2func main() {3 t.Add(1)4 t.Add(2)5 t.Add(3)6 t.Add(4)7 t.Add(5)8 t.DeleteAll()9 fmt.Println(t.GetResults())10}11import (12type TestResult struct {13}14func (t *TestResult) Add(result int) {15 t.Results = append(t.Results, result)16}17func (t *TestResult) DeleteAll() {18 t.Results = []int{}19}20func (t *TestResult) GetResults() []int {21}
DeleteAll
Using AI Code Generation
1import (2func main() {3 testresult.DeleteAll()4 fmt.Println("All test results deleted")5}6import (7func main() {8 testresult.Delete("Test 1")9 fmt.Println("Test 1 deleted")10}11import (12func main() {13 testResults := testresult.GetAll()14 fmt.Println(testResults)15}16[{Test 1 2020-06-09 18:36:51.433 +0530 IST m=+0.000000001 true} {Test 2 2020-06-09 18:36:51.433 +0530 IST m=+0.000000002 false} {Test 3 2020-06-09 18:36:51.433 +0530 IST m=+0.000000003 true}]17import (18func main() {19 testResults := testresult.GetAllByStatus(true)20 fmt.Println(test
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!!