Best K6 code snippet using execution.New
executor_test.go
Source:executor_test.go
...18}19// Test that the execution step summary is properly constructed.20func TestConstructExecutionSummaryStep(t *testing.T) {21 // Arrange22 dump := dump.NewBasicDump("foo")23 err := errors.New("bar")24 // Act25 step := newExecutionSummaryStep(dump, err)26 // Asset27 require.EqualValues(t, "foo", step.Dump.GetName())28 require.Error(t, step.Error)29}30// Test that the execution summary is properly constructed with steps.31func TestConstructExecutionSummaryWithSteps(t *testing.T) {32 // Act33 summary := newExecutionSummary(34 newExecutionSummaryStep(35 dump.NewBasicDump("foo"), nil,36 ),37 newExecutionSummaryStep(38 dump.NewBasicDump("bar"), errors.New("bar"),39 ),40 )41 // Assert42 require.Len(t, summary.Steps, 2)43}44// Test that the execution step returns a correct success status.45func TestExecutionStepIsSuccess(t *testing.T) {46 // Arrange47 successStep := newExecutionSummaryStep(nil, nil)48 failedStep := newExecutionSummaryStep(nil, errors.New("fail"))49 // Act50 expectedSuccess := successStep.isSuccess()51 expectedFail := failedStep.isSuccess()52 // Assert53 require.True(t, expectedSuccess)54 require.False(t, expectedFail)55}56// Test that the successful dumps are extracted.57func TestGetSuccessfulDumps(t *testing.T) {58 // Arrange59 summary := newExecutionSummary(60 newExecutionSummaryStep(61 dump.NewBasicDump("foo"),62 nil,63 ),64 newExecutionSummaryStep(65 dump.NewBasicDump("bar"),66 errors.New("bar"),67 ),68 newExecutionSummaryStep(69 dump.NewBasicDump("baz"),70 errors.New("baz"),71 ),72 newExecutionSummaryStep(73 dump.NewBasicDump("boz"),74 nil,75 ),76 )77 // Act78 dumps := summary.getSuccessfulDumps()79 // Assert80 require.Len(t, dumps, 2)81 require.EqualValues(t, "foo", dumps[0].GetName())82 require.EqualValues(t, "boz", dumps[1].GetName())83}84// Test that the execution step without an error is simplified as expected.85func TestSimplifySuccessExecutionStep(t *testing.T) {86 // Arrange87 step := newExecutionSummaryStep(88 dump.NewBasicDump("foo",89 dump.NewBasicArtifact("alfa", ".a"),90 dump.NewBasicArtifact("beta", ".b"),91 dump.NewBasicArtifact("gamma", ".g"),92 ),93 nil,94 )95 // Act96 simplify := step.simplify()97 // Assert98 require.EqualValues(t, "foo", simplify.Name)99 require.NoError(t, simplify.Error)100 require.EqualValues(t, []string{"alfa.a", "beta.b", "gamma.g"}, simplify.Artifacts)101 require.EqualValues(t, "SUCCESS", simplify.Status)102}103// Test that the execution step with an empty dump is simplified as expected.104func TestSimplifyExecutionStepWithEmptyDump(t *testing.T) {105 // Arrange106 step := newExecutionSummaryStep(dump.NewBasicDump("foo"), nil)107 // Act108 simplify := step.simplify()109 // Assert110 require.EqualValues(t, "foo", simplify.Name)111 require.NoError(t, simplify.Error)112 require.EqualValues(t, "SUCCESS", simplify.Status)113 require.NotNil(t, simplify.Artifacts)114 require.Len(t, simplify.Artifacts, 0)115}116// Test that the execution step with an error is simplified as expected.117func TestSimplifyFailedExecutionStep(t *testing.T) {118 // Arrange119 step := newExecutionSummaryStep(120 dump.NewBasicDump("foo",121 dump.NewBasicArtifact("alfa", ".a"),122 dump.NewBasicArtifact("beta", ".b"),123 dump.NewBasicArtifact("gamma", ".g"),124 ),125 errors.New("foo"),126 )127 // Act128 simplify := step.simplify()129 // Assert130 require.EqualValues(t, "foo", simplify.Name)131 require.Error(t, simplify.Error)132 require.EqualValues(t, []string{"alfa.a", "beta.b", "gamma.g"}, simplify.Artifacts)133 require.EqualValues(t, "FAIL", simplify.Status)134}135// Test that the execution summary is simplified properly.136func TestSimplifyExecutionSummary(t *testing.T) {137 // Arrange138 summary := newExecutionSummary(139 newExecutionSummaryStep(140 dump.NewBasicDump("foo",141 dump.NewBasicArtifact("alfa", ".a"),142 dump.NewBasicArtifact("beta", ".b"),143 dump.NewBasicArtifact("gamma", ".g"),144 ),145 errors.New("foo"),146 ),147 )148 // Act149 simplified := summary.simplify()150 actualTimestamp, err := time.Parse(time.RFC3339, simplified.Timestamp)151 // Assert152 require.NoError(t, err)153 timeDelta := summary.Timestamp.Sub(actualTimestamp)154 require.LessOrEqual(t, timeDelta.Seconds(), float64(1))155 require.Len(t, simplified.Steps, 1)156}157// Test that the empty execution summary is simplified properly.158func TestSimplifyEmptyExecutionSummary(t *testing.T) {159 // Arrange160 summary := newExecutionSummary()161 // Act162 simplified := summary.simplify()163 actualTimestamp, err := time.Parse(time.RFC3339, simplified.Timestamp)164 // Assert165 require.NoError(t, err)166 timeDelta := summary.Timestamp.Sub(actualTimestamp)167 require.LessOrEqual(t, timeDelta.Seconds(), float64(1))168 require.NotNil(t, simplified.Steps)169 require.Len(t, simplified.Steps, 0)170}171// Test that the dumps are executed properly.172func TestExecuteDumps(t *testing.T) {173 // Arrange174 successMock := storktest.NewMockDump("foo", nil)175 failedMock := storktest.NewMockDump("foobar", errors.New("fail"))176 dumps := []dump.Dump{177 successMock,178 dump.NewBasicDump("bar", dump.NewBasicArtifact("bir", ".eir")),179 dump.NewBasicDump("baz", dump.NewBasicArtifact("buz", ".euz"),180 dump.NewBasicArtifact("bez", ".eez")),181 failedMock,182 }183 // Act184 summary := executeDumps(dumps)185 // Assert186 require.EqualValues(t, successMock.CallCount, 1)187 require.EqualValues(t, failedMock.CallCount, 1)188 require.Len(t, summary.Steps, 5)189 require.EqualValues(t, "bar", summary.Steps[1].Dump.GetName())190 require.NoError(t, summary.Steps[1].Error)191 require.EqualValues(t, "foobar", summary.Steps[3].Dump.GetName())192 require.Error(t, summary.Steps[3].Error)193}194// Test that the dump execution produces the proper summary dump.195func TestExecuteDumpProducesSummaryDump(t *testing.T) {196 // Arrange197 summary := executeDumps([]dump.Dump{198 dump.NewBasicDump("baz", dump.NewBasicArtifact("buz", ""),199 dump.NewBasicArtifact("bez", "")),200 storktest.NewMockDump("bar", errors.New("fail")),201 })202 // Act203 summaryStep := summary.Steps[2]204 summaryArtifact := summaryStep.Dump.GetArtifact(0)205 summaryObject := summaryArtifact.(*dump.BasicStructArtifact)206 simplifySummary, ok := summaryObject.GetStruct().(*executionSummarySimplified)207 // Assert208 require.True(t, ok)209 require.EqualValues(t, 1, summaryStep.Dump.GetArtifactsNumber())210 require.Len(t, simplifySummary.Steps, 3)211}...
execution_cache_store_test.go
Source:execution_cache_store_test.go
...29 EndedAtInSec: 1,30 }31}32func TestCreateExecutionCache(t *testing.T) {33 db := NewFakeDbOrFatal()34 defer db.Close()35 executionCacheStore := NewExecutionCacheStore(db, util.NewFakeTimeForEpoch())36 executionCacheExpected := model.ExecutionCache{37 ID: 1,38 ExecutionCacheKey: "test",39 ExecutionTemplate: "testTemplate",40 ExecutionOutput: "testOutput",41 MaxCacheStaleness: -1,42 StartedAtInSec: 1,43 EndedAtInSec: 1,44 }45 executionCache := &model.ExecutionCache{46 ExecutionCacheKey: "test",47 ExecutionTemplate: "testTemplate",48 ExecutionOutput: "testOutput",49 MaxCacheStaleness: -1,50 }51 executionCache, err := executionCacheStore.CreateExecutionCache(executionCache)52 assert.Nil(t, err)53 require.Equal(t, executionCacheExpected, *executionCache)54}55func TestCreateExecutionCacheWithDuplicateRecord(t *testing.T) {56 executionCache := &model.ExecutionCache{57 ID: 1,58 ExecutionCacheKey: "test",59 ExecutionTemplate: "testTemplate",60 ExecutionOutput: "testOutput",61 MaxCacheStaleness: -1,62 StartedAtInSec: 1,63 EndedAtInSec: 1,64 }65 db := NewFakeDbOrFatal()66 defer db.Close()67 executionCacheStore := NewExecutionCacheStore(db, util.NewFakeTimeForEpoch())68 executionCacheStore.CreateExecutionCache(executionCache)69 cache, err := executionCacheStore.CreateExecutionCache(executionCache)70 assert.Nil(t, cache)71 assert.Contains(t, err.Error(), "Failed to create a new execution cache")72}73func TestGetExecutionCache(t *testing.T) {74 db := NewFakeDbOrFatal()75 defer db.Close()76 executionCacheStore := NewExecutionCacheStore(db, util.NewFakeTimeForEpoch())77 executionCacheStore.CreateExecutionCache(createExecutionCache("testKey", "testOutput"))78 executionCacheExpected := model.ExecutionCache{79 ID: 1,80 ExecutionCacheKey: "testKey",81 ExecutionTemplate: "testTemplate",82 ExecutionOutput: "testOutput",83 MaxCacheStaleness: -1,84 StartedAtInSec: 1,85 EndedAtInSec: 1,86 }87 var executionCache *model.ExecutionCache88 executionCache, err := executionCacheStore.GetExecutionCache("testKey", -1)89 require.Nil(t, err)90 require.Equal(t, &executionCacheExpected, executionCache)91}92func TestGetExecutionCacheWithEmptyCacheEntry(t *testing.T) {93 db := NewFakeDbOrFatal()94 defer db.Close()95 executionCacheStore := NewExecutionCacheStore(db, util.NewFakeTimeForEpoch())96 executionCacheStore.CreateExecutionCache(createExecutionCache("testKey", "testOutput"))97 var executionCache *model.ExecutionCache98 executionCache, err := executionCacheStore.GetExecutionCache("wrongKey", -1)99 require.Nil(t, executionCache)100 require.Contains(t, err.Error(), `Execution cache not found with cache key: "wrongKey"`)101}102func TestGetExecutionCacheWithLatestCacheEntry(t *testing.T) {103 db := NewFakeDbOrFatal()104 defer db.Close()105 executionCacheStore := NewExecutionCacheStore(db, util.NewFakeTimeForEpoch())106 executionCacheStore.CreateExecutionCache(createExecutionCache("testKey", "testOutput"))107 executionCacheStore.CreateExecutionCache(createExecutionCache("testKey", "testOutput2"))108 executionCacheExpected := model.ExecutionCache{109 ID: 2,110 ExecutionCacheKey: "testKey",111 ExecutionTemplate: "testTemplate",112 ExecutionOutput: "testOutput2",113 MaxCacheStaleness: -1,114 StartedAtInSec: 2,115 EndedAtInSec: 2,116 }117 var executionCache *model.ExecutionCache118 executionCache, err := executionCacheStore.GetExecutionCache("testKey", -1)119 require.Nil(t, err)120 require.Equal(t, &executionCacheExpected, executionCache)121}122func TestGetExecutionCacheWithExpiredMaxCacheStaleness(t *testing.T) {123 db := NewFakeDbOrFatal()124 defer db.Close()125 executionCacheStore := NewExecutionCacheStore(db, util.NewFakeTimeForEpoch())126 executionCacheToPersist := &model.ExecutionCache{127 ExecutionCacheKey: "testKey",128 ExecutionTemplate: "testTemplate",129 ExecutionOutput: "testOutput",130 MaxCacheStaleness: 0,131 }132 executionCacheStore.CreateExecutionCache(executionCacheToPersist)133 var executionCache *model.ExecutionCache134 executionCache, err := executionCacheStore.GetExecutionCache("testKey", -1)135 require.Contains(t, err.Error(), "Execution cache not found")136 require.Nil(t, executionCache)137}138func TestDeleteExecutionCache(t *testing.T) {139 db := NewFakeDbOrFatal()140 defer db.Close()141 executionCacheStore := NewExecutionCacheStore(db, util.NewFakeTimeForEpoch())142 executionCacheStore.CreateExecutionCache(createExecutionCache("testKey", "testOutput"))143 executionCache, err := executionCacheStore.GetExecutionCache("testKey", -1)144 assert.Nil(t, err)145 assert.NotNil(t, executionCache)146 err = executionCacheStore.DeleteExecutionCache("1")147 assert.Nil(t, err)148 _, err = executionCacheStore.GetExecutionCache("testKey", -1)149 assert.NotNil(t, err)150 assert.Contains(t, err.Error(), "not found")151}...
metrics.go
Source:metrics.go
...12}13const (14 MetricsNamespace = "cloudnative"15)16// NewExecutionTimer provides a timer for Updater's RunOnce execution17func NewTimer() *ExecutionTimer {18 return NewExecutionTimer(functionLatency)19}20var (21 functionLatency = CreateExecutionTimeMetric(MetricsNamespace,22 "Time spent.")23)24// NewExecutionTimer provides a timer for admission latency; call ObserveXXX() on it to measure25func NewExecutionTimer(histo *prometheus.HistogramVec) *ExecutionTimer {26 now := time.Now()27 return &ExecutionTimer{28 histo: histo,29 start: now,30 last: now,31 }32}33// ObserveTotal measures the execution time from the creation of the ExecutionTimer34func (t *ExecutionTimer) ObserveTotal() {35 (*t.histo).WithLabelValues("total").Observe(time.Now().Sub(t.start).Seconds())36}37// CreateExecutionTimeMetric prepares a new histogram labeled with execution step38func CreateExecutionTimeMetric(namespace string, help string) *prometheus.HistogramVec {39 return prometheus.NewHistogramVec(40 prometheus.HistogramOpts{41 Namespace: namespace,42 Name: "execution_latency_seconds",43 Help: help,44 Buckets: prometheus.ExponentialBuckets(0.001, 2, 15),45 }, []string{"step"},46 )47}48// ExecutionTimer measures execution time of a computation, split into major steps49// usual usage pattern is: timer := NewExecutionTimer(...) ; compute ; timer.ObserveStep() ; ... ; timer.ObserveTotal()50type ExecutionTimer struct {51 histo *prometheus.HistogramVec52 start time.Time53 last time.Time54}...
New
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-l")4 out, err := cmd.Output()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(string(out))9}10import (11func main() {12 cmd := exec.Command("ls", "-l")13 err := cmd.Start()14 if err != nil {15 fmt.Println(err)16 }17 fmt.Println("Command is running in background")18}19import (20func main() {21 cmd := exec.Command("ls", "-l")22 err := cmd.Start()23 if err != nil {24 fmt.Println(err)25 }26 err = cmd.Wait()27 if err != nil {28 fmt.Println(err)29 }30 fmt.Println("Command finished successfully")31}32import (33func main() {34 cmd := exec.Command("ls", "-l")
New
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-l")4 out, err := cmd.Output()5 if err != nil {6 fmt.Println(err)7 }8 fmt.Println(string(out))9}10import (11func main() {12 ctx := context.Background()13 cmd := exec.CommandContext(ctx, "ls", "-l")14 out, err := cmd.Output()15 if err != nil {16 fmt.Println(err)17 }18 fmt.Println(string(out))19}20import (21func main() {22 cmd := exec.Command("ls", "-l")23 out, err := cmd.Output()24 if err != nil {25 fmt.Println(err)26 }27 fmt.Println(string(out))28}
New
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-la")4 err := cmd.Start()5 if err != nil {6 fmt.Println(err)7 }8 err = cmd.Wait()9 if err != nil {10 fmt.Println(err)11 }12 cmd.Process.Kill()13 cmd.Process.Signal(syscall.SIGINT)14 fmt.Println(cmd.ProcessState)15 fmt.Println(cmd.Process.Pid)16 cmd.Process.Release()17 cmd.Process.Signal(syscall.SIGINT)18 fmt.Println(cmd.ProcessState.Sys())19 fmt.Println(cmd.SysProcAttr)20 fmt.Println(cmd.ProcessState.UserTime())21 fmt.Println(cmd.ProcessState.UserTime())22 err = cmd.Wait()23 if err != nil {24 fmt.Println(err)25 }
New
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-ltr")4 stdout, err := cmd.Output()5 if err != nil {6 fmt.Println(err.Error())7 }8 fmt.Println(string(stdout))9}10import (11func main() {12 cmd := exec.Command("ls", "-ltr")13 stdoutStderr, err := cmd.CombinedOutput()14 if err != nil {15 fmt.Println(err.Error())16 }17 fmt.Println(string(stdoutStderr))18}19import (20func main() {21 path, err := exec.LookPath("ls")22 if err != nil {23 fmt.Println(err.Error())24 }25 fmt.Println(path)26}27import (28func main() {29 cmd := exec.Command("ls", "-ltr")30 err := cmd.Run()31 if err != nil {32 fmt.Println(err.Error())33 }34 fmt.Println("Successfully executed command")35}
New
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-la")4 fmt.Println(cmd.Args)5 fmt.Println(cmd.Path)6 fmt.Println(cmd.Dir)7 cmd2 := exec.Command("ls", "-la")8 err := cmd2.Run()9 if err != nil {10 fmt.Println(err.Error())11 os.Exit(1)12 }13}
New
Using AI Code Generation
1import (2func main() {3 cmd := exec.Command("ls", "-l")4 stdout, err := cmd.StdoutPipe()5 if err != nil {6 fmt.Println(err)7 }8 if err := cmd.Start(); err != nil {9 fmt.Println(err)10 }11 opBytes, err := os.ReadAll(stdout)12 if err != nil {13 fmt.Println(err)14 }15 fmt.Println(string(opBytes))16}17func (c *Cmd) Run() error18import (19func main() {20 cmd := exec.Command("ls", "-l")21 err := cmd.Run()22 if err != nil {23 fmt.Println(err)24 }25}
New
Using AI Code Generation
1func main() {2 execution := execution.New()3 execution.Run()4}5func main() {6 execution := execution.Execution{}7 execution.Run()8}
New
Using AI Code Generation
1import (2func main() {3 fmt.Println("main method started")4 e := execution{}5 e.execute()6 fmt.Println("main method ended")7}8import (9type execution struct {10}11func (e execution) execute() {12 fmt.Println("execute method started")13 fmt.Println("execute method ended")14}
New
Using AI Code Generation
1func main() {2 exec := execution.New()3 task := exec.NewTask("task1")4 task.SetCommand("echo", "hello", "world")5 err := exec.Run()6 if err != nil {7 log.Fatal(err)8 }9}10func main() {11 exec := execution.New()12 task := exec.NewTask("task1")13 task.SetCommand("echo", "hello", "world")14 err := exec.Run()15 if err != nil {16 log.Fatal(err)17 }18}19func main() {20 exec := execution.New()21 task := exec.NewTask("task1").SetCommand("echo", "hello", "world")22 err := exec.Run()23 if err != nil {24 log.Fatal(err)25 }26}27func main() {28 exec := execution.New()29 err := exec.NewTask("task1").SetCommand("echo", "hello", "world").Run()30 if err != nil {31 log.Fatal(err)32 }33}34func main() {35 exec := execution.New()36 err := exec.NewTask("task1").SetCommand("echo", "hello", "world").SetEnv("HOME", "/home/").Run()
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!!