github.com/smithx10/nomad@v0.9.1-rc1/client/driver_manager_test.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/hashicorp/nomad/client/config"
     9  	"github.com/hashicorp/nomad/client/pluginmanager/drivermanager"
    10  	"github.com/hashicorp/nomad/helper/pluginutils/catalog"
    11  	nconfig "github.com/hashicorp/nomad/nomad/structs/config"
    12  	"github.com/hashicorp/nomad/testutil"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  // TestDriverManager_Fingerprint_Run asserts that node is populated with
    17  // driver fingerprints
    18  func TestDriverManager_Fingerprint_Run(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	testClient, cleanup := TestClient(t, nil)
    22  	defer cleanup()
    23  
    24  	dm := drivermanager.New(&drivermanager.Config{
    25  		Logger:              testClient.logger,
    26  		Loader:              testClient.config.PluginSingletonLoader,
    27  		PluginConfig:        testClient.configCopy.NomadPluginConfig(),
    28  		Updater:             testClient.updateNodeFromDriver,
    29  		EventHandlerFactory: testClient.GetTaskEventHandler,
    30  		State:               testClient.stateDB,
    31  	})
    32  
    33  	go dm.Run()
    34  	defer dm.Shutdown()
    35  
    36  	testutil.WaitForResult(func() (bool, error) {
    37  		node := testClient.configCopy.Node
    38  
    39  		d, ok := node.Drivers["mock_driver"]
    40  		if !ok {
    41  			return false, fmt.Errorf("mock_driver driver is not present: %+v", node.Drivers)
    42  		}
    43  
    44  		if !d.Detected || !d.Healthy {
    45  			return false, fmt.Errorf("mock_driver driver is not marked healthy: %+v", d)
    46  		}
    47  
    48  		return true, nil
    49  	}, func(err error) {
    50  		require.NoError(t, err)
    51  	})
    52  }
    53  
    54  // TestDriverManager_Fingerprint_Run asserts that node is populated with
    55  // driver fingerprints and it's updated periodically
    56  func TestDriverManager_Fingerprint_Periodic(t *testing.T) {
    57  	t.Parallel()
    58  
    59  	testClient, cleanup := TestClient(t, func(c *config.Config) {
    60  		pluginConfig := []*nconfig.PluginConfig{
    61  			{
    62  				Name: "mock_driver",
    63  				Config: map[string]interface{}{
    64  					"shutdown_periodic_after":    true,
    65  					"shutdown_periodic_duration": 2 * time.Second,
    66  				},
    67  			},
    68  		}
    69  
    70  		c.PluginLoader = catalog.TestPluginLoaderWithOptions(t, "", map[string]string{}, pluginConfig)
    71  
    72  	})
    73  	defer cleanup()
    74  
    75  	dm := drivermanager.New(&drivermanager.Config{
    76  		Logger:              testClient.logger,
    77  		Loader:              testClient.config.PluginSingletonLoader,
    78  		PluginConfig:        testClient.configCopy.NomadPluginConfig(),
    79  		Updater:             testClient.updateNodeFromDriver,
    80  		EventHandlerFactory: testClient.GetTaskEventHandler,
    81  		State:               testClient.stateDB,
    82  	})
    83  
    84  	go dm.Run()
    85  	defer dm.Shutdown()
    86  
    87  	// we get a healthy mock_driver first
    88  	testutil.WaitForResult(func() (bool, error) {
    89  		node := testClient.configCopy.Node
    90  
    91  		d, ok := node.Drivers["mock_driver"]
    92  		if !ok {
    93  			return false, fmt.Errorf("mock_driver driver is not present: %+v", node.Drivers)
    94  		}
    95  
    96  		if !d.Detected || !d.Healthy {
    97  			return false, fmt.Errorf("mock_driver driver is not marked healthy: %+v", d)
    98  		}
    99  
   100  		return true, nil
   101  	}, func(err error) {
   102  		require.NoError(t, err)
   103  	})
   104  
   105  	// eventually, the mock_driver is marked as unhealthy
   106  	testutil.WaitForResult(func() (bool, error) {
   107  		node := testClient.configCopy.Node
   108  
   109  		d, ok := node.Drivers["mock_driver"]
   110  		if !ok {
   111  			return false, fmt.Errorf("mock_driver driver is not present: %+v", node.Drivers)
   112  		}
   113  
   114  		if d.Detected || d.Healthy {
   115  			return false, fmt.Errorf("mock_driver driver is still marked as healthy: %+v", d)
   116  		}
   117  
   118  		return true, nil
   119  	}, func(err error) {
   120  		require.NoError(t, err)
   121  	})
   122  }
   123  
   124  // TestDriverManager_NodeAttributes_Run asserts that node attributes are populated
   125  // in addition to node.Drivers until we fully deprecate it
   126  func TestDriverManager_NodeAttributes_Run(t *testing.T) {
   127  	t.Parallel()
   128  
   129  	testClient, cleanup := TestClient(t, func(c *config.Config) {
   130  		c.Options = map[string]string{
   131  			"driver.raw_exec.enable": "1",
   132  		}
   133  	})
   134  	defer cleanup()
   135  
   136  	dm := drivermanager.New(&drivermanager.Config{
   137  		Logger:              testClient.logger,
   138  		Loader:              testClient.config.PluginSingletonLoader,
   139  		PluginConfig:        testClient.configCopy.NomadPluginConfig(),
   140  		Updater:             testClient.updateNodeFromDriver,
   141  		EventHandlerFactory: testClient.GetTaskEventHandler,
   142  		State:               testClient.stateDB,
   143  	})
   144  
   145  	go dm.Run()
   146  	defer dm.Shutdown()
   147  
   148  	// we should have mock_driver as well as raw_exec in node attributes
   149  	testutil.WaitForResult(func() (bool, error) {
   150  		node := testClient.configCopy.Node
   151  
   152  		// check mock driver
   153  		if node.Attributes["driver.mock_driver"] == "" {
   154  			return false, fmt.Errorf("mock_driver is not present in attributes: %#v", node.Attributes)
   155  		}
   156  		d, ok := node.Drivers["mock_driver"]
   157  		if !ok {
   158  			return false, fmt.Errorf("mock_driver is not present in drivers: %#v", node.Drivers)
   159  		}
   160  
   161  		if !d.Detected || !d.Healthy {
   162  			return false, fmt.Errorf("mock_driver driver is not marked as healthy: %+v", d)
   163  		}
   164  
   165  		if d.Attributes["driver.mock_driver"] != "" {
   166  			return false, fmt.Errorf("mock driver driver attributes contain duplicate health info: %#v", d.Attributes)
   167  		}
   168  
   169  		// check raw_exec
   170  		if node.Attributes["driver.raw_exec"] == "" {
   171  			return false, fmt.Errorf("raw_exec is not present in attributes: %#v", node.Attributes)
   172  		}
   173  		d, ok = node.Drivers["raw_exec"]
   174  		if !ok {
   175  			return false, fmt.Errorf("raw_exec is not present in drivers: %#v", node.Drivers)
   176  		}
   177  
   178  		if !d.Detected || !d.Healthy {
   179  			return false, fmt.Errorf("raw_exec driver is not marked as healthy: %+v", d)
   180  		}
   181  
   182  		return true, nil
   183  	}, func(err error) {
   184  		require.NoError(t, err)
   185  	})
   186  }