dendrite-upgrade-test: support --from HEAD-N, cleanup on startup (#1903)

This commit is contained in:
kegsay 2021-07-07 14:20:07 +01:00 committed by GitHub
parent d72d634391
commit bb6e4487dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -14,6 +14,7 @@ import (
"regexp" "regexp"
"runtime" "runtime"
"sort" "sort"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -22,6 +23,7 @@ import (
"github.com/codeclysm/extract" "github.com/codeclysm/extract"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/volume" "github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client" "github.com/docker/docker/client"
@ -30,7 +32,7 @@ import (
var ( var (
flagTempDir = flag.String("tmp", "tmp", "Path to temporary directory to dump tarballs to") flagTempDir = flag.String("tmp", "tmp", "Path to temporary directory to dump tarballs to")
flagFrom = flag.String("from", "0.1.0", "The version to start from e.g '0.3.1'.") flagFrom = flag.String("from", "HEAD-1", "The version to start from e.g '0.3.1'. If 'HEAD-N' then starts N versions behind HEAD.")
flagTo = flag.String("to", "HEAD", "The version to end on e.g '0.3.3'.") flagTo = flag.String("to", "HEAD", "The version to end on e.g '0.3.3'.")
flagBuildConcurrency = flag.Int("build-concurrency", runtime.NumCPU(), "The amount of build concurrency when building images") flagBuildConcurrency = flag.Int("build-concurrency", runtime.NumCPU(), "The amount of build concurrency when building images")
alphaNumerics = regexp.MustCompile("[^a-zA-Z0-9]+") alphaNumerics = regexp.MustCompile("[^a-zA-Z0-9]+")
@ -84,6 +86,8 @@ ENV SERVER_NAME=localhost
EXPOSE 8008 8448 EXPOSE 8008 8448
CMD /build/run_dendrite.sh ` CMD /build/run_dendrite.sh `
const dendriteUpgradeTestLabel = "dendrite_upgrade_test"
// downloadArchive downloads an arbitrary github archive of the form: // downloadArchive downloads an arbitrary github archive of the form:
// https://github.com/matrix-org/dendrite/archive/v0.3.11.tar.gz // https://github.com/matrix-org/dendrite/archive/v0.3.11.tar.gz
// and re-tarballs it without the top-level directory which contains branch information. It inserts // and re-tarballs it without the top-level directory which contains branch information. It inserts
@ -203,18 +207,32 @@ func calculateVersions(cli *http.Client, from, to string) []string {
} }
// snip the lower bound depending on --from // snip the lower bound depending on --from
if from != "" { if from != "" {
fromVer, err := semver.NewVersion(from) if strings.HasPrefix(from, "HEAD-") {
if err != nil { var headN int
log.Fatalf("invalid --from: %s", err) headN, err = strconv.Atoi(strings.TrimPrefix(from, "HEAD-"))
} if err != nil {
i := 0 log.Fatalf("invalid --from, try 'HEAD-1'")
for i = 0; i < len(semvers); i++ {
if semvers[i].LessThan(fromVer) {
continue
} }
break if headN >= len(semvers) {
log.Fatalf("only have %d versions, but asked to go to HEAD-%d", len(semvers), headN)
}
if headN > 0 {
semvers = semvers[len(semvers)-headN:]
}
} else {
fromVer, err := semver.NewVersion(from)
if err != nil {
log.Fatalf("invalid --from: %s", err)
}
i := 0
for i = 0; i < len(semvers); i++ {
if semvers[i].LessThan(fromVer) {
continue
}
break
}
semvers = semvers[i:]
} }
semvers = semvers[i:]
} }
if to != "" && to != "HEAD" { if to != "" && to != "HEAD" {
toVer, err := semver.NewVersion(to) toVer, err := semver.NewVersion(to)
@ -279,6 +297,9 @@ func runImage(dockerClient *client.Client, volumeName, version, imageID string)
body, err := dockerClient.ContainerCreate(ctx, &container.Config{ body, err := dockerClient.ContainerCreate(ctx, &container.Config{
Image: imageID, Image: imageID,
Env: []string{"SERVER_NAME=hs1"}, Env: []string{"SERVER_NAME=hs1"},
Labels: map[string]string{
dendriteUpgradeTestLabel: "yes",
},
}, &container.HostConfig{ }, &container.HostConfig{
PublishAllPorts: true, PublishAllPorts: true,
Mounts: []mount.Mount{ Mounts: []mount.Mount{
@ -373,6 +394,28 @@ func verifyTests(dockerClient *client.Client, volumeName string, versions []stri
return verifyTestsRan(csAPIURL, versions) return verifyTestsRan(csAPIURL, versions)
} }
// cleanup old containers/volumes from a previous run
func cleanup(dockerClient *client.Client) {
// ignore all errors, we are just cleaning up and don't want to fail just because we fail to cleanup
containers, _ := dockerClient.ContainerList(context.Background(), types.ContainerListOptions{
Filters: label(dendriteUpgradeTestLabel),
})
for _, c := range containers {
s := time.Second
_ = dockerClient.ContainerStop(context.Background(), c.ID, &s)
_ = dockerClient.ContainerRemove(context.Background(), c.ID, types.ContainerRemoveOptions{
Force: true,
})
}
_ = dockerClient.VolumeRemove(context.Background(), "dendrite_upgrade_test", true)
}
func label(in string) filters.Args {
f := filters.NewArgs()
f.Add("label", in)
return f
}
func main() { func main() {
flag.Parse() flag.Parse()
httpClient := &http.Client{ httpClient := &http.Client{
@ -386,6 +429,7 @@ func main() {
flag.Usage() flag.Usage()
os.Exit(1) os.Exit(1)
} }
cleanup(dockerClient)
versions := calculateVersions(httpClient, *flagFrom, *flagTo) versions := calculateVersions(httpClient, *flagFrom, *flagTo)
log.Printf("Testing dendrite versions: %v\n", versions) log.Printf("Testing dendrite versions: %v\n", versions)
@ -394,6 +438,9 @@ func main() {
// make a shared postgres volume // make a shared postgres volume
volume, err := dockerClient.VolumeCreate(context.Background(), volume.VolumeCreateBody{ volume, err := dockerClient.VolumeCreate(context.Background(), volume.VolumeCreateBody{
Name: "dendrite_upgrade_test", Name: "dendrite_upgrade_test",
Labels: map[string]string{
dendriteUpgradeTestLabel: "yes",
},
}) })
if err != nil { if err != nil {
log.Fatalf("failed to make docker volume: %s", err) log.Fatalf("failed to make docker volume: %s", err)