github.com/manicqin/nomad@v0.9.5/client/allocdir/testing.go (about)

     1  package allocdir
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	hclog "github.com/hashicorp/go-hclog"
     8  	testing "github.com/mitchellh/go-testing-interface"
     9  )
    10  
    11  // TestAllocDir returns a built alloc dir in a temporary directory and cleanup
    12  // func.
    13  func TestAllocDir(t testing.T, l hclog.Logger, prefix string) (*AllocDir, func()) {
    14  	dir, err := ioutil.TempDir("", prefix)
    15  	if err != nil {
    16  		t.Fatalf("Couldn't create temp dir: %v", err)
    17  	}
    18  
    19  	allocDir := NewAllocDir(l, dir)
    20  
    21  	cleanup := func() {
    22  		if err := os.RemoveAll(dir); err != nil {
    23  			t.Logf("error cleaning up alloc dir %q: %v", prefix, err)
    24  		}
    25  
    26  		if err := allocDir.Destroy(); err != nil {
    27  			t.Logf("error cleaning up alloc dir %q: %v", prefix, err)
    28  		}
    29  	}
    30  
    31  	if err := allocDir.Build(); err != nil {
    32  		cleanup()
    33  		t.Fatalf("error building alloc dir %q: %v", prefix, err)
    34  	}
    35  
    36  	return allocDir, cleanup
    37  }