github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/client/driver/logging/rotator_test.go (about)

     1  package logging
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/hashicorp/nomad/testutil"
    12  )
    13  
    14  var (
    15  	logger       = log.New(os.Stdout, "", log.LstdFlags)
    16  	pathPrefix   = "logrotator"
    17  	baseFileName = "redis.stdout"
    18  )
    19  
    20  func TestFileRotator_IncorrectPath(t *testing.T) {
    21  	t.Parallel()
    22  	if _, err := NewFileRotator("/foo", baseFileName, 10, 10, logger); err == nil {
    23  		t.Fatalf("expected error")
    24  	}
    25  }
    26  
    27  func TestFileRotator_CreateNewFile(t *testing.T) {
    28  	t.Parallel()
    29  	var path string
    30  	var err error
    31  	if path, err = ioutil.TempDir("", pathPrefix); err != nil {
    32  		t.Fatalf("test setup err: %v", err)
    33  	}
    34  	defer os.RemoveAll(path)
    35  
    36  	_, err = NewFileRotator(path, baseFileName, 10, 10, logger)
    37  	if err != nil {
    38  		t.Fatalf("test setup err: %v", err)
    39  	}
    40  
    41  	if _, err := os.Stat(filepath.Join(path, "redis.stdout.0")); err != nil {
    42  		t.Fatalf("expected file")
    43  	}
    44  }
    45  
    46  func TestFileRotator_OpenLastFile(t *testing.T) {
    47  	t.Parallel()
    48  	var path string
    49  	var err error
    50  	if path, err = ioutil.TempDir("", pathPrefix); err != nil {
    51  		t.Fatalf("test setup err: %v", err)
    52  	}
    53  	defer os.RemoveAll(path)
    54  
    55  	fname1 := filepath.Join(path, "redis.stdout.0")
    56  	fname2 := filepath.Join(path, "redis.stdout.2")
    57  	if _, err := os.Create(fname1); err != nil {
    58  		t.Fatalf("test setup failure: %v", err)
    59  	}
    60  	if _, err := os.Create(fname2); err != nil {
    61  		t.Fatalf("test setup failure: %v", err)
    62  	}
    63  
    64  	fr, err := NewFileRotator(path, baseFileName, 10, 10, logger)
    65  	if err != nil {
    66  		t.Fatalf("test setup err: %v", err)
    67  	}
    68  
    69  	if fr.currentFile.Name() != fname2 {
    70  		t.Fatalf("expected current file: %v, got: %v", fname2, fr.currentFile.Name())
    71  	}
    72  }
    73  
    74  func TestFileRotator_WriteToCurrentFile(t *testing.T) {
    75  	t.Parallel()
    76  	var path string
    77  	var err error
    78  	if path, err = ioutil.TempDir("", pathPrefix); err != nil {
    79  		t.Fatalf("test setup err: %v", err)
    80  	}
    81  	defer os.RemoveAll(path)
    82  
    83  	fname1 := filepath.Join(path, "redis.stdout.0")
    84  	if _, err := os.Create(fname1); err != nil {
    85  		t.Fatalf("test setup failure: %v", err)
    86  	}
    87  
    88  	fr, err := NewFileRotator(path, baseFileName, 10, 5, logger)
    89  	if err != nil {
    90  		t.Fatalf("test setup err: %v", err)
    91  	}
    92  
    93  	fr.Write([]byte("abcde"))
    94  
    95  	var actual int64
    96  	testutil.WaitForResult(func() (bool, error) {
    97  		fi, err := os.Stat(fname1)
    98  		if err != nil {
    99  			return false, err
   100  		}
   101  		actual = fi.Size()
   102  		if actual != 5 {
   103  			return false, nil
   104  		}
   105  
   106  		return true, nil
   107  	}, func(err error) {
   108  		t.Fatalf("expected size: %v, actual: %v", 5, actual)
   109  	})
   110  }
   111  
   112  func TestFileRotator_RotateFiles(t *testing.T) {
   113  	t.Parallel()
   114  	var path string
   115  	var err error
   116  	if path, err = ioutil.TempDir("", pathPrefix); err != nil {
   117  		t.Fatalf("test setup err: %v", err)
   118  	}
   119  	defer os.RemoveAll(path)
   120  
   121  	fr, err := NewFileRotator(path, baseFileName, 10, 5, logger)
   122  	if err != nil {
   123  		t.Fatalf("test setup err: %v", err)
   124  	}
   125  
   126  	str := "abcdefgh"
   127  	nw, err := fr.Write([]byte(str))
   128  	if err != nil {
   129  		t.Fatalf("got error while writing: %v", err)
   130  	}
   131  
   132  	if nw != len(str) {
   133  		t.Fatalf("expected %v, got %v", len(str), nw)
   134  	}
   135  
   136  	var lastErr error
   137  	testutil.WaitForResult(func() (bool, error) {
   138  		fname1 := filepath.Join(path, "redis.stdout.0")
   139  		fi, err := os.Stat(fname1)
   140  		if err != nil {
   141  			lastErr = err
   142  			return false, nil
   143  		}
   144  		if fi.Size() != 5 {
   145  			lastErr = fmt.Errorf("expected size: %v, actual: %v", 5, fi.Size())
   146  			return false, nil
   147  		}
   148  
   149  		fname2 := filepath.Join(path, "redis.stdout.1")
   150  		if _, err := os.Stat(fname2); err != nil {
   151  			lastErr = fmt.Errorf("expected file %v to exist", fname2)
   152  			return false, nil
   153  		}
   154  
   155  		if fi2, err := os.Stat(fname2); err == nil {
   156  			if fi2.Size() != 3 {
   157  				lastErr = fmt.Errorf("expected size: %v, actual: %v", 3, fi2.Size())
   158  				return false, nil
   159  			}
   160  		} else {
   161  			lastErr = fmt.Errorf("error getting the file info: %v", err)
   162  			return false, nil
   163  		}
   164  
   165  		return true, nil
   166  	}, func(err error) {
   167  		t.Fatalf("%v", lastErr)
   168  	})
   169  }
   170  
   171  func TestFileRotator_WriteRemaining(t *testing.T) {
   172  	t.Parallel()
   173  	var path string
   174  	var err error
   175  	if path, err = ioutil.TempDir("", pathPrefix); err != nil {
   176  		t.Fatalf("test setup err: %v", err)
   177  	}
   178  	defer os.RemoveAll(path)
   179  
   180  	fname1 := filepath.Join(path, "redis.stdout.0")
   181  	if f, err := os.Create(fname1); err == nil {
   182  		f.Write([]byte("abcd"))
   183  	} else {
   184  		t.Fatalf("test setup failure: %v", err)
   185  	}
   186  
   187  	fr, err := NewFileRotator(path, baseFileName, 10, 5, logger)
   188  	if err != nil {
   189  		t.Fatalf("test setup err: %v", err)
   190  	}
   191  
   192  	str := "efghijkl"
   193  	nw, err := fr.Write([]byte(str))
   194  	if err != nil {
   195  		t.Fatalf("got error while writing: %v", err)
   196  	}
   197  	if nw != len(str) {
   198  		t.Fatalf("expected %v, got %v", len(str), nw)
   199  	}
   200  	var lastErr error
   201  	testutil.WaitForResult(func() (bool, error) {
   202  		fi, err := os.Stat(fname1)
   203  		if err != nil {
   204  			lastErr = fmt.Errorf("error getting the file info: %v", err)
   205  			return false, nil
   206  		}
   207  		if fi.Size() != 5 {
   208  			lastErr = fmt.Errorf("expected size: %v, actual: %v", 5, fi.Size())
   209  			return false, nil
   210  		}
   211  
   212  		fname2 := filepath.Join(path, "redis.stdout.1")
   213  		if _, err := os.Stat(fname2); err != nil {
   214  			lastErr = fmt.Errorf("expected file %v to exist", fname2)
   215  			return false, nil
   216  		}
   217  
   218  		if fi2, err := os.Stat(fname2); err == nil {
   219  			if fi2.Size() != 5 {
   220  				lastErr = fmt.Errorf("expected size: %v, actual: %v", 5, fi2.Size())
   221  				return false, nil
   222  			}
   223  		} else {
   224  			lastErr = fmt.Errorf("error getting the file info: %v", err)
   225  			return false, nil
   226  		}
   227  
   228  		fname3 := filepath.Join(path, "redis.stdout.2")
   229  		if _, err := os.Stat(fname3); err != nil {
   230  			lastErr = fmt.Errorf("expected file %v to exist", fname3)
   231  			return false, nil
   232  		}
   233  
   234  		if fi3, err := os.Stat(fname3); err == nil {
   235  			if fi3.Size() != 2 {
   236  				lastErr = fmt.Errorf("expected size: %v, actual: %v", 2, fi3.Size())
   237  				return false, nil
   238  			}
   239  		} else {
   240  			lastErr = fmt.Errorf("error getting the file info: %v", err)
   241  			return false, nil
   242  		}
   243  
   244  		return true, nil
   245  	}, func(err error) {
   246  		t.Fatalf("%v", lastErr)
   247  	})
   248  
   249  }
   250  
   251  func TestFileRotator_PurgeOldFiles(t *testing.T) {
   252  	t.Parallel()
   253  	var path string
   254  	var err error
   255  	if path, err = ioutil.TempDir("", pathPrefix); err != nil {
   256  		t.Fatalf("test setup err: %v", err)
   257  	}
   258  	defer os.RemoveAll(path)
   259  
   260  	fr, err := NewFileRotator(path, baseFileName, 2, 2, logger)
   261  	if err != nil {
   262  		t.Fatalf("test setup err: %v", err)
   263  	}
   264  
   265  	str := "abcdeghijklmn"
   266  	nw, err := fr.Write([]byte(str))
   267  	if err != nil {
   268  		t.Fatalf("got error while writing: %v", err)
   269  	}
   270  	if nw != len(str) {
   271  		t.Fatalf("expected %v, got %v", len(str), nw)
   272  	}
   273  
   274  	var lastErr error
   275  	testutil.WaitForResult(func() (bool, error) {
   276  		f, err := ioutil.ReadDir(path)
   277  		if err != nil {
   278  			lastErr = fmt.Errorf("test error: %v", err)
   279  			return false, nil
   280  		}
   281  
   282  		if len(f) != 2 {
   283  			lastErr = fmt.Errorf("expected number of files: %v, got: %v", 2, len(f))
   284  			return false, nil
   285  		}
   286  
   287  		return true, nil
   288  	}, func(err error) {
   289  		t.Fatalf("%v", lastErr)
   290  	})
   291  }