github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/internal/testhelpers/helpers.go (about)

     1  // Copyright (c) 2018 DDN. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package testhelpers
     6  
     7  import (
     8  	"io"
     9  	"io/ioutil"
    10  	"os"
    11  	"testing"
    12  )
    13  
    14  var testPrefix = "ptest"
    15  
    16  // TempDir returns path to a new temporary directory and function that will
    17  // forcibly remove it.
    18  func TempDir(t *testing.T) (string, func()) {
    19  	tdir, err := ioutil.TempDir("", testPrefix)
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	return tdir, func() {
    24  		err = os.RemoveAll(tdir)
    25  		if err != nil {
    26  			t.Fatal(err)
    27  		}
    28  	}
    29  }
    30  
    31  // ChdirTemp changes the working directory to a new TempDir. The cleanup
    32  // function returns to the previous working directoy and removes the temp
    33  // directory.
    34  func ChdirTemp(t *testing.T) func() {
    35  	tdir, cleanDir := TempDir(t)
    36  
    37  	cwd, err := os.Getwd()
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	err = os.Chdir(tdir)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	return func() {
    48  		err := os.Chdir(cwd)
    49  		if err != nil {
    50  			t.Fatal(err)
    51  		}
    52  		cleanDir()
    53  	}
    54  }
    55  
    56  // Fill writes size amount of bytes to the file.
    57  func Fill(t *testing.T, fp io.Writer, size int64) {
    58  	var bs int64 = 1024 * 1024
    59  	buf := make([]byte, bs)
    60  
    61  	for i := 0; i < len(buf); i++ {
    62  		buf[i] = byte(i)
    63  	}
    64  
    65  	for i := int64(0); i < size; i += bs {
    66  		if size < bs {
    67  			bs = size
    68  		}
    69  		fp.Write(buf[:bs])
    70  
    71  	}
    72  }
    73  
    74  // CorruptFile writes an string to the beginning of the file.
    75  func CorruptFile(t *testing.T, path string) {
    76  	fp, err := os.OpenFile(path, os.O_RDWR, 0644)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	_, err = fp.Write([]byte("Silent data corruption. :)"))
    81  	if err != nil {
    82  		t.Fatal(err)
    83  
    84  	}
    85  	err = fp.Close()
    86  	if err != nil {
    87  		t.Fatal(err)
    88  
    89  	}
    90  }
    91  
    92  // TempFile creates a temporary file. If size is >0 then that amount of bytes
    93  // will be written to the file.
    94  func TempFile(t *testing.T, size int64) (string, func()) {
    95  	fp, err := ioutil.TempFile(".", testPrefix)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  	defer fp.Close()
   100  
   101  	if size > 0 {
   102  		Fill(t, fp, size)
   103  	}
   104  	name := fp.Name()
   105  	return name, func() {
   106  		err := os.Remove(name)
   107  		if err != nil {
   108  			t.Fatal(err)
   109  		}
   110  	}
   111  }
   112  
   113  // CopyFile copies data from one file another. If the target file does  not
   114  // exist then it will be created with the given mode. This is a non-optimal copy
   115  // and not intended to be used for very large files.
   116  func CopyFile(t *testing.T, src string, dest string, mode os.FileMode) {
   117  	buf, err := ioutil.ReadFile(src)
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  
   122  	err = ioutil.WriteFile(dest, buf, mode)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  }
   127  
   128  // TempCopy copies provided file to a new temp file that will be assigned the
   129  // provided mode after the copy. (So the mode can specify a read-only file.)
   130  func TempCopy(t *testing.T, src string, mode os.FileMode) (string, func()) {
   131  	tmpFile, cleanup := TempFile(t, 0)
   132  	CopyFile(t, src, tmpFile, mode)
   133  
   134  	// ensure file has correct mode, in case we're overwriting
   135  	err := os.Chmod(tmpFile, mode)
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  
   140  	return tmpFile, cleanup
   141  }
   142  
   143  // Action yields "action".
   144  // Srsly, wtf?
   145  func Action(t *testing.T) string {
   146  	return "action"
   147  }