github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/fingerprint/landlock_test.go (about)

     1  package fingerprint
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/ci"
     8  	"github.com/hashicorp/nomad/client/testutil"
     9  	"github.com/hashicorp/nomad/helper/testlog"
    10  	"github.com/shoenig/go-landlock"
    11  	"github.com/shoenig/test/must"
    12  )
    13  
    14  func TestLandlockFingerprint(t *testing.T) {
    15  	testutil.RequireLinux(t)
    16  	ci.Parallel(t)
    17  
    18  	version, err := landlock.Detect()
    19  	must.NoError(t, err)
    20  
    21  	logger := testlog.HCLogger(t)
    22  	f := NewLandlockFingerprint(logger)
    23  
    24  	var response FingerprintResponse
    25  	err = f.Fingerprint(nil, &response)
    26  	must.NoError(t, err)
    27  
    28  	result := response.Attributes[landlockKey]
    29  	exp := map[int]string{
    30  		0: "", // unavailable
    31  		1: "v1",
    32  		2: "v2",
    33  		3: "v3",
    34  	}
    35  	must.Eq(t, exp[version], result)
    36  }
    37  
    38  func TestLandlockFingerprint_absent(t *testing.T) {
    39  	ci.Parallel(t)
    40  
    41  	logger := testlog.HCLogger(t)
    42  	f := NewLandlockFingerprint(logger)
    43  	f.(*LandlockFingerprint).detector = func() (int, error) {
    44  		return 0, nil
    45  	}
    46  
    47  	var response FingerprintResponse
    48  	err := f.Fingerprint(nil, &response)
    49  	must.NoError(t, err)
    50  
    51  	_, exists := response.Attributes[landlockKey]
    52  	must.False(t, exists)
    53  }
    54  
    55  func TestLandlockFingerprint_error(t *testing.T) {
    56  	ci.Parallel(t)
    57  
    58  	logger := testlog.HCLogger(t)
    59  	f := NewLandlockFingerprint(logger)
    60  	f.(*LandlockFingerprint).detector = func() (int, error) {
    61  		return 0, errors.New("oops")
    62  	}
    63  
    64  	var response FingerprintResponse
    65  	err := f.Fingerprint(nil, &response)
    66  	must.NoError(t, err)
    67  
    68  	_, exists := response.Attributes[landlockKey]
    69  	must.False(t, exists)
    70  }