Skip to content

Commit 246e381

Browse files
committed
fix: use ocm version from Dockerfile
1 parent c7a896e commit 246e381

File tree

2 files changed

+81
-9
lines changed

2 files changed

+81
-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: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
package utils
22

33
import (
4+
"bufio"
5+
"fmt"
46
"io"
57
"net/http"
68
"os"
79
"os/exec"
810
"path/filepath"
11+
"regexp"
912
"runtime"
1013
"testing"
1114
)
1215

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

2221
// DownloadOCMAndAddToPath downloads the OCM cli for the current platform and puts it to the PATH of the test
2322
func DownloadOCMAndAddToPath(t *testing.T) {
2423
t.Helper()
2524

25+
ocmVersion := getOCMVersion(t)
26+
2627
cacheDir := filepath.Join(CacheDirRoot, "ocm-cli-cache")
2728
if err := os.MkdirAll(cacheDir, 0o755); err != nil {
2829
t.Fatalf("failed to create cache dir: %v", err)
2930
}
3031

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

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

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

4041
tempDir := t.TempDir()
4142
archivePath := filepath.Join(tempDir, "ocm.tar.gz")
@@ -130,3 +131,75 @@ func BuildComponent(componentConstructorLocation string, t *testing.T) string {
130131

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

0 commit comments

Comments
 (0)