github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/helper/pluginutils/catalog/testing.go (about)

     1  package catalog
     2  
     3  import (
     4  	testing "github.com/mitchellh/go-testing-interface"
     5  
     6  	"github.com/hashicorp/nomad/helper/pluginutils/loader"
     7  	"github.com/hashicorp/nomad/helper/testlog"
     8  	"github.com/hashicorp/nomad/nomad/structs/config"
     9  )
    10  
    11  // TestPluginLoader returns a plugin loader populated only with internal plugins
    12  func TestPluginLoader(t testing.T) loader.PluginCatalog {
    13  	driverConfigs := []*config.PluginConfig{
    14  		{
    15  			Name: "raw_exec",
    16  			Config: map[string]interface{}{
    17  				"enabled": true,
    18  			},
    19  		},
    20  	}
    21  	return TestPluginLoaderWithOptions(t, "", nil, driverConfigs)
    22  }
    23  
    24  // TestPluginLoaderWithOptions allows configuring the plugin loader fully.
    25  func TestPluginLoaderWithOptions(t testing.T,
    26  	pluginDir string,
    27  	options map[string]string,
    28  	configs []*config.PluginConfig) loader.PluginCatalog {
    29  
    30  	// Get a logger
    31  	logger := testlog.HCLogger(t)
    32  
    33  	// Get the registered plugins
    34  	catalog := Catalog()
    35  
    36  	// Create our map of plugins
    37  	internal := make(map[loader.PluginID]*loader.InternalPluginConfig, len(catalog))
    38  
    39  	for id, reg := range catalog {
    40  		if reg.Config == nil {
    41  			logger.Warn("skipping loading internal plugin because it is missing its configuration", "plugin", id)
    42  			continue
    43  		}
    44  
    45  		pluginConfig := reg.Config.Config
    46  		if reg.ConfigLoader != nil {
    47  			pc, err := reg.ConfigLoader(options)
    48  			if err != nil {
    49  				t.Fatalf("failed to retrieve config for internal plugin %v: %v", id, err)
    50  			}
    51  
    52  			pluginConfig = pc
    53  		}
    54  
    55  		internal[id] = &loader.InternalPluginConfig{
    56  			Factory: reg.Config.Factory,
    57  			Config:  pluginConfig,
    58  		}
    59  	}
    60  
    61  	// Build the plugin loader
    62  	config := &loader.PluginLoaderConfig{
    63  		Logger:            logger,
    64  		PluginDir:         "",
    65  		Configs:           configs,
    66  		InternalPlugins:   internal,
    67  		SupportedVersions: loader.AgentSupportedApiVersions,
    68  	}
    69  	l, err := loader.NewPluginLoader(config)
    70  	if err != nil {
    71  		t.Fatalf("failed to create plugin loader: %v", err)
    72  	}
    73  
    74  	return l
    75  }