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

View file

@ -178,7 +178,7 @@ func DeleteEC2Image(e *ec2.EC2, imageDesc *imageDescription) error {
// WithBootedImageInEC2 runs the function f in the context of booted
// image in AWS EC2
func WithBootedImageInEC2(e *ec2.EC2, imageDesc *imageDescription, publicKey string, f func(address string) error) (retErr error) {
func WithBootedImageInEC2(e *ec2.EC2, securityGroupName string, imageDesc *imageDescription, publicKey string, f func(address string) error) (retErr error) {
// generate user data with given public key
userData, err := CreateUserData(publicKey)
if err != nil {
@ -187,12 +187,6 @@ func WithBootedImageInEC2(e *ec2.EC2, imageDesc *imageDescription, publicKey str
// Security group must be now generated, because by default
// all traffic to EC2 instance is filtered.
securityGroupName, err := GenerateRandomString("osbuild-image-tests-security-group-")
if err != nil {
return fmt.Errorf("cannot generate a random name for the image: %#v", err)
}
// Firstly create a security group
securityGroup, err := e.CreateSecurityGroup(&ec2.CreateSecurityGroupInput{
GroupName: aws.String(securityGroupName),

View file

@ -7,8 +7,6 @@ import (
"os"
"syscall"
"time"
"github.com/google/uuid"
)
// durationMin returns the smaller of two given durations
@ -52,14 +50,3 @@ func killProcessCleanly(process *os.Process, timeout time.Duration) error {
return process.Kill()
}
// GenerateRandomString generates a new random string with specified prefix.
// The random part is based on UUID.
func GenerateRandomString(prefix string) (string, error) {
id, err := uuid.NewRandom()
if err != nil {
return "", err
}
return prefix + id.String(), nil
}

View file

@ -133,6 +133,7 @@ pipeline {
AZURE_CREDS = credentials('azure')
OPENSTACK_CREDS = credentials("psi-openstack-creds")
VCENTER_CREDS = credentials('vmware-vcenter-credentials')
DISTRO_CODE = "fedora31"
}
steps {
unstash 'fedora31'
@ -182,6 +183,7 @@ pipeline {
AZURE_CREDS = credentials('azure')
OPENSTACK_CREDS = credentials("psi-openstack-creds")
VCENTER_CREDS = credentials('vmware-vcenter-credentials')
DISTRO_CODE = "fedora32"
}
steps {
unstash 'fedora32'
@ -246,6 +248,7 @@ pipeline {
OPENSTACK_CREDS = credentials("psi-openstack-creds")
RHN_REGISTRATION_SCRIPT = credentials('rhn-register-script-production')
VCENTER_CREDS = credentials('vmware-vcenter-credentials')
DISTRO_CODE = "rhel8"
}
steps {
unstash 'rhel8cdn'
@ -300,6 +303,7 @@ pipeline {
OPENSTACK_CREDS = credentials("psi-openstack-creds")
RHN_REGISTRATION_SCRIPT = credentials('rhn-register-script-production-beta')
VCENTER_CREDS = credentials('vmware-vcenter-credentials')
DISTRO_CODE = "rhel83"
}
steps {
unstash 'rhel83'

View file

@ -58,7 +58,7 @@ run_test_case () {
AZURE_CREDS=${AZURE_CREDS-/dev/null}
OPENSTACK_CREDS=${OPENSTACK_CREDS-/dev/null}
VCENTER_CREDS=${VCENTER_CREDS-/dev/null}
TEST_CMD="env $(cat $AZURE_CREDS $OPENSTACK_CREDS $VCENTER_CREDS) $TEST_RUNNER -test.v ${IMAGE_TEST_CASES_PATH}/${TEST_CASE_FILENAME}"
TEST_CMD="env $(cat $AZURE_CREDS $OPENSTACK_CREDS $VCENTER_CREDS) CHANGE_ID=$CHANGE_ID BUILD_ID=$BUILD_ID DISTRO_CODE=$DISTRO_CODE $TEST_RUNNER -test.v ${IMAGE_TEST_CASES_PATH}/${TEST_CASE_FILENAME}"
# Run the test and add the test name to the list of passed or failed
# tests depending on the result.