Skip to content

Commit

Permalink
Fix path and timing issues with integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
NightTsarina committed Aug 19, 2017
1 parent 8839640 commit 2cd49eb
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions node_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
"github.com/prometheus/procfs"
)

var (
binary = filepath.Join(os.Getenv("GOPATH"), "bin/node_exporter")
)
const (
binary = "./node_exporter"
address = "localhost:19100"
)

Expand Down Expand Up @@ -54,7 +56,7 @@ func TestFileDescriptorLeak(t *testing.T) {
return nil
}

if err := runCommandAndTests(exporter, test); err != nil {
if err := runCommandAndTests(exporter, address, test); err != nil {
t.Error(err)
}
}
Expand Down Expand Up @@ -83,7 +85,7 @@ func TestHandlingOfDuplicatedMetrics(t *testing.T) {
return queryExporter(address)
}

if err := runCommandAndTests(exporter, test); err != nil {
if err := runCommandAndTests(exporter, address, test); err != nil {
t.Error(err)
}
}
Expand All @@ -106,27 +108,22 @@ func queryExporter(address string) error {
return nil
}

func runCommandAndTests(cmd *exec.Cmd, fn func(pid int) error) error {
func runCommandAndTests(cmd *exec.Cmd, address string, fn func(pid int) error) error {
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start command: %s", err)
}

errc := make(chan error)
go func() {
if err := cmd.Wait(); err != nil {
errc <- fmt.Errorf("execution of command failed: %s", err)
} else {
errc <- nil
time.Sleep(50 * time.Millisecond)
for i := 0; i < 10; i++ {
if err := queryExporter(address); err == nil {
break
}
time.Sleep(500 * time.Millisecond)
if cmd.Process== nil || i == 9 {
return fmt.Errorf("can't start command")
}
}()

// Allow the process to start before running any tests.
select {
case err := <-errc:
return err
case <-time.After(100 * time.Millisecond):
}

errc := make(chan error)
go func(pid int) {
errc <- fn(pid)
}(cmd.Process.Pid)
Expand Down

0 comments on commit 2cd49eb

Please sign in to comment.