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

     1  package fingerprint
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/ci"
     7  	"github.com/hashicorp/nomad/client/config"
     8  	"github.com/hashicorp/nomad/helper/testlog"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  // Test that CNI fingerprinter is reloadable
    14  var _ ReloadableFingerprint = &CNIFingerprint{}
    15  
    16  func TestCNIFingerprint(t *testing.T) {
    17  	ci.Parallel(t)
    18  
    19  	cases := []struct {
    20  		name     string
    21  		req      *FingerprintRequest
    22  		exp      *FingerprintResponse
    23  		err      bool
    24  		errMatch string
    25  	}{
    26  		{
    27  			name: "cni config dir not set",
    28  			req: &FingerprintRequest{
    29  				Config: &config.Config{},
    30  			},
    31  			exp: &FingerprintResponse{
    32  				Detected: false,
    33  			},
    34  		},
    35  		{
    36  			name: "cni config dir non-existent",
    37  			req: &FingerprintRequest{
    38  				Config: &config.Config{
    39  					CNIConfigDir: "text_fixtures/cni_nonexistent",
    40  				},
    41  			},
    42  			exp: &FingerprintResponse{
    43  				Detected: false,
    44  			},
    45  		},
    46  		{
    47  			name: "two networks, no errors",
    48  			req: &FingerprintRequest{
    49  				Config: &config.Config{
    50  					CNIConfigDir: "test_fixtures/cni",
    51  				},
    52  			},
    53  			exp: &FingerprintResponse{
    54  				NodeResources: &structs.NodeResources{
    55  					Networks: []*structs.NetworkResource{
    56  						{
    57  							Mode: "cni/net1",
    58  						},
    59  						{
    60  							Mode: "cni/net2",
    61  						},
    62  					},
    63  				},
    64  				Detected: true,
    65  			},
    66  			err: false,
    67  		},
    68  	}
    69  
    70  	for _, c := range cases {
    71  		t.Run(c.name, func(t *testing.T) {
    72  			r := require.New(t)
    73  			fp := NewCNIFingerprint(testlog.HCLogger(t))
    74  			resp := &FingerprintResponse{}
    75  			err := fp.Fingerprint(c.req, resp)
    76  			if c.err {
    77  				r.Error(err)
    78  				r.Contains(err.Error(), c.errMatch)
    79  			} else {
    80  				r.NoError(err)
    81  				r.Equal(c.exp.Detected, resp.Detected)
    82  				if resp.NodeResources != nil || c.exp.NodeResources != nil {
    83  					r.ElementsMatch(c.exp.NodeResources.Networks, resp.NodeResources.Networks)
    84  				}
    85  			}
    86  		})
    87  	}
    88  }