(t *testing.T)
| 956 | } |
| 957 | |
| 958 | func TestHook(t *testing.T) { |
| 959 | if testing.Short() { |
| 960 | return |
| 961 | } |
| 962 | |
| 963 | config := newTemplateConfig(t, nil) |
| 964 | expectedBundle := t.TempDir() |
| 965 | config.Labels = append(config.Labels, "bundle="+expectedBundle) |
| 966 | |
| 967 | getRootfsFromBundle := func(bundle string) (string, error) { |
| 968 | f, err := os.Open(filepath.Join(bundle, "config.json")) |
| 969 | if err != nil { |
| 970 | return "", err |
| 971 | } |
| 972 | |
| 973 | var config configs.Config |
| 974 | if err = json.NewDecoder(f).Decode(&config); err != nil { |
| 975 | return "", err |
| 976 | } |
| 977 | return config.Rootfs, nil |
| 978 | } |
| 979 | createFileFromBundle := func(filename, bundle string) error { |
| 980 | root, err := getRootfsFromBundle(bundle) |
| 981 | if err != nil { |
| 982 | return err |
| 983 | } |
| 984 | |
| 985 | f, err := os.Create(filepath.Join(root, filename)) |
| 986 | if err != nil { |
| 987 | return err |
| 988 | } |
| 989 | return f.Close() |
| 990 | } |
| 991 | |
| 992 | // Note FunctionHooks can't be serialized to json this means they won't be passed down to the container |
| 993 | // For CreateContainer and StartContainer which run in the container namespace, this means we need to pass Command Hooks. |
| 994 | hookFiles := map[configs.HookName]string{ |
| 995 | configs.Prestart: "prestart", |
| 996 | configs.CreateRuntime: "createRuntime", |
| 997 | configs.CreateContainer: "createContainer", |
| 998 | configs.StartContainer: "startContainer", |
| 999 | configs.Poststart: "poststart", |
| 1000 | } |
| 1001 | |
| 1002 | config.Hooks = configs.Hooks{ |
| 1003 | configs.Prestart: configs.HookList{ |
| 1004 | configs.NewFunctionHook(func(s *specs.State) error { |
| 1005 | if s.Bundle != expectedBundle { |
| 1006 | t.Fatalf("Expected prestart hook bundlePath '%s'; got '%s'", expectedBundle, s.Bundle) |
| 1007 | } |
| 1008 | return createFileFromBundle(hookFiles[configs.Prestart], s.Bundle) |
| 1009 | }), |
| 1010 | }, |
| 1011 | configs.CreateRuntime: configs.HookList{ |
| 1012 | configs.NewFunctionHook(func(s *specs.State) error { |
| 1013 | if s.Bundle != expectedBundle { |
| 1014 | t.Fatalf("Expected createRuntime hook bundlePath '%s'; got '%s'", expectedBundle, s.Bundle) |
| 1015 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…