...

Source file src/github.com/prometheus/procfs/proc_cgroups_test.go

Documentation: github.com/prometheus/procfs

     1  // Copyright 2021 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package procfs
    15  
    16  import (
    17  	"reflect"
    18  	"testing"
    19  )
    20  
    21  func TestParseCgroupSummaryString(t *testing.T) {
    22  	tests := []struct {
    23  		name          string
    24  		s             string
    25  		shouldErr     bool
    26  		CgroupSummary *CgroupSummary
    27  	}{
    28  		{
    29  			name:      "cpuset simple line",
    30  			s:         "cpuset	7	148	1",
    31  			shouldErr: false,
    32  			CgroupSummary: &CgroupSummary{
    33  				SubsysName: "cpuset",
    34  				Hierarchy:  7,
    35  				Cgroups:    148,
    36  				Enabled:    1,
    37  			},
    38  		},
    39  		{
    40  			name:          "memory cgroup number mis format",
    41  			s:             "memory	9	##	1",
    42  			shouldErr:     true,
    43  			CgroupSummary: nil,
    44  		},
    45  	}
    46  
    47  	for i, test := range tests {
    48  		t.Logf("[%02d] test %q", i, test.name)
    49  
    50  		CgroupSummary, err := parseCgroupSummaryString(test.s)
    51  
    52  		if test.shouldErr && err == nil {
    53  			t.Errorf("%s: expected an error, but none occurred", test.name)
    54  		}
    55  		if !test.shouldErr && err != nil {
    56  			t.Errorf("%s: unexpected error: %v", test.name, err)
    57  		}
    58  
    59  		if want, have := test.CgroupSummary, CgroupSummary; !reflect.DeepEqual(want, have) {
    60  			t.Errorf("cgroup:\nwant:\n%+v\nhave:\n%+v", want, have)
    61  		}
    62  	}
    63  
    64  }
    65  

View as plain text