k8s.io/registry.k8s.io@v0.3.1/cmd/archeio/internal/e2e/e2e_containerd_linux_test.go (about)

     1  //go:build linux && !noe2e
     2  // +build linux,!noe2e
     3  
     4  /*
     5  Copyright 2023 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package e2e
    21  
    22  import (
    23  	"os"
    24  	"os/exec"
    25  	"path/filepath"
    26  	"testing"
    27  	"time"
    28  )
    29  
    30  func TestE2EContainerdPull(t *testing.T) {
    31  	t.Parallel()
    32  	containerdVersions := []string{"1.6.20", "1.7.0"}
    33  	for i := range containerdVersions {
    34  		containerdVersion := containerdVersions[i]
    35  		t.Run("v"+containerdVersion, func(t *testing.T) {
    36  			testE2EContainerdPull(t, containerdVersion)
    37  		})
    38  	}
    39  }
    40  
    41  func testE2EContainerdPull(t *testing.T, containerdVersion string) {
    42  	t.Parallel()
    43  	// install containerd and image puller tool
    44  	installDir := filepath.Join(binDir, "containerd-"+containerdVersion)
    45  	// nolint:gosec
    46  	installCmd := exec.Command(filepath.Join(repoRoot, "hack", "tools", "e2e-setup-containerd.sh"))
    47  	installCmd.Env = append(installCmd.Env,
    48  		"CONTAINERD_VERSION="+containerdVersion,
    49  		"CONTAINERD_INSTALL_DIR="+installDir,
    50  	)
    51  	installCmd.Stderr = os.Stderr
    52  	if err := installCmd.Run(); err != nil {
    53  		t.Fatalf("Failed to install containerd: %v", err)
    54  	}
    55  
    56  	// start rootless containerd, which only needs to be able to pull images
    57  	tmpDir, err := os.MkdirTemp("", "containerd")
    58  	if err != nil {
    59  		t.Fatalf("Failed to setup tmpdir: %v", err)
    60  	}
    61  	t.Cleanup(func() {
    62  		os.RemoveAll(tmpDir)
    63  	})
    64  	socketAddress := filepath.Join(tmpDir, "containerd.sock")
    65  	// nolint:gosec
    66  	containerdCmd := exec.Command(
    67  		filepath.Join(installDir, "containerd"),
    68  		// config generated by e2e-setup-containerd.sh
    69  		"--config="+filepath.Join(installDir, "containerd-config.toml"),
    70  		"--root="+filepath.Join(tmpDir, "root"),
    71  		"--state="+filepath.Join(tmpDir, "state"),
    72  		"--address="+socketAddress,
    73  		"--log-level=trace",
    74  	)
    75  	containerdCmd.Stderr = os.Stderr
    76  	if err := containerdCmd.Start(); err != nil {
    77  		t.Fatalf("Failed to start containerd: %v", err)
    78  	}
    79  	t.Cleanup(func() {
    80  		if err := containerdCmd.Process.Signal(os.Interrupt); err != nil {
    81  			t.Fatalf("failed to signal containerd: %v", err)
    82  		}
    83  		// kill if it doesn't exit gracefully after 1s
    84  		done := make(chan error)
    85  		go func() { done <- containerdCmd.Wait() }()
    86  		select {
    87  		case <-done:
    88  			// exited
    89  		case <-time.After(time.Second):
    90  			// timed out
    91  			if err := containerdCmd.Process.Kill(); err != nil {
    92  				t.Fatalf("Failed to kill containerd: %v", err)
    93  			}
    94  		}
    95  	})
    96  
    97  	// wait for containerd to be ready
    98  	containerdReady := false
    99  	for i := 0; i < 5; i++ {
   100  		// nolint:gosec
   101  		if err := exec.Command(filepath.Join(installDir, "ctr"), "--address="+socketAddress, "version").Run(); err == nil {
   102  			containerdReady = true
   103  			break
   104  		}
   105  		time.Sleep(time.Duration(i) * time.Second)
   106  	}
   107  	if !containerdReady {
   108  		t.Fatalf("Failed to wait for containerd to be ready")
   109  	}
   110  
   111  	// pull test images
   112  	for i := range testCases {
   113  		tc := &testCases[i]
   114  		t.Run(tc.Name, func(t *testing.T) {
   115  			t.Parallel()
   116  			// nolint:gosec
   117  			pullCmd := exec.Command(filepath.Join(installDir, "ctr"), "--address="+socketAddress, "content", "fetch", tc.Ref())
   118  			testPull(t, tc, pullCmd)
   119  		})
   120  	}
   121  }