From ec9e535d17dd4633bf809e7cf0fd7dd5b661e03f Mon Sep 17 00:00:00 2001 From: Micah Young Date: Thu, 8 Oct 2020 09:08:18 -0400 Subject: [PATCH] Adds setters for OS/OSVersion/Architecture to image Signed-off-by: Micah Young --- fakes/image.go | 23 ++++++++++++++++------- image.go | 3 +++ local/local.go | 15 +++++++++++++++ local/local_test.go | 30 ++++++++++++++++++++++++++++++ remote/remote.go | 30 ++++++++++++++++++++++++++++++ remote/remote_test.go | 21 +++++++++++++++++++++ 6 files changed, 115 insertions(+), 7 deletions(-) diff --git a/fakes/image.go b/fakes/image.go index ad23f788..03b7bac4 100644 --- a/fakes/image.go +++ b/fakes/image.go @@ -80,7 +80,7 @@ func (i *Image) OS() (string, error) { } func (i *Image) OSVersion() (string, error) { - return i.os, nil + return i.osVersion, nil } func (i *Image) Architecture() (string, error) { @@ -122,6 +122,21 @@ func (i *Image) SetEnv(k string, v string) error { return nil } +func (i *Image) SetOS(o string) error { + i.os = o + return nil +} + +func (i *Image) SetOSVersion(v string) error { + i.osVersion = v + return nil +} + +func (i *Image) SetArchitecture(a string) error { + i.architecture = a + return nil +} + func (i *Image) SetWorkingDir(dir string) error { i.workingDir = dir return nil @@ -268,12 +283,6 @@ func (i *Image) SetIdentifier(identifier imgutil.Identifier) { i.identifier = identifier } -func (i *Image) SetPlatform(os, osVersion, architecture string) { - i.os = os - i.osVersion = osVersion - i.architecture = architecture -} - func (i *Image) Cleanup() error { return os.RemoveAll(i.layerDir) } diff --git a/image.go b/image.go index 5031e644..320e22a9 100644 --- a/image.go +++ b/image.go @@ -38,6 +38,9 @@ type Image interface { SetEntrypoint(...string) error SetWorkingDir(string) error SetCmd(...string) error + SetOS(string) error + SetOSVersion(string) error + SetArchitecture(string) error Rebase(string, Image) error AddLayer(path string) error AddLayerWithDiffID(path, diffID string) error diff --git a/local/local.go b/local/local.go index 5eabc978..ed13feaf 100644 --- a/local/local.go +++ b/local/local.go @@ -242,6 +242,21 @@ func (i *Image) SetLabel(key, val string) error { return nil } +func (i *Image) SetOS(osVal string) error { + i.inspect.Os = osVal + return nil +} + +func (i *Image) SetOSVersion(osVersion string) error { + i.inspect.OsVersion = osVersion + return nil +} + +func (i *Image) SetArchitecture(architecture string) error { + i.inspect.Architecture = architecture + return nil +} + func (i *Image) RemoveLabel(key string) error { delete(i.inspect.Config.Labels, key) return nil diff --git a/local/local_test.go b/local/local_test.go index 8beda4bd..d8938822 100644 --- a/local/local_test.go +++ b/local/local_test.go @@ -706,6 +706,36 @@ func testImage(t *testing.T, when spec.G, it spec.S) { }) }) + when("#SetOS #SetOSVersion #SetArchitecture", func() { + var repoName = newTestImageName() + + it.After(func() { + h.AssertNil(t, h.DockerRmi(dockerClient, repoName)) + }) + + it("sets the os/arch", func() { + img, err := local.NewImage(repoName, dockerClient) + h.AssertNil(t, err) + + //os has to match daemon + err = img.SetOS(daemonOS) + h.AssertNil(t, err) + err = img.SetOSVersion("1.2.3.4") + h.AssertNil(t, err) + err = img.SetArchitecture("arm64") + h.AssertNil(t, err) + + h.AssertNil(t, img.Save()) + + inspect, _, err := dockerClient.ImageInspectWithRaw(context.TODO(), repoName) + h.AssertNil(t, err) + + h.AssertEq(t, inspect.Os, daemonOS) + h.AssertEq(t, inspect.OsVersion, "1.2.3.4") + h.AssertEq(t, inspect.Architecture, "arm64") + }) + }) + when("#Rebase", func() { when("image exists", func() { var ( diff --git a/remote/remote.go b/remote/remote.go index e9ab9934..6dd7521f 100644 --- a/remote/remote.go +++ b/remote/remote.go @@ -351,6 +351,36 @@ func (i *Image) SetCmd(cmd ...string) error { return err } +func (i *Image) SetOS(osVal string) error { + configFile, err := i.image.ConfigFile() + if err != nil { + return err + } + configFile.OS = osVal + i.image, err = mutate.ConfigFile(i.image, configFile) + return err +} + +func (i *Image) SetOSVersion(osVersion string) error { + configFile, err := i.image.ConfigFile() + if err != nil { + return err + } + configFile.OSVersion = osVersion + i.image, err = mutate.ConfigFile(i.image, configFile) + return err +} + +func (i *Image) SetArchitecture(architecture string) error { + configFile, err := i.image.ConfigFile() + if err != nil { + return err + } + configFile.Architecture = architecture + i.image, err = mutate.ConfigFile(i.image, configFile) + return err +} + func (i *Image) TopLayer() (string, error) { all, err := i.image.Layers() if err != nil { diff --git a/remote/remote_test.go b/remote/remote_test.go index 9c94b384..db471426 100644 --- a/remote/remote_test.go +++ b/remote/remote_test.go @@ -612,6 +612,27 @@ func testImage(t *testing.T, when spec.G, it spec.S) { }) }) + when("#SetOS #SetOSVersion #SetArchitecture", func() { + it("sets the os/arch", func() { + img, err := remote.NewImage(repoName, authn.DefaultKeychain) + h.AssertNil(t, err) + + err = img.SetOS("foobaros") + h.AssertNil(t, err) + err = img.SetOSVersion("1.2.3.4") + h.AssertNil(t, err) + err = img.SetArchitecture("arm64") + h.AssertNil(t, err) + + h.AssertNil(t, img.Save()) + + configFile := h.FetchManifestImageConfigFile(t, repoName) + h.AssertEq(t, configFile.OS, "foobaros") + h.AssertEq(t, configFile.OSVersion, "1.2.3.4") + h.AssertEq(t, configFile.Architecture, "arm64") + }) + }) + when("#Rebase", func() { when("image exists", func() { var oldBase, newBase, oldTopLayerDiffID string