| 
									
										
										
										
											2018-06-06 07:49:19 -07:00
										 |  |  | // Copyright 2015 The Prometheus Authors
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-15 02:38:13 -07:00
										 |  |  | //go:build !nofilesystem
 | 
					
						
							|  |  |  | // +build !nofilesystem
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 07:49:19 -07:00
										 |  |  | package collector | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	"io" | 
					
						
							|  |  |  | 	"log/slog" | 
					
						
							| 
									
										
										
										
											2018-10-30 14:12:42 -07:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2018-06-06 07:49:19 -07:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-07 00:25:05 -08:00
										 |  |  | 	"github.com/alecthomas/kingpin/v2" | 
					
						
							| 
									
										
										
										
											2018-06-06 07:49:19 -07:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-30 14:12:42 -07:00
										 |  |  | func Test_parseFilesystemLabelsError(t *testing.T) { | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		name string | 
					
						
							|  |  |  | 		in   string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			name: "too few fields", | 
					
						
							|  |  |  | 			in:   "hello world", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, tt := range tests { | 
					
						
							|  |  |  | 		t.Run(tt.name, func(t *testing.T) { | 
					
						
							|  |  |  | 			if _, err := parseFilesystemLabels(strings.NewReader(tt.in)); err == nil { | 
					
						
							|  |  |  | 				t.Fatal("expected an error, but none occurred") | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 07:49:19 -07:00
										 |  |  | func TestMountPointDetails(t *testing.T) { | 
					
						
							|  |  |  | 	if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures/proc"}); err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	expected := map[string]string{ | 
					
						
							|  |  |  | 		"/":                               "", | 
					
						
							|  |  |  | 		"/sys":                            "", | 
					
						
							|  |  |  | 		"/proc":                           "", | 
					
						
							|  |  |  | 		"/dev":                            "", | 
					
						
							|  |  |  | 		"/dev/pts":                        "", | 
					
						
							|  |  |  | 		"/run":                            "", | 
					
						
							|  |  |  | 		"/sys/kernel/security":            "", | 
					
						
							|  |  |  | 		"/dev/shm":                        "", | 
					
						
							|  |  |  | 		"/run/lock":                       "", | 
					
						
							|  |  |  | 		"/sys/fs/cgroup":                  "", | 
					
						
							|  |  |  | 		"/sys/fs/cgroup/systemd":          "", | 
					
						
							|  |  |  | 		"/sys/fs/pstore":                  "", | 
					
						
							|  |  |  | 		"/sys/fs/cgroup/cpuset":           "", | 
					
						
							|  |  |  | 		"/sys/fs/cgroup/cpu,cpuacct":      "", | 
					
						
							|  |  |  | 		"/sys/fs/cgroup/devices":          "", | 
					
						
							|  |  |  | 		"/sys/fs/cgroup/freezer":          "", | 
					
						
							|  |  |  | 		"/sys/fs/cgroup/net_cls,net_prio": "", | 
					
						
							|  |  |  | 		"/sys/fs/cgroup/blkio":            "", | 
					
						
							|  |  |  | 		"/sys/fs/cgroup/perf_event":       "", | 
					
						
							|  |  |  | 		"/proc/sys/fs/binfmt_misc":        "", | 
					
						
							|  |  |  | 		"/dev/mqueue":                     "", | 
					
						
							|  |  |  | 		"/sys/kernel/debug":               "", | 
					
						
							|  |  |  | 		"/dev/hugepages":                  "", | 
					
						
							|  |  |  | 		"/sys/fs/fuse/connections":        "", | 
					
						
							|  |  |  | 		"/boot":                           "", | 
					
						
							|  |  |  | 		"/run/rpc_pipefs":                 "", | 
					
						
							|  |  |  | 		"/run/user/1000":                  "", | 
					
						
							|  |  |  | 		"/run/user/1000/gvfs":             "", | 
					
						
							|  |  |  | 		"/var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore] bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk": "", | 
					
						
							|  |  |  | 		"/var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore]	bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk": "", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	filesystems, err := mountPointDetails(slog.New(slog.NewTextHandler(io.Discard, nil))) | 
					
						
							| 
									
										
										
										
											2018-06-06 07:49:19 -07:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Log(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-14 07:33:12 -07:00
										 |  |  | 	foundSet := map[string]bool{} | 
					
						
							| 
									
										
										
										
											2018-06-06 07:49:19 -07:00
										 |  |  | 	for _, fs := range filesystems { | 
					
						
							|  |  |  | 		if _, ok := expected[fs.mountPoint]; !ok { | 
					
						
							|  |  |  | 			t.Errorf("Got unexpected %s", fs.mountPoint) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-07-14 07:33:12 -07:00
										 |  |  | 		foundSet[fs.mountPoint] = true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for mountPoint := range expected { | 
					
						
							|  |  |  | 		if _, ok := foundSet[mountPoint]; !ok { | 
					
						
							|  |  |  | 			t.Errorf("Expected %s, got nothing", mountPoint) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-06-06 07:49:19 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-11-30 05:01:55 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestMountsFallback(t *testing.T) { | 
					
						
							|  |  |  | 	if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_hidepid/proc"}); err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	expected := map[string]string{ | 
					
						
							|  |  |  | 		"/": "", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	filesystems, err := mountPointDetails(slog.New(slog.NewTextHandler(io.Discard, nil))) | 
					
						
							| 
									
										
										
										
											2018-11-30 05:01:55 -08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Log(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, fs := range filesystems { | 
					
						
							|  |  |  | 		if _, ok := expected[fs.mountPoint]; !ok { | 
					
						
							|  |  |  | 			t.Errorf("Got unexpected %s", fs.mountPoint) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-07-19 07:51:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestPathRootfs(t *testing.T) { | 
					
						
							|  |  |  | 	if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_bindmount/proc", "--path.rootfs", "/host"}); err != nil { | 
					
						
							|  |  |  | 		t.Fatal(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	expected := map[string]string{ | 
					
						
							|  |  |  | 		// should modify these mountpoints (removes /host, see fixture proc file)
 | 
					
						
							| 
									
										
										
										
											2019-09-09 08:39:25 -07:00
										 |  |  | 		"/":              "", | 
					
						
							| 
									
										
										
										
											2019-07-19 07:51:17 -07:00
										 |  |  | 		"/media/volume1": "", | 
					
						
							|  |  |  | 		"/media/volume2": "", | 
					
						
							|  |  |  | 		// should not modify these mountpoints
 | 
					
						
							|  |  |  | 		"/dev/shm":       "", | 
					
						
							|  |  |  | 		"/run/lock":      "", | 
					
						
							|  |  |  | 		"/sys/fs/cgroup": "", | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-11 01:51:28 -07:00
										 |  |  | 	filesystems, err := mountPointDetails(slog.New(slog.NewTextHandler(io.Discard, nil))) | 
					
						
							| 
									
										
										
										
											2019-07-19 07:51:17 -07:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		t.Log(err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, fs := range filesystems { | 
					
						
							|  |  |  | 		if _, ok := expected[fs.mountPoint]; !ok { | 
					
						
							|  |  |  | 			t.Errorf("Got unexpected %s", fs.mountPoint) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |