From e0273696651e77917ef3f097cde29f1a8164cfd9 Mon Sep 17 00:00:00 2001 From: DeShuai Ma Date: Sat, 18 Nov 2017 21:31:45 +0800 Subject: [PATCH] Add unit test for RunPodSandbox Signed-off-by: DeShuai Ma --- server/fixtures/sandbox_config.json | 49 ++++++++++++++++ server/sandbox_run_test.go | 90 ++++++++++++++++++++++++++++- 2 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 server/fixtures/sandbox_config.json diff --git a/server/fixtures/sandbox_config.json b/server/fixtures/sandbox_config.json new file mode 100644 index 00000000..c424f748 --- /dev/null +++ b/server/fixtures/sandbox_config.json @@ -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" + } + } + } +} diff --git a/server/sandbox_run_test.go b/server/sandbox_run_test.go index 6da36e2d..018d316d 100644 --- a/server/sandbox_run_test.go +++ b/server/sandbox_run_test.go @@ -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) +}