Skip to content

Commit

Permalink
Fix unit tests (Mirantis#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwneisen authored Dec 22, 2023
1 parent 52b3182 commit ff375cf
Show file tree
Hide file tree
Showing 37 changed files with 47,401 additions and 29,345 deletions.
17 changes: 12 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ help: ## show make targets
cri-dockerd: ## build cri-dockerd
GOARCH=$(ARCH) go build -trimpath $(CRI_DOCKERD_LDFLAGS) -o $@

### Release
.PHONY: deb
deb: ## build deb packages
$(MAKE) APP_DIR=$(APP_DIR) -C $(PACKAGING_DIR) deb
Expand Down Expand Up @@ -57,6 +58,7 @@ cross-win: ## build static packages
cross-arm: ## build static packages
$(MAKE) APP_DIR=$(APP_DIR) -C $(PACKAGING_DIR) cross-arm

### Development
.PHONY: clean
clean: ## clean the build artifacts
$(RM) cri-dockerd
Expand All @@ -71,11 +73,16 @@ run: cri-dockerd ## Run cri-docker in a running minikube
dev: cri-dockerd ## Run cri-docker in a running minikube
./scripts/replace-in-minikube

.PHONY: docs
docs:
hugo server --source docs/

#### Testing
.PHONY: integration
integration:
integration: ## Run integration tests
sudo critest -runtime-endpoint=unix:///var/run/cri-dockerd.sock -ginkgo.skip="runtime should support apparmor|runtime should support reopening container log|runtime should support execSync with timeout|runtime should support selinux|.*should support propagation.*"

.PHONY: test
test: ## Run unit tests
go test ./...

### Documentation
.PHONY: docs
docs: ## Run docs server
hugo server --source docs/
2 changes: 1 addition & 1 deletion backend/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
runtimeapi_alpha "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi_alpha "k8s.io/cri-api/v1alpha2/pkg/apis/runtime/v1alpha2"
"k8s.io/kubernetes/pkg/kubelet/util"

"github.com/Mirantis/cri-dockerd/core"
Expand Down
27 changes: 25 additions & 2 deletions core/docker_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import (

v1 "k8s.io/api/core/v1"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
runtimeapi_alpha "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
runtimeapi_alpha "k8s.io/cri-api/v1alpha2/pkg/apis/runtime/v1alpha2"
)

const (
Expand Down Expand Up @@ -310,7 +310,6 @@ func (ds *dockerService) Version(
}, nil
}

// Version returns the runtime name, runtime version and runtime API version
func (ds *dockerService) AlphaVersion(
_ context.Context,
r *runtimeapi.VersionRequest,
Expand All @@ -327,6 +326,30 @@ func (ds *dockerService) AlphaVersion(
}, nil
}

func (ds *dockerService) CheckpointContainer(context.Context, *runtimeapi.CheckpointContainerRequest) (*runtimeapi.CheckpointContainerResponse, error) {
return nil, fmt.Errorf("CheckpointContainer is not implemented")
}

func (ds *dockerService) GetContainerEvents(*runtimeapi.GetEventsRequest, runtimeapi.RuntimeService_GetContainerEventsServer) error {
return fmt.Errorf("GetContainerEvents is not implemented")
}

func (ds *dockerService) ListMetricDescriptors(context.Context, *runtimeapi.ListMetricDescriptorsRequest) (*runtimeapi.ListMetricDescriptorsResponse, error) {
return nil, fmt.Errorf("ListMetricDescriptors is not implemented")
}

func (ds *dockerService) ListPodSandboxMetrics(context.Context, *runtimeapi.ListPodSandboxMetricsRequest) (*runtimeapi.ListPodSandboxMetricsResponse, error) {
return nil, fmt.Errorf("ListPodSandboxMetrics is not implemented")
}

func (ds *dockerService) ListPodSandboxStats(context.Context, *runtimeapi.ListPodSandboxStatsRequest) (*runtimeapi.ListPodSandboxStatsResponse, error) {
return nil, fmt.Errorf("ListPodSandboxStats is not implemented")
}

func (ds *dockerService) PodSandboxStats(context.Context, *runtimeapi.PodSandboxStatsRequest) (*runtimeapi.PodSandboxStatsResponse, error) {
return nil, fmt.Errorf("PodSandboxStats is not implemented")
}

// getDockerVersion gets the version information from docker.
func (ds *dockerService) getDockerVersion() (*dockertypes.Version, error) {
res, err := ds.systemInfoCache.Memoize("docker_version", systemInfoCacheMinTTL, func() (interface{}, error) {
Expand Down
17 changes: 9 additions & 8 deletions core/docker_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/stretchr/testify/require"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing"
"k8s.io/utils/clock"
clock "k8s.io/utils/clock/testing"

"github.com/Mirantis/cri-dockerd/libdocker"
"github.com/Mirantis/cri-dockerd/network"
Expand Down Expand Up @@ -97,12 +97,13 @@ func newTestDockerService() (*dockerService, *libdocker.FakeDockerClient, *clock
pm := network.NewPluginManager(&network.NoopNetworkPlugin{})
ckm := newMockCheckpointManager()
return &dockerService{
client: c,
os: &containertest.FakeOS{},
network: pm,
checkpointManager: ckm,
networkReady: make(map[string]bool),
dockerRootDir: "/docker/root/dir",
client: c,
os: &containertest.FakeOS{},
network: pm,
checkpointManager: ckm,
networkReady: make(map[string]bool),
dockerRootDir: "/docker/root/dir",
containerStatsCache: newContainerStatsCache(),
}, c, fakeClock
}

Expand Down Expand Up @@ -140,7 +141,7 @@ func TestStatus(t *testing.T) {
statusResp, err = ds.Status(getTestCTX(), &runtimeapi.StatusRequest{})
assert.NoError(t, err)
assertStatus(map[string]bool{
runtimeapi.RuntimeReady: false,
runtimeapi.RuntimeReady: true,
runtimeapi.NetworkReady: true,
}, statusResp.Status)

Expand Down
67 changes: 42 additions & 25 deletions core/helpers_linux_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

/*
Expand Down Expand Up @@ -25,7 +26,7 @@ import (
"path/filepath"
"testing"

"github.com/Mirantis/cri-dockerd/config"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -34,24 +35,31 @@ import (
func TestGetSeccompSecurityOpts(t *testing.T) {
tests := []struct {
msg string
seccompProfile string
seccompProfile *runtimeapi.SecurityProfile
expectedOpts []string
}{{
msg: "No security annotations",
seccompProfile: "",
seccompProfile: nil,
expectedOpts: []string{"seccomp=unconfined"},
}, {
msg: "Seccomp unconfined",
seccompProfile: "unconfined",
expectedOpts: []string{"seccomp=unconfined"},
msg: "Seccomp unconfined",
seccompProfile: &runtimeapi.SecurityProfile{
ProfileType: runtimeapi.SecurityProfile_Unconfined,
},
expectedOpts: []string{"seccomp=unconfined"},
}, {
msg: "Seccomp default",
seccompProfile: config.SeccompProfileRuntimeDefault,
expectedOpts: nil,
msg: "Seccomp default",
seccompProfile: &runtimeapi.SecurityProfile{
ProfileType: runtimeapi.SecurityProfile_RuntimeDefault,
},
expectedOpts: nil,
}, {
msg: "Seccomp deprecated default",
seccompProfile: config.DeprecatedSeccompProfileDockerDefault,
expectedOpts: nil,
msg: "Seccomp deprecated default",
seccompProfile: &runtimeapi.SecurityProfile{
ProfileType: runtimeapi.SecurityProfile_RuntimeDefault,
LocalhostRef: "docker/default",
},
expectedOpts: nil,
}}

for i, test := range tests {
Expand All @@ -74,24 +82,33 @@ func TestLoadSeccompLocalhostProfiles(t *testing.T) {

tests := []struct {
msg string
seccompProfile string
seccompProfile *runtimeapi.SecurityProfile
expectedOpts []string
expectErr bool
}{{
msg: "Seccomp localhost/test profile should return correct seccomp profiles",
seccompProfile: "localhost/" + filepath.Join(tmpdir, "test"),
expectedOpts: []string{`seccomp={"foo":"bar"}`},
expectErr: false,
msg: "Seccomp localhost/test profile should return correct seccomp profiles",
seccompProfile: &runtimeapi.SecurityProfile{
ProfileType: runtimeapi.SecurityProfile_Localhost,
LocalhostRef: filepath.Join(tmpdir, "test"),
},
expectedOpts: []string{`seccomp={"foo":"bar"}`},
expectErr: false,
}, {
msg: "Non-existent profile should return error",
seccompProfile: "localhost/" + filepath.Join(tmpdir, "fixtures/non-existent"),
expectedOpts: nil,
expectErr: true,
msg: "Non-existent profile should return error",
seccompProfile: &runtimeapi.SecurityProfile{
ProfileType: runtimeapi.SecurityProfile_Localhost,
LocalhostRef: "localhost/" + filepath.Join(tmpdir, "fixtures/non-existent"),
},
expectedOpts: nil,
expectErr: true,
}, {
msg: "Relative profile path should return error",
seccompProfile: "localhost/fixtures/test",
expectedOpts: nil,
expectErr: true,
msg: "Relative profile path should return error",
seccompProfile: &runtimeapi.SecurityProfile{
ProfileType: runtimeapi.SecurityProfile_Localhost,
LocalhostRef: "localhost/fixtures/test",
},
expectedOpts: nil,
expectErr: true,
}}

for i, test := range tests {
Expand Down
13 changes: 12 additions & 1 deletion core/service_alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ package core

import (
"context"
"fmt"

"github.com/Mirantis/cri-dockerd/config"
"github.com/Mirantis/cri-dockerd/utils"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
runtimeapi_alpha "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"k8s.io/cri-api/v1alpha2/pkg/apis/runtime/v1alpha2"
runtimeapi_alpha "k8s.io/cri-api/v1alpha2/pkg/apis/runtime/v1alpha2"
)

func (as *dockerServiceAlpha) RunPodSandbox(ctx context.Context, r *runtimeapi_alpha.RunPodSandboxRequest) (res *runtimeapi_alpha.RunPodSandboxResponse, err error) {
Expand Down Expand Up @@ -601,3 +604,11 @@ func (as *dockerServiceAlpha) Version(
}
return nil, err
}

func (as *dockerServiceAlpha) ListPodSandboxStats(context.Context, *v1alpha2.ListPodSandboxStatsRequest) (*v1alpha2.ListPodSandboxStatsResponse, error) {
return nil, fmt.Errorf("ListPodSandboxStats is not implemented")
}

func (ds *dockerServiceAlpha) PodSandboxStats(context.Context, *v1alpha2.PodSandboxStatsRequest) (*v1alpha2.PodSandboxStatsResponse, error) {
return nil, fmt.Errorf("PodSandboxStats is not implemented")
}
3 changes: 3 additions & 0 deletions core/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func (cs *cstats) getContainerRWSize() uint64 {
func (c *containerStatsCache) getStats(containerID string) *cstats {
c.RLock()
defer c.RUnlock()
if _, exist := c.stats[containerID]; !exist {
return nil
}
return c.stats[containerID]
}

Expand Down
3 changes: 0 additions & 3 deletions core/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ func TestContainerStats(t *testing.T) {
[]libdocker.CalledDetail{
libdocker.NewCalledDetail("list", nil),
libdocker.NewCalledDetail("get_container_stats", nil),
libdocker.NewCalledDetail("inspect_container_withsize", nil),
libdocker.NewCalledDetail("inspect_container", nil),
libdocker.NewCalledDetail("inspect_image", nil),
},
},
"container doesn't exists": {
Expand Down
48 changes: 25 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ require (
google.golang.org/grpc v1.59.0
k8s.io/api v0.27.8
k8s.io/apimachinery v0.27.8
k8s.io/apiserver v0.22.8
k8s.io/apiserver v0.27.8
k8s.io/client-go v0.27.8
k8s.io/component-base v0.27.8
k8s.io/component-helpers v0.26.0
k8s.io/cri-api v0.22.8
k8s.io/component-helpers v0.27.8
k8s.io/cri-api v0.27.8
k8s.io/cri-api/v1alpha2 v0.25.8
k8s.io/kubernetes v1.27.8
k8s.io/utils v0.0.0-20230209194617-a36077c30491
)
Expand Down Expand Up @@ -133,10 +134,10 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/cloud-provider v0.22.8 // indirect
k8s.io/cloud-provider v0.27.8 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/mount-utils v0.22.8 // indirect
k8s.io/mount-utils v0.27.8 // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.1.2 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
Expand All @@ -148,25 +149,26 @@ replace (
k8s.io/api => k8s.io/api v0.27.8
k8s.io/apimachinery => k8s.io/apimachinery v0.27.8
k8s.io/apiserver => k8s.io/apiserver v0.27.8
k8s.io/cli-runtime => k8s.io/cli-runtime v0.22.8
k8s.io/cli-runtime => k8s.io/cli-runtime v0.27.8
k8s.io/client-go => k8s.io/client-go v0.27.8
k8s.io/cloud-provider => k8s.io/cloud-provider v0.22.8
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.22.8
k8s.io/code-generator => k8s.io/code-generator v0.22.8
k8s.io/cloud-provider => k8s.io/cloud-provider v0.27.8
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.27.8
k8s.io/code-generator => k8s.io/code-generator v0.27.8
k8s.io/component-base => k8s.io/component-base v0.27.8
k8s.io/component-helpers => k8s.io/component-helpers v0.26.0
k8s.io/controller-manager => k8s.io/controller-manager v0.22.8
k8s.io/cri-api => k8s.io/cri-api v0.22.8
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.22.8
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.22.8
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.22.8
k8s.io/kube-proxy => k8s.io/kube-proxy v0.22.8
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.22.8
k8s.io/kubectl => k8s.io/kubectl v0.22.8
k8s.io/kubelet => k8s.io/kubelet v0.22.8
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.22.8
k8s.io/metrics => k8s.io/metrics v0.22.8
k8s.io/mount-utils => k8s.io/mount-utils v0.22.8
k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.22.8
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.22.8
k8s.io/controller-manager => k8s.io/controller-manager v0.27.8
k8s.io/cri-api => k8s.io/cri-api v0.27.8
k8s.io/cri-api/v1alpha2 => k8s.io/cri-api v0.25.16
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.27.8
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.27.8
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.27.8
k8s.io/kube-proxy => k8s.io/kube-proxy v0.27.8
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.27.8
k8s.io/kubectl => k8s.io/kubectl v0.27.8
k8s.io/kubelet => k8s.io/kubelet v0.27.8
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.27.8
k8s.io/metrics => k8s.io/metrics v0.27.8
k8s.io/mount-utils => k8s.io/mount-utils v0.27.8
k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.27.8
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.27.8
)
Loading

0 comments on commit ff375cf

Please sign in to comment.