server: add inspect unit test
The inspect endpoint is used mainly in the CRI-O cAdvisor handler. Let's make sure we don't break it by adding some trivial unit tests. Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
parent
400713a58b
commit
e26e48ec87
6 changed files with 332 additions and 54 deletions
|
@ -2,10 +2,14 @@ package server
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-zoo/bone"
|
||||
"github.com/kubernetes-incubator/cri-o/libkpod/sandbox"
|
||||
"github.com/kubernetes-incubator/cri-o/oci"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ContainerInfo stores information about containers
|
||||
|
@ -29,16 +33,59 @@ type CrioInfo struct {
|
|||
CgroupDriver string `json:"cgroup_driver"`
|
||||
}
|
||||
|
||||
func (s *Server) getInfo() CrioInfo {
|
||||
return CrioInfo{
|
||||
StorageDriver: s.config.Config.Storage,
|
||||
StorageRoot: s.config.Config.Root,
|
||||
CgroupDriver: s.config.Config.CgroupManager,
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
errCtrNotFound = errors.New("container not found")
|
||||
errCtrStateNil = errors.New("container state is nil")
|
||||
errSandboxNotFound = errors.New("sandbox for container not found")
|
||||
)
|
||||
|
||||
func (s *Server) getContainerInfo(id string, getContainerFunc func(id string) *oci.Container, getInfraContainerFunc func(id string) *oci.Container, getSandboxFunc func(id string) *sandbox.Sandbox) (ContainerInfo, error) {
|
||||
ctr := getContainerFunc(id)
|
||||
if ctr == nil {
|
||||
ctr = getInfraContainerFunc(id)
|
||||
if ctr == nil {
|
||||
return ContainerInfo{}, errCtrNotFound
|
||||
}
|
||||
}
|
||||
// TODO(mrunalp): should we call UpdateStatus()?
|
||||
ctrState := ctr.State()
|
||||
if ctrState == nil {
|
||||
return ContainerInfo{}, errCtrStateNil
|
||||
}
|
||||
sb := getSandboxFunc(ctr.Sandbox())
|
||||
if sb == nil {
|
||||
logrus.Debugf("can't find sandbox %s for container %s", ctr.Sandbox(), id)
|
||||
return ContainerInfo{}, errSandboxNotFound
|
||||
}
|
||||
return ContainerInfo{
|
||||
Name: ctr.Name(),
|
||||
Pid: ctrState.Pid,
|
||||
Image: ctr.Image(),
|
||||
CreatedTime: ctrState.Created.UnixNano(),
|
||||
Labels: ctr.Labels(),
|
||||
Annotations: ctr.Annotations(),
|
||||
Root: ctr.MountPoint(),
|
||||
LogPath: ctr.LogPath(),
|
||||
Sandbox: ctr.Sandbox(),
|
||||
IP: sb.IP(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
// GetInfoMux returns the mux used to serve info requests
|
||||
func (s *Server) GetInfoMux() *bone.Mux {
|
||||
mux := bone.New()
|
||||
|
||||
mux.Get("/info", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
ci := CrioInfo{
|
||||
StorageDriver: s.config.Config.Storage,
|
||||
StorageRoot: s.config.Config.Root,
|
||||
CgroupDriver: s.config.Config.CgroupManager,
|
||||
}
|
||||
ci := s.getInfo()
|
||||
js, err := json.Marshal(ci)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
@ -50,36 +97,20 @@ func (s *Server) GetInfoMux() *bone.Mux {
|
|||
|
||||
mux.Get("/containers/:id", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
containerID := bone.GetValue(req, "id")
|
||||
ctr := s.GetContainer(containerID)
|
||||
if ctr == nil {
|
||||
ctr = s.getInfraContainer(containerID)
|
||||
if ctr == nil {
|
||||
http.Error(w, fmt.Sprintf("container with id: %s not found", containerID), http.StatusNotFound)
|
||||
return
|
||||
ci, err := s.getContainerInfo(containerID, s.GetContainer, s.getInfraContainer, s.getSandbox)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case errCtrNotFound:
|
||||
http.Error(w, fmt.Sprintf("can't find the container with id %s", containerID), http.StatusNotFound)
|
||||
case errCtrStateNil:
|
||||
http.Error(w, fmt.Sprintf("can't find container state for container with id %s", containerID), http.StatusInternalServerError)
|
||||
case errSandboxNotFound:
|
||||
http.Error(w, fmt.Sprintf("can't find the sandbox for container id %s", containerID), http.StatusNotFound)
|
||||
default:
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
ctrState := ctr.State()
|
||||
if ctrState == nil {
|
||||
http.Error(w, fmt.Sprintf("container %s state is nil", containerID), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
sb := s.getSandbox(ctr.Sandbox())
|
||||
if sb == nil {
|
||||
http.Error(w, fmt.Sprintf("can't find the sandbox for container id, sandbox id %s: %s", containerID, ctr.Sandbox()), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
ci := ContainerInfo{
|
||||
Name: ctr.Name(),
|
||||
Pid: ctrState.Pid,
|
||||
Image: ctr.Image(),
|
||||
CreatedTime: ctrState.Created.UnixNano(),
|
||||
Labels: ctr.Labels(),
|
||||
Annotations: ctr.Annotations(),
|
||||
Root: ctr.MountPoint(),
|
||||
LogPath: ctr.LogPath(),
|
||||
Sandbox: ctr.Sandbox(),
|
||||
IP: sb.IP(),
|
||||
}
|
||||
js, err := json.Marshal(ci)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue