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

Code before:
}

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

	ctestutil.QemuCompatible(t)

	t.Run("fingerpints all emulators", func(t *testing.T) {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		d := NewQemuDriver(ctx, testlog.HCLogger(t))
		harness := dtestutil.NewDriverHarness(t, d)

		fingerCh, err := harness.Fingerprint(context.Background())
		must.NoError(t, err)
		select {
		case finger := <-fingerCh:
			must.Eq(t, drivers.HealthStateHealthy, finger.Health)
			ok, _ := finger.Attributes["driver.qemu"].GetBool()
			must.True(t, ok)

			emulators, _ := finger.Attributes[driverEmulatorsAttr].GetString()
			// Don't want to hardcode the exact amount of emulators installed by qemu-system
			// but it's definitely more than 5
			must.True(t, len(strings.Split(emulators, ",")) > 5)
		case <-time.After(time.Duration(testutil.TestMultiplier()*5) * time.Second):
			t.Fatal("timeout receiving fingerprint")
		}
	})

	t.Run("fingerprints only allowed emulators", func(t *testing.T) {

		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		allowedEms := []string{"x86_64", "aarch64"}
		d := NewQemuDriver(ctx, testlog.HCLogger(t))
		config := &Config{
			EmulatorsAllowList: allowedEms,
		}

		var data []byte
		must.NoError(t, base.MsgPackEncode(&data, config))
		baseConfig := &base.Config{
			PluginConfig: data,
		}
		harness := dtestutil.NewDriverHarness(t, d)
		harness.SetConfig(baseConfig)

		fingerCh, err := harness.Fingerprint(context.Background())

Review comment (bug):
this fails on my machine (fedora), and pretty vaguely

> driver_test.go:261: expected condition to be true; is false

I only have 3

```
$ qemu-system-<tab><tab>
qemu-system-aarch64  qemu-system-i386     qemu-system-x86_64
```

and the error would be clearer with something like this

```go
must.Greater(t, 5, len(strings.Split(emulators, ",")), must.Sprint("found 5 or fewer qemu-system-* binaries"))
```

         driver_test.go:261: expected 3 > 5
        ↪ PostScript | annotation ↷
                found 5 or fewer qemu-system-* binaries



Code after:
}

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

	ctestutil.QemuCompatible_x86_64(t)
	ctestutil.QemuCompatible_aarch64(t)

	t.Run("fingerpints all emulators", func(t *testing.T) {
		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		d := NewQemuDriver(ctx, testlog.HCLogger(t))
		harness := dtestutil.NewDriverHarness(t, d)

		fingerCh, err := harness.Fingerprint(context.Background())
		must.NoError(t, err)
		select {
		case finger := <-fingerCh:
			must.Eq(t, drivers.HealthStateHealthy, finger.Health)
			ok, _ := finger.Attributes["driver.qemu"].GetBool()
			must.True(t, ok)

			emulators, _ := finger.Attributes[driverEmulatorsAttr].GetString()
			must.Greater(t, 1, len(strings.Split(emulators, ",")))
		case <-time.After(time.Duration(testutil.TestMultiplier()*5) * time.Second):
			t.Fatal("timeout receiving fingerprint")
		}
	})

	t.Run("fingerprints only allowed emulators", func(t *testing.T) {

		ctx, cancel := context.WithCancel(context.Background())
		defer cancel()

		allowedEms := []string{"x86_64"}
		d := NewQemuDriver(ctx, testlog.HCLogger(t))
		config := &Config{
			EmulatorsAllowList: allowedEms,
		}

		var data []byte
		must.NoError(t, base.MsgPackEncode(&data, config))
		baseConfig := &base.Config{
			PluginConfig: data,
		}
		harness := dtestutil.NewDriverHarness(t, d)
		harness.SetConfig(baseConfig)

		fingerCh, err := harness.Fingerprint(context.Background())
		must.NoError(t, err)