github.com/flatt-online-training/libcompose@v0.4.0/yaml/ulimit_test.go (about)

     1  package yaml
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gopkg.in/yaml.v2"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestMarshalUlimit(t *testing.T) {
    12  	ulimits := []struct {
    13  		ulimits  *Ulimits
    14  		expected string
    15  	}{
    16  		{
    17  			ulimits: &Ulimits{
    18  				Elements: []Ulimit{
    19  					{
    20  						ulimitValues: ulimitValues{
    21  							Soft: 65535,
    22  							Hard: 65535,
    23  						},
    24  						Name: "nproc",
    25  					},
    26  				},
    27  			},
    28  			expected: `nproc: 65535
    29  `,
    30  		},
    31  		{
    32  			ulimits: &Ulimits{
    33  				Elements: []Ulimit{
    34  					{
    35  						Name: "nofile",
    36  						ulimitValues: ulimitValues{
    37  							Soft: 20000,
    38  							Hard: 40000,
    39  						},
    40  					},
    41  				},
    42  			},
    43  			expected: `nofile:
    44    soft: 20000
    45    hard: 40000
    46  `,
    47  		},
    48  	}
    49  
    50  	for _, ulimit := range ulimits {
    51  
    52  		bytes, err := yaml.Marshal(ulimit.ulimits)
    53  
    54  		assert.Nil(t, err)
    55  		assert.Equal(t, ulimit.expected, string(bytes), "should be equal")
    56  	}
    57  }
    58  
    59  func TestUnmarshalUlimits(t *testing.T) {
    60  	ulimits := []struct {
    61  		yaml     string
    62  		expected *Ulimits
    63  	}{
    64  		{
    65  			yaml: "nproc: 65535",
    66  			expected: &Ulimits{
    67  				Elements: []Ulimit{
    68  					{
    69  						Name: "nproc",
    70  						ulimitValues: ulimitValues{
    71  							Soft: 65535,
    72  							Hard: 65535,
    73  						},
    74  					},
    75  				},
    76  			},
    77  		},
    78  		{
    79  			yaml: `nofile:
    80    soft: 20000
    81    hard: 40000`,
    82  			expected: &Ulimits{
    83  				Elements: []Ulimit{
    84  					{
    85  						Name: "nofile",
    86  						ulimitValues: ulimitValues{
    87  							Soft: 20000,
    88  							Hard: 40000,
    89  						},
    90  					},
    91  				},
    92  			},
    93  		},
    94  		{
    95  			yaml: `nproc: 65535
    96  nofile:
    97    soft: 20000
    98    hard: 40000`,
    99  			expected: &Ulimits{
   100  				Elements: []Ulimit{
   101  					{
   102  						Name: "nofile",
   103  						ulimitValues: ulimitValues{
   104  							Soft: 20000,
   105  							Hard: 40000,
   106  						},
   107  					},
   108  					{
   109  						Name: "nproc",
   110  						ulimitValues: ulimitValues{
   111  							Soft: 65535,
   112  							Hard: 65535,
   113  						},
   114  					},
   115  				},
   116  			},
   117  		},
   118  	}
   119  
   120  	for _, ulimit := range ulimits {
   121  		actual := &Ulimits{}
   122  		err := yaml.Unmarshal([]byte(ulimit.yaml), actual)
   123  
   124  		assert.Nil(t, err)
   125  		assert.Equal(t, ulimit.expected, actual, "should be equal")
   126  	}
   127  }