1// Copyright 2017 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
14package procfs
15
16import (
17	"strings"
18	"testing"
19)
20
21func TestBuddyInfo(t *testing.T) {
22	buddyInfo, err := getProcFixtures(t).BuddyInfo()
23	if err != nil {
24		t.Fatal(err)
25	}
26
27	if want, got := "DMA", buddyInfo[0].Zone; want != got {
28		t.Errorf("want Node 0, Zone %s, got %s", want, got)
29	}
30
31	if want, got := "Normal", buddyInfo[2].Zone; want != got {
32		t.Errorf("want Node 0, Zone %s, got %s", want, got)
33	}
34
35	if want, got := 4381.0, buddyInfo[2].Sizes[0]; want != got {
36		t.Errorf("want Node 0, Zone Normal %f, got %f", want, got)
37	}
38
39	if want, got := 572.0, buddyInfo[1].Sizes[1]; want != got {
40		t.Errorf("want Node 0, Zone DMA32 %f, got %f", want, got)
41	}
42}
43
44func TestParseBuddyInfoShort(t *testing.T) {
45
46	testdata := `Node 0, zone
47Node 0, zone
48Node 0, zone
49`
50	reader := strings.NewReader(testdata)
51	_, err := parseBuddyInfo(reader)
52	if err == nil {
53		t.Fatalf("expected error, but none occurred")
54	}
55	if want, got := "invalid number of fields when parsing buddyinfo", err.Error(); want != got {
56		t.Fatalf("wrong error returned, wanted %q, got %q", want, got)
57	}
58}
59
60func TestParseBuddyInfoSizeMismatch(t *testing.T) {
61
62	testdata := `Node 0, zone      DMA      1      0      1      0      2      1      1      0      1      1      3
63Node 0, zone    DMA32    759    572    791    475    194     45     12      0      0      0      0      0
64Node 0, zone   Normal   4381   1093    185   1530    567    102      4      0      0      0
65`
66	reader := strings.NewReader(testdata)
67	_, err := parseBuddyInfo(reader)
68	if err == nil {
69		t.Fatalf("expected error, but none occurred")
70	}
71	if want, got := "mismatch in number of buddyinfo buckets", err.Error(); !strings.HasPrefix(got, want) {
72		t.Fatalf("wrong error returned, wanted prefix %q, got %q", want, got)
73	}
74}
75