image-tests: don't use random names for CI artifacts

When using random names for artifacts like AWS snapshots, or Azure
images, it becomes hard to clean them up in case of CI failure. See this
issue for more details:
https://github.com/osbuild/osbuild-composer/issues/942

This PR introduces predictable names so that we can easily determine
which artifact belongs to which PR and therefore we can decide to wipe
all resources that are not needed any more.
This commit is contained in:
Martin Sehnoutka 2020-09-08 13:59:14 +02:00 committed by Ondřej Budai
parent e80110a12c
commit 509964bede
5 changed files with 27 additions and 26 deletions

View file

@ -47,6 +47,20 @@ type testcaseStruct struct {
var disableLocalBoot = flag.Bool("disable-local-boot", false, "when this flag is given, no images are booted locally using qemu (this does not affect testing in clouds)")
// GenerateCIArtifactName generates a new identifier for CI artifacts which is based
// on environment variables specified by Jenkins
// note: in case of migration to sth else like Github Actions, change it to whatever variables GH Action provides
func GenerateCIArtifactName(prefix string) (string, error) {
distroCode := os.Getenv("DISTRO_CODE")
changeId := os.Getenv("CHANGE_ID")
buildId := os.Getenv("BUILD_ID")
if changeId == "" || buildId == "" || distroCode == "" {
return "", fmt.Errorf("The environment variables must specify CHANGE_ID, BUILD_ID, and DISTRO_CODE")
}
return fmt.Sprintf("%s%s-%s-%s", prefix, distroCode, changeId, buildId), nil
}
// runOsbuild runs osbuild with the specified manifest and output-directory.
func runOsbuild(manifest []byte, store, outputDirectory string) error {
cmd := constants.GetOsbuildCommand(store, outputDirectory)
@ -225,7 +239,7 @@ func testBootUsingAWS(t *testing.T, imagePath string) {
}
imageName, err := boot.GenerateRandomString("osbuild-image-tests-image-")
imageName, err := GenerateCIArtifactName("osbuild-image-tests-image-")
require.NoError(t, err)
e, err := boot.NewEC2(creds)
@ -244,9 +258,11 @@ func testBootUsingAWS(t *testing.T, imagePath string) {
require.NoErrorf(t, err, "cannot delete the ec2 image, resources could have been leaked")
}()
securityGroupName, err := GenerateCIArtifactName("osbuild-image-tests-security-group-")
require.NoError(t, err)
// boot the uploaded image and try to connect to it
err = boot.WithSSHKeyPair(func(privateKey, publicKey string) error {
return boot.WithBootedImageInEC2(e, imageDesc, publicKey, func(address string) error {
return boot.WithBootedImageInEC2(e, securityGroupName, imageDesc, publicKey, func(address string) error {
testSSH(t, address, privateKey, nil)
return nil
})
@ -266,7 +282,7 @@ func testBootUsingAzure(t *testing.T, imagePath string) {
}
// create a random test id to name all the resources used in this test
testId, err := boot.GenerateRandomString("")
testId, err := GenerateCIArtifactName("")
require.NoError(t, err)
imageName := "image-" + testId + ".vhd"
@ -307,7 +323,7 @@ func testBootUsingOpenStack(t *testing.T, imagePath string) {
require.NoError(t, err)
// create a random test id to name all the resources used in this test
imageName, err := boot.GenerateRandomString("osbuild-image-tests-openstack-image-")
imageName, err := GenerateCIArtifactName("osbuild-image-tests-openstack-image-")
require.NoError(t, err)
// the following line should be done by osbuild-composer at some point
@ -357,7 +373,7 @@ func testBootUsingVMware(t *testing.T, imagePath string) {
defer os.Remove(imagePath)
// create a random test id to name all the resources used in this test
imageName, err := boot.GenerateRandomString("osbuild-image-tests-vmware-image-")
imageName, err := GenerateCIArtifactName("osbuild-image-tests-vmware-image-")
require.NoError(t, err)
// the following line should be done by osbuild-composer at some point