Add unit test for RunPodSandbox
Signed-off-by: DeShuai Ma <dma@redhat.com>
This commit is contained in:
parent
91fa09590a
commit
e027369665
2 changed files with 138 additions and 1 deletions
49
server/fixtures/sandbox_config.json
Normal file
49
server/fixtures/sandbox_config.json
Normal file
|
@ -0,0 +1,49 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "podsandbox1",
|
||||
"uid": "redhat-test-crio",
|
||||
"namespace": "redhat.test.crio",
|
||||
"attempt": 1
|
||||
},
|
||||
"hostname": "crictl_host",
|
||||
"log_directory": "",
|
||||
"dns_config": {
|
||||
"searches": [
|
||||
"8.8.8.8"
|
||||
]
|
||||
},
|
||||
"port_mappings": [],
|
||||
"resources": {
|
||||
"cpu": {
|
||||
"limits": 3,
|
||||
"requests": 2
|
||||
},
|
||||
"memory": {
|
||||
"limits": 50000000,
|
||||
"requests": 2000000
|
||||
}
|
||||
},
|
||||
"labels": {
|
||||
"group": "test"
|
||||
},
|
||||
"annotations": {
|
||||
"owner": "hmeng",
|
||||
"security.alpha.kubernetes.io/seccomp/pod": "unconfined"
|
||||
},
|
||||
"linux": {
|
||||
"cgroup_parent": "/Burstable/pod_123-456",
|
||||
"security_context": {
|
||||
"namespace_options": {
|
||||
"host_network": false,
|
||||
"host_pid": false,
|
||||
"host_ipc": false
|
||||
},
|
||||
"selinux_options": {
|
||||
"user": "system_u",
|
||||
"role": "system_r",
|
||||
"type": "svirt_lxc_net_t",
|
||||
"level": "s0:c4,c5"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,15 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/kubernetes-incubator/cri-o/libkpod"
|
||||
"github.com/kubernetes-incubator/cri-o/pkg/annotations"
|
||||
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
||||
"testing"
|
||||
"os"
|
||||
)
|
||||
|
||||
func TestPrivilegedSandbox(t *testing.T) {
|
||||
|
@ -184,3 +190,85 @@ func TestTrustedSandbox(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
type fakeServer struct {
|
||||
*Server
|
||||
}
|
||||
|
||||
func newFakeServer() (*fakeServer, error) {
|
||||
c := libkpod.DefaultConfig()
|
||||
apiConfig := APIConfig{}
|
||||
s, err := New(&Config{*c, apiConfig})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &fakeServer{s}, nil
|
||||
}
|
||||
|
||||
func (s *fakeServer) cleanPodSandbox(ctx context.Context, podSandboxID string) error {
|
||||
if _, err := s.StopPodSandbox(ctx, &pb.StopPodSandboxRequest{PodSandboxId: podSandboxID}); err != nil {
|
||||
fmt.Println("fakeServer StopPodSandbox get error: %v", err)
|
||||
return err
|
||||
}
|
||||
if _, err := s.RemovePodSandbox(ctx, &pb.RemovePodSandboxRequest{PodSandboxId: podSandboxID}); err != nil {
|
||||
fmt.Println("fakeServer RemovePodSandbox get error: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func openFile(path string) (*os.File, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("config at %s not found", path)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func loadPodSandboxConfig(path string) (*pb.PodSandboxConfig, error) {
|
||||
f, err := openFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var config pb.PodSandboxConfig
|
||||
if err := json.NewDecoder(f).Decode(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func TestRunPodSandbox(t *testing.T) {
|
||||
s, err := newFakeServer()
|
||||
if err != nil {
|
||||
t.Fatalf("Create fakeServer error, %v", err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
|
||||
//when name is empty, should get "PodSandboxConfig.Name should not be empty" error
|
||||
sdconf, _ := loadPodSandboxConfig("fixtures/sandbox_config.json")
|
||||
req := &pb.RunPodSandboxRequest{Config: sdconf}
|
||||
req.Config.Metadata.Name = ""
|
||||
fmt.Println("RunPodSandboxRequest: 0")
|
||||
resp, err := s.RunPodSandbox(ctx, req)
|
||||
if resp != nil && err.Error() != "PodSandboxConfig.Name should not be empty" {
|
||||
t.Fatalf("RunPodSandbox should fail when name is empty")
|
||||
}
|
||||
|
||||
//run with same sandbox_configure twice should also create sandbox success
|
||||
sdconf1, _ := loadPodSandboxConfig("fixtures/sandbox_config.json")
|
||||
req1 := &pb.RunPodSandboxRequest{Config: sdconf1}
|
||||
resp, err = s.RunPodSandbox(ctx, req1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err = s.RunPodSandbox(ctx, req1)
|
||||
if err != nil {
|
||||
t.Fatalf("Run with same sandbox_configure twice %v", err)
|
||||
}
|
||||
s.cleanPodSandbox(ctx, resp.PodSandboxId)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue