Skip to content

Commit b241f8b

Browse files
committed
kola: support relative indices for --build arg
I often want to target the previous build when e.g. doing `cosa run` or `kola testiso` to compare between an older and newer build. Instead of having to copy/paste the previous build ID, let's support passing negative indices like `-1` for the previous build, `-2` for the one before that, etc... This is similar to the `journalctl --boot N` syntax.
1 parent c426a1c commit b241f8b

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

mantle/cmd/kola/options.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func init() {
6767
bv(&kola.Options.SSHOnTestFailure, "ssh-on-test-failure", false, "SSH into a machine when tests fail")
6868
sv(&kola.Options.Stream, "stream", "", "CoreOS stream ID (e.g. for Fedora CoreOS: stable, testing, next)")
6969
sv(&kola.Options.CosaWorkdir, "workdir", "", "coreos-assembler working directory")
70-
sv(&kola.Options.CosaBuildId, "build", "", "coreos-assembler build ID")
70+
sv(&kola.Options.CosaBuildId, "build", "", "coreos-assembler build ID (or e.g. -1, -2, for previous builds)")
7171
sv(&kola.Options.CosaBuildArch, "arch", coreosarch.CurrentRpmArch(), "The target architecture of the build")
7272
sv(&kola.Options.AppendButane, "append-butane", "", "Path to Butane config which is merged with test code")
7373
sv(&kola.Options.AppendIgnition, "append-ignition", "", "Path to Ignition config which is merged with test code")
@@ -273,6 +273,14 @@ func syncOptionsImpl(useCosa bool) error {
273273
kola.Options.CosaWorkdir = "."
274274
}
275275

276+
if strings.HasPrefix(kola.Options.CosaBuildId, "-") {
277+
var err error
278+
kola.Options.CosaBuildId, err = util.GetRelativeLocalBuildId(kola.Options.CosaWorkdir, kola.Options.CosaBuildId)
279+
if err != nil {
280+
return err
281+
}
282+
}
283+
276284
localbuild, err := util.GetLocalBuild(kola.Options.CosaWorkdir,
277285
kola.Options.CosaBuildId,
278286
kola.Options.CosaBuildArch)

mantle/util/repo.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"os"
2020
"path/filepath"
21+
"strconv"
2122
"strings"
2223

2324
"github.com/pkg/errors"
@@ -82,6 +83,27 @@ func GetLatestLocalBuild(root, arch string) (*LocalBuild, error) {
8283
return GetLocalBuild(root, "latest", arch)
8384
}
8485

86+
func GetRelativeLocalBuildId(root, relid string) (string, error) {
87+
// resolve relative builds, e.g. -1, -2
88+
relBuild, err := strconv.ParseInt(relid, 10, 0)
89+
if err != nil {
90+
return "", fmt.Errorf("invalid relative build index %s: %w", relid, err)
91+
}
92+
if relBuild >= 0 {
93+
return "", fmt.Errorf("invalid relative build index %s: not negative", relid)
94+
}
95+
// we know it'll fit in an int since we used bitSize: 0 above
96+
relBuildInt := int(-relBuild)
97+
builds, err := cosa.GetBuilds(filepath.Join(root, "builds"))
98+
if err != nil {
99+
return "", err
100+
}
101+
if relBuildInt >= len(builds.Builds) {
102+
return "", fmt.Errorf("relative build index %d does not exist", -relBuild)
103+
}
104+
return builds.Builds[relBuildInt].ID, nil
105+
}
106+
85107
func GetLocalBuild(root, buildid, arch string) (*LocalBuild, error) {
86108
if err := RequireCosaRoot(root); err != nil {
87109
return nil, err

pkg/builds/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func ReadBuild(dir, buildID, arch string) (*Build, string, error) {
9696
}
9797

9898
if buildID == "" {
99-
b, err := getBuilds(dir)
99+
b, err := GetBuilds(dir)
100100
if err != nil {
101101
return nil, "", err
102102
}

pkg/builds/builds.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ type build struct {
2626
Arches []string `json:"arches"`
2727
}
2828

29-
// buildsJSON represents the JSON that records the builds
29+
// BuildsJSON represents the JSON that records the builds
3030
// TODO: this should be generated by a schema
31-
type buildsJSON struct {
31+
type BuildsJSON struct {
3232
SchemaVersion string `json:"schema-version"`
3333
Builds []build `json:"builds"`
3434
TimeStamp string `json:"timestamp"`
3535
}
3636

37-
func getBuilds(dir string) (*buildsJSON, error) {
37+
func GetBuilds(dir string) (*BuildsJSON, error) {
3838
path := filepath.Join(dir, CosaBuildsJSON)
3939
f, err := os.Open(path)
4040
if err != nil {
@@ -45,15 +45,15 @@ func getBuilds(dir string) (*buildsJSON, error) {
4545
if _, err := io.Copy(bufD, f); err != nil {
4646
return nil, err
4747
}
48-
b := &buildsJSON{}
48+
b := &BuildsJSON{}
4949
if err := json.Unmarshal(bufD.Bytes(), b); err != nil {
5050
return nil, err
5151
}
5252
return b, nil
5353
}
5454

5555
// getLatest returns the latest build for the arch.
56-
func (b *buildsJSON) getLatest(arch string) (string, bool) {
56+
func (b *BuildsJSON) getLatest(arch string) (string, bool) {
5757
for _, b := range b.Builds {
5858
for _, a := range b.Arches {
5959
if a == arch {

pkg/builds/builds_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestBuildsMeta(t *testing.T) {
3030
t.Fatalf("failed to write the test data %v", err)
3131
}
3232

33-
b, err := getBuilds(tmpd)
33+
b, err := GetBuilds(tmpd)
3434
if err != nil {
3535
t.Fatalf("failed to find the builds")
3636
}

pkg/builds/schema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func TestMergeMeta(t *testing.T) {
151151

152152
// Create a fake build dir
153153
fakeBuildID := "999.1"
154-
bjson, _ := json.Marshal(buildsJSON{
154+
bjson, _ := json.Marshal(BuildsJSON{
155155
SchemaVersion: "0.1.0",
156156
Builds: []build{
157157
{

0 commit comments

Comments
 (0)