-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set the default platform for select sources based on host arch (#152)
* set the default platform for select sources based on host arch Signed-off-by: Alex Goodman <alex.goodman@anchore.com> * decompose into smaller function and add tests for setting default platform Signed-off-by: Alex Goodman <alex.goodman@anchore.com> --------- Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
- Loading branch information
Showing
2 changed files
with
222 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package stereoscope | ||
|
||
import ( | ||
"github.com/anchore/stereoscope/pkg/image" | ||
"github.com/scylladb/go-set/i8set" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func Test_setPlatform(t *testing.T) { | ||
|
||
expectedSources := i8set.New() | ||
for _, s := range image.AllSources { | ||
expectedSources.Add(int8(s)) | ||
} | ||
actualSources := i8set.New() | ||
|
||
tests := []struct { | ||
name string | ||
source image.Source | ||
defaultArch string | ||
initialPlatform *image.Platform | ||
wantPlatform *image.Platform | ||
wantErr require.ErrorAssertionFunc | ||
}{ | ||
// allow defaults --------------------------------------------------------- | ||
{ | ||
name: "docker daemon", | ||
source: image.DockerDaemonSource, | ||
defaultArch: "amd64", | ||
initialPlatform: nil, | ||
wantPlatform: &image.Platform{ | ||
Architecture: "amd64", | ||
OS: "linux", | ||
}, | ||
}, | ||
{ | ||
name: "docker daemon (do not override)", | ||
source: image.DockerDaemonSource, | ||
defaultArch: "amd64", | ||
initialPlatform: &image.Platform{ | ||
Architecture: "arm64", // not different than default arch | ||
OS: "linux", | ||
}, | ||
wantPlatform: &image.Platform{ | ||
Architecture: "arm64", // note: did not change | ||
OS: "linux", | ||
}, | ||
}, | ||
{ | ||
name: "podman daemon", | ||
source: image.PodmanDaemonSource, | ||
defaultArch: "amd64", | ||
initialPlatform: nil, | ||
wantPlatform: &image.Platform{ | ||
Architecture: "amd64", | ||
OS: "linux", | ||
}, | ||
}, | ||
{ | ||
name: "podman daemon (do not override)", | ||
source: image.PodmanDaemonSource, | ||
defaultArch: "amd64", | ||
initialPlatform: &image.Platform{ | ||
Architecture: "arm64", // not different than default arch | ||
OS: "linux", | ||
}, | ||
wantPlatform: &image.Platform{ | ||
Architecture: "arm64", // note: did not change | ||
OS: "linux", | ||
}, | ||
}, | ||
{ | ||
name: "OCI registry", | ||
source: image.OciRegistrySource, | ||
defaultArch: "amd64", | ||
initialPlatform: nil, | ||
wantPlatform: &image.Platform{ | ||
Architecture: "amd64", | ||
OS: "linux", | ||
}, | ||
}, | ||
{ | ||
name: "OCI registry (do not override)", | ||
source: image.OciRegistrySource, | ||
defaultArch: "amd64", | ||
initialPlatform: &image.Platform{ | ||
Architecture: "arm64", // not different than default arch | ||
OS: "linux", | ||
}, | ||
wantPlatform: &image.Platform{ | ||
Architecture: "arm64", // note: did not change | ||
OS: "linux", | ||
}, | ||
}, | ||
// disallow defaults --------------------------------------------------------- | ||
{ | ||
name: "docker tarball", | ||
source: image.DockerTarballSource, | ||
defaultArch: "amd64", | ||
initialPlatform: nil, | ||
wantPlatform: nil, | ||
}, | ||
{ | ||
name: "docker tarball (override fails)", | ||
source: image.DockerTarballSource, | ||
defaultArch: "amd64", | ||
initialPlatform: &image.Platform{ | ||
Architecture: "amd64", | ||
OS: "linux", | ||
}, | ||
wantErr: require.Error, | ||
}, | ||
{ | ||
name: "OCI dir", | ||
source: image.OciDirectorySource, | ||
defaultArch: "amd64", | ||
initialPlatform: nil, | ||
wantPlatform: nil, | ||
}, | ||
{ | ||
name: "OCI dir (override fails)", | ||
source: image.OciDirectorySource, | ||
defaultArch: "amd64", | ||
initialPlatform: &image.Platform{ | ||
Architecture: "amd64", | ||
OS: "linux", | ||
}, | ||
wantErr: require.Error, | ||
}, | ||
{ | ||
name: "OCI tarball", | ||
source: image.OciTarballSource, | ||
defaultArch: "amd64", | ||
initialPlatform: nil, | ||
wantPlatform: nil, | ||
}, | ||
{ | ||
name: "OCI tarball (override fails)", | ||
source: image.OciTarballSource, | ||
defaultArch: "amd64", | ||
initialPlatform: &image.Platform{ | ||
Architecture: "amd64", | ||
OS: "linux", | ||
}, | ||
wantErr: require.Error, | ||
}, | ||
{ | ||
name: "singularity", | ||
source: image.SingularitySource, | ||
defaultArch: "amd64", | ||
initialPlatform: nil, | ||
wantPlatform: nil, | ||
}, | ||
{ | ||
name: "singularity (override fails)", | ||
source: image.SingularitySource, | ||
defaultArch: "amd64", | ||
initialPlatform: &image.Platform{ | ||
Architecture: "amd64", | ||
OS: "linux", | ||
}, | ||
wantErr: require.Error, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.wantErr == nil { | ||
tt.wantErr = require.NoError | ||
} | ||
|
||
actualSources.Add(int8(tt.source)) | ||
cfg := config{ | ||
Platform: tt.initialPlatform, | ||
} | ||
err := setPlatform(tt.source, &cfg, tt.defaultArch) | ||
tt.wantErr(t, err) | ||
if err != nil { | ||
return | ||
} | ||
|
||
assert.Equal(t, tt.wantPlatform, cfg.Platform) | ||
}) | ||
} | ||
|
||
diff := i8set.Difference(expectedSources, actualSources) | ||
if !diff.IsEmpty() { | ||
t.Errorf("missing test cases for sources: %v", diff.List()) | ||
} | ||
} |