云镜像测试
云镜像测试 是一个测试框架和一组测试套件,用于 测试 GCE 镜像。
调用
测试组件被构建到一个容器镜像中。入口点是
/manager,它支持以下选项:
Usage:
-machine_type string
sets default machine_type for test runs, regardless of architecture
prefer the use -x86_shape and/or -arm64_shape instead
-arm64_shape string
default arm64 vm shape for tests not requiring a specific shape (default "t2a-standard-1")
-x86_shape string
default x86(-32 and -64) vm shape for tests not requiring a specific shape (default "n1-standard-1")
-zone string
zone to be used for tests
-project string
project to use for test runner
-test_projects string
comma separated list of projects to be used for tests. Defaults to the test runner project
-compute_endpoint_override string
use a different endpoint for compute client libraries
-exclude string
skip tests matching filter
-filter string
only run tests matching filter
-exclude_discrete_tests string
skip individual tests within the suite that match the filter
-gcs_path string
GCS Path for Daisy working directory
-images string
comma separated list of images to test. These can be fully qualified
image URLs (like "projects/my-project/global/images/my-image" or
"projects/my-project/global/images/family/my-family") or just the name
of the family if the family is a standard image (like "debian-12")
-local_path string
path where test binaries are stored
-out_path string
junit xml output path (default "junit.xml")
-write_local_artifacts string
Local path to download test artifacts from gcs. (default none)
-parallel_count int
tests to run at one time
-parallel_stagger string
parsable time.Duration to stagger each parallel test (default "60s")
-set_exit_status
Exit with non-zero exit code if test suites are failing (default true)
-timeout string
timeout for each step in the test workflow (default "20m")
-print
instead of running, print out the parsed test workflows and exit
-validate
validate all the test workflows and exit
以下标志提供给管理器,但在测试套件运行时由测试套件解释,
请参阅 the test_suites documentation 以
获取更多信息。
-shapevalidation_test_filter string
regexp filter for shapevalidation test cases, only cases with a matching family name will be run (default ".*")
-storageperf_test_filter string
regexp filter for storageperf test cases, only cases with a matching name will be run (default ".*")
-networkinterfacenaming_metal_zone string
zone in which to create the C3 Metal instance for images supporting IDPF. For zones with availability, refer to https://cloud.google.com/compute/docs/general-purpose-machines#c3_regions.
-networkperf_test_filter string
regexp filter for networkperf test cases, only cases with a matching name will be run (default ".*")
-nicsetup_vmtype string
string indicating type of VMs to create for nicsetup test cases.
Valid values are "both", "single", and "multi". "single" creates only
single-NIC VMs, "multi" creates only multi-NIC VMs, and "both" creates
both (default "both")
它可以通过 Docker 调用,如下所示:
images="projects/debian-cloud/global/images/family/debian-11,rhel-9"
docker run gcr.io/cloud-image-tools/cloud-image-tests --project $PROJECT \
--zone $ZONE --images $images
凭据
测试管理器旨在 Google Cloud 环境中运行,并将
使用应用默认凭据。如果您不在 Google Cloud
环境中且需要指定要使用的凭据,您可以将其作为
docker 卷提供,并通过 GOOGLE_APPLICATION_CREDENTIALS
环境变量指定路径。
假设您的应用默认凭据或服务账号凭据位于名为 credentials.json 的文件中:
docker run -v /path/to/local/creds:/creds \
-e GOOGLE_APPLICATION_CREDENTIALS=/creds/credentials.json \
gcr.io/gcp-guest/cloud-image-tests -project $PROJECT \
-zone $ZONE -images $images
如果所有测试均成功完成,管理器将以 0 退出,否则以 1 退出。 同时也会输出 JUnit 格式的 XML。
编写测试
测试组织在 test_suites 目录中的 go 包中,并使用 go 编写。每个包至少必须包含一个 setup 文件(按惯例命名为 setup.go)和至少一个测试文件(按惯例命名为
$packagename_test.go)。由于 Golang 风格约定,包名
不能包含下划线。因此,为了使测试套件名称与包名
匹配,测试套件的名称不应包含下划线。例如,
如果创建了一个新的测试套件来测试镜像许可证,它应该被称为
imagelicensing,而不是 image_licensing。
setup.go 文件描述了要运行的工作流,包括要创建的 VM 和其他
GCE 资源,这些资源所需的任何配置,要运行哪些
特定测试等。在这里,您还可以根据输入(例如镜像、区域或计算端点)或其他
条件跳过整个测试
包。
测试本身作为 go 单元测试编写在测试文件中。测试可以使用
标准 testing 包提供的任何测试 fixture。这些将
被打包成一个二进制文件,并使用
Google Compute Engine 启动脚本运行器在 setup 期间创建的测试 VM 上运行。
在编写针对 Linux 和 Windows 运行的测试时,建议根据操作系统之间的差异(例如 powershell 与 bash 命令),在适当的情况下使用同一测试内的独立函数。这使得测试定义更易于阅读和维护。
例如,如果测试 TestSomeCondition() 需要运行不同的命令
以取得类似的结果(并且该测试位于目录
mydefaulttest 中):
package mydefaulttest
import (
"runtime"
"testing"
)
func RunTestConditionWindows() {
//Test something in Windows
}
func RunTestCondition() {
//Test something in Linux
}
func TestSomeCondition(t *testing.T) {
if runtime.GOOS == "windows" {
RunTestConditionWindows()
} else {
RunTestCondition()
}
}
如果存在在不同操作系统之间有所差异的函数或库,则应
将相关函数按不同操作系统进行拆分(此类库的一个示例是内部 syscall 库)。例如,如果在 Windows
库中 X 定义了常量 Y(但未定义 Z),而在 Linux 库中它
定义了常量 Z(但未定义 Y),则测试可以类似于
以下内容
mydefaulttest.go:
package mydefaulttest
import (
"testing"
)
func TestSomeCondition(t *testing.T) {
runTestCondition(t)
}
mydefaulttest_windows.go
//go:build windows
package mydefaulttest
import (
"testing"
"X"
)
func runTestCondition(t *testing.T) {
t.Helper()
testVar := X.Y
// Test something on Windows.
}
mydefaulttest_linux.go
//go:build linux
package mydefaulttest
import (
"testing"
"X"
)
func runTestCondition(t *testing.T) {
t.Helper()
testVar := X.Z
// Test something on Linux.
}
请注意,test_utils.go 中还提供了用于某些操作系统级别抽象的函数,例如运行 Windows powershell 命令或 检查 Linux 二进制文件是否存在。
建议从复制一个现有的测试包开始。不要忘记
将您的测试添加到相关的 setup.go 文件中,以便将测试添加到
测试套件中。
根据镜像属性修改测试行为
对于需要根据镜像是 arm 还是 x86, 或 linux 还是 windows 而表现不同的测试, 建议使用 compute API 属性,而不是 依赖镜像命名约定。这些属性可以在 testworkflow Image 值中找到。值的列表可以在 Compute API 文档中此处找到。 以下代码片段中有一些示例。
func Setup(t *imagetest.Testworkflow) {
if t.Image.Architecture == "ARM64" {
//...
} else if utils.HasFeature(t.Image, "GVNIC") {
//...
}
}
对于需要跳过测试用例或根据所运行的镜像修改其行为的测试,你可以使用 utils/exceptions 库来定义它们。你可以参考 此处 的实现
测试计算 beta API 中的功能
需要针对 beta API 中的功能运行的测试,可以通过使用 CreateTestVMBeta 或 CreateTestVMFromInstanceBeta 创建 TestVM 来使用 beta 实例 API。然而,由于 daisy 创建实例步骤的限制,如果 TestWorkflow 中的一个实例使用 beta API,则该工作流中的所有实例都必须使用 beta API。
构建和运行容器镜像
从该仓库的根目录:
docker build -t cloud-image-tests -f Dockerfile .
要运行本地构建的 Docker 镜像:
docker run cloud-image-tests --project $PROJECT \
--zone $ZONE --images $images
确保在需要时定义并包含 应用默认凭据。
在本地机器上测试
从本仓库的 imagetest 目录开始,其中 outspath 是
存储测试输出的文件夹:
local_build.sh -o $outspath
默认情况下,所有测试套件都会被构建。要仅构建一个测试套件:
local_build.sh -o $outspath -s $test_suite_name
要从 imagetest 以外的目录进行构建
local_build.sh -o $outspath -i $path_to_imagetest
要运行测试,请 cd 到 $outspath,设置 shell 变量并运行
manager -zone $ZONE -project $PROJECT -images $images -filter $test_suite_name -local_path .
测试内容
测试是多种类型的组合——针对某些软件组件的端到端测试、镜像验证和功能验证等。 整体而言,它代表了发布 [受支持的 GCE 镜像][gce-images] 的质量保证标准,且在此处的所有测试套件都必须通过,Google 工程师才会发布新的 GCE 镜像。
测试文档位于 the test_suites 目录]。