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 27, 2024
1 parent f86d982 commit 320e4e7
Show file tree
Hide file tree
Showing 2 changed files with 94 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
31 changes: 28 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 @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
}

Expand Down

0 comments on commit 320e4e7

Please sign in to comment.