2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-05-15 15:09:01 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2020-05-15 15:09:01 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Test that CNI fingerprinter is reloadable
|
|
|
|
var _ ReloadableFingerprint = &CNIFingerprint{}
|
|
|
|
|
|
|
|
func TestCNIFingerprint(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2020-05-15 15:09:01 +00:00
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
req *FingerprintRequest
|
|
|
|
exp *FingerprintResponse
|
|
|
|
err bool
|
|
|
|
errMatch string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "cni config dir not set",
|
|
|
|
req: &FingerprintRequest{
|
|
|
|
Config: &config.Config{},
|
|
|
|
},
|
|
|
|
exp: &FingerprintResponse{
|
|
|
|
Detected: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "cni config dir non-existent",
|
|
|
|
req: &FingerprintRequest{
|
|
|
|
Config: &config.Config{
|
|
|
|
CNIConfigDir: "text_fixtures/cni_nonexistent",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
exp: &FingerprintResponse{
|
|
|
|
Detected: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "two networks, no errors",
|
|
|
|
req: &FingerprintRequest{
|
|
|
|
Config: &config.Config{
|
|
|
|
CNIConfigDir: "test_fixtures/cni",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
exp: &FingerprintResponse{
|
|
|
|
NodeResources: &structs.NodeResources{
|
|
|
|
Networks: []*structs.NetworkResource{
|
|
|
|
{
|
|
|
|
Mode: "cni/net1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Mode: "cni/net2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Detected: true,
|
|
|
|
},
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
r := require.New(t)
|
|
|
|
fp := NewCNIFingerprint(testlog.HCLogger(t))
|
|
|
|
resp := &FingerprintResponse{}
|
|
|
|
err := fp.Fingerprint(c.req, resp)
|
|
|
|
if c.err {
|
|
|
|
r.Error(err)
|
|
|
|
r.Contains(err.Error(), c.errMatch)
|
|
|
|
} else {
|
|
|
|
r.NoError(err)
|
|
|
|
r.Equal(c.exp.Detected, resp.Detected)
|
|
|
|
if resp.NodeResources != nil || c.exp.NodeResources != nil {
|
|
|
|
r.ElementsMatch(c.exp.NodeResources.Networks, resp.NodeResources.Networks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|