github.com/hernad/nomad@v1.6.112/helper/pluginutils/catalog/testing.go (about)

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