github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/stage1/init/kvm/resources_test.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package kvm
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/appc/spec/schema/types"
    21  )
    22  
    23  // Check that findResources is returning the answers we expect.
    24  // In the current implementation, that is exactly what we pass in to it for
    25  // the cpu value, and the memory value is divided by (1024 * 1024) - MB
    26  func TestFindResources(t *testing.T) {
    27  	tests := []struct {
    28  		in types.Isolators
    29  
    30  		wmem int64
    31  		wcpu int64
    32  	}{
    33  		//empty values should return us empty results
    34  		{
    35  			types.Isolators{},
    36  
    37  			0,
    38  			0,
    39  		},
    40  		//check values in isolators actually come back
    41  		{
    42  			types.Isolators([]types.Isolator{
    43  				newIsolator(`
    44  				{
    45  					"name":     "resource/cpu",
    46  					"value": {
    47  						"limit": 0,
    48  						"request": 0
    49  						}
    50  				}`),
    51  			}),
    52  
    53  			0,
    54  			0,
    55  		},
    56  		{
    57  			types.Isolators([]types.Isolator{
    58  				newIsolator(`
    59  				{
    60  					"name":     "resource/cpu",
    61  					"value": {
    62  						"limit": 1,
    63  						"request": 1
    64  						}
    65  				}`),
    66  			}),
    67  
    68  			0,
    69  			1,
    70  		},
    71  		{
    72  			// 1M test
    73  			types.Isolators([]types.Isolator{
    74  				newIsolator(`
    75  				{
    76  					"name":     "resource/memory",
    77  					"value": {
    78  						"limit": 1048576,
    79  						"request": 1048576
    80  						}
    81  				}`),
    82  			}),
    83  
    84  			1,
    85  			0,
    86  		},
    87  		{
    88  			// 128M test
    89  			types.Isolators([]types.Isolator{
    90  				newIsolator(`
    91  				{
    92  					"name":     "resource/memory",
    93  					"value": {
    94  						"limit": 134217728,
    95  						"request": 134217728
    96  						}
    97  				}`),
    98  			}),
    99  
   100  			128,
   101  			0,
   102  		},
   103  	}
   104  
   105  	for i, tt := range tests {
   106  		gmem, gcpu := findResources(tt.in)
   107  		if gmem != tt.wmem {
   108  			t.Errorf("#%d: got mem=%d, want %d", i, gmem, tt.wmem)
   109  		}
   110  		if gcpu != tt.wcpu {
   111  			t.Errorf("#%d: got cpu=%d, want %d", i, gcpu, tt.wcpu)
   112  		}
   113  	}
   114  }
   115  
   116  func newIsolator(body string) (i types.Isolator) {
   117  	err := i.UnmarshalJSON([]byte(body))
   118  	if err != nil {
   119  		panic(err)
   120  	}
   121  	return
   122  }