diff --git a/.woodpecker/integration.yaml b/.woodpecker/integration.yaml index 6abe026de..41c0eb83e 100644 --- a/.woodpecker/integration.yaml +++ b/.woodpecker/integration.yaml @@ -24,14 +24,6 @@ steps: - <<: *when_path - '.woodpecker/**' - dummy-web: - image: *golang_image - commands: - - mkdir -p web/dist/ - - echo "test" > web/dist/index.html - when: - - path: *when_path - integration: image: *golang_image commands: diff --git a/test/integration/main_test.go b/test/integration/main_test.go index 9733bfb24..d3d1d71c6 100644 --- a/test/integration/main_test.go +++ b/test/integration/main_test.go @@ -7,11 +7,11 @@ import ( ) func TestEnvStart(t *testing.T) { - forge, err := utils.StartForge(t) - if err != nil { - t.Fatalf("Could not start forge %s", err) - } - defer forge.Stop() + // forge, err := utils.StartForge(t) + // if err != nil { + // t.Fatalf("Could not start forge %s", err) + // } + // defer forge.Stop() server, err := utils.StartServer(t) if err != nil { diff --git a/test/integration/utils/command.go b/test/integration/utils/command.go new file mode 100644 index 000000000..c99e13ccf --- /dev/null +++ b/test/integration/utils/command.go @@ -0,0 +1,50 @@ +package utils + +import ( + "fmt" + "log" + "os/exec" + "strings" + "testing" +) + +type Command struct { + cmd *exec.Cmd + env map[string]string +} + +func NewTask(cmdName string, args ...string) *Command { + cmd := exec.Command(cmdName, args...) + return &Command{ + cmd: cmd, + } +} + +func (t *Command) WorkDir(workDir string) *Command { + t.cmd.Dir = workDir + return t +} + +func (t *Command) SetEnv(key, value string) *Command { + t.env[key] = value + return t +} + +func (t *Command) Run() (string, error) { + log.Printf("# %s %s", t.cmd.Path, strings.Join(t.cmd.Args, " ")) + env := []string{} + for key, value := range t.env { + env = append(env, fmt.Sprintf("%s=%s", key, value)) + } + t.cmd.Env = env + + output, err := t.cmd.Output() + return string(output), err +} + +func (t *Command) RunOrFail(te *testing.T) { + output, err := t.Run() + if err != nil { + te.Fatalf("Failed to execute command '%s %s': %v\n%s", t.cmd.Path, strings.Join(t.cmd.Args, " "), err, output) + } +} diff --git a/test/integration/utils/repo.go b/test/integration/utils/repo.go index f50bbaf12..4fdd8af1e 100644 --- a/test/integration/utils/repo.go +++ b/test/integration/utils/repo.go @@ -9,11 +9,9 @@ type TestRepo struct { func (r *TestRepo) Clone(t *testing.T, sourcePath, remoteURL string) error { r.folder = t.TempDir() - runOrFail(t, "cp", "-r", sourcePath, r.folder) - - runOrFail(t, "git", "init") - - runOrFail(t, "git", "remote", "add", remoteURL) + NewTask("cp", "-r", sourcePath, r.folder).RunOrFail(t) + NewTask("git", "init").RunOrFail(t) + NewTask("git", "remote", "add", remoteURL).RunOrFail(t) r.Commit(t, ":tada: init") @@ -23,13 +21,13 @@ func (r *TestRepo) Clone(t *testing.T, sourcePath, remoteURL string) error { } func (r *TestRepo) Commit(t *testing.T, message string) { - runOrFail(t, "git", "commit", "-m", message) + NewTask("git", "commit", "-m", message).RunOrFail(t) } func (r *TestRepo) Push(t *testing.T) { - runOrFail(t, "git", "push", "-u", "origin", "main") + NewTask("git", "push", "-u", "origin", "main").RunOrFail(t) } func (r *TestRepo) Tag(t *testing.T, name, message string) { - runOrFail(t, "git", "tag", "-a", name, "-m", message) + NewTask("git", "tag", "-a", name, "-m", message).RunOrFail(t) } diff --git a/test/integration/utils/service.go b/test/integration/utils/service.go index a0f7bc5f2..305e948ad 100644 --- a/test/integration/utils/service.go +++ b/test/integration/utils/service.go @@ -6,16 +6,18 @@ import ( ) type Service struct { - cmdName string - args []string - env map[string]string - workingDir string - cmd *exec.Cmd + cmd *exec.Cmd + env map[string]string +} + +func NewService(cmdName string, args ...string) *Service { + cmd := exec.Command(cmdName, args...) + return &Service{ + cmd: cmd, + } } func (s *Service) Start() error { - s.cmd = exec.Command(s.cmdName, s.args...) - s.cmd.Dir = s.workingDir env := []string{} for key, value := range s.env { env = append(env, fmt.Sprintf("%s=%s", key, value)) @@ -32,17 +34,12 @@ func (s *Service) Stop() error { return s.cmd.Process.Kill() } -func (s *Service) SetEnv(key, value string) { +func (s *Service) SetEnv(key, value string) *Service { s.env[key] = value + return s } -func (s *Service) WorkDir(workDir string) { - s.workingDir = workDir -} - -func NewService(cmdName string, args ...string) *Service { - return &Service{ - cmdName: cmdName, - args: args, - } +func (s *Service) WorkDir(workDir string) *Service { + s.cmd.Dir = workDir + return s } diff --git a/test/integration/utils/utils.go b/test/integration/utils/utils.go index 51cf7a8a1..9a692a213 100644 --- a/test/integration/utils/utils.go +++ b/test/integration/utils/utils.go @@ -1,41 +1,24 @@ package utils import ( - "log" - "os/exec" - "strings" "testing" ) -func runOrFail(t *testing.T, workingDir, cmdName string, args ...string) { - log.Printf("# %s %s", cmdName, strings.Join(args, " ")) - - cmd := exec.Command(cmdName, args...) - cmd.Dir = workingDir - output, err := cmd.Output() - if err != nil { - t.Fatalf("Failed to execute command '%s %s': %v\n%s", cmdName, strings.Join(args, " "), err, output) - } -} - -func StartForge(t *testing.T) (*Service, error) { - runOrFail(t, "", "mkdir", "-p", "web/dist") - runOrFail(t, "", "echo", "\"test\"", "web/dist/index.html") - +func StartForge(_ *testing.T) (*Service, error) { service := NewService("echo", "start", "forge") - service.WorkDir("forge/") return service, service.Start() } -func StartServer(_ *testing.T) (*Service, error) { - service := NewService("go", "run", "./cmd/server/") +func StartServer(t *testing.T) (*Service, error) { + NewTask("mkdir", "-p", "web/dist").RunOrFail(t) + NewTask("echo", "\"test\"", ">", "web/dist/index.html").RunOrFail(t) + service := NewService("go", "run", "./cmd/server/") return service, service.Start() } func StartAgent(_ *testing.T) (*Service, error) { service := NewService("go", "run", "./cmd/agent/") - return service, service.Start() }