github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/old/quota_manager/quota_manager_test.go (about)

     1  package quota_manager_test
     2  
     3  import (
     4  	"errors"
     5  	"os/exec"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	"github.com/pivotal-golang/lager/lagertest"
    10  
    11  	"github.com/cloudfoundry-incubator/garden"
    12  	"github.com/cloudfoundry-incubator/garden-linux/old/quota_manager"
    13  	"github.com/cloudfoundry/gunk/command_runner/fake_command_runner"
    14  	. "github.com/cloudfoundry/gunk/command_runner/fake_command_runner/matchers"
    15  )
    16  
    17  var _ = Describe("Linux Quota manager", func() {
    18  	var fakeRunner *fake_command_runner.FakeCommandRunner
    19  	var logger *lagertest.TestLogger
    20  	var quotaManager *quota_manager.LinuxQuotaManager
    21  
    22  	BeforeEach(func() {
    23  		fakeRunner = fake_command_runner.New()
    24  		logger = lagertest.NewTestLogger("test")
    25  		quotaManager = quota_manager.New(fakeRunner, "/some/mount/point", "/root/path")
    26  	})
    27  
    28  	Describe("setting quotas", func() {
    29  		limits := garden.DiskLimits{
    30  			BlockSoft: 1,
    31  			BlockHard: 2,
    32  
    33  			InodeSoft: 11,
    34  			InodeHard: 12,
    35  		}
    36  
    37  		It("executes setquota on the container depo's mount point", func() {
    38  			err := quotaManager.SetLimits(logger, 1234, limits)
    39  
    40  			Expect(err).ToNot(HaveOccurred())
    41  
    42  			Expect(fakeRunner).To(HaveExecutedSerially(
    43  				fake_command_runner.CommandSpec{
    44  					Path: "setquota",
    45  					Args: []string{
    46  						"-u", "1234",
    47  						"1", "2", "11", "12",
    48  						"/some/mount/point",
    49  					},
    50  				},
    51  			))
    52  		})
    53  
    54  		Context("when bytes are given", func() {
    55  			limits := garden.DiskLimits{
    56  				InodeSoft: 11,
    57  				InodeHard: 12,
    58  
    59  				ByteSoft: 102401,
    60  				ByteHard: 204801,
    61  			}
    62  
    63  			It("executes setquota with them converted to blocks", func() {
    64  				err := quotaManager.SetLimits(logger, 1234, limits)
    65  
    66  				Expect(err).ToNot(HaveOccurred())
    67  
    68  				Expect(fakeRunner).To(HaveExecutedSerially(
    69  					fake_command_runner.CommandSpec{
    70  						Path: "setquota",
    71  						Args: []string{
    72  							"-u", "1234",
    73  							"101", "201", "11", "12",
    74  							"/some/mount/point",
    75  						},
    76  					},
    77  				))
    78  			})
    79  		})
    80  
    81  		Context("when setquota fails", func() {
    82  			nastyError := errors.New("oh no!")
    83  
    84  			BeforeEach(func() {
    85  				fakeRunner.WhenRunning(
    86  					fake_command_runner.CommandSpec{
    87  						Path: "setquota",
    88  					}, func(*exec.Cmd) error {
    89  						return nastyError
    90  					},
    91  				)
    92  			})
    93  
    94  			It("returns the error", func() {
    95  				err := quotaManager.SetLimits(logger, 1234, limits)
    96  				Expect(err).To(Equal(nastyError))
    97  			})
    98  		})
    99  
   100  		Context("when quotas are disabled", func() {
   101  			BeforeEach(func() {
   102  				quotaManager.Disable()
   103  			})
   104  
   105  			It("runs nothing", func() {
   106  				err := quotaManager.SetLimits(logger, 1234, limits)
   107  
   108  				Expect(err).ToNot(HaveOccurred())
   109  
   110  				Expect(fakeRunner).ToNot(HaveExecutedSerially(
   111  					fake_command_runner.CommandSpec{
   112  						Path: "setquota",
   113  					},
   114  				))
   115  			})
   116  		})
   117  	})
   118  
   119  	Describe("getting quotas limits", func() {
   120  		It("executes repquota in the root path", func() {
   121  			fakeRunner.WhenRunning(
   122  				fake_command_runner.CommandSpec{
   123  					Path: "/root/path/repquota",
   124  					Args: []string{"/some/mount/point", "1234"},
   125  				}, func(cmd *exec.Cmd) error {
   126  					cmd.Stdout.Write([]byte("1234 111 222 333 444 555 666 777 888\n"))
   127  
   128  					return nil
   129  				},
   130  			)
   131  
   132  			limits, err := quotaManager.GetLimits(logger, 1234)
   133  			Expect(err).ToNot(HaveOccurred())
   134  
   135  			Expect(limits.BlockSoft).To(Equal(uint64(222)))
   136  			Expect(limits.BlockHard).To(Equal(uint64(333)))
   137  
   138  			Expect(limits.InodeSoft).To(Equal(uint64(666)))
   139  			Expect(limits.InodeHard).To(Equal(uint64(777)))
   140  		})
   141  
   142  		Context("when repquota fails", func() {
   143  			disaster := errors.New("oh no!")
   144  
   145  			BeforeEach(func() {
   146  				fakeRunner.WhenRunning(
   147  					fake_command_runner.CommandSpec{
   148  						Path: "/root/path/repquota",
   149  						Args: []string{"/some/mount/point", "1234"},
   150  					}, func(cmd *exec.Cmd) error {
   151  						return disaster
   152  					},
   153  				)
   154  			})
   155  
   156  			It("returns the error", func() {
   157  				_, err := quotaManager.GetLimits(logger, 1234)
   158  				Expect(err).To(Equal(disaster))
   159  			})
   160  		})
   161  
   162  		Context("when the output of repquota is malformed", func() {
   163  			It("returns an error", func() {
   164  				fakeRunner.WhenRunning(
   165  					fake_command_runner.CommandSpec{
   166  						Path: "/root/path/repquota",
   167  						Args: []string{"/some/mount/point", "1234"},
   168  					}, func(cmd *exec.Cmd) error {
   169  						cmd.Stdout.Write([]byte("abc\n"))
   170  
   171  						return nil
   172  					},
   173  				)
   174  
   175  				_, err := quotaManager.GetLimits(logger, 1234)
   176  				Expect(err).To(HaveOccurred())
   177  			})
   178  		})
   179  
   180  		Context("when quotas are disabled", func() {
   181  			BeforeEach(func() {
   182  				quotaManager.Disable()
   183  			})
   184  
   185  			It("runs nothing", func() {
   186  				limits, err := quotaManager.GetLimits(logger, 1234)
   187  				Expect(err).ToNot(HaveOccurred())
   188  
   189  				Expect(limits).To(BeZero())
   190  
   191  				Expect(fakeRunner).ToNot(HaveExecutedSerially(
   192  					fake_command_runner.CommandSpec{
   193  						Path: "/root/path/bin/repquota",
   194  					},
   195  				))
   196  			})
   197  		})
   198  	})
   199  
   200  	Describe("getting usage", func() {
   201  		It("executes repquota in the root path", func() {
   202  			fakeRunner.WhenRunning(
   203  				fake_command_runner.CommandSpec{
   204  					Path: "/root/path/repquota",
   205  					Args: []string{"/some/mount/point", "1234"},
   206  				}, func(cmd *exec.Cmd) error {
   207  					cmd.Stdout.Write([]byte("1234 111 222 333 444 555 666 777 888\n"))
   208  
   209  					return nil
   210  				},
   211  			)
   212  
   213  			limits, err := quotaManager.GetUsage(logger, 1234)
   214  			Expect(err).ToNot(HaveOccurred())
   215  
   216  			Expect(limits.BytesUsed).To(Equal(uint64(111)))
   217  			Expect(limits.InodesUsed).To(Equal(uint64(555)))
   218  		})
   219  
   220  		Context("when repquota fails", func() {
   221  			disaster := errors.New("oh no!")
   222  
   223  			BeforeEach(func() {
   224  				fakeRunner.WhenRunning(
   225  					fake_command_runner.CommandSpec{
   226  						Path: "/root/path/repquota",
   227  						Args: []string{"/some/mount/point", "1234"},
   228  					}, func(cmd *exec.Cmd) error {
   229  						return disaster
   230  					},
   231  				)
   232  			})
   233  
   234  			It("returns the error", func() {
   235  				_, err := quotaManager.GetUsage(logger, 1234)
   236  				Expect(err).To(Equal(disaster))
   237  			})
   238  		})
   239  
   240  		Context("when the output of repquota is malformed", func() {
   241  			It("returns an error", func() {
   242  				fakeRunner.WhenRunning(
   243  					fake_command_runner.CommandSpec{
   244  						Path: "/root/path/repquota",
   245  						Args: []string{"/some/mount/point", "1234"},
   246  					}, func(cmd *exec.Cmd) error {
   247  						cmd.Stdout.Write([]byte("abc\n"))
   248  
   249  						return nil
   250  					},
   251  				)
   252  
   253  				_, err := quotaManager.GetUsage(logger, 1234)
   254  				Expect(err).To(HaveOccurred())
   255  			})
   256  		})
   257  
   258  		Context("when quotas are disabled", func() {
   259  			BeforeEach(func() {
   260  				quotaManager.Disable()
   261  			})
   262  
   263  			It("runs nothing", func() {
   264  				usage, err := quotaManager.GetUsage(logger, 1234)
   265  				Expect(err).ToNot(HaveOccurred())
   266  
   267  				Expect(usage).To(BeZero())
   268  
   269  				Expect(fakeRunner).ToNot(HaveExecutedSerially(
   270  					fake_command_runner.CommandSpec{
   271  						Path: "/root/path/repquota",
   272  					},
   273  				))
   274  			})
   275  		})
   276  	})
   277  
   278  	Describe("getting the mount point", func() {
   279  		It("returns the mount point of the container depot", func() {
   280  			Expect(quotaManager.MountPoint()).To(Equal("/some/mount/point"))
   281  		})
   282  	})
   283  })