func (s *AdapterSuite) TestCompleteFlow(c *C) { var cmd *client.Command var err error var result *client.CommandResult if os.Getenv("CHANGES") == "1" { c.ExpectFailure("For as yet unknown reasons, container initialization fails on Changes.") } clientLog := client.NewLog() adapter, err := adapter.Get("lxc") c.Assert(err, IsNil) wg := sync.WaitGroup{} wg.Add(1) go func() { defer wg.Done() s.reportLogChunks(clientLog) }() config := &client.Config{} config.JobstepID = containerName err = adapter.Init(config) c.Assert(err, IsNil) err = adapter.Prepare(clientLog) c.Assert(err, IsNil) defer adapter.Shutdown(clientLog) cmd, err = client.NewCommand("test", "#!/bin/bash -e\necho hello > foo.txt\nexit 0") c.Assert(err, IsNil) result, err = adapter.Run(cmd, clientLog) c.Assert(err, IsNil) c.Assert(string(result.Output), Equals, "") c.Assert(result.Success, Equals, true) cmd, err = client.NewCommand("test", "#!/bin/bash -e\necho $HOME\nexit 0") cmd.CaptureOutput = true c.Assert(err, IsNil) result, err = adapter.Run(cmd, clientLog) c.Assert(err, IsNil) c.Assert(string(result.Output), Equals, "/home/ubuntu\n") c.Assert(result.Success, Equals, true) cmd, err = client.NewCommand("test", "#!/bin/bash -e\ndd if=/dev/zero of=test.img bs=1M count=10 && mkfs.ext4 -b 1024 -j -F test.img && sudo mount -v -o loop test.img /mnt") cmd.CaptureOutput = true c.Assert(err, IsNil) result, err = adapter.Run(cmd, clientLog) c.Assert(err, IsNil) c.Assert(result.Success, Equals, true) // test with a command that expects stdin cmd, err = client.NewCommand("test", "#!/bin/bash -e\nread foo\nexit 1") c.Assert(err, IsNil) result, err = adapter.Run(cmd, clientLog) c.Assert(err, IsNil) c.Assert(string(result.Output), Equals, "") c.Assert(result.Success, Equals, false) artifacts, err := adapter.CollectArtifacts([]string{"foo.txt"}, clientLog) c.Assert(err, IsNil) c.Assert(len(artifacts), Equals, 1) c.Assert(artifacts[0], Matches, ".*/home/ubuntu/foo.txt") clientLog.Close() wg.Wait() }
func (e *Engine) executeCommands() (Result, error) { wg := sync.WaitGroup{} defer wg.Wait() for _, cmdConfig := range e.config.Cmds { e.clientLog.Writeln(fmt.Sprintf("==> Running command %s", cmdConfig.ID)) e.clientLog.Writeln(fmt.Sprintf("==> with script %s", cmdConfig.Script)) cmd, err := client.NewCommand(cmdConfig.ID, cmdConfig.Script) if err != nil { e.reporter.PushCommandStatus(cmd.ID, STATUS_FINISHED, 255) e.clientLog.Writeln(fmt.Sprintf("==> Error: %s", err.Error())) return RESULT_INFRA_FAILED, err } e.reporter.PushCommandStatus(cmd.ID, STATUS_IN_PROGRESS, -1) cmd.CaptureOutput = cmdConfig.CaptureOutput env := os.Environ() for k, v := range cmdConfig.Env { env = append(env, k+"="+v) } cmd.Env = env if len(cmdConfig.Cwd) > 0 { cmd.Cwd = cmdConfig.Cwd } cmdResult, err := e.adapter.Run(cmd, e.clientLog) if err != nil { e.reporter.PushCommandStatus(cmd.ID, STATUS_FINISHED, 255) e.clientLog.Writeln(fmt.Sprintf("==> Error: %s", err.Error())) return RESULT_INFRA_FAILED, err } result := RESULT_FAILED if cmdResult.Success { result = RESULT_PASSED if cmd.CaptureOutput { e.reporter.PushCommandOutput(cmd.ID, STATUS_FINISHED, 0, cmdResult.Output) } else { e.reporter.PushCommandStatus(cmd.ID, STATUS_FINISHED, 0) } } else { e.reporter.PushCommandStatus(cmd.ID, STATUS_FINISHED, 1) } wg.Add(1) go func(cfgcmd client.ConfigCmd) { // publishArtifacts is a synchronous operation and doesnt follow the normal queue flow of // other operations e.reporter.PublishArtifacts(cfgcmd, e.adapter, e.clientLog) wg.Done() }(cmdConfig) if result.IsFailure() { return result, nil } } // Made it through all commands without failure. Success. return RESULT_PASSED, nil }
func (s *AdapterSuite) TestCompleteFlow(c *C) { var cmd *client.Command var err error var result *client.CommandResult clientLog := client.NewLog() adapter, err := adapter.Get("lxc") c.Assert(err, IsNil) wg := sync.WaitGroup{} wg.Add(1) go func() { defer wg.Done() s.reportLogChunks(clientLog) }() config := &client.Config{} config.JobstepID = containerName err = adapter.Init(config) c.Assert(err, IsNil) err = adapter.Prepare(clientLog) c.Assert(err, IsNil) defer adapter.Shutdown(clientLog) cmd, err = client.NewCommand("test", "#!/bin/bash -e\necho hello > foo.txt\nexit 0") c.Assert(err, IsNil) result, err = adapter.Run(cmd, clientLog) c.Assert(err, IsNil) c.Assert(string(result.Output), Equals, "") c.Assert(result.Success, Equals, true) cmd, err = client.NewCommand("test", "#!/bin/bash -e\necho $HOME\nexit 0") cmd.CaptureOutput = true c.Assert(err, IsNil) result, err = adapter.Run(cmd, clientLog) c.Assert(err, IsNil) c.Assert(string(result.Output), Equals, "/home/ubuntu\n") c.Assert(result.Success, Equals, true) // test with a command that expects stdin cmd, err = client.NewCommand("test", "#!/bin/bash -e\nread foo\nexit 1") c.Assert(err, IsNil) result, err = adapter.Run(cmd, clientLog) c.Assert(err, IsNil) c.Assert(string(result.Output), Equals, "") c.Assert(result.Success, Equals, false) artifacts, err := adapter.CollectArtifacts([]string{"foo.txt"}, clientLog) c.Assert(err, IsNil) c.Assert(len(artifacts), Equals, 1) c.Assert(artifacts[0], Matches, ".*/home/ubuntu/foo.txt") clientLog.Close() wg.Wait() }