Skip to content

Commit cb40f84

Browse files
amanycodessozercanashnamehrotra
authored
fix: Get host platform info in single arch integration test (project-copacetic#1197)
Signed-off-by: amanycodes <amanycodes@gmail.com> Co-authored-by: Sertaç Özercan <852750+sozercan@users.noreply.github.com> Co-authored-by: Ashna Mehrotra <ashnamehrotra@gmail.com>
1 parent 0943c57 commit cb40f84

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

integration/singlearch/patch_test.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"testing"
1313

14+
"github.com/containerd/platforms"
1415
"github.com/distribution/reference"
1516
"github.com/opencontainers/go-digest"
1617
"github.com/project-copacetic/copacetic/integration/common"
@@ -34,6 +35,12 @@ type testImage struct {
3435
IsManifestList bool `json:"isManifestList"`
3536
}
3637

38+
type manifestPlatform struct {
39+
OS string `json:"os"`
40+
Architecture string `json:"architecture"`
41+
Variant string `json:"variant,omitempty"`
42+
}
43+
3744
func TestPatch(t *testing.T) {
3845
var images []testImage
3946
err := json.Unmarshal(testImages, &images)
@@ -111,7 +118,22 @@ func TestPatch(t *testing.T) {
111118
// The scanning should look for the tag that Copa actually created
112119
scanTag := tagPatched
113120
if !reportFile && img.IsManifestList {
114-
scanTag += "-amd64"
121+
hostPlatform := platforms.DefaultSpec().Architecture
122+
imagePlatforms := getManifestPlatforms(t, ref)
123+
124+
found := false
125+
for _, p := range imagePlatforms {
126+
if p.Architecture == hostPlatform {
127+
found = true
128+
break
129+
}
130+
}
131+
132+
targetArch := hostPlatform
133+
if !found && len(imagePlatforms) > 0 {
134+
targetArch = imagePlatforms[0].Architecture
135+
}
136+
scanTag += "-" + targetArch
115137
}
116138
patchedRef := fmt.Sprintf("%s:%s", r.Name(), scanTag)
117139

@@ -145,6 +167,49 @@ func TestPatch(t *testing.T) {
145167
}
146168
}
147169

170+
func getManifestPlatforms(t *testing.T, imageRef string) []manifestPlatform {
171+
validPlatforms := map[string]bool{
172+
"linux/386": true,
173+
"linux/amd64": true,
174+
"linux/arm": true,
175+
"linux/arm/v5": true,
176+
"linux/arm/v6": true,
177+
"linux/arm/v7": true,
178+
"linux/arm64": true,
179+
"linux/arm64/v8": true,
180+
"linux/ppc64le": true,
181+
"linux/s390x": true,
182+
"linux/riscv64": true,
183+
}
184+
185+
cmd := exec.Command("docker", "manifest", "inspect", imageRef)
186+
output, err := cmd.CombinedOutput()
187+
if err != nil {
188+
return nil
189+
}
190+
var manifest struct {
191+
Manifests []struct {
192+
Platform manifestPlatform `json:"platform"`
193+
} `json:"manifests"`
194+
}
195+
err = json.Unmarshal(output, &manifest)
196+
require.NoError(t, err, "failed to parse manifest JSON")
197+
198+
var filteredPlatforms []manifestPlatform
199+
for _, m := range manifest.Manifests {
200+
p := m.Platform
201+
platformStr := p.OS + "/" + p.Architecture
202+
if p.Variant != "" {
203+
platformStr += "/" + p.Variant
204+
}
205+
206+
if _, ok := validPlatforms[platformStr]; ok {
207+
filteredPlatforms = append(filteredPlatforms, p)
208+
}
209+
}
210+
return filteredPlatforms
211+
}
212+
148213
func dockerPull(t *testing.T, ref string) {
149214
dockerCmd(t, `pull`, ref)
150215
}

0 commit comments

Comments
 (0)