Best Venom code snippet using run.mergeVariables
executor.go
Source:executor.go
1package shell2import (3 "context"4 "io"5 "io/ioutil"6 "os"7 "os/exec"8 "github.com/pkg/errors"9 "github.com/sirupsen/logrus"10 "github.com/projecteru2/phistage/common"11 "github.com/projecteru2/phistage/helpers/command"12 "github.com/projecteru2/phistage/helpers/variable"13 "github.com/projecteru2/phistage/store"14)15type ShellJobExecutor struct {16 store store.Store17 config *common.Config18 job *common.Job19 phistage *common.Phistage20 output io.Writer21 workingDir string22 jobEnvironment map[string]string23}24// NewEruJobExecutor creates an ERU executor for this job.25// Since job needs to know its context, phistage is assigned too.26func NewShellJobExecutor(job *common.Job, phistage *common.Phistage, output io.Writer, store store.Store, config *common.Config) (*ShellJobExecutor, error) {27 return &ShellJobExecutor{28 store: store,29 config: config,30 job: job,31 phistage: phistage,32 output: output,33 jobEnvironment: phistage.Environment,34 }, nil35}36// Prepare does all the preparations before actually running a job37func (ls *ShellJobExecutor) Prepare(ctx context.Context) error {38 preparations := []func(context.Context) error{39 ls.prepareJobRuntime,40 ls.prepareFileContext,41 }42 for _, f := range preparations {43 if err := f(ctx); err != nil {44 return err45 }46 }47 return nil48}49// Prepare creates a temp working dir for this job.50func (ls *ShellJobExecutor) prepareJobRuntime(ctx context.Context) error {51 var err error52 ls.workingDir, err = ioutil.TempDir("", "phistage-*")53 return err54}55func (ls *ShellJobExecutor) prepareFileContext(ctx context.Context) error {56 dependentJobs := ls.phistage.GetJobs(ls.job.DependsOn)57 for _, job := range dependentJobs {58 fc := job.GetFileCollector()59 if fc == nil {60 continue61 }62 if err := fc.CopyTo(ctx, ls.workingDir, nil); err != nil {63 return err64 }65 }66 return nil67}68// defaultEnvironmentVariables sets some useful information into environment variables.69// This will be set to the whole running context within the workload.70func (ls *ShellJobExecutor) defaultEnvironmentVariables() map[string]string {71 return map[string]string{72 "PHISTAGE_WORKING_DIR": ls.workingDir,73 "PHISTAGE_JOB_NAME": ls.job.Name,74 }75}76// Execute will execute all steps within this job one by one77func (ls *ShellJobExecutor) Execute(ctx context.Context) error {78 for _, step := range ls.job.Steps {79 var err error80 switch step.Uses {81 case "":82 err = ls.executeStep(ctx, step)83 default:84 // step, err = e.replaceStepWithUses(ctx, step)85 // if err != nil {86 // return err87 // }88 err = ls.executeKhoriumStep(ctx, step)89 }90 if err != nil {91 return err92 }93 }94 return nil95}96// replaceStepWithUses replaces the step with the actual step identified by uses.97// Name will not be replaced, the commands to execute aka Run and OnError will be replaced,98// also Environment and With will be merged, uses' environment and with has a lower priority,99// will be overridden by step's environment and with,100// If uses is not given, directly return the original step.101func (ls *ShellJobExecutor) replaceStepWithUses(ctx context.Context, step *common.Step) (*common.Step, error) {102 if step.Uses == "" {103 return step, nil104 }105 uses, err := ls.store.GetRegisteredStep(ctx, step.Uses)106 if err != nil {107 return nil, err108 }109 s := &common.Step{110 Name: step.Name,111 Run: uses.Run,112 OnError: uses.OnError,113 Environment: command.MergeVariables(uses.Environment, step.Environment),114 With: command.MergeVariables(uses.With, step.With),115 }116 // in case name of this step is empty117 if s.Name == "" {118 s.Name = uses.Name119 }120 return s, nil121}122// executeStep executes a step.123// It first replace the step with uses if uses is given,124// then prepare the arguments and environments to the command.125// Then execute the command, retrieve the output, the execution will stop if any error occurs.126// It then retries to execute the OnError commands, also with the arguments and environments.127func (ls *ShellJobExecutor) executeStep(ctx context.Context, step *common.Step) error {128 var (129 err error130 vars map[string]string131 )132 vars, err = ls.store.GetVariablesForPhistage(ctx, ls.phistage.Name)133 if err != nil {134 return err135 }136 environment := command.MergeVariables(ls.jobEnvironment, step.Environment)137 defer func() {138 if !errors.Is(err, common.ErrExecutionError) {139 return140 }141 if err := ls.executeCommands(ctx, step.OnError, step.With, environment, vars); err != nil {142 logrus.WithField("step", step.Name).WithError(err).Errorf("[EruJobExecutor] error when executing on_error")143 }144 }()145 err = ls.executeCommands(ctx, step.Run, step.With, environment, vars)146 return err147}148// executeKhoriumStep executes a KhoriumStep defined by step.Uses.149func (ls *ShellJobExecutor) executeKhoriumStep(ctx context.Context, step *common.Step) error {150 ks, err := ls.store.GetRegisteredKhoriumStep(ctx, step.Uses)151 if err != nil {152 return err153 }154 vars, err := ls.store.GetVariablesForPhistage(ctx, ls.phistage.Name)155 if err != nil {156 return err157 }158 arguments, err := variable.RenderArguments(step.With, step.Environment, vars)159 if err != nil {160 return err161 }162 ksEnv, err := ks.BuildEnvironmentVariables(arguments)163 if err != nil {164 return err165 }166 envs := command.MergeVariables(ls.defaultEnvironmentVariables(), step.Environment)167 envs = command.MergeVariables(envs, ksEnv)168 khoriumStepWorkingDir, err := ioutil.TempDir("", "phistage-khoriumstep-*")169 if err != nil {170 return err171 }172 defer os.RemoveAll(khoriumStepWorkingDir)173 fc := NewShellFileCollector()174 fc.SetFiles(ks.Files)175 if err := fc.CopyTo(ctx, khoriumStepWorkingDir, nil); err != nil {176 return err177 }178 cmd := exec.CommandContext(ctx, "/bin/sh", "-c", ks.Run.Main)179 cmd.Dir = khoriumStepWorkingDir180 cmd.Env = command.ToEnvironmentList(envs)181 cmd.Stdout = ls.output182 cmd.Stderr = ls.output183 if err := cmd.Run(); err != nil {184 return errors.WithMessagef(common.ErrExecutionError, "exec error: %v", err)185 }186 return nil187}188// executeCommands executes cmd with given arguments, environments and variables.189// use args, envs, and reserved vars to build the cmd.190func (ls *ShellJobExecutor) executeCommands(ctx context.Context, cmds []string, args, env, vars map[string]string) error {191 if len(cmds) == 0 {192 return nil193 }194 var commands []string195 for _, cmd := range cmds {196 c, err := command.RenderCommand(cmd, args, env, vars)197 if err != nil {198 return err199 }200 commands = append(commands, c)201 }202 for _, c := range commands {203 cmd := exec.CommandContext(ctx, "/bin/sh", "-c", c)204 cmd.Dir = ls.workingDir205 cmd.Env = command.ToEnvironmentList(command.MergeVariables(ls.defaultEnvironmentVariables(), env))206 cmd.Stdout = ls.output207 cmd.Stderr = ls.output208 if err := cmd.Run(); err != nil {209 return errors.WithMessagef(common.ErrExecutionError, "exec error: %v", err)210 }211 }212 return nil213}214// beforeCleanup collects files215func (ls *ShellJobExecutor) beforeCleanup(ctx context.Context) error {216 if len(ls.job.Files) == 0 {217 return nil218 }219 fc := NewShellFileCollector()220 if err := fc.Collect(ctx, ls.workingDir, ls.job.Files); err != nil {221 return err222 }223 ls.job.SetFileCollector(fc)224 return nil225}226// cleanup removes the temp working dir.227func (ls *ShellJobExecutor) cleanup(ctx context.Context) error {228 return os.RemoveAll(ls.workingDir)229}230// Cleanup does all the cleanup work231func (ls *ShellJobExecutor) Cleanup(ctx context.Context) error {232 cleanups := []func(context.Context) error{233 ls.beforeCleanup,234 ls.cleanup,235 }236 for _, f := range cleanups {237 if err := f(ctx); err != nil {238 return err239 }240 }241 return nil242}...
cicd_executor.go
Source:cicd_executor.go
...53 WorkerNum int `dft:"20"`54 Data string `dft:"data"`55 SleepTime time.Duration `dft:"5s"`56}57func mergeVariables(variables []*api.Variable) (map[string]interface{}, error) {58 kvs := map[string]interface{}{}59 for _, variable := range variables {60 var v interface{}61 if err := json.Unmarshal([]byte(variable.Kvs), &v); err != nil {62 return nil, errors.Wrap(err, "mergeVariables failed")63 }64 kvs[variable.Name] = v65 }66 return kvs, nil67}68func (e *CICDExecutor) runLoop(ctx context.Context) {69 errs := make(chan error, 1)70out:71 for {72 go func() {73 errs <- e.runOnce(NewExecutorContext(ctx))74 }()75 select {76 case <-ctx.Done():77 break out78 case <-errs:79 }80 }81}82func (e *CICDExecutor) runOnce(ctx context.Context) error {83 now := time.Now()84 var err error85 var job *api.Job86 var acquire bool87 defer func() {88 if acquire {89 e.log.Info(map[string]interface{}{90 "id": job.Id,91 "err": err,92 "errStack": fmt.Sprintf("%+v", err),93 "timeMs": time.Now().Sub(now).Milliseconds(),94 "ctx": ctx.Value(executorKey{}),95 })96 }97 }()98 job, err = e.storage.FindOneUnscheduleJob(ctx)99 if err != nil {100 return err101 }102 if job == nil {103 select {104 case <-ctx.Done():105 case <-time.After(e.options.SleepTime):106 }107 return nil108 }109 acquire, err = e.storage.UpdateJobStatus(ctx, job.Id, storage.JobStatusWaiting, storage.JobStatusWaiting)110 if err != nil {111 return err112 }113 if !acquire {114 return nil115 }116 err = e.runTask(ctx, job.Id)117 return err118}119func (e *CICDExecutor) runTask(ctx context.Context, jobID string) error {120 job, err := e.storage.GetJob(ctx, jobID)121 if err != nil {122 return err123 }124 defer CtxSet(ctx, "job", job)125 task, err := e.storage.GetTask(ctx, job.TaskID)126 if err != nil {127 return err128 }129 job.TaskName = task.Name130 defer CtxSet(ctx, "task", task)131 job.Status = storage.JobStatusRunning132 job.ScheduleAt = int32(time.Now().Unix())133 if err := e.storage.UpdateJob(ctx, job); err != nil {134 return err135 }136 now := time.Now()137 if err := e.runSubTasks(ctx, job, task); err != nil {138 job.Error = err.Error()139 job.Status = storage.JobStatusFailed140 } else {141 job.Status = storage.JobStatusFinish142 }143 job.ElapseSeconds = int32(time.Now().Sub(now).Seconds())144 if err := e.storage.UpdateJob(ctx, job); err != nil {145 return err146 }147 return nil148}149func (e *CICDExecutor) runSubTasks(ctx context.Context, job *api.Job, task *api.Task) error {150 variables, err := e.storage.GetVariableByIDs(ctx, task.VariableIDs)151 if err != nil {152 return errors.Wrap(err, "GetVariables failed")153 }154 templates, err := e.storage.GetSubTaskByIDs(ctx, task.SubTaskIDs)155 if err != nil {156 return errors.Wrap(err, "GetSubTasks failed")157 }158 m := map[string]*api.SubTask{}159 for _, t := range templates {160 m[t.Id] = t161 }162 for i, tid := range task.SubTaskIDs {163 templates[i] = m[tid]164 }165 kvs, err := mergeVariables(variables)166 if err != nil {167 return errors.Wrap(err, "mergeVariables failed")168 }169 for _, i := range templates {170 str, err := mustache.Render(i.ScriptSubTask.Script, kvs)171 if err != nil {172 return errors.Wrapf(err, "mustache.Render failed. template: [%v]", i.Name)173 }174 job.Subs = append(job.Subs, &api.Job_Sub{175 SubTaskID: i.Id,176 SubTaskName: i.Name,177 Language: i.ScriptSubTask.Language,178 Script: str,179 Status: storage.JobStatusWaiting,180 UpdateAt: int32(time.Now().Unix()),181 })...
cmd_test.go
Source:cmd_test.go
...70 require.EqualValues(t, tt.want, got)71 })72 }73}74func Test_mergeVariables(t *testing.T) {75 ma := mergeVariables("aa=bb", []string{"cc=dd", "ee=ff"})76 require.Equal(t, 3, len(ma))77 mb := mergeVariables("aa=bb", []string{"aa=dd"})78 require.Equal(t, 1, len(mb))79 mc := mergeVariables("aa=bb=dd", []string{"aa=dd"})80 require.Equal(t, 1, len(mc))81 md := mergeVariables("aa=bb=dd", []string{"cc=dd"})82 require.Equal(t, 2, len(md))83}84func Test_initFromEnv(t *testing.T) {85 env := []string{`VENOM_VAR_a=1`, `VENOM_VAR_b="B"`, `VENOM_VAR_c=[1,2,3]`}86 found, err := initFromEnv(env)87 require.NoError(t, err)88 require.Equal(t, 3, len(found))89 var nb int90 for i := range found {91 if found[i] == "a=1" {92 nb++93 } else if found[i] == "b=B" {94 nb++95 } else if found[i] == "c=[1,2,3]" {...
mergeVariables
Using AI Code Generation
1import "fmt"2func main() {3 var a = []string{"a", "b", "c"}4 var b = []string{"d", "e", "f"}5 var c = []string{"g", "h", "i"}6 var d = []string{"j", "k", "l"}7 var e = []string{"m", "n", "o"}8 var f = []string{"p", "q", "r"}9 var g = []string{"s", "t", "u"}10 var h = []string{"v", "w", "x"}11 var i = []string{"y", "z"}12 var j = []string{"1", "2", "3", "4", "5"}13 var k = []string{"6", "7", "8", "9", "0"}14 var l = []string{"!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+"}15 var m = []string{".", ",", ";", ":", "<", ">", "?", "/", "|", "[", "]", "{", "}", "`", "~"}16 var n = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}17 var o = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}18 var p = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}19 var q = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "
mergeVariables
Using AI Code Generation
1import (2func main() {3 var1 := map[string]string{"var1": "value1"}4 var2 := map[string]string{"var2": "value2"}5 var3 := map[string]string{"var3": "value3"}6 var4 := map[string]string{"var4": "value4"}7 var5 := map[string]string{"var5": "value5"}8 var6 := map[string]string{"var6": "value6"}9 var7 := map[string]string{"var7": "value7"}10 var8 := map[string]string{"var8": "value8"}11 var9 := map[string]string{"var9": "value9"}12 var10 := map[string]string{"var10": "value10"}13 var11 := map[string]string{"var11": "value11"}14 var12 := map[string]string{"var12": "value12"}15 var13 := map[string]string{"var13": "value13"}16 var14 := map[string]string{"var14": "value14"}17 var15 := map[string]string{"var15": "value15"}18 var16 := map[string]string{"var16": "value16"}19 var17 := map[string]string{"var17": "value17"}20 var18 := map[string]string{"var18": "value18"}21 var19 := map[string]string{"var19": "value19"}22 var20 := map[string]string{"var20": "value20"}23 var21 := map[string]string{"var21": "value21"}24 var22 := map[string]string{"var22": "value22"}25 var23 := map[string]string{"var23": "value23"}26 var24 := map[string]string{"var24": "value24"}27 var25 := map[string]string{"var25": "value25"}28 var26 := map[string]string{"var26": "value26"}29 var27 := map[string]string{"var27": "value27"}30 var28 := map[string]string{"var28": "value28"}31 var29 := map[string]string{"var29": "value29"}32 var30 := map[string]string{"var30": "value30"}33 var31 := map[string]string{"var31": "value31"}34 var32 := map[string]string{"var32": "value32"}
mergeVariables
Using AI Code Generation
1import (2type run struct {3}4func main() {5 r := run{6 variables: map[string]string{7 },8 }9 r.mergeVariables(map[string]string{10 })11 fmt.Println(r.variables)12}13import (14type run struct {15}16func main() {17 r := run{18 variables: map[string]string{19 },20 }21 r.mergeVariables(map[string]string{22 })23 fmt.Println(r.variables)24}25import (26type run struct {27}28func main() {29 r := run{30 variables: map[string]string{31 },32 }33 r.mergeVariables(map[string]string{34 })35 fmt.Println(r.variables)36}37import (38type run struct {39}40func main() {41 r := run{42 variables: map[string]string{43 },44 }45 r.mergeVariables(map[string]string{46 })47 fmt.Println(r.variables)48}49import (50type run struct {51}52func main() {53 r := run{54 variables: map[string]string{55 },56 }57 r.mergeVariables(map[string]string{58 })
mergeVariables
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Printf("value of c is %d4}5import "fmt"6func main() {7 fmt.Printf("value of c is %d8}9import "fmt"10func main() {11 fmt.Printf("value of c is %d12}13import "fmt"14func main() {15 fmt.Printf("value of c is %d16}17import "fmt"18func main() {19 fmt.Printf("value of c is %d20}21import "fmt"22func main() {23 fmt.Printf("value of c is %d24}25import "fmt"26func main() {
mergeVariables
Using AI Code Generation
1import "fmt"2func main() {3 r := new(run)4 r.mergeVariables()5}6import "fmt"7func main() {8 r := new(run)9 r.mergeVariables()10}11import "fmt"12type run struct {13}14func (r *run) mergeVariables() {15 fmt.Println("Merged Variables")16}
mergeVariables
Using AI Code Generation
1import (2func main() {3 file, err := os.Open("test.txt")4 if err != nil {5 fmt.Println(err)6 }7 defer file.Close()8 run := newRun()9 run.mergeVariables(file)10 fmt.Println(run.variables)11}12import (13func main() {14 file, err := os.Open("test.txt")15 if err != nil {16 fmt.Println(err)17 }18 defer file.Close()19 run := newRun()20 run.mergeVariables(file)21 fmt.Println(run.variables)22}23import (24func main() {25 file, err := os.Open("test.txt")26 if err != nil {27 fmt.Println(err)28 }29 defer file.Close()30 run := newRun()31 run.mergeVariables(file)32 fmt.Println(run.variables)33}34import (35func main() {36 file, err := os.Open("test.txt")37 if err != nil {38 fmt.Println(err)39 }40 defer file.Close()41 run := newRun()42 run.mergeVariables(file)43 fmt.Println(run.variables)44}45import (46func main() {47 file, err := os.Open("test.txt")48 if err != nil {49 fmt.Println(err)50 }51 defer file.Close()
mergeVariables
Using AI Code Generation
1import (2type run struct {3}4func main() {5 data, err := ioutil.ReadFile("1.tcx")6 if err != nil {7 panic(err)8 }9 str := string(data)10 lines := strings.Split(str, "11 run := run{}12 for _, line := range lines {13 if strings.Contains(line, "<Id>") {14 id := strings.Split(line, ">")[1]15 id = strings.Split(id, "<")[0]16 idInt, err := strconv.Atoi(id)17 if err != nil {18 panic(err)19 }20 }21 if strings.Contains(line, "<Id>StartTime") {22 startTime := strings.Split(line, ">")[1]23 startTime = strings.Split(startTime, "<")[0]24 startTimeFloat, err := strconv.ParseFloat(startTime, 64)25 if err != nil {26 panic(err)27 }28 }29 if strings.Contains(line, "<Id>EndTime") {30 endTime := strings.Split(line, ">")[1]31 endTime = strings.Split(endTime, "<")[0]32 endTimeFloat, err := strconv.ParseFloat(endTime, 64)
mergeVariables
Using AI Code Generation
1import (2func main() {3 run := new(Run)4 run.mergeVariables("a=1", "b=2", "c=3")5 fmt.Println(run.variables)6}7type Run struct {8}9func (r *Run) mergeVariables(variables ...string) {10 if r.variables == nil {11 r.variables = make(map[string]string)12 }13 for _, variable := range variables {14 splitted := strings.SplitN(variable, "=", 2)15 if len(splitted) == 2 {16 }17 }18}19func (r *Run) mergeVariables2(variables ...string) {20 if r.variables == nil {21 r.variables = make(map[string]string)22 }23 for _, variable := range variables {24 splitted := strings.SplitN(variable, "=", 2)25 if len(splitted) == 2 {26 }27 }28}29func (r *Run) mergeVariables3(variables ...string) {30 if r.variables == nil {31 r.variables = make(map[string]string)32 }33 for _, variable := range variables {34 splitted := strings.SplitN(variable, "=", 2)35 if len(splitted) == 2 {36 }37 }38}39func (r *Run) mergeVariables4(variables ...string) {40 if r.variables == nil {41 r.variables = make(map[string]string)42 }43 for _, variable := range variables {44 splitted := strings.SplitN(variable, "=", 2)45 if len(splitted) == 2 {46 }47 }48}49func (r *Run) mergeVariables5(variables ...string) {50 if r.variables == nil {51 r.variables = make(map[string]string)52 }53 for _, variable := range variables {54 splitted := strings.SplitN(variable, "=", 2)55 if len(splitted) == 2 {
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!!