Skip to content

Commit

Permalink
adaptation: tests for runtime version, timeouts.
Browse files Browse the repository at this point in the history
Add tests for passing runtime version and configured timeouts
to plugins.

Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub committed Sep 25, 2024
1 parent 8cfc175 commit 37b9262
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
66 changes: 66 additions & 0 deletions pkg/adaptation/adaptation_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 33 additions & 3 deletions pkg/adaptation/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -107,6 +116,11 @@ func (s *Suite) WaitForPluginsToSync() {
for _, plugin := range s.plugins {
Expect(plugin.Wait(PluginSynchronized, timeout)).To(Succeed())
}

// TODO(klihub): We lack the means to wait for synchronized plugins
// to get fully registered by mockRuntime. However, the tests still
// expect WaitForPluginsToSync to do that...
time.Sleep(25 * time.Millisecond)
}

// Cleanup the test suite.
Expand All @@ -126,6 +140,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
Expand All @@ -149,7 +165,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
}
Expand Down Expand Up @@ -305,6 +321,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
Expand Down Expand Up @@ -459,6 +478,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()
Expand All @@ -470,9 +497,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
}

Expand Down

0 comments on commit 37b9262

Please sign in to comment.