github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/integration/lifecycle/rlimit_test.go (about)

     1  package lifecycle_test
     2  
     3  import (
     4  	"io"
     5  	"syscall"
     6  
     7  	"github.com/cloudfoundry-incubator/garden"
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/onsi/gomega/gbytes"
    11  )
    12  
    13  var _ = Describe("Resource limits", func() {
    14  	var (
    15  		container           garden.Container
    16  		privilegedContainer bool
    17  		rlimitValue         uint64
    18  		prevRlimit          syscall.Rlimit
    19  		rlimitResource      int
    20  	)
    21  
    22  	JustBeforeEach(func() {
    23  		err := syscall.Getrlimit(rlimitResource, &prevRlimit)
    24  		Expect(err).ToNot(HaveOccurred())
    25  
    26  		rlimit := syscall.Rlimit{Cur: rlimitValue, Max: rlimitValue}
    27  		err = syscall.Setrlimit(rlimitResource, &rlimit)
    28  		Expect(err).ToNot(HaveOccurred())
    29  
    30  		client = startGarden()
    31  		container, err = client.Create(garden.ContainerSpec{
    32  			Privileged: privilegedContainer,
    33  		})
    34  		Expect(err).ToNot(HaveOccurred())
    35  	})
    36  
    37  	AfterEach(func() {
    38  		err := client.Destroy(container.Handle())
    39  		Expect(err).ToNot(HaveOccurred())
    40  
    41  		err = syscall.Setrlimit(rlimitResource, &prevRlimit)
    42  		Expect(err).ToNot(HaveOccurred())
    43  	})
    44  
    45  	Context("NOFILE rlimit", func() {
    46  		BeforeEach(func() {
    47  			rlimitResource = syscall.RLIMIT_NOFILE
    48  			rlimitValue = 100
    49  		})
    50  
    51  		Context("with a privileged container", func() {
    52  			BeforeEach(func() {
    53  				privilegedContainer = true
    54  			})
    55  
    56  			It("rlimits can be set", func() {
    57  				var nofile uint64 = 1000
    58  				stdout := gbytes.NewBuffer()
    59  				process, err := container.Run(garden.ProcessSpec{
    60  					Path: "sh",
    61  					Args: []string{"-c", "ulimit -n"},
    62  					Limits: garden.ResourceLimits{
    63  						Nofile: &nofile,
    64  					},
    65  				}, garden.ProcessIO{Stdout: io.MultiWriter(stdout, GinkgoWriter), Stderr: GinkgoWriter})
    66  				Expect(err).ToNot(HaveOccurred())
    67  
    68  				Eventually(stdout).Should(gbytes.Say("1000"))
    69  				Expect(process.Wait()).To(Equal(0))
    70  			})
    71  		})
    72  
    73  		Context("with a non-privileged container", func() {
    74  			BeforeEach(func() {
    75  				privilegedContainer = false
    76  			})
    77  
    78  			It("rlimits can be set", func() {
    79  				var nofile uint64 = 1000
    80  				stdout := gbytes.NewBuffer()
    81  				process, err := container.Run(garden.ProcessSpec{
    82  					Path: "sh",
    83  					User: "vcap",
    84  					Args: []string{"-c", "ulimit -n"},
    85  					Limits: garden.ResourceLimits{
    86  						Nofile: &nofile,
    87  					},
    88  				}, garden.ProcessIO{Stdout: io.MultiWriter(stdout, GinkgoWriter), Stderr: GinkgoWriter})
    89  				Expect(err).ToNot(HaveOccurred())
    90  
    91  				Eventually(stdout).Should(gbytes.Say("1000"))
    92  				Expect(process.Wait()).To(Equal(0))
    93  			})
    94  		})
    95  	})
    96  
    97  	Context("AS rlimit", func() {
    98  		BeforeEach(func() {
    99  			rlimitResource = syscall.RLIMIT_AS
   100  			rlimitValue = 2147483648
   101  		})
   102  
   103  		Context("with a privileged container", func() {
   104  			BeforeEach(func() {
   105  				privilegedContainer = true
   106  			})
   107  
   108  			It("rlimits can be set", func() {
   109  				var as uint64 = 4294967296
   110  				stdout := gbytes.NewBuffer()
   111  				process, err := container.Run(garden.ProcessSpec{
   112  					Path: "sh",
   113  					Args: []string{"-c", "ulimit -v"},
   114  					Limits: garden.ResourceLimits{
   115  						As: &as,
   116  					},
   117  				}, garden.ProcessIO{Stdout: io.MultiWriter(stdout, GinkgoWriter), Stderr: GinkgoWriter})
   118  				Expect(err).ToNot(HaveOccurred())
   119  
   120  				Eventually(stdout).Should(gbytes.Say("4194304"))
   121  				Expect(process.Wait()).To(Equal(0))
   122  			})
   123  		})
   124  
   125  		Context("with a non-privileged container", func() {
   126  			BeforeEach(func() {
   127  				privilegedContainer = false
   128  			})
   129  
   130  			It("rlimits can be set", func() {
   131  				var as uint64 = 4294967296
   132  				stdout := gbytes.NewBuffer()
   133  				process, err := container.Run(garden.ProcessSpec{
   134  					Path: "sh",
   135  					User: "vcap",
   136  					Args: []string{"-c", "ulimit -v"},
   137  					Limits: garden.ResourceLimits{
   138  						As: &as,
   139  					},
   140  				}, garden.ProcessIO{Stdout: io.MultiWriter(stdout, GinkgoWriter), Stderr: GinkgoWriter})
   141  				Expect(err).ToNot(HaveOccurred())
   142  
   143  				Eventually(stdout).Should(gbytes.Say("4194304"))
   144  				Expect(process.Wait()).To(Equal(0))
   145  			})
   146  		})
   147  	})
   148  })