Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Peng <ppbits3@gmail.com>
  • Loading branch information
ppbits committed Feb 27, 2025
1 parent ebb8577 commit f207c42
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pkg/daemon/criruntime/factory_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
)

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

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

// containerd, with the same behavior of pullImage as commonCRI
{
if _, err = os.Stat(fmt.Sprintf("%s/containerd.sock", varRunMountPath)); err == nil {
if _, err = statFunc(fmt.Sprintf("%s/containerd.sock", varRunMountPath)); err == nil {
cfgs = append(cfgs, runtimeConfig{
runtimeType: ContainerRuntimeContainerd,
runtimeRemoteURI: fmt.Sprintf("unix://%s/containerd.sock", varRunMountPath),
})
}
if _, err = os.Stat(fmt.Sprintf("%s/containerd/containerd.sock", varRunMountPath)); err == nil {
if _, err = statFunc(fmt.Sprintf("%s/containerd/containerd.sock", varRunMountPath)); err == nil {
cfgs = append(cfgs, runtimeConfig{
runtimeType: ContainerRuntimeContainerd,
runtimeRemoteURI: fmt.Sprintf("unix://%s/containerd/containerd.sock", varRunMountPath),
Expand All @@ -73,13 +74,13 @@ func detectRuntime() (cfgs []runtimeConfig) {

// cri-o
{
if _, err = os.Stat(fmt.Sprintf("%s/crio.sock", varRunMountPath)); err == nil {
if _, err = statFunc(fmt.Sprintf("%s/crio.sock", varRunMountPath)); err == nil {
cfgs = append(cfgs, runtimeConfig{
runtimeType: ContainerRuntimeCommonCRI,
runtimeRemoteURI: fmt.Sprintf("unix://%s/crio.sock", varRunMountPath),
})
}
if _, err = os.Stat(fmt.Sprintf("%s/crio/crio.sock", varRunMountPath)); err == nil {
if _, err = statFunc(fmt.Sprintf("%s/crio/crio.sock", varRunMountPath)); err == nil {
cfgs = append(cfgs, runtimeConfig{
runtimeType: ContainerRuntimeCommonCRI,
runtimeRemoteURI: fmt.Sprintf("unix://%s/crio/crio.sock", varRunMountPath),
Expand All @@ -89,15 +90,15 @@ func detectRuntime() (cfgs []runtimeConfig) {

// cri-docker dockerd as a compliant Container Runtime Interface, detail see https://github.com/Mirantis/cri-dockerd
{
if _, err = os.Stat(fmt.Sprintf("%s/cri-dockerd.sock", varRunMountPath)); err == nil {
if _, err = statFunc(fmt.Sprintf("%s/cri-dockerd.sock", varRunMountPath)); err == nil {
cfgs = append(cfgs, runtimeConfig{
runtimeType: ContainerRuntimeCommonCRI,
runtimeRemoteURI: fmt.Sprintf("unix://%s/cri-dockerd.sock", varRunMountPath),
})
}
// Check if the cri-dockerd runtime socket exists in the expected k3s runtime directory.
// If found, append it to the runtime configuration list to ensure k3s can use cri-dockerd.
if _, err = os.Stat(fmt.Sprintf("%s/cri-dockerd/cri-dockerd.sock", varRunMountPath)); err == nil {
if _, err = statFunc(fmt.Sprintf("%s/cri-dockerd/cri-dockerd.sock", varRunMountPath)); err == nil {
cfgs = append(cfgs, runtimeConfig{
runtimeType: ContainerRuntimeCommonCRI,
runtimeRemoteURI: fmt.Sprintf("unix://%s/cri-dockerd/cri-dockerd.sock", varRunMountPath),
Expand Down
180 changes: 180 additions & 0 deletions pkg/daemon/criruntime/factory_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
//go:build !windows
// +build !windows

/*
Copyright 2021 The Kruise Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package criruntime

import (
"flag"
"os"
"testing"
"time"
)

type fileInfo struct {
name string
}

func (f *fileInfo) Name() string { return f.name }
func (f *fileInfo) Size() int64 { return 0 }
func (f *fileInfo) Mode() os.FileMode { return 0644 }
func (f *fileInfo) ModTime() time.Time { return time.Time{} }
func (f *fileInfo) IsDir() bool { return false }
func (f *fileInfo) Sys() interface{} { return nil }

func TestDetectRuntimeUnix(t *testing.T) {
testCases := []struct {
name string
flag string
runtimeType ContainerRuntimeType
runtimeRemoteURI string
statFunc func(name string) (os.FileInfo, error)
expectedCfgsCount int
}{
{
name: "non-existent-socket-with-flag",
flag: "non-existent-socket",
runtimeType: ContainerRuntimeCommonCRI,
runtimeRemoteURI: "unix://hostvarrun/non-existent-socket",
statFunc: func(name string) (os.FileInfo, error) {
return nil, os.ErrNotExist
},
expectedCfgsCount: 0,
},
{
name: "crio.sock-with-flag",
flag: "crio.sock",
runtimeType: ContainerRuntimeCommonCRI,
runtimeRemoteURI: "unix://hostvarrun/crio.sock",
statFunc: func(name string) (os.FileInfo, error) {
if name == "hostvarrun/crio.sock" {
return &fileInfo{name: name}, nil
}
return nil, os.ErrNotExist
},
expectedCfgsCount: 1,
},
{
name: "containerd.sock",
flag: "",
runtimeType: ContainerRuntimeContainerd,
runtimeRemoteURI: "unix://hostvarrun/containerd.sock",
statFunc: func(name string) (os.FileInfo, error) {
if name == "hostvarrun/containerd.sock" {
return &fileInfo{name: name}, nil
}
return nil, os.ErrNotExist
},
expectedCfgsCount: 1,
},
{
name: "containerd/containerd.sock",
flag: "",
runtimeType: ContainerRuntimeContainerd,
runtimeRemoteURI: "unix://hostvarrun/containerd/containerd.sock",
statFunc: func(name string) (os.FileInfo, error) {
if name == "hostvarrun/containerd/containerd.sock" {
return &fileInfo{name: name}, nil
}
return nil, os.ErrNotExist
},
expectedCfgsCount: 1,
},
{
name: "crio.sock",
flag: "",
runtimeType: ContainerRuntimeCommonCRI,
runtimeRemoteURI: "unix://hostvarrun/crio.sock",
statFunc: func(name string) (os.FileInfo, error) {
if name == "hostvarrun/crio.sock" {
return &fileInfo{name: name}, nil
}
return nil, os.ErrNotExist
},
expectedCfgsCount: 1,
},
{
name: "crio/crio.sock",
flag: "",
runtimeType: ContainerRuntimeCommonCRI,
runtimeRemoteURI: "unix://hostvarrun/crio/crio.sock",
statFunc: func(name string) (os.FileInfo, error) {
if name == "hostvarrun/crio/crio.sock" {
return &fileInfo{name: name}, nil
}
return nil, os.ErrNotExist
},
expectedCfgsCount: 1,
},
{
name: "cri-dockerd",
flag: "",
runtimeType: ContainerRuntimeCommonCRI,
runtimeRemoteURI: "unix://hostvarrun/cri-dockerd.sock",
statFunc: func(name string) (os.FileInfo, error) {
if name == "hostvarrun/cri-dockerd.sock" {
return &fileInfo{name: name}, nil
}
return nil, os.ErrNotExist
},
expectedCfgsCount: 1,
},
{
name: "cri-dockerd/cri-dockerd.sock",
flag: "",
runtimeType: ContainerRuntimeCommonCRI,
runtimeRemoteURI: "unix://hostvarrun/cri-dockerd/cri-dockerd.sock",
statFunc: func(name string) (os.FileInfo, error) {
if name == "hostvarrun/cri-dockerd/cri-dockerd.sock" {
return &fileInfo{name: name}, nil
}
return nil, os.ErrNotExist
},
expectedCfgsCount: 1,
},
{
name: "non-existent-socket-without-flag",
flag: "",
runtimeType: ContainerRuntimeCommonCRI,
runtimeRemoteURI: "unix://hostvarrun/non-existent-socket",
statFunc: func(name string) (os.FileInfo, error) {
return nil, os.ErrNotExist
},
expectedCfgsCount: 0,
},
}

for _, testCase := range testCases {
flag.Set("socket-file", testCase.flag)
statFunc = testCase.statFunc
defer func() { statFunc = os.Stat }()

cfgs := detectRuntime()
if len(cfgs) != testCase.expectedCfgsCount {
t.Fatalf("expected %d runtime config, got %d", testCase.expectedCfgsCount, len(cfgs))
}
if testCase.expectedCfgsCount > 0 {
if cfgs[0].runtimeRemoteURI != testCase.runtimeRemoteURI {
t.Fatalf("expected runtime remote URI to be %s, got %s", testCase.runtimeRemoteURI, cfgs[0].runtimeRemoteURI)
}
if cfgs[0].runtimeType != testCase.runtimeType {
t.Fatalf("expected runtime type to be %s, got %s", testCase.runtimeType, cfgs[0].runtimeType)
}
}
}
}
5 changes: 5 additions & 0 deletions pkg/daemon/criruntime/factory_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ limitations under the License.
package criruntime

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

// detectRuntime returns containerd runtime config
// Windows node pools support only the containerd runtime for most Kubernetes service providers.
func detectRuntime() (cfgs []runtimeConfig) {
cfgs = append(cfgs, runtimeConfig{
runtimeType: ContainerRuntimeContainerd,
Expand Down
34 changes: 34 additions & 0 deletions pkg/daemon/criruntime/factory_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build windows
// +build windows

/*
Copyright 2021 The Kruise Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package criruntime

import (
"testing"
)

func TestDetectRuntimeWindows(t *testing.T) {
cfgs := detectRuntime()
if len(cfgs) != 1 {
t.Fatalf("expected 1 runtime config, got %d", len(cfgs))
}
if cfgs[0].runtimeRemoteURI != "npipe://./pipe/containerd-containerd" {
t.Fatalf("expected runtime remote URI to be npipe://./pipe/containerd-containerd, got %s", cfgs[0].runtimeRemoteURI)
}
}

0 comments on commit f207c42

Please sign in to comment.