...

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

Documentation: github.com/prometheus/procfs

     1  // Copyright 2022 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  	"testing"
    18  
    19  	"github.com/google/go-cmp/cmp"
    20  )
    21  
    22  func TestSysctlInts(t *testing.T) {
    23  	fs := getProcFixtures(t)
    24  
    25  	for _, tc := range []struct {
    26  		sysctl string
    27  		want   []int
    28  	}{
    29  		{"kernel.random.entropy_avail", []int{3943}},
    30  		{"vm.lowmem_reserve_ratio", []int{256, 256, 32, 0, 0}},
    31  	} {
    32  		t.Run(tc.sysctl, func(t *testing.T) {
    33  			got, err := fs.SysctlInts(tc.sysctl)
    34  			if err != nil {
    35  				t.Fatal(err)
    36  			}
    37  			if diff := cmp.Diff(tc.want, got); diff != "" {
    38  				t.Fatalf("unexpected syscall value(-want +got):\n%s", diff)
    39  			}
    40  		})
    41  	}
    42  }
    43  
    44  func TestSysctlStrings(t *testing.T) {
    45  	fs := getProcFixtures(t)
    46  
    47  	for _, tc := range []struct {
    48  		sysctl string
    49  		want   []string
    50  	}{
    51  		{"kernel.seccomp.actions_avail", []string{"kill_process", "kill_thread", "trap", "errno", "trace", "log", "allow"}},
    52  	} {
    53  		t.Run(tc.sysctl, func(t *testing.T) {
    54  			got, err := fs.SysctlStrings(tc.sysctl)
    55  			if err != nil {
    56  				t.Fatal(err)
    57  			}
    58  			if diff := cmp.Diff(tc.want, got); diff != "" {
    59  				t.Fatalf("unexpected syscall value(-want +got):\n%s", diff)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestSysctlIntsError(t *testing.T) {
    66  	fs := getProcFixtures(t)
    67  
    68  	for _, tc := range []struct {
    69  		sysctl string
    70  		want   string
    71  	}{
    72  		{"kernel.seccomp.actions_avail", "Error Parsing File: field 0 in sysctl kernel.seccomp.actions_avail is not a valid int: strconv.ParseInt: parsing \"kill_process\": invalid syntax"},
    73  	} {
    74  		t.Run(tc.sysctl, func(t *testing.T) {
    75  			_, err := fs.SysctlInts(tc.sysctl)
    76  			if err == nil {
    77  				t.Fatal("Error Parsing File: expected error")
    78  			}
    79  			if diff := cmp.Diff(tc.want, err.Error()); diff != "" {
    80  				t.Fatalf("unexpected syscall value(-want +got):\n%s", diff)
    81  			}
    82  		})
    83  	}
    84  }
    85  

View as plain text