github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/client/fingerprint/cni_test.go (about)

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