From 320e4e7e52a856b119cfa1c06a4a135ab5f88f56 Mon Sep 17 00:00:00 2001 From: Krisztian Litkey Date: Fri, 20 Sep 2024 16:31:17 +0300 Subject: [PATCH] adaptation: tests for runtime version, timeouts. Add tests for passing runtime version and configured timeouts to plugins. Signed-off-by: Krisztian Litkey --- pkg/adaptation/adaptation_suite_test.go | 66 +++++++++++++++++++++++++ pkg/adaptation/suite_test.go | 31 ++++++++++-- 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/pkg/adaptation/adaptation_suite_test.go b/pkg/adaptation/adaptation_suite_test.go index 8c7a75b..c64c074 100644 --- a/pkg/adaptation/adaptation_suite_test.go +++ b/pkg/adaptation/adaptation_suite_test.go @@ -1869,6 +1869,72 @@ var _ = Describe("Unsolicited container update requests", func() { }) }) +var _ = Describe("Plugin configuration request", func() { + var ( + s = &Suite{} + ) + + AfterEach(func() { + s.Cleanup() + }) + + BeforeEach(func() { + s.Prepare(&mockRuntime{}, &mockPlugin{idx: "00", name: "test"}) + }) + + It("should pass runtime version information to plugins", func() { + var ( + runtimeName = "test-runtime" + runtimeVersion = "1.2.3" + ) + + s.runtime.name = runtimeName + s.runtime.version = runtimeVersion + + s.Startup() + + Expect(s.plugins[0].RuntimeName()).To(Equal(runtimeName)) + Expect(s.plugins[0].RuntimeVersion()).To(Equal(runtimeVersion)) + }) + + When("unchanged", func() { + It("should pass default timeout information to plugins", func() { + var ( + registerTimeout = nri.DefaultPluginRegistrationTimeout + requestTimeout = nri.DefaultPluginRequestTimeout + ) + + s.Startup() + Expect(s.plugins[0].stub.RegistrationTimeout()).To(Equal(registerTimeout)) + Expect(s.plugins[0].stub.RequestTimeout()).To(Equal(requestTimeout)) + }) + }) + + When("reconfigured", func() { + var ( + registerTimeout = nri.DefaultPluginRegistrationTimeout + 5*time.Millisecond + requestTimeout = nri.DefaultPluginRequestTimeout + 7*time.Millisecond + ) + + BeforeEach(func() { + nri.SetPluginRegistrationTimeout(registerTimeout) + nri.SetPluginRequestTimeout(requestTimeout) + s.Prepare(&mockRuntime{}, &mockPlugin{idx: "00", name: "test"}) + }) + + AfterEach(func() { + nri.SetPluginRegistrationTimeout(nri.DefaultPluginRegistrationTimeout) + nri.SetPluginRequestTimeout(nri.DefaultPluginRequestTimeout) + }) + + It("should pass configured timeout information to plugins", func() { + s.Startup() + Expect(s.plugins[0].stub.RegistrationTimeout()).To(Equal(registerTimeout)) + Expect(s.plugins[0].stub.RequestTimeout()).To(Equal(requestTimeout)) + }) + }) +}) + // Notes: // // XXX FIXME KLUDGE diff --git a/pkg/adaptation/suite_test.go b/pkg/adaptation/suite_test.go index a5099e4..369f0d6 100644 --- a/pkg/adaptation/suite_test.go +++ b/pkg/adaptation/suite_test.go @@ -43,7 +43,9 @@ func TestRuntime(t *testing.T) { } const ( - startupTimeout = 2 * time.Second + startupTimeout = 2 * time.Second + defaultRuntimeName = "default-runtime-name" + defaultRuntimeVersion = "0.1.2" ) // A test suite consist of a runtime and a set of plugins. @@ -70,6 +72,13 @@ func (s *Suite) Prepare(runtime *mockRuntime, plugins ...*mockPlugin) string { Expect(os.MkdirAll(etc, 0o755)).To(Succeed()) + if runtime.name == "" { + runtime.name = defaultRuntimeName + } + if runtime.version == "" { + runtime.version = defaultRuntimeVersion + } + s.dir = dir s.runtime = runtime s.plugins = plugins @@ -126,6 +135,8 @@ func Log(format string, args ...interface{}) { } type mockRuntime struct { + name string + version string options []nri.Option runtime *nri.Adaptation pods map[string]*api.PodSandbox @@ -149,7 +160,7 @@ func (m *mockRuntime) Start(dir string) error { } options = append(options, m.options...) - m.runtime, err = nri.New("mockRuntime", "0.0.1", m.synchronize, m.update, options...) + m.runtime, err = nri.New(m.name, m.version, m.synchronize, m.update, options...) if err != nil { return err } @@ -323,6 +334,9 @@ type mockPlugin struct { stub stub.Stub mask stub.EventMask + runtime string + version string + q *EventQ pods map[string]*api.PodSandbox ctrs map[string]*api.Container @@ -477,6 +491,14 @@ func (m *mockPlugin) Stop() { m.q.Add(PluginStopped) } +func (m *mockPlugin) RuntimeName() string { + return m.runtime +} + +func (m *mockPlugin) RuntimeVersion() string { + return m.version +} + func (m *mockPlugin) onClose() { if m.stub != nil { m.stub.Stop() @@ -488,9 +510,12 @@ func (m *mockPlugin) onClose() { } } -func (m *mockPlugin) Configure(_ context.Context, _, _, _ string) (stub.EventMask, error) { +func (m *mockPlugin) Configure(_ context.Context, _, runtime, version string) (stub.EventMask, error) { m.q.Add(PluginConfigured) + m.runtime = runtime + m.version = version + return m.mask, nil }