Skip to content

Commit cbeb45d

Browse files
authored
fix: use ocm version from Dockerfile (#12)
1 parent c7a896e commit cbeb45d

File tree

2 files changed

+79
-9
lines changed

2 files changed

+79
-9
lines changed

renovate.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
"description": "All component dependencies in other locations.",
4343
"customType": "regex",
4444
"managerFilePatterns": [
45-
"/Dockerfile/",
46-
"/test/utils/ocm.go"
45+
"/Dockerfile/"
4746
],
4847
"matchStrings": [
4948
"renovate: datasource=(?<datasource>[a-z-.]+?) depName=(?<depName>[^\\s]+?)(?: (lookupName|packageName)=(?<packageName>[^\\s]+?))?(?: versioning=(?<versioning>[^\\s]+?))?(?: extractVersion=(?<extractVersion>[^\\s]+?))?(?: registryUrl=(?<registryUrl>[^\\s]+?))?\\s.+?(_version|_VERSION)( |)=( |)(\"|)(?<currentValue>.+?)(\"|)?\\s"

test/utils/ocm.go

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
package utils
22

33
import (
4+
"bufio"
45
"io"
56
"net/http"
67
"os"
78
"os/exec"
89
"path/filepath"
10+
"regexp"
911
"runtime"
1012
"testing"
1113
)
1214

13-
const (
14-
// renovate: datasource=github-releases depName=ocm packageName=open-component-model/ocm
15-
OCM_VERSION = "0.27.0"
16-
)
17-
1815
var (
1916
CacheDirRoot = filepath.Join(os.TempDir(), "openmcp-bootstrapper-test")
17+
OCMVersion = ""
2018
)
2119

2220
// DownloadOCMAndAddToPath downloads the OCM cli for the current platform and puts it to the PATH of the test
2321
func DownloadOCMAndAddToPath(t *testing.T) {
2422
t.Helper()
2523

24+
ocmVersion := getOCMVersion(t)
25+
2626
cacheDir := filepath.Join(CacheDirRoot, "ocm-cli-cache")
2727
if err := os.MkdirAll(cacheDir, 0o755); err != nil {
2828
t.Fatalf("failed to create cache dir: %v", err)
2929
}
3030

31-
ocmBinaryName := "ocm-" + OCM_VERSION + "-" + runtime.GOOS + "-" + runtime.GOARCH
31+
ocmBinaryName := "ocm-" + ocmVersion + "-" + runtime.GOOS + "-" + runtime.GOARCH
3232
ocmPath := filepath.Join(cacheDir, ocmBinaryName)
3333

3434
if _, err := os.Stat(ocmPath); os.IsNotExist(err) {
3535
t.Log("Downloading OCM as it is not present in the cache directory, starting download...")
3636

3737
downloadURL := "https://github.com/open-component-model/ocm/releases/download/v" +
38-
OCM_VERSION + "/ocm-" + OCM_VERSION + "-" + runtime.GOOS + "-" + runtime.GOARCH + ".tar.gz"
38+
ocmVersion + "/ocm-" + ocmVersion + "-" + runtime.GOOS + "-" + runtime.GOARCH + ".tar.gz"
3939

4040
tempDir := t.TempDir()
4141
archivePath := filepath.Join(tempDir, "ocm.tar.gz")
@@ -130,3 +130,74 @@ func BuildComponent(componentConstructorLocation string, t *testing.T) string {
130130

131131
return ctfDir
132132
}
133+
134+
func getOCMVersion(t *testing.T) string {
135+
var err error
136+
137+
if OCMVersion != "" {
138+
t.Logf("Using cached OCM_VERSION: %s", OCMVersion)
139+
return OCMVersion
140+
}
141+
142+
// Find the parent directory containing the Dockerfile
143+
cwd, err := os.Getwd()
144+
if err != nil {
145+
t.Fatalf("failed to get working directory: %v", err)
146+
}
147+
148+
var (
149+
dockerfilePath string
150+
currentDir = cwd
151+
)
152+
153+
for {
154+
dockerfilePath = filepath.Join(currentDir, "Dockerfile")
155+
if _, err = os.Stat(dockerfilePath); err == nil {
156+
break
157+
} else {
158+
if !os.IsNotExist(err) {
159+
t.Fatalf("failed to check Dockerfile existence: %v", err)
160+
}
161+
}
162+
parent := filepath.Dir(currentDir)
163+
if parent == currentDir {
164+
t.Fatalf("Dockerfile not found in any parent directory of %s", cwd)
165+
}
166+
167+
currentDir = parent
168+
}
169+
OCMVersion = parseDockerfileOCMVersion(dockerfilePath, t)
170+
171+
t.Logf("Parsed OCM_VERSION from Dockerfile: %s", OCMVersion)
172+
return OCMVersion
173+
}
174+
175+
// ParseDockerfileOCMVersion parses the Dockerfile to extract the OCM_VERSION argument value.
176+
func parseDockerfileOCMVersion(dockerfilePath string, t *testing.T) string {
177+
file, err := os.Open(dockerfilePath)
178+
if err != nil {
179+
t.Fatalf("failed to open Dockerfile: %v", err)
180+
}
181+
defer func(file *os.File) {
182+
err := file.Close()
183+
if err != nil {
184+
t.Fatalf("failed to close Dockerfile: %v", err)
185+
}
186+
}(file)
187+
188+
scanner := bufio.NewScanner(file)
189+
re := regexp.MustCompile(`^ARG OCM_VERSION=([\w.-]+)`)
190+
for scanner.Scan() {
191+
line := scanner.Text()
192+
matches := re.FindStringSubmatch(line)
193+
if len(matches) == 2 {
194+
return matches[1]
195+
}
196+
}
197+
if err = scanner.Err(); err != nil {
198+
t.Fatalf("failed to read Dockerfile: %v", err)
199+
}
200+
201+
t.Fatalf("OCM_VERSION not found in Dockerfile: %s", dockerfilePath)
202+
return ""
203+
}

0 commit comments

Comments
 (0)