|
| 1 | +//go:build !windows |
| 2 | +// +build !windows |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2021 The Kruise Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package criruntime |
| 21 | + |
| 22 | +import ( |
| 23 | + "flag" |
| 24 | + "os" |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | +) |
| 28 | + |
| 29 | +type fileInfo struct { |
| 30 | + name string |
| 31 | +} |
| 32 | + |
| 33 | +func (f *fileInfo) Name() string { return f.name } |
| 34 | +func (f *fileInfo) Size() int64 { return 0 } |
| 35 | +func (f *fileInfo) Mode() os.FileMode { return 0644 } |
| 36 | +func (f *fileInfo) ModTime() time.Time { return time.Time{} } |
| 37 | +func (f *fileInfo) IsDir() bool { return false } |
| 38 | +func (f *fileInfo) Sys() interface{} { return nil } |
| 39 | + |
| 40 | +func TestDetectRuntimeUnix(t *testing.T) { |
| 41 | + testCases := []struct { |
| 42 | + name string |
| 43 | + flag string |
| 44 | + runtimeType ContainerRuntimeType |
| 45 | + runtimeRemoteURI string |
| 46 | + statFunc func(name string) (os.FileInfo, error) |
| 47 | + expectedCfgsCount int |
| 48 | + }{ |
| 49 | + { |
| 50 | + name: "non-existent-socket-with-flag", |
| 51 | + flag: "non-existent-socket", |
| 52 | + runtimeType: ContainerRuntimeCommonCRI, |
| 53 | + runtimeRemoteURI: "unix://hostvarrun/non-existent-socket", |
| 54 | + statFunc: func(name string) (os.FileInfo, error) { |
| 55 | + return nil, os.ErrNotExist |
| 56 | + }, |
| 57 | + expectedCfgsCount: 0, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "crio.sock-with-flag", |
| 61 | + flag: "crio.sock", |
| 62 | + runtimeType: ContainerRuntimeCommonCRI, |
| 63 | + runtimeRemoteURI: "unix://hostvarrun/crio.sock", |
| 64 | + statFunc: func(name string) (os.FileInfo, error) { |
| 65 | + if name == "hostvarrun/crio.sock" { |
| 66 | + return &fileInfo{name: name}, nil |
| 67 | + } |
| 68 | + return nil, os.ErrNotExist |
| 69 | + }, |
| 70 | + expectedCfgsCount: 1, |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "containerd.sock", |
| 74 | + flag: "", |
| 75 | + runtimeType: ContainerRuntimeContainerd, |
| 76 | + runtimeRemoteURI: "unix://hostvarrun/containerd.sock", |
| 77 | + statFunc: func(name string) (os.FileInfo, error) { |
| 78 | + if name == "hostvarrun/containerd.sock" { |
| 79 | + return &fileInfo{name: name}, nil |
| 80 | + } |
| 81 | + return nil, os.ErrNotExist |
| 82 | + }, |
| 83 | + expectedCfgsCount: 1, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "containerd/containerd.sock", |
| 87 | + flag: "", |
| 88 | + runtimeType: ContainerRuntimeContainerd, |
| 89 | + runtimeRemoteURI: "unix://hostvarrun/containerd/containerd.sock", |
| 90 | + statFunc: func(name string) (os.FileInfo, error) { |
| 91 | + if name == "hostvarrun/containerd/containerd.sock" { |
| 92 | + return &fileInfo{name: name}, nil |
| 93 | + } |
| 94 | + return nil, os.ErrNotExist |
| 95 | + }, |
| 96 | + expectedCfgsCount: 1, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "crio.sock", |
| 100 | + flag: "", |
| 101 | + runtimeType: ContainerRuntimeCommonCRI, |
| 102 | + runtimeRemoteURI: "unix://hostvarrun/crio.sock", |
| 103 | + statFunc: func(name string) (os.FileInfo, error) { |
| 104 | + if name == "hostvarrun/crio.sock" { |
| 105 | + return &fileInfo{name: name}, nil |
| 106 | + } |
| 107 | + return nil, os.ErrNotExist |
| 108 | + }, |
| 109 | + expectedCfgsCount: 1, |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "crio/crio.sock", |
| 113 | + flag: "", |
| 114 | + runtimeType: ContainerRuntimeCommonCRI, |
| 115 | + runtimeRemoteURI: "unix://hostvarrun/crio/crio.sock", |
| 116 | + statFunc: func(name string) (os.FileInfo, error) { |
| 117 | + if name == "hostvarrun/crio/crio.sock" { |
| 118 | + return &fileInfo{name: name}, nil |
| 119 | + } |
| 120 | + return nil, os.ErrNotExist |
| 121 | + }, |
| 122 | + expectedCfgsCount: 1, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "cri-dockerd", |
| 126 | + flag: "", |
| 127 | + runtimeType: ContainerRuntimeCommonCRI, |
| 128 | + runtimeRemoteURI: "unix://hostvarrun/cri-dockerd.sock", |
| 129 | + statFunc: func(name string) (os.FileInfo, error) { |
| 130 | + if name == "hostvarrun/cri-dockerd.sock" { |
| 131 | + return &fileInfo{name: name}, nil |
| 132 | + } |
| 133 | + return nil, os.ErrNotExist |
| 134 | + }, |
| 135 | + expectedCfgsCount: 1, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "cri-dockerd/cri-dockerd.sock", |
| 139 | + flag: "", |
| 140 | + runtimeType: ContainerRuntimeCommonCRI, |
| 141 | + runtimeRemoteURI: "unix://hostvarrun/cri-dockerd/cri-dockerd.sock", |
| 142 | + statFunc: func(name string) (os.FileInfo, error) { |
| 143 | + if name == "hostvarrun/cri-dockerd/cri-dockerd.sock" { |
| 144 | + return &fileInfo{name: name}, nil |
| 145 | + } |
| 146 | + return nil, os.ErrNotExist |
| 147 | + }, |
| 148 | + expectedCfgsCount: 1, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "non-existent-socket-without-flag", |
| 152 | + flag: "", |
| 153 | + runtimeType: ContainerRuntimeCommonCRI, |
| 154 | + runtimeRemoteURI: "unix://hostvarrun/non-existent-socket", |
| 155 | + statFunc: func(name string) (os.FileInfo, error) { |
| 156 | + return nil, os.ErrNotExist |
| 157 | + }, |
| 158 | + expectedCfgsCount: 0, |
| 159 | + }, |
| 160 | + } |
| 161 | + |
| 162 | + for _, testCase := range testCases { |
| 163 | + flag.Set("socket-file", testCase.flag) |
| 164 | + statFunc = testCase.statFunc |
| 165 | + defer func() { statFunc = os.Stat }() |
| 166 | + |
| 167 | + cfgs := detectRuntime() |
| 168 | + if len(cfgs) != testCase.expectedCfgsCount { |
| 169 | + t.Fatalf("expected %d runtime config, got %d", testCase.expectedCfgsCount, len(cfgs)) |
| 170 | + } |
| 171 | + if testCase.expectedCfgsCount > 0 { |
| 172 | + if cfgs[0].runtimeRemoteURI != testCase.runtimeRemoteURI { |
| 173 | + t.Fatalf("expected runtime remote URI to be %s, got %s", testCase.runtimeRemoteURI, cfgs[0].runtimeRemoteURI) |
| 174 | + } |
| 175 | + if cfgs[0].runtimeType != testCase.runtimeType { |
| 176 | + t.Fatalf("expected runtime type to be %s, got %s", testCase.runtimeType, cfgs[0].runtimeType) |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments