github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/mongo/prealloc_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package mongo_test
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	jc "github.com/juju/testing/checkers"
     9  	gc "gopkg.in/check.v1"
    10  
    11  	"github.com/juju/juju/mongo"
    12  	coretesting "github.com/juju/juju/testing"
    13  )
    14  
    15  type preallocSuite struct {
    16  	coretesting.BaseSuite
    17  }
    18  
    19  var _ = gc.Suite(&preallocSuite{})
    20  
    21  func (s *preallocSuite) TestOplogSize(c *gc.C) {
    22  	type test struct {
    23  		hostWordSize int
    24  		runtimeGOOS  string
    25  		availSpace   int
    26  		expected     int
    27  	}
    28  	tests := []test{{
    29  		hostWordSize: 64,
    30  		runtimeGOOS:  "darwin",
    31  		availSpace:   99999,
    32  		expected:     183,
    33  	}, {
    34  		hostWordSize: 64,
    35  		runtimeGOOS:  "linux",
    36  		availSpace:   1024,
    37  		expected:     512,
    38  	}, {
    39  		hostWordSize: 64,
    40  		runtimeGOOS:  "linux",
    41  		availSpace:   420 * 1024,
    42  		expected:     1024,
    43  	}, {
    44  		hostWordSize: 64,
    45  		runtimeGOOS:  "linux",
    46  		availSpace:   1024 * 1024,
    47  		expected:     1024,
    48  	}}
    49  	var availSpace int
    50  	getAvailSpace := func(dir string) (float64, error) {
    51  		return float64(availSpace), nil
    52  	}
    53  	s.PatchValue(mongo.AvailSpace, getAvailSpace)
    54  	for i, test := range tests {
    55  		c.Logf("test %d: %+v", i, test)
    56  		s.PatchValue(mongo.RuntimeGOOS, test.runtimeGOOS)
    57  		availSpace = test.availSpace
    58  		size, err := mongo.DefaultOplogSize("")
    59  		c.Check(err, jc.ErrorIsNil)
    60  		c.Check(size, gc.Equals, test.expected)
    61  	}
    62  }
    63  
    64  func (s *preallocSuite) TestFsAvailSpace(c *gc.C) {
    65  	output := `Filesystem     1K-blocks    Used Available Use% Mounted on
    66      /dev/vda1        8124856 1365292     12345  18% /`
    67  	testing.PatchExecutable(c, s, "df", "#!/bin/sh\ncat<<EOF\n"+output+"\nEOF")
    68  
    69  	mb, err := mongo.FsAvailSpace("")
    70  	c.Assert(err, jc.ErrorIsNil)
    71  	c.Assert(mb, gc.Equals, float64(12345)/1024)
    72  }
    73  
    74  func (s *preallocSuite) TestFsAvailSpaceErrors(c *gc.C) {
    75  	tests := []struct {
    76  		desc   string
    77  		output string
    78  		err    string
    79  	}{{
    80  		desc: "result is non-numeric",
    81  		output: `Filesystem     1K-blocks    Used Available Use% Mounted on
    82      /dev/vda1        8124856 1365292       abc  18% /`,
    83  		err: `strconv.(ParseInt|Atoi): parsing "abc": invalid syntax`,
    84  	}, {
    85  		desc:   "not enough lines",
    86  		output: "abc",
    87  		err:    `could not determine available space on ""`,
    88  	}, {
    89  		desc:   "not enough fields on second line",
    90  		output: "abc\ndef",
    91  		err:    `could not determine available space on ""`,
    92  	}}
    93  	for i, test := range tests {
    94  		c.Logf("test %d: %s", i, test.desc)
    95  		testing.PatchExecutable(c, s, "df", "#!/bin/sh\ncat<<EOF\n"+test.output+"\nEOF")
    96  		_, err := mongo.FsAvailSpace("")
    97  		c.Check(err, gc.ErrorMatches, test.err)
    98  	}
    99  }