Skip to content

Commit

Permalink
tests: add tests for wait-paths
Browse files Browse the repository at this point in the history
Follow up PR to add tests for wait-paths after initial PR microsoft#1258
was merged.

Signed-off-by: Maksim An <maksiman@microsoft.com>
  • Loading branch information
anmaxvl committed Apr 30, 2022
1 parent a783367 commit 296c002
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 6 deletions.
21 changes: 15 additions & 6 deletions cmd/hooks/wait-paths/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// +build linux

package main

import (
"context"
"errors"
"fmt"
"os"
"strings"
Expand All @@ -22,6 +21,14 @@ const (
// The hook has required list of comma-separated paths and a default timeout in seconds.

func main() {
app := newCliApp()
if err := app.Run(os.Args); err != nil {
logrus.Fatalf("%s\n", err)
}
os.Exit(0)
}

func newCliApp() *cli.App {
app := cli.NewApp()
app.Name = "wait-paths"
app.Usage = "Provide a list paths and an optional timeout"
Expand All @@ -38,14 +45,16 @@ func main() {
},
}
app.Action = run
if err := app.Run(os.Args); err != nil {
logrus.Fatalf("%s\n", err)
}
os.Exit(0)
return app
}

func run(cCtx *cli.Context) error {
timeout := cCtx.GlobalInt(timeoutFlag)

pathsVal := cCtx.GlobalString(pathsFlag)
if pathsVal == "" {
return errors.New("paths cannot be empty")
}
paths := strings.Split(cCtx.GlobalString(pathsFlag), ",")

waitCtx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
Expand Down
136 changes: 136 additions & 0 deletions cmd/hooks/wait-paths/wait_paths_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package main

import (
"math"
"os"
"path/filepath"
"strings"
"testing"
"time"
)

func Test_Paths_EmptyString_NotAllowed(t *testing.T) {
args := []string{
"wait-paths",
"-p",
"",
}
app := newCliApp()
err := app.Run(args)
if err == nil {
t.Fatal("expected error, got nothing")
}
if !strings.Contains(err.Error(), "cannot be empty") {
t.Fatalf("expected 'cannot be empty' error, got: %s", err)
}
}

func Test_InvalidWaitPath_DefaultTimeout(t *testing.T) {
args := []string{
"wait-paths",
"-p",
"non-existent",
}
app := newCliApp()
err := app.Run(args)
if err == nil {
t.Fatalf("expected error, got nothing")
}
if !strings.Contains(err.Error(), "timeout while waiting") {
t.Fatalf("expected 'timeout error', got: %s", err)
}
}

func Test_InvalidWaitPath_5SecondTimeout(t *testing.T) {
args := []string{
"wait-paths",
"-p",
"non-existent",
"-t",
"5",
}
app := newCliApp()
start := time.Now()
err := app.Run(args)
if err == nil {
t.Fatal("expected timeout error, got nothing")
}
if !strings.Contains(err.Error(), "timeout while waiting") {
t.Fatalf("expected 'timeout error', got: %s", err)
}

end := time.Now()
diff := end.Sub(start)
diffSeconds := math.Round(diff.Seconds())
if diffSeconds != 5 {
t.Fatalf("expected 5 second timeout, got: %f", diffSeconds)
}
}

func Test_Valid_Paths_AlreadyPresent(t *testing.T) {
tmpDir := t.TempDir()
var files []string
for _, name := range []string{"file1", "file2"} {
filePath := filepath.Join(tmpDir, name)
if f, err := os.Create(filePath); err != nil {
t.Fatalf("failed to create temporary file %s: %s", name, err)
} else {
_ = f.Close()
}
files = append(files, filePath)
}
defer func() {
_ = os.RemoveAll(tmpDir)
}()
pathsArg := strings.Join(files, ",")

args := []string{
"wait-paths",
"-p",
pathsArg,
}
app := newCliApp()
if err := app.Run(args); err != nil {
t.Fatalf("expected no error, got: %s", err)
}
}

func Test_Valid_Paths_BecomeAvailableLater(t *testing.T) {
tmpDir := t.TempDir()
var files []string
for _, name := range []string{"file1", "file2"} {
files = append(files, filepath.Join(tmpDir, name))
}
pathsArg := strings.Join(files, ",")
defer func() {
_ = os.RemoveAll(tmpDir)
}()

errChan := make(chan error)
args := []string{
"wait-paths",
"-p",
pathsArg,
}
app := newCliApp()
go func() {
errChan <- app.Run(args)
}()

go func() {
time.Sleep(5 * time.Second)
for _, fileName := range files {
if f, err := os.Create(fileName); err != nil {
errChan <- err
return
} else {
_ = f.Close()
}
}
}()

if err := <-errChan; err != nil {
close(errChan)
t.Fatalf("expected no error, got: %s", err)
}
}

0 comments on commit 296c002

Please sign in to comment.