...

Source file src/github.com/Microsoft/hcsshim/cmd/hooks/wait-paths/wait_paths_test.go

Documentation: github.com/Microsoft/hcsshim/cmd/hooks/wait-paths

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"math"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  	"sync"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  func Test_Paths_EmptyString_NotAllowed(t *testing.T) {
    16  	args := []string{
    17  		"wait-paths",
    18  		"-p",
    19  		"",
    20  	}
    21  	app := newCliApp()
    22  	err := app.Run(args)
    23  	if !errors.Is(err, errEmptyPaths) {
    24  		t.Fatalf("expected 'cannot be empty' error, got: %s", err)
    25  	}
    26  }
    27  
    28  func Test_InvalidWaitPath_DefaultTimeout(t *testing.T) {
    29  	args := []string{
    30  		"wait-paths",
    31  		"-p",
    32  		"non-existent",
    33  	}
    34  	app := newCliApp()
    35  	err := app.Run(args)
    36  	if cause := errors.Unwrap(err); !errors.Is(cause, context.DeadlineExceeded) {
    37  		t.Fatalf("expected 'timeout error', got: %s", err)
    38  	}
    39  }
    40  
    41  func Test_InvalidWaitPath_5SecondTimeout(t *testing.T) {
    42  	args := []string{
    43  		"wait-paths",
    44  		"-p",
    45  		"non-existent",
    46  		"-t",
    47  		"5",
    48  	}
    49  	app := newCliApp()
    50  	start := time.Now()
    51  	err := app.Run(args)
    52  	if cause := errors.Unwrap(err); !errors.Is(cause, context.DeadlineExceeded) {
    53  		t.Fatalf("expected 'timeout error', got: %s", err)
    54  	}
    55  
    56  	end := time.Now()
    57  	diff := end.Sub(start)
    58  	diffSeconds := math.Round(diff.Seconds())
    59  	if diffSeconds != 5 {
    60  		t.Fatalf("expected 5 second timeout, got: %f", diffSeconds)
    61  	}
    62  }
    63  
    64  func Test_Valid_Paths_AlreadyPresent(t *testing.T) {
    65  	tmpDir := t.TempDir()
    66  	var files []string
    67  	for _, name := range []string{"file1", "file2"} {
    68  		filePath := filepath.Join(tmpDir, name)
    69  		if f, err := os.Create(filePath); err != nil {
    70  			t.Fatalf("failed to create temporary file %s: %s", name, err)
    71  		} else {
    72  			_ = f.Close()
    73  		}
    74  		files = append(files, filePath)
    75  	}
    76  	pathsArg := strings.Join(files, ",")
    77  
    78  	args := []string{
    79  		"wait-paths",
    80  		"-p",
    81  		pathsArg,
    82  	}
    83  	app := newCliApp()
    84  	if err := app.Run(args); err != nil {
    85  		t.Fatalf("expected no error, got: %s", err)
    86  	}
    87  }
    88  
    89  func Test_Valid_Paths_BecomeAvailableLater(t *testing.T) {
    90  	tmpDir := t.TempDir()
    91  	var files []string
    92  	for _, name := range []string{"file1", "file2"} {
    93  		files = append(files, filepath.Join(tmpDir, name))
    94  	}
    95  	pathsArg := strings.Join(files, ",")
    96  
    97  	errChan := make(chan error)
    98  	var wg sync.WaitGroup
    99  	defer func() {
   100  		wg.Wait()
   101  		close(errChan)
   102  	}()
   103  
   104  	args := []string{
   105  		"wait-paths",
   106  		"-p",
   107  		pathsArg,
   108  	}
   109  	app := newCliApp()
   110  	wg.Add(1)
   111  	go func() {
   112  		defer wg.Done()
   113  		errChan <- app.Run(args)
   114  	}()
   115  
   116  	wg.Add(1)
   117  	go func() {
   118  		defer wg.Done()
   119  		time.Sleep(5 * time.Second)
   120  		for _, fileName := range files {
   121  			if f, err := os.Create(fileName); err != nil {
   122  				errChan <- err
   123  				return
   124  			} else {
   125  				_ = f.Close()
   126  			}
   127  		}
   128  	}()
   129  
   130  	if err := <-errChan; err != nil {
   131  		t.Fatalf("expected no error, got: %s", err)
   132  	}
   133  }
   134  

View as plain text