|
| 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