MCPcopy
hub / github.com/jaypipes/ghw / TestLoopDevicesWithOption

Function TestLoopDevicesWithOption

pkg/block/block_linux_test.go:321–369  ·  view source on GitHub ↗

TestLoopDevicesWithOption tests to see if we find loop devices when the config is activated

(t *testing.T)

Source from the content-addressed store, hash-verified

319
320// TestLoopDevicesWithOption tests to see if we find loop devices when the config is activated
321func TestLoopDevicesWithOption(t *testing.T) {
322 if _, ok := os.LookupEnv("GHW_TESTING_SKIP_BLOCK"); ok {
323 t.Skip("Skipping block tests.")
324 }
325 baseDir, _ := os.MkdirTemp("", "test")
326 defer os.RemoveAll(baseDir)
327 ctx := context.TODO()
328 ctx = config.WithChroot(baseDir)(ctx)
329 ctx = config.WithDisableTools()(ctx)
330 paths := linuxpath.New(ctx)
331 fsType := "ext4"
332 expectedLoopName := "loop0"
333 loopNotUsed := "loop1"
334 loopPartitionName := "loop0p1"
335
336 _ = os.MkdirAll(paths.SysBlock, 0755)
337 _ = os.MkdirAll(paths.RunUdevData, 0755)
338
339 // Emulate a loop device with one partition and another loop deviced not used
340 _ = os.Mkdir(filepath.Join(paths.SysBlock, expectedLoopName), 0755)
341 _ = os.Mkdir(filepath.Join(paths.SysBlock, loopNotUsed), 0755)
342 _ = os.Mkdir(filepath.Join(paths.SysBlock, expectedLoopName, "queue"), 0755)
343 _ = os.Mkdir(filepath.Join(paths.SysBlock, loopNotUsed, "queue"), 0755)
344 _ = os.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, "queue", "rotational"), []byte("1\n"), 0644)
345 _ = os.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, "size"), []byte("62810112\n"), 0644)
346 _ = os.WriteFile(filepath.Join(paths.SysBlock, loopNotUsed, "size"), []byte("0\n"), 0644)
347 _ = os.Mkdir(filepath.Join(paths.SysBlock, expectedLoopName, loopPartitionName), 0755)
348 _ = os.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, loopPartitionName, "dev"), []byte("259:0\n"), 0644)
349 _ = os.WriteFile(filepath.Join(paths.SysBlock, expectedLoopName, loopPartitionName, "size"), []byte("102400\n"), 0644)
350 _ = os.WriteFile(filepath.Join(paths.RunUdevData, "b259:0"), []byte(fmt.Sprintf("E:ID_FS_TYPE=%s\n", fsType)), 0644)
351 d := disks(ctx)
352 // There should be one disk, the other should be ignored due to 0 size
353 if len(d) != 1 {
354 t.Fatalf("expected one disk device but the function reported %d", len(d))
355 }
356 foundDisk := d[0]
357 // Should be the one we faked
358 if foundDisk.Name != expectedLoopName {
359 t.Fatalf("got loop device %s but expected %s", foundDisk.Name, expectedLoopName)
360 }
361 // Should have only one partition
362 if len(foundDisk.Partitions) != 1 {
363 t.Fatalf("expected one partition but the function reported %d", len(foundDisk.Partitions))
364 }
365 // Name should match
366 if foundDisk.Partitions[0].Name != loopPartitionName {
367 t.Fatalf("got partition %s but expected %s", foundDisk.Partitions[0], loopPartitionName)
368 }
369}

Callers

nothing calls this directly

Calls 4

WithChrootFunction · 0.92
WithDisableToolsFunction · 0.92
NewFunction · 0.92
disksFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…