Skip to content

Commit

Permalink
Adds setters for OS/OSVersion/Architecture to image
Browse files Browse the repository at this point in the history
Signed-off-by: Micah Young <[email protected]>
  • Loading branch information
Micah Young committed Oct 12, 2020
1 parent cea9fc5 commit ec9e535
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 7 deletions.
23 changes: 16 additions & 7 deletions fakes/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
30 changes: 30 additions & 0 deletions remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ec9e535

Please sign in to comment.