diff --git a/hack/test-integration.sh b/hack/test-integration.sh index 565cd0e77..0f55c8ebf 100755 --- a/hack/test-integration.sh +++ b/hack/test-integration.sh @@ -27,7 +27,7 @@ REPORT_DIR=${REPORT_DIR:-"/tmp/test-integration"} # RUNTIME is the runtime handler to use in the test. RUNTIME=${RUNTIME:-""} -CRI_ROOT="/var/lib/containerd/io.containerd.grpc.v1.cri" +CRI_ROOT="${CONTAINERD_ROOT}/io.containerd.grpc.v1.cri" mkdir -p ${REPORT_DIR} test_setup ${REPORT_DIR} diff --git a/hack/test-utils.sh b/hack/test-utils.sh index e6e806b25..6ba52584c 100755 --- a/hack/test-utils.sh +++ b/hack/test-utils.sh @@ -23,13 +23,23 @@ CONTAINERD_FLAGS="--log-level=debug " # Use a configuration file for containerd. CONTAINERD_CONFIG_FILE=${CONTAINERD_CONFIG_FILE:-""} +# CONTAINERD_TEST_SUFFIX is the suffix appended to the root/state directory used +# by test containerd. +CONTAINERD_TEST_SUFFIX=${CONTAINERD_TEST_SUFFIX:-"-test"} +# The containerd root directory. +CONTAINERD_ROOT=${CONTAINERD_ROOT:-"/var/lib/containerd${CONTAINERD_TEST_SUFFIX}"} +# The containerd state directory. +CONTAINERD_STATE=${CONTAINERD_STATE:-"/run/containerd${CONTAINERD_TEST_SUFFIX}"} +# The containerd socket address. +CONTAINERD_SOCK=${CONTAINERD_SOCK:-unix://${CONTAINERD_STATE}/containerd.sock} if [ -f "${CONTAINERD_CONFIG_FILE}" ]; then CONTAINERD_FLAGS+="--config ${CONTAINERD_CONFIG_FILE} " fi +CONTAINERD_FLAGS+="--address ${CONTAINERD_SOCK#"unix://"} \ + --state ${CONTAINERD_STATE} \ + --root ${CONTAINERD_ROOT}" -CONTAINERD_SOCK=unix:///run/containerd/containerd.sock - -containerd_pid= +containerd_groupid= # test_setup starts containerd. test_setup() { @@ -39,10 +49,14 @@ test_setup() { echo "containerd is not built" exit 1 fi - sudo pkill -x containerd + set -m + # Create containerd in a different process group + # so that we can easily clean them up. keepalive "sudo PATH=${PATH} ${ROOT}/_output/containerd ${CONTAINERD_FLAGS}" \ ${RESTART_WAIT_PERIOD} &> ${report_dir}/containerd.log & - containerd_pid=$! + pid=$! + set +m + containerd_groupid=$(ps -o pgid= -p ${pid}) # Wait for containerd to be running by using the containerd client ctr to check the version # of the containerd server. Wait an increasing amount of time after each of five attempts local -r ctr_path=$(which ctr) @@ -61,10 +75,9 @@ test_setup() { # test_teardown kills containerd. test_teardown() { - if [ -n "${containerd_pid}" ]; then - kill ${containerd_pid} + if [ -n "${containerd_groupid}" ]; then + sudo pkill -g ${containerd_groupid} fi - sudo pkill -x containerd } # keepalive runs a command and keeps it alive. diff --git a/integration/image_load_test.go b/integration/image_load_test.go index aaf855dbf..c13e12792 100644 --- a/integration/image_load_test.go +++ b/integration/image_load_test.go @@ -17,17 +17,15 @@ limitations under the License. package integration import ( - "golang.org/x/net/context" "io/ioutil" "os" "os/exec" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2" - - api "github.com/containerd/cri/pkg/api/v1" ) // Test to load an image from tarball. @@ -58,14 +56,22 @@ func TestImageLoad(t *testing.T) { } t.Logf("load image in cri") - res, err := criPluginClient.LoadImage(context.Background(), &api.LoadImageRequest{FilePath: tar}) - require.NoError(t, err) - require.Equal(t, []string{loadedImage}, res.GetImages()) + ctr, err := exec.LookPath("ctr") + require.NoError(t, err, "ctr should be installed, make sure you've run `make install.deps`") + output, err = exec.Command(ctr, "-address="+containerdEndpoint, + "-n=k8s.io", "images", "import", tar).CombinedOutput() + require.NoError(t, err, "output: %q", output) t.Logf("make sure image is loaded") - img, err = imageService.ImageStatus(&runtime.ImageSpec{Image: testImage}) - require.NoError(t, err) - require.NotNil(t, img) + // Use Eventually because the cri plugin needs a short period of time + // to pick up images imported into containerd directly. + require.NoError(t, Eventually(func() (bool, error) { + img, err = imageService.ImageStatus(&runtime.ImageSpec{Image: testImage}) + if err != nil { + return false, err + } + return img != nil, nil + }, 100*time.Millisecond, 10*time.Second)) require.Equal(t, []string{loadedImage}, img.RepoTags) t.Logf("create a container with the loaded image") diff --git a/integration/test_utils.go b/integration/test_utils.go index c24853e5d..12d2b92fe 100644 --- a/integration/test_utils.go +++ b/integration/test_utils.go @@ -38,8 +38,6 @@ import ( "k8s.io/kubernetes/pkg/kubelet/remote" kubeletutil "k8s.io/kubernetes/pkg/kubelet/util" - api "github.com/containerd/cri/pkg/api/v1" - "github.com/containerd/cri/pkg/client" criconfig "github.com/containerd/cri/pkg/config" "github.com/containerd/cri/pkg/constants" "github.com/containerd/cri/pkg/server" @@ -47,17 +45,16 @@ import ( ) const ( - timeout = 1 * time.Minute - pauseImage = "k8s.gcr.io/pause:3.1" // This is the same with default sandbox image. - k8sNamespace = constants.K8sContainerdNamespace - containerdEndpoint = "/run/containerd/containerd.sock" + timeout = 1 * time.Minute + pauseImage = "k8s.gcr.io/pause:3.1" // This is the same with default sandbox image. + k8sNamespace = constants.K8sContainerdNamespace ) var ( - runtimeService cri.RuntimeService - imageService cri.ImageManagerService - containerdClient *containerd.Client - criPluginClient api.CRIPluginServiceClient + runtimeService cri.RuntimeService + imageService cri.ImageManagerService + containerdClient *containerd.Client + containerdEndpoint string ) var criEndpoint = flag.String("cri-endpoint", "unix:///run/containerd/containerd.sock", "The endpoint of cri plugin.") @@ -93,16 +90,12 @@ func ConnectDaemons() error { if err != nil { return errors.Wrap(err, "failed to list images") } + // containerdEndpoint is the same with criEndpoint now + containerdEndpoint = strings.TrimPrefix(*criEndpoint, "unix://") containerdClient, err = containerd.New(containerdEndpoint, containerd.WithDefaultNamespace(k8sNamespace)) if err != nil { return errors.Wrap(err, "failed to connect containerd") } - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - criPluginClient, err = client.NewCRIPluginClient(ctx, *criEndpoint) - if err != nil { - return errors.Wrap(err, "failed to connect cri plugin") - } return nil }