2018-09-18 17:08:46 +00:00
|
|
|
package loader
|
|
|
|
|
|
|
|
import (
|
2018-11-01 01:00:30 +00:00
|
|
|
"net"
|
2018-11-07 20:22:07 +00:00
|
|
|
"sync"
|
2018-11-01 01:00:30 +00:00
|
|
|
|
2018-09-18 17:08:46 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2018-11-01 01:00:30 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2018-09-18 17:08:46 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MockCatalog provides a mock PluginCatalog to be used for testing
|
|
|
|
type MockCatalog struct {
|
2018-12-18 00:40:58 +00:00
|
|
|
DispenseF func(name, pluginType string, cfg *base.AgentConfig, logger log.Logger) (PluginInstance, error)
|
2018-09-18 17:08:46 +00:00
|
|
|
ReattachF func(name, pluginType string, config *plugin.ReattachConfig) (PluginInstance, error)
|
|
|
|
CatalogF func() map[string][]*base.PluginInfoResponse
|
|
|
|
}
|
|
|
|
|
2018-12-18 00:40:58 +00:00
|
|
|
func (m *MockCatalog) Dispense(name, pluginType string, cfg *base.AgentConfig, logger log.Logger) (PluginInstance, error) {
|
2018-10-17 02:21:15 +00:00
|
|
|
return m.DispenseF(name, pluginType, cfg, logger)
|
2018-09-18 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockCatalog) Reattach(name, pluginType string, config *plugin.ReattachConfig) (PluginInstance, error) {
|
|
|
|
return m.ReattachF(name, pluginType, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockCatalog) Catalog() map[string][]*base.PluginInfoResponse {
|
|
|
|
return m.CatalogF()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MockInstance provides a mock PluginInstance to be used for testing
|
|
|
|
type MockInstance struct {
|
|
|
|
InternalPlugin bool
|
|
|
|
KillF func()
|
|
|
|
ReattachConfigF func() (*plugin.ReattachConfig, bool)
|
|
|
|
PluginF func() interface{}
|
|
|
|
ExitedF func() bool
|
2018-12-18 00:40:58 +00:00
|
|
|
ApiVersionF func() string
|
2018-09-18 17:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MockInstance) Internal() bool { return m.InternalPlugin }
|
|
|
|
func (m *MockInstance) Kill() { m.KillF() }
|
|
|
|
func (m *MockInstance) ReattachConfig() (*plugin.ReattachConfig, bool) { return m.ReattachConfigF() }
|
|
|
|
func (m *MockInstance) Plugin() interface{} { return m.PluginF() }
|
|
|
|
func (m *MockInstance) Exited() bool { return m.ExitedF() }
|
2018-12-18 00:40:58 +00:00
|
|
|
func (m *MockInstance) ApiVersion() string { return m.ApiVersionF() }
|
2018-11-01 01:00:30 +00:00
|
|
|
|
|
|
|
// MockBasicExternalPlugin returns a MockInstance that simulates an external
|
|
|
|
// plugin returning it has been exited after kill is called. It returns the
|
|
|
|
// passed inst as the plugin
|
2018-12-18 00:40:58 +00:00
|
|
|
func MockBasicExternalPlugin(inst interface{}, apiVersion string) *MockInstance {
|
2018-11-07 20:22:07 +00:00
|
|
|
var killedLock sync.Mutex
|
2018-11-01 01:00:30 +00:00
|
|
|
killed := helper.BoolToPtr(false)
|
|
|
|
return &MockInstance{
|
|
|
|
InternalPlugin: false,
|
|
|
|
KillF: func() {
|
2018-11-07 20:22:07 +00:00
|
|
|
killedLock.Lock()
|
|
|
|
defer killedLock.Unlock()
|
2018-11-01 01:00:30 +00:00
|
|
|
*killed = true
|
|
|
|
},
|
|
|
|
|
|
|
|
ReattachConfigF: func() (*plugin.ReattachConfig, bool) {
|
|
|
|
return &plugin.ReattachConfig{
|
|
|
|
Protocol: "tcp",
|
|
|
|
Addr: &net.TCPAddr{
|
|
|
|
IP: net.IPv4(127, 0, 0, 1),
|
|
|
|
Port: 3200,
|
|
|
|
Zone: "",
|
|
|
|
},
|
|
|
|
Pid: 1000,
|
|
|
|
}, true
|
|
|
|
},
|
|
|
|
|
|
|
|
PluginF: func() interface{} {
|
|
|
|
return inst
|
|
|
|
},
|
|
|
|
|
|
|
|
ExitedF: func() bool {
|
2018-11-07 20:22:07 +00:00
|
|
|
killedLock.Lock()
|
|
|
|
defer killedLock.Unlock()
|
2018-11-01 01:00:30 +00:00
|
|
|
return *killed
|
|
|
|
},
|
2018-12-18 00:40:58 +00:00
|
|
|
|
|
|
|
ApiVersionF: func() string { return apiVersion },
|
2018-11-01 01:00:30 +00:00
|
|
|
}
|
|
|
|
}
|