Skip to content

Commit

Permalink
test the plugin system
Browse files Browse the repository at this point in the history
...building towards a test for hooks
  • Loading branch information
knopers8 committed Jun 27, 2024
1 parent 1e83c51 commit 459a823
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ INSTALL_WHAT:=$(patsubst %, install_%, $(WHAT))

GENERATE_DIRS := ./apricot ./coconut/cmd ./common ./common/runtype ./common/system ./core ./core/integration/ccdb ./core/integration/dcs ./core/integration/ddsched ./core/integration/kafka ./core/integration/odc ./executor ./walnut ./core/integration/trg ./core/integration/bookkeeping
SRC_DIRS := ./apricot ./cmd/* ./core ./coconut ./executor ./common ./configuration ./occ/peanut ./walnut
TEST_DIRS := ./apricot/local ./configuration/cfgbackend ./configuration/componentcfg ./configuration/template ./core/task/sm ./core/workflow ./core/integration/odc/fairmq
TEST_DIRS := ./apricot/local ./configuration/cfgbackend ./configuration/componentcfg ./configuration/template ./core/task/sm ./core/workflow ./core/integration/odc/fairmq ./core/integration
GO_TEST_DIRS := ./core/repos ./core/integration/dcs

coverage:COVERAGE_PREFIX := ./coverage_results
Expand Down
8 changes: 8 additions & 0 deletions core/integration/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,11 @@ func PluginsInstance() Plugins {
})
return instance
}

// Reset resets the plugin system for testing purposes.
func Reset() {
once = sync.Once{}
instance = Plugins{}
loaderOnce = sync.Once{}
pluginLoaders = make(map[string]func() Plugin)
}
229 changes: 229 additions & 0 deletions core/integration/plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
package integration_test

import (
"github.com/AliceO2Group/Control/common/utils/uid"
"github.com/AliceO2Group/Control/core/integration"
"github.com/AliceO2Group/Control/core/integration/testplugin"
"github.com/AliceO2Group/Control/core/workflow/callable"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/viper"
"testing"
)

var _ = Describe("plugin framework", Ordered, func() {

BeforeEach(func() {
// Reset the integration package state before each test
integration.Reset()
viper.Reset()
})

var _ = Describe("Plugin Registration", Ordered, func() {
It("should register a plugin correctly", func() {
// ... even if we register it multiple times
integration.RegisterPlugin("testplugin", "testPluginEndpoint", testplugin.NewPlugin)
integration.RegisterPlugin("testplugin", "testPluginEndpoint", testplugin.NewPlugin)

registeredPlugins := integration.RegisteredPlugins()
Expect(registeredPlugins).To(HaveLen(1))
Expect(registeredPlugins).To(HaveKey("testplugin"))

pluginLoader := registeredPlugins["testplugin"]
Expect(pluginLoader).ToNot(BeNil())
})
})

var _ = Describe("Loading Plugins", Ordered, func() {
BeforeEach(func() {
// Reset the integration package state before each test
integration.Reset()
viper.Reset()
})
It("should load a plugin correctly", func() {
integration.RegisterPlugin("testplugin", "testPluginEndpoint", testplugin.NewPlugin)
viper.Set("testPluginEndpoint", "http://example.com")
viper.Set("integrationPlugins", []string{"testplugin"})
plugins := integration.PluginsInstance()
Expect(plugins).To(HaveLen(1))
Expect(plugins[0].GetName()).To(Equal("testplugin"))
})
It("should fail to load a plugin if endpoint is not set", func() {
integration.RegisterPlugin("testplugin", "testPluginEndpoint", testplugin.NewPlugin)
viper.Set("integrationPlugins", []string{"testplugin"})
plugins := integration.PluginsInstance()
Expect(plugins).To(BeEmpty())
})
It("should not load a plugin which is not registered", func() {
viper.Set("testPluginEndpoint", "http://example.com")
viper.Set("integrationPlugins", []string{"testplugin"})
plugins := integration.PluginsInstance()
Expect(plugins).To(BeEmpty())
})
It("should not load a plugin which is not in the list of integration plugins", func() {
integration.RegisterPlugin("testplugin", "testPluginEndpoint", testplugin.NewPlugin)
viper.Set("testPluginEndpoint", "http://example.com")
viper.Set("integrationPlugins", []string{})
plugins := integration.PluginsInstance()
Expect(plugins).To(BeEmpty())
})
})

Describe("Plugin Getters", Ordered, func() {
var pluginInstance integration.Plugin

BeforeEach(func() {
integration.RegisterPlugin("testplugin", "testPluginEndpoint", testplugin.NewPlugin)

registeredPlugins := integration.RegisteredPlugins()
pluginLoader := registeredPlugins["testplugin"]

// Simulate setting the endpoint in Viper
viper.Set("testPluginEndpoint", "http://example.com")
pluginInstance = pluginLoader()
})

It("should return the correct name", func() {
Expect(pluginInstance.GetName()).To(Equal("testplugin"))
})

It("should return the correct pretty name", func() {
Expect(pluginInstance.GetPrettyName()).To(Equal("Test plugin"))
})

It("should return the correct connection state", func() {
Expect(pluginInstance.GetConnectionState()).To(Equal("READY"))
})
It("should set and get the endpoint correctly", func() {
Expect(pluginInstance.GetEndpoint()).To(Equal("http://example.com"))
})

It("should return the correct data", func() {
Expect(pluginInstance.GetData(nil)).To(Equal("test_data"))
})

It("should return the correct environments data", func() {
envId, _ := uid.FromString("2oDvieFrVTi")
envData := pluginInstance.GetEnvironmentsData([]uid.ID{envId})
Expect(envData).To(HaveLen(1))
Expect(envData).To(HaveKeyWithValue(envId, "test_data_2oDvieFrVTi"))
})

It("should return the correct environments short data", func() {
envId, _ := uid.FromString("2oDvieFrVTi")
envData := pluginInstance.GetEnvironmentsShortData([]uid.ID{envId})
Expect(envData).To(HaveLen(1))
Expect(envData).To(HaveKeyWithValue(envId, "test_short_data_2oDvieFrVTi"))
})
})

Describe("Global Plugin Getters", Ordered, func() {
var plugins integration.Plugins

BeforeEach(func() {
integration.Reset()
integration.RegisterPlugin("testplugin", "testPluginEndpoint", testplugin.NewPlugin)
viper.Reset()
viper.Set("integrationPlugins", []string{"testplugin"})
viper.Set("testPluginEndpoint", "http://example.com")
plugins = integration.PluginsInstance()
})
It("should return the data returned by the plugin", func() {
data := plugins.GetData(nil)
Expect(data).To(HaveLen(1))
Expect(data).To(HaveKeyWithValue("testplugin", "test_data"))
})

It("should return the env data returned by the plugin", func() {
envId, _ := uid.FromString("2oDvieFrVTi")
envData := plugins.GetEnvironmentsData([]uid.ID{envId})
Expect(envData).To(HaveLen(1))
Expect(envData).To(HaveKey(envId))
envDataTestPlugin := envData[envId]
Expect(envDataTestPlugin).To(HaveLen(1))
Expect(envDataTestPlugin).To(HaveKeyWithValue("testplugin", "test_data_2oDvieFrVTi"))
})

It("should return the short env data returned by the plugin", func() {
envId, _ := uid.FromString("2oDvieFrVTi")
envData := plugins.GetEnvironmentsShortData([]uid.ID{envId})
Expect(envData).To(HaveLen(1))
Expect(envData).To(HaveKey(envId))
envDataTestPlugin := envData[envId]
Expect(envDataTestPlugin).To(HaveLen(1))
Expect(envDataTestPlugin).To(HaveKeyWithValue("testplugin", "test_short_data_2oDvieFrVTi"))
})
})

Describe("InitAll and DestroyAll", Ordered, func() {
var plugins integration.Plugins

BeforeEach(func() {
viper.Set("integrationPlugins", []string{"testplugin"})
viper.Set("testPluginEndpoint", "http://example.com")
integration.RegisterPlugin("testplugin", "testPluginEndpoint", testplugin.NewPlugin)
plugins = integration.PluginsInstance()
})

It("should initialize and destroy all plugins correctly", func() {
plugins.InitAll("instance_id")
plugins.DestroyAll()
})
})

Describe("Call Stack", Ordered, func() {
var plugins integration.Plugins

BeforeEach(func() {
viper.Reset()
viper.Set("integrationPlugins", []string{"testplugin"})
viper.Set("testPluginEndpoint", "http://example.com")
integration.RegisterPlugin("testplugin", "testPluginEndpoint", testplugin.NewPlugin)
plugins = integration.PluginsInstance()
})

It("should create a call stack for all registered plugins", func() {
call := &callable.Call{
VarStack: map[string]string{
"environment_id": "2oDvieFrVTi",
},
}

callStack := plugins.CallStack(call)

Expect(callStack).To(HaveLen(1))
Expect(callStack).To(HaveKey("testplugin"))

testPluginCallStack := callStack["testplugin"]
Expect(testPluginCallStack).To(HaveKey("Noop"))
Expect(testPluginCallStack).To(HaveKey("Test"))
})
})

Describe("Object Stack", Ordered, func() {
var plugins integration.Plugins

BeforeEach(func() {
viper.Reset()
viper.Set("integrationPlugins", []string{"testplugin"})
viper.Set("testPluginEndpoint", "http://example.com")
integration.RegisterPlugin("testplugin", "testPluginEndpoint", testplugin.NewPlugin)
plugins = integration.PluginsInstance()
})

It("should create an object stack for all registered plugins", func() {
objectStack := plugins.ObjectStack(nil, nil)

Expect(objectStack).To(HaveLen(2))
Expect(objectStack).To(HaveKey("odc"))
Expect(objectStack).To(HaveKey("testplugin"))
testPluginObjectStack := objectStack["testplugin"]
Expect(testPluginObjectStack).To(HaveKeyWithValue("test", "test_data"))
})
})
})

func TestCoreIntegration(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Core Integration Test Suite")
}
19 changes: 14 additions & 5 deletions core/integration/testplugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,23 @@ func (p *Plugin) GetConnectionState() string {
}

func (p *Plugin) GetData(_ []any) string {
return ""
return "test_data"
}

func (p *Plugin) GetEnvironmentsData(_ []uid.ID) map[uid.ID]string {
return nil
func (p *Plugin) GetEnvironmentsData(envIds []uid.ID) map[uid.ID]string {
envData := make(map[uid.ID]string)
for _, envId := range envIds {
envData[envId] = "test_data_" + envId.String()
}
return envData
}

func (p *Plugin) GetEnvironmentsShortData(_ []uid.ID) map[uid.ID]string {
return nil
func (p *Plugin) GetEnvironmentsShortData(envIds []uid.ID) map[uid.ID]string {
envData := make(map[uid.ID]string)
for _, envId := range envIds {
envData[envId] = "test_short_data_" + envId.String()
}
return envData
}

func (p *Plugin) Init(_ string) error {
Expand All @@ -88,6 +96,7 @@ func (p *Plugin) Init(_ string) error {

func (p *Plugin) ObjectStack(_ map[string]string, _ map[string]string) (stack map[string]interface{}) {
stack = make(map[string]interface{})
stack["test"] = "test_data"
return stack
}

Expand Down

0 comments on commit 459a823

Please sign in to comment.