2017-09-12 10:17:44 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-02-12 20:13:07 +00:00
|
|
|
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
|
2017-09-12 10:17:44 +00:00
|
|
|
|
|
|
|
"github.com/containernetworking/plugins/pkg/ns"
|
2017-11-30 15:46:11 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/lib"
|
|
|
|
"github.com/kubernetes-incubator/cri-o/lib/sandbox"
|
2017-09-12 10:17:44 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetInfo(t *testing.T) {
|
2017-11-30 15:46:11 +00:00
|
|
|
c := lib.DefaultConfig()
|
2017-09-12 10:17:44 +00:00
|
|
|
c.RootConfig.Storage = "afoobarstorage"
|
|
|
|
c.RootConfig.Root = "afoobarroot"
|
|
|
|
c.RuntimeConfig.CgroupManager = "systemd"
|
|
|
|
apiConfig := APIConfig{}
|
|
|
|
s := &Server{
|
|
|
|
config: Config{*c, apiConfig},
|
|
|
|
}
|
|
|
|
ci := s.getInfo()
|
|
|
|
if ci.CgroupDriver != "systemd" {
|
|
|
|
t.Fatalf("expected 'systemd', got %q", ci.CgroupDriver)
|
|
|
|
}
|
|
|
|
if ci.StorageDriver != "afoobarstorage" {
|
|
|
|
t.Fatalf("expected 'afoobarstorage', got %q", ci.StorageDriver)
|
|
|
|
}
|
|
|
|
if ci.StorageRoot != "afoobarroot" {
|
|
|
|
t.Fatalf("expected 'afoobarroot', got %q", ci.StorageRoot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockNetNS struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns mockNetNS) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (ns mockNetNS) Fd() uintptr {
|
|
|
|
ptr := new(uintptr)
|
|
|
|
return *ptr
|
|
|
|
}
|
|
|
|
func (ns mockNetNS) Do(toRun func(ns.NetNS) error) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (ns mockNetNS) Set() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (ns mockNetNS) Path() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContainerInfo(t *testing.T) {
|
|
|
|
s := &Server{}
|
|
|
|
created := time.Now()
|
|
|
|
labels := map[string]string{
|
|
|
|
"io.kubernetes.container.name": "POD",
|
|
|
|
"io.kubernetes.test2": "value2",
|
|
|
|
"io.kubernetes.test3": "value3",
|
|
|
|
}
|
|
|
|
annotations := map[string]string{
|
|
|
|
"io.kubernetes.test": "value",
|
|
|
|
"io.kubernetes.test1": "value1",
|
|
|
|
}
|
|
|
|
getContainerFunc := func(id string) *oci.Container {
|
Return image references from the storage package
The image's canonical reference is a name with a digest of the image's
manifest, so in imageService.ImageStatus() and
imageService.ListImages(), divide the image's name list into tagged and
digested values, and if we have names, add canonical versions.
In Server.ContainerStatus(), return the image name as it was given to us
as the image, and the image digested reference as the image reference.
In Server.ListImages(), be sure to only return tagged names in the
RepoTags field. In Server.ImageStatus(), also return canonical
references in the RepoDigests field.
In Server.PullImage(), be sure that we consistently return the same
image reference for an image, whether we ended up pulling it or not.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2017-07-12 16:41:38 +00:00
|
|
|
container, err := oci.NewContainer("testid", "testname", "", "/container/logs", mockNetNS{}, labels, annotations, annotations, "image", "imageName", "imageRef", &runtime.ContainerMetadata{}, "testsandboxid", false, false, false, false, false, "/root/for/container", created, "SIGKILL")
|
2017-09-12 10:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container.SetMountPoint("/var/foo/container")
|
|
|
|
cstate := &oci.ContainerState{}
|
|
|
|
cstate.State = specs.State{
|
|
|
|
Pid: 42,
|
|
|
|
}
|
|
|
|
cstate.Created = created
|
|
|
|
container.SetState(cstate)
|
|
|
|
return container
|
|
|
|
}
|
|
|
|
getInfraContainerFunc := func(id string) *oci.Container {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
getSandboxFunc := func(id string) *sandbox.Sandbox {
|
|
|
|
s := &sandbox.Sandbox{}
|
|
|
|
s.AddIP("1.1.1.42")
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
ci, err := s.getContainerInfo("", getContainerFunc, getInfraContainerFunc, getSandboxFunc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if ci.CreatedTime != created.UnixNano() {
|
|
|
|
t.Fatalf("expected same created time %d, got %d", created.UnixNano(), ci.CreatedTime)
|
|
|
|
}
|
|
|
|
if ci.Pid != 42 {
|
2017-10-10 23:23:54 +00:00
|
|
|
t.Fatalf("expected pid 42, got %v", ci.Pid)
|
2017-09-12 10:17:44 +00:00
|
|
|
}
|
|
|
|
if ci.Name != "testname" {
|
|
|
|
t.Fatalf("expected name testname, got %s", ci.Name)
|
|
|
|
}
|
Return image references from the storage package
The image's canonical reference is a name with a digest of the image's
manifest, so in imageService.ImageStatus() and
imageService.ListImages(), divide the image's name list into tagged and
digested values, and if we have names, add canonical versions.
In Server.ContainerStatus(), return the image name as it was given to us
as the image, and the image digested reference as the image reference.
In Server.ListImages(), be sure to only return tagged names in the
RepoTags field. In Server.ImageStatus(), also return canonical
references in the RepoDigests field.
In Server.PullImage(), be sure that we consistently return the same
image reference for an image, whether we ended up pulling it or not.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2017-07-12 16:41:38 +00:00
|
|
|
if ci.Image != "image" {
|
|
|
|
t.Fatalf("expected image name image, got %s", ci.Image)
|
|
|
|
}
|
|
|
|
if ci.ImageRef != "imageRef" {
|
|
|
|
t.Fatalf("expected image ref imageRef, got %s", ci.ImageRef)
|
2017-09-12 10:17:44 +00:00
|
|
|
}
|
|
|
|
if ci.Root != "/var/foo/container" {
|
|
|
|
t.Fatalf("expected root to be /var/foo/container, got %s", ci.Root)
|
|
|
|
}
|
|
|
|
if ci.LogPath != "/container/logs" {
|
|
|
|
t.Fatalf("expected log path to be /containers/logs, got %s", ci.LogPath)
|
|
|
|
}
|
|
|
|
if ci.Sandbox != "testsandboxid" {
|
|
|
|
t.Fatalf("expected sandbox to be testsandboxid, got %s", ci.Sandbox)
|
|
|
|
}
|
|
|
|
if ci.IP != "1.1.1.42" {
|
2017-10-11 21:49:35 +00:00
|
|
|
t.Fatalf("expected ip 1.1.1.42, got %s", ci.IP)
|
2017-09-12 10:17:44 +00:00
|
|
|
}
|
|
|
|
if len(ci.Annotations) == 0 {
|
|
|
|
t.Fatal("annotations are empty")
|
|
|
|
}
|
|
|
|
if len(ci.Labels) == 0 {
|
|
|
|
t.Fatal("labels are empty")
|
|
|
|
}
|
|
|
|
if len(ci.Annotations) != len(annotations) {
|
|
|
|
t.Fatalf("container info annotations len (%d) isn't the same as original annotations len (%d)", len(ci.Annotations), len(annotations))
|
|
|
|
}
|
|
|
|
if len(ci.Labels) != len(labels) {
|
|
|
|
t.Fatalf("container info labels len (%d) isn't the same as original labels len (%d)", len(ci.Labels), len(labels))
|
|
|
|
}
|
|
|
|
var found bool
|
|
|
|
for k, v := range annotations {
|
|
|
|
found = false
|
|
|
|
for key, value := range ci.Annotations {
|
|
|
|
if k == key && v == value {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("key %s with value %v wasn't in container info annotations", k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range labels {
|
|
|
|
found = false
|
|
|
|
for key, value := range ci.Labels {
|
|
|
|
if k == key && v == value {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatalf("key %s with value %v wasn't in container info labels", k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContainerInfoCtrNotFound(t *testing.T) {
|
|
|
|
s := &Server{}
|
|
|
|
getContainerFunc := func(id string) *oci.Container {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
getInfraContainerFunc := func(id string) *oci.Container {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
getSandboxFunc := func(id string) *sandbox.Sandbox {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err := s.getContainerInfo("", getContainerFunc, getInfraContainerFunc, getSandboxFunc)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error but got nothing")
|
|
|
|
}
|
|
|
|
if err != errCtrNotFound {
|
|
|
|
t.Fatalf("expected errCtrNotFound error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
2017-09-12 14:06:20 +00:00
|
|
|
|
2017-09-12 10:17:44 +00:00
|
|
|
func TestGetContainerInfoCtrStateNil(t *testing.T) {
|
|
|
|
s := &Server{}
|
|
|
|
created := time.Now()
|
|
|
|
labels := map[string]string{}
|
|
|
|
annotations := map[string]string{}
|
|
|
|
getContainerFunc := func(id string) *oci.Container {
|
2017-09-22 23:44:02 +00:00
|
|
|
container, err := oci.NewContainer("testid", "testname", "", "/container/logs", mockNetNS{}, labels, annotations, annotations, "imageName", "imageName", "imageRef", &runtime.ContainerMetadata{}, "testsandboxid", false, false, false, false, false, "/root/for/container", created, "SIGKILL")
|
2017-09-12 10:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container.SetMountPoint("/var/foo/container")
|
|
|
|
container.SetState(nil)
|
|
|
|
return container
|
|
|
|
}
|
|
|
|
getInfraContainerFunc := func(id string) *oci.Container {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
getSandboxFunc := func(id string) *sandbox.Sandbox {
|
|
|
|
s := &sandbox.Sandbox{}
|
|
|
|
s.AddIP("1.1.1.42")
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
_, err := s.getContainerInfo("", getContainerFunc, getInfraContainerFunc, getSandboxFunc)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error but got nothing")
|
|
|
|
}
|
|
|
|
if err != errCtrStateNil {
|
|
|
|
t.Fatalf("expected errCtrStateNil error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContainerInfoSandboxNotFound(t *testing.T) {
|
|
|
|
s := &Server{}
|
|
|
|
created := time.Now()
|
|
|
|
labels := map[string]string{}
|
|
|
|
annotations := map[string]string{}
|
|
|
|
getContainerFunc := func(id string) *oci.Container {
|
2017-09-22 23:44:02 +00:00
|
|
|
container, err := oci.NewContainer("testid", "testname", "", "/container/logs", mockNetNS{}, labels, annotations, annotations, "imageName", "imageName", "imageRef", &runtime.ContainerMetadata{}, "testsandboxid", false, false, false, false, false, "/root/for/container", created, "SIGKILL")
|
2017-09-12 10:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
container.SetMountPoint("/var/foo/container")
|
|
|
|
return container
|
|
|
|
}
|
|
|
|
getInfraContainerFunc := func(id string) *oci.Container {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
getSandboxFunc := func(id string) *sandbox.Sandbox {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err := s.getContainerInfo("", getContainerFunc, getInfraContainerFunc, getSandboxFunc)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error but got nothing")
|
|
|
|
}
|
|
|
|
if err != errSandboxNotFound {
|
|
|
|
t.Fatalf("expected errSandboxNotFound error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|