github.com/carlanton/docker@v1.8.0-rc1/pkg/sysinfo/sysinfo_linux_test.go (about)

     1  package sysinfo
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  func TestReadProcBool(t *testing.T) {
    12  	tmpDir, err := ioutil.TempDir("", "test-sysinfo-proc")
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	defer os.RemoveAll(tmpDir)
    17  
    18  	procFile := filepath.Join(tmpDir, "read-proc-bool")
    19  	if err := ioutil.WriteFile(procFile, []byte("1"), 644); err != nil {
    20  		t.Fatal(err)
    21  	}
    22  
    23  	if !readProcBool(procFile) {
    24  		t.Fatal("expected proc bool to be true, got false")
    25  	}
    26  
    27  	if err := ioutil.WriteFile(procFile, []byte("0"), 644); err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	if readProcBool(procFile) {
    31  		t.Fatal("expected proc bool to be false, got false")
    32  	}
    33  
    34  	if readProcBool(path.Join(tmpDir, "no-exist")) {
    35  		t.Fatal("should be false for non-existent entry")
    36  	}
    37  
    38  }
    39  
    40  func TestCgroupEnabled(t *testing.T) {
    41  	cgroupDir, err := ioutil.TempDir("", "cgroup-test")
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	defer os.RemoveAll(cgroupDir)
    46  
    47  	if cgroupEnabled(cgroupDir, "test") {
    48  		t.Fatal("cgroupEnabled should be false")
    49  	}
    50  
    51  	if err := ioutil.WriteFile(path.Join(cgroupDir, "test"), []byte{}, 644); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	if !cgroupEnabled(cgroupDir, "test") {
    56  		t.Fatal("cgroupEnabled should be true")
    57  	}
    58  }