Update integration test.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2018-10-10 17:20:44 -07:00
parent c1740d8291
commit 775bc6e5f4
2 changed files with 39 additions and 25 deletions

View File

@ -17,7 +17,6 @@ limitations under the License.
package integration package integration
import ( import (
"encoding/json"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -32,8 +31,6 @@ import (
"golang.org/x/net/context" "golang.org/x/net/context"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2" runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
"github.com/containerd/cri/pkg/server"
) )
func TestSandboxCleanRemove(t *testing.T) { func TestSandboxCleanRemove(t *testing.T) {
@ -77,7 +74,6 @@ func TestSandboxCleanRemove(t *testing.T) {
} }
func TestSandboxRemoveWithoutIPLeakage(t *testing.T) { func TestSandboxRemoveWithoutIPLeakage(t *testing.T) {
ctx := context.Background()
const hostLocalCheckpointDir = "/var/lib/cni" const hostLocalCheckpointDir = "/var/lib/cni"
t.Logf("Make sure host-local ipam is in use") t.Logf("Make sure host-local ipam is in use")
@ -104,22 +100,13 @@ func TestSandboxRemoveWithoutIPLeakage(t *testing.T) {
}() }()
t.Logf("Get pod information") t.Logf("Get pod information")
client, err := RawRuntimeClient() status, info, err := SandboxInfo(sb)
require.NoError(t, err) require.NoError(t, err)
resp, err := client.PodSandboxStatus(ctx, &runtime.PodSandboxStatusRequest{
PodSandboxId: sb,
Verbose: true,
})
require.NoError(t, err)
status := resp.GetStatus()
info := resp.GetInfo()
ip := status.GetNetwork().GetIp() ip := status.GetNetwork().GetIp()
require.NotEmpty(t, ip) require.NotEmpty(t, ip)
var sbInfo server.SandboxInfo require.NotNil(t, info.RuntimeSpec.Linux)
require.NoError(t, json.Unmarshal([]byte(info["info"]), &sbInfo))
require.NotNil(t, sbInfo.RuntimeSpec.Linux)
var netNS string var netNS string
for _, n := range sbInfo.RuntimeSpec.Linux.Namespaces { for _, n := range info.RuntimeSpec.Linux.Namespaces {
if n.Type == runtimespec.NetworkNamespace { if n.Type == runtimespec.NetworkNamespace {
netNS = n.Path netNS = n.Path
} }
@ -140,14 +127,23 @@ func TestSandboxRemoveWithoutIPLeakage(t *testing.T) {
require.True(t, checkIP(ip)) require.True(t, checkIP(ip))
t.Logf("Kill sandbox container") t.Logf("Kill sandbox container")
require.NoError(t, KillPid(int(sbInfo.Pid))) require.NoError(t, KillPid(int(info.Pid)))
t.Logf("Unmount network namespace") t.Logf("Unmount network namespace")
// The umount will take effect after containerd is stopped.
require.NoError(t, unix.Unmount(netNS, unix.MNT_DETACH)) require.NoError(t, unix.Unmount(netNS, unix.MNT_DETACH))
t.Logf("Restart containerd") t.Logf("Network namespace should be closed")
RestartContainerd(t) _, info, err = SandboxInfo(sb)
require.NoError(t, err)
assert.True(t, info.NetNSClosed)
t.Logf("Remove network namespace")
require.NoError(t, os.RemoveAll(netNS))
t.Logf("Network namespace should still be closed")
_, info, err = SandboxInfo(sb)
require.NoError(t, err)
assert.True(t, info.NetNSClosed)
t.Logf("Sandbox state should be NOTREADY") t.Logf("Sandbox state should be NOTREADY")
assert.NoError(t, Eventually(func() (bool, error) { assert.NoError(t, Eventually(func() (bool, error) {
@ -158,14 +154,10 @@ func TestSandboxRemoveWithoutIPLeakage(t *testing.T) {
return status.GetState() == runtime.PodSandboxState_SANDBOX_NOTREADY, nil return status.GetState() == runtime.PodSandboxState_SANDBOX_NOTREADY, nil
}, time.Second, 30*time.Second), "sandbox state should become NOTREADY") }, time.Second, 30*time.Second), "sandbox state should become NOTREADY")
t.Logf("Network namespace should have been removed")
_, err = os.Stat(netNS)
assert.True(t, os.IsNotExist(err))
t.Logf("Should still be able to find the pod ip in host-local checkpoint") t.Logf("Should still be able to find the pod ip in host-local checkpoint")
assert.True(t, checkIP(ip)) assert.True(t, checkIP(ip))
t.Logf("Should be able to remove the sandbox after properly stopped") t.Logf("Should be able to stop and remove the sandbox")
assert.NoError(t, runtimeService.StopPodSandbox(sb)) assert.NoError(t, runtimeService.StopPodSandbox(sb))
assert.NoError(t, runtimeService.RemovePodSandbox(sb)) assert.NoError(t, runtimeService.RemovePodSandbox(sb))

View File

@ -42,6 +42,7 @@ import (
"github.com/containerd/cri/pkg/client" "github.com/containerd/cri/pkg/client"
criconfig "github.com/containerd/cri/pkg/config" criconfig "github.com/containerd/cri/pkg/config"
"github.com/containerd/cri/pkg/constants" "github.com/containerd/cri/pkg/constants"
"github.com/containerd/cri/pkg/server"
"github.com/containerd/cri/pkg/util" "github.com/containerd/cri/pkg/util"
) )
@ -352,6 +353,27 @@ func CRIConfig() (*criconfig.Config, error) {
return config, nil return config, nil
} }
// SandboxInfo gets sandbox info.
func SandboxInfo(id string) (*runtime.PodSandboxStatus, *server.SandboxInfo, error) {
client, err := RawRuntimeClient()
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get raw runtime client")
}
resp, err := client.PodSandboxStatus(context.Background(), &runtime.PodSandboxStatusRequest{
PodSandboxId: id,
Verbose: true,
})
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get sandbox status")
}
status := resp.GetStatus()
var info server.SandboxInfo
if err := json.Unmarshal([]byte(resp.GetInfo()["info"]), &info); err != nil {
return nil, nil, errors.Wrap(err, "failed to unmarshal sandbox info")
}
return status, &info, nil
}
func RestartContainerd(t *testing.T) { func RestartContainerd(t *testing.T) {
require.NoError(t, KillProcess("containerd")) require.NoError(t, KillProcess("containerd"))