github.com/geofffranks/garden-linux@v0.0.0-20160715111146-26c893169cfa/sysinfo/max_valid_uid_test.go (about)

     1  package sysinfo_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	"code.cloudfoundry.org/garden-linux/sysinfo"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("MaxValidUid", func() {
    13  	var (
    14  		tmpFiles []string
    15  	)
    16  
    17  	writeTmpFile := func(contents string) string {
    18  		tmpFile, err := ioutil.TempFile("", "")
    19  		Expect(err).ToNot(HaveOccurred())
    20  		defer tmpFile.Close()
    21  
    22  		_, err = tmpFile.WriteString(contents)
    23  		Expect(err).ToNot(HaveOccurred())
    24  
    25  		tmpFiles = append(tmpFiles, tmpFile.Name())
    26  		return tmpFile.Name()
    27  	}
    28  
    29  	BeforeEach(func() {
    30  		tmpFiles = make([]string, 0)
    31  	})
    32  
    33  	AfterEach(func() {
    34  		for _, f := range tmpFiles {
    35  			Expect(os.RemoveAll(f)).To(Succeed())
    36  		}
    37  	})
    38  
    39  	Context("when the file has no entries", func() {
    40  		It("should return 0", func() {
    41  			Expect(sysinfo.IDMap(writeTmpFile("")).MaxValid()).To(Equal(0))
    42  		})
    43  	})
    44  
    45  	Context("when the file has a single line", func() {
    46  		It("returns ones less than the containerid column + the size column", func() {
    47  			Expect(sysinfo.IDMap(writeTmpFile("12345 0 3")).MaxValid()).To(Equal(12347))
    48  		})
    49  	})
    50  
    51  	Context("when the file has a multiple lines", func() {
    52  		It("returns the largest value", func() {
    53  			Expect(sysinfo.IDMap(writeTmpFile("12345 0 3\n44 0 1")).MaxValid()).To(Equal(12347))
    54  			Expect(sysinfo.IDMap(writeTmpFile("44 0 1\n12345 0 1")).MaxValid()).To(Equal(12345))
    55  		})
    56  	})
    57  
    58  	Context("when a line is invalid", func() {
    59  		It("returns an error", func() {
    60  			_, err := sysinfo.IDMap(writeTmpFile("cake")).MaxValid()
    61  			Expect(err).To(MatchError(`expected integer while parsing line "cake"`))
    62  		})
    63  	})
    64  })