Skip to content

Commit 2a00af2

Browse files
author
Peng Peng
committed
Add unit tests
1 parent ebb8577 commit 2a00af2

File tree

4 files changed

+227
-7
lines changed

4 files changed

+227
-7
lines changed

pkg/daemon/criruntime/factory_unix.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
)
3333

3434
var (
35+
statFunc = os.Stat
3536
criSocketFileName = flag.String("socket-file", "", "The name of CRI socket file, and it should be in the mounted /hostvarrun directory.")
3637
)
3738

@@ -41,7 +42,7 @@ func detectRuntime() (cfgs []runtimeConfig) {
4142
// firstly check if it is configured from flag
4243
if criSocketFileName != nil && len(*criSocketFileName) > 0 {
4344
filePath := fmt.Sprintf("%s/%s", varRunMountPath, *criSocketFileName)
44-
if _, err = os.Stat(filePath); err == nil {
45+
if _, err = statFunc(filePath); err == nil {
4546
cfgs = append(cfgs, runtimeConfig{
4647
runtimeType: ContainerRuntimeCommonCRI,
4748
runtimeRemoteURI: fmt.Sprintf("unix://%s/%s", varRunMountPath, *criSocketFileName),
@@ -57,13 +58,13 @@ func detectRuntime() (cfgs []runtimeConfig) {
5758

5859
// containerd, with the same behavior of pullImage as commonCRI
5960
{
60-
if _, err = os.Stat(fmt.Sprintf("%s/containerd.sock", varRunMountPath)); err == nil {
61+
if _, err = statFunc(fmt.Sprintf("%s/containerd.sock", varRunMountPath)); err == nil {
6162
cfgs = append(cfgs, runtimeConfig{
6263
runtimeType: ContainerRuntimeContainerd,
6364
runtimeRemoteURI: fmt.Sprintf("unix://%s/containerd.sock", varRunMountPath),
6465
})
6566
}
66-
if _, err = os.Stat(fmt.Sprintf("%s/containerd/containerd.sock", varRunMountPath)); err == nil {
67+
if _, err = statFunc(fmt.Sprintf("%s/containerd/containerd.sock", varRunMountPath)); err == nil {
6768
cfgs = append(cfgs, runtimeConfig{
6869
runtimeType: ContainerRuntimeContainerd,
6970
runtimeRemoteURI: fmt.Sprintf("unix://%s/containerd/containerd.sock", varRunMountPath),
@@ -73,13 +74,13 @@ func detectRuntime() (cfgs []runtimeConfig) {
7374

7475
// cri-o
7576
{
76-
if _, err = os.Stat(fmt.Sprintf("%s/crio.sock", varRunMountPath)); err == nil {
77+
if _, err = statFunc(fmt.Sprintf("%s/crio.sock", varRunMountPath)); err == nil {
7778
cfgs = append(cfgs, runtimeConfig{
7879
runtimeType: ContainerRuntimeCommonCRI,
7980
runtimeRemoteURI: fmt.Sprintf("unix://%s/crio.sock", varRunMountPath),
8081
})
8182
}
82-
if _, err = os.Stat(fmt.Sprintf("%s/crio/crio.sock", varRunMountPath)); err == nil {
83+
if _, err = statFunc(fmt.Sprintf("%s/crio/crio.sock", varRunMountPath)); err == nil {
8384
cfgs = append(cfgs, runtimeConfig{
8485
runtimeType: ContainerRuntimeCommonCRI,
8586
runtimeRemoteURI: fmt.Sprintf("unix://%s/crio/crio.sock", varRunMountPath),
@@ -89,15 +90,15 @@ func detectRuntime() (cfgs []runtimeConfig) {
8990

9091
// cri-docker dockerd as a compliant Container Runtime Interface, detail see https://github.com/Mirantis/cri-dockerd
9192
{
92-
if _, err = os.Stat(fmt.Sprintf("%s/cri-dockerd.sock", varRunMountPath)); err == nil {
93+
if _, err = statFunc(fmt.Sprintf("%s/cri-dockerd.sock", varRunMountPath)); err == nil {
9394
cfgs = append(cfgs, runtimeConfig{
9495
runtimeType: ContainerRuntimeCommonCRI,
9596
runtimeRemoteURI: fmt.Sprintf("unix://%s/cri-dockerd.sock", varRunMountPath),
9697
})
9798
}
9899
// Check if the cri-dockerd runtime socket exists in the expected k3s runtime directory.
99100
// If found, append it to the runtime configuration list to ensure k3s can use cri-dockerd.
100-
if _, err = os.Stat(fmt.Sprintf("%s/cri-dockerd/cri-dockerd.sock", varRunMountPath)); err == nil {
101+
if _, err = statFunc(fmt.Sprintf("%s/cri-dockerd/cri-dockerd.sock", varRunMountPath)); err == nil {
101102
cfgs = append(cfgs, runtimeConfig{
102103
runtimeType: ContainerRuntimeCommonCRI,
103104
runtimeRemoteURI: fmt.Sprintf("unix://%s/cri-dockerd/cri-dockerd.sock", varRunMountPath),
+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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+
}

pkg/daemon/criruntime/factory_windows.go

+5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ limitations under the License.
2020
package criruntime
2121

2222
var (
23+
// containerdRemoteURI is the remote URI for containerd.
24+
// On Windows the default CRI endpoint is npipe://./pipe/containerd-containerd .
25+
// source: https://kubernetes.io/docs/setup/production-environment/container-runtimes/#containerd
2326
containerdRemoteURI = `npipe://./pipe/containerd-containerd`
2427
)
2528

29+
// detectRuntime returns containerd runtime config
30+
// Windows node pools support only the containerd runtime for most Kubernetes service providers.
2631
func detectRuntime() (cfgs []runtimeConfig) {
2732
cfgs = append(cfgs, runtimeConfig{
2833
runtimeType: ContainerRuntimeContainerd,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
"testing"
24+
)
25+
26+
func TestDetectRuntimeWindows(t *testing.T) {
27+
cfgs := detectRuntime()
28+
if len(cfgs) != 1 {
29+
t.Fatalf("expected 1 runtime config, got %d", len(cfgs))
30+
}
31+
if cfgs[0].runtimeRemoteURI != "npipe://./pipe/containerd-containerd" {
32+
t.Fatalf("expected runtime remote URI to be npipe://./pipe/containerd-containerd, got %s", cfgs[0].runtimeRemoteURI)
33+
}
34+
}

0 commit comments

Comments
 (0)