Repository: hashicorp/nomad
File: drivers/qemu/driver_test.go (Go)

Code before:
			requestedEmulator: "qemu-system-x86_64",
			exp:               nil,
		},
		{
			name:              "empty valid emulators, invalid request",
			validEmulators:    nil,
			requestedEmulator: "some-binary",
			exp:               errors.New("emulator 'some-binary' is not valid"),
		},
		{
			name:              "non-empty valid emulators, valid request",
			validEmulators:    []string{"qemu-system-x86_64"},
			requestedEmulator: "qemu-system-x86_64",
			exp:               nil,
		},
		{
			name:              "non-empty valid emulators, invalid request",
			validEmulators:    []string{"qemu-system-x86_64"},
			requestedEmulator: "qemu-system-aarch64",
			exp:               errors.New("emulator 'qemu-system-aarch64' is not an allowed emulator"),
		},
	}

	for _, tc := range testcases {
		t.Run(tc.name, func(t *testing.T) {
			must.Eq(t, validateEmulator(tc.requestedEmulator, tc.validEmulators), tc.exp)
		})
	}
}

func TestIsAllowedDriveInterface(t *testing.T) {
	validInterfaces := []string{"ide", "scsi", "sd", "mtd", "floppy", "pflash", "virtio", "none"}
	invalidInterfaces := []string{"foo", "virtio-foo"}

	for _, i := range validInterfaces {
		must.True(t, isAllowedDriveInterface(i),
			must.Sprintf("drive_interface should be allowed: %v", i))
	}

	for _, i := range invalidInterfaces {
		must.False(t, isAllowedDriveInterface(i),
			must.Sprintf("drive_interface should not be allowed: %v", i))
	}
}

func TestIsAllowedImagePath(t *testing.T) {
	ci.Parallel(t)

	allowedPaths := []string{"/tmp", "/opt/qemu"}
	allocDir := "/opt/nomad/some-alloc-dir"


Review comment (suggestion):
similar to above, I tend to prefer `ErrorIs` or `EqError` or `ErrorContains` in the test library, since their messages may be more informative.

Code after:
			exp:               nil,
		},
		{
			name:              "non-empty valid emulators, valid request",
			validEmulators:    []string{"qemu-system-x86_64"},
			requestedEmulator: "qemu-system-x86_64",
			exp:               nil,
		},
		{
			name:              "non-empty valid emulators, invalid request",
			validEmulators:    []string{"qemu-system-x86_64"},
			requestedEmulator: "qemu-system-aarch64",
			exp:               errors.New("'qemu-system-aarch64' is not an allowed emulator"),
		},
	}

	for _, tc := range testcases {
		t.Run(tc.name, func(t *testing.T) {
			err := validateEmulator(tc.requestedEmulator, tc.validEmulators)
			if tc.exp != nil {
				must.ErrorContains(t, err, tc.exp.Error())
			} else {
				must.NoError(t, err)
			}
		})
	}
}

func TestIsAllowedDriveInterface(t *testing.T) {
	validInterfaces := []string{"ide", "scsi", "sd", "mtd", "floppy", "pflash", "virtio", "none"}
	invalidInterfaces := []string{"foo", "virtio-foo"}

	for _, i := range validInterfaces {
		must.True(t, isAllowedDriveInterface(i),
			must.Sprintf("drive_interface should be allowed: %v", i))
	}

	for _, i := range invalidInterfaces {
		must.False(t, isAllowedDriveInterface(i),
			must.Sprintf("drive_interface should not be allowed: %v", i))
	}
}

func TestIsAllowedImagePath(t *testing.T) {
	ci.Parallel(t)

	allowedPaths := []string{"/tmp", "/opt/qemu"}
	allocDir := "/opt/nomad/some-alloc-dir"

	validPaths := []string{
		"local/path",