Skip to content

Commit ed596cb

Browse files
authored
Merge pull request #187 from sameersaeed/hook-injector-tests
Add integration tests for hook injector plugin
2 parents 012604f + 67ebccd commit ed596cb

3 files changed

Lines changed: 132 additions & 2 deletions

File tree

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ $(BIN_PATH)/wasm build/bin/wasm: FORCE
138138
# test targets
139139
#
140140

141-
test-gopkgs: ginkgo-tests test-ulimits test-rdt
141+
test-gopkgs: ginkgo-tests test-ulimits test-rdt test-hook-injector
142142

143-
SKIPPED_PKGS="ulimit-adjuster,device-injector,rdt"
143+
SKIPPED_PKGS="ulimit-adjuster,device-injector,rdt,hook-injector"
144144

145145
ginkgo-tests:
146146
$(Q)$(GINKGO) run \
@@ -165,6 +165,9 @@ test-device-injector:
165165
test-rdt:
166166
$(Q)cd ./plugins/rdt && $(GO_TEST) -v
167167

168+
test-hook-injector:
169+
$(Q)cd ./plugins/hook-injector && $(GO_TEST) -v
170+
168171
codecov: SHELL := $(shell which bash)
169172
codecov:
170173
bash <(curl -s https://codecov.io/bash) -f $(COVERAGE_PATH)/coverprofile

plugins/hook-injector/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@ require (
66
github.com/containerd/nri v0.6.1
77
github.com/opencontainers/runtime-spec v1.3.0
88
github.com/sirupsen/logrus v1.9.3
9+
github.com/stretchr/testify v1.11.1
910
go.podman.io/common v0.66.1
1011
sigs.k8s.io/yaml v1.6.0
1112
)
1213

1314
require (
1415
github.com/containerd/log v0.1.0 // indirect
1516
github.com/containerd/ttrpc v1.2.7 // indirect
17+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
1618
github.com/fsnotify/fsnotify v1.9.0 // indirect
1719
github.com/knqyf263/go-plugin v0.9.0 // indirect
1820
github.com/kr/text v0.2.0 // indirect
21+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
1922
github.com/tetratelabs/wazero v1.11.0 // indirect
2023
go.podman.io/storage v1.61.0 // indirect
2124
go.yaml.in/yaml/v2 v2.4.2 // indirect
2225
golang.org/x/sys v0.38.0 // indirect
2326
google.golang.org/genproto/googleapis/rpc v0.0.0-20250414145226-207652e42e2e // indirect
2427
google.golang.org/grpc v1.72.2 // indirect
2528
google.golang.org/protobuf v1.36.9 // indirect
29+
gopkg.in/yaml.v3 v3.0.1 // indirect
2630
)
2731

2832
replace github.com/containerd/nri => ../..
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"os"
22+
"path/filepath"
23+
"testing"
24+
25+
"github.com/containerd/nri/pkg/api"
26+
"github.com/sirupsen/logrus"
27+
"github.com/stretchr/testify/assert"
28+
"go.podman.io/common/pkg/hooks"
29+
)
30+
31+
func TestHookInjector(t *testing.T) {
32+
log = logrus.StandardLogger()
33+
log.SetFormatter(&logrus.TextFormatter{PadLevelText: true})
34+
35+
t.Run("no hooks configured", func(t *testing.T) {
36+
testCreateContainerWithoutHooks(t)
37+
})
38+
39+
t.Run("hooks injected correctly", func(t *testing.T) {
40+
testCreateContainerWithHooks(t)
41+
})
42+
}
43+
44+
// testCreateContainerWithoutHooks validates that a container without hooks configured gets ignored
45+
func testCreateContainerWithoutHooks(t *testing.T) {
46+
t.Helper()
47+
48+
tempDir := t.TempDir()
49+
50+
mgr, err := hooks.New(context.Background(), []string{tempDir}, []string{})
51+
assert.NoError(t, err)
52+
53+
p := &plugin{mgr: mgr}
54+
pod, container := createTestPodAndContainer()
55+
56+
adjust, updates, err := p.CreateContainer(context.Background(), pod, container)
57+
58+
assert.NoError(t, err)
59+
assert.Nil(t, adjust)
60+
assert.Nil(t, updates)
61+
}
62+
63+
// testCreateContainerWithHooks validates that OCI hooks are correctly injected
64+
// into the container spec during creation when they are configured
65+
func testCreateContainerWithHooks(t *testing.T) {
66+
t.Helper()
67+
68+
tempDir := t.TempDir()
69+
70+
hookJSON := []byte(`{
71+
"version": "1.0.0",
72+
"hook": {
73+
"path": "/bin/echo",
74+
"args": ["echo", "testing from hook"]
75+
},
76+
"when": {
77+
"always": true
78+
},
79+
"stages": ["createRuntime"]
80+
}`)
81+
82+
hookPath := filepath.Join(tempDir, "test-hook.json")
83+
err := os.WriteFile(hookPath, hookJSON, 0644)
84+
assert.NoError(t, err)
85+
86+
mgr, err := hooks.New(context.Background(), []string{tempDir}, []string{})
87+
assert.NoError(t, err)
88+
89+
p := &plugin{mgr: mgr}
90+
pod, container := createTestPodAndContainer()
91+
92+
adjust, updates, err := p.CreateContainer(context.Background(), pod, container)
93+
94+
assert.NoError(t, err)
95+
assert.NotNil(t, adjust)
96+
assert.Nil(t, updates)
97+
98+
hooks := adjust.Hooks
99+
assert.NotNil(t, hooks.Hooks())
100+
assert.NotEmpty(t, hooks.CreateRuntime, "expected createRuntime hooks to be injected")
101+
102+
found := false
103+
for _, h := range hooks.CreateRuntime {
104+
if h.Path == "/bin/echo" && len(h.Args) > 0 && h.Args[0] == "echo" {
105+
found = true
106+
break
107+
}
108+
}
109+
assert.True(t, found, "couldn't find injected hook, or it was incorrect")
110+
}
111+
112+
func createTestPodAndContainer() (*api.PodSandbox, *api.Container) {
113+
pod := &api.PodSandbox{
114+
Name: "test-pod-hook-injector",
115+
Annotations: map[string]string{},
116+
}
117+
container := &api.Container{
118+
Name: "test-container-hook-injector",
119+
Annotations: map[string]string{},
120+
Args: []string{"run.sh"},
121+
}
122+
return pod, container
123+
}

0 commit comments

Comments
 (0)