From bd9808c3ad9f04542fb70ac13722f0886a75bf62 Mon Sep 17 00:00:00 2001 From: Micah Young Date: Thu, 15 Oct 2020 11:02:03 -0400 Subject: [PATCH] Change local SetOS to accept only valid values - Keep as a noop setter for consitency remote - Compare against default value to fail fast Signed-off-by: Micah Young --- local/local.go | 4 +++- local/local_test.go | 31 +++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/local/local.go b/local/local.go index ed13feaf..a546140d 100644 --- a/local/local.go +++ b/local/local.go @@ -243,7 +243,9 @@ func (i *Image) SetLabel(key, val string) error { } func (i *Image) SetOS(osVal string) error { - i.inspect.Os = osVal + if osVal != i.inspect.Os { + return fmt.Errorf(`invalid os: must match the daemon: "%s"`, i.inspect.Os) + } return nil } diff --git a/local/local_test.go b/local/local_test.go index d8938822..1c410f61 100644 --- a/local/local_test.go +++ b/local/local_test.go @@ -706,22 +706,46 @@ func testImage(t *testing.T, when spec.G, it spec.S) { }) }) - when("#SetOS #SetOSVersion #SetArchitecture", func() { + when("#SetOS", func() { var repoName = newTestImageName() it.After(func() { h.AssertNil(t, h.DockerRmi(dockerClient, repoName)) }) - it("sets the os/arch", func() { + it("allows noop sets for values that match the daemon", func() { img, err := local.NewImage(repoName, dockerClient) h.AssertNil(t, err) - //os has to match daemon + err = img.SetOS("fakeos") + h.AssertError(t, err, "invalid os: must match the daemon") + err = img.SetOS(daemonOS) 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) + }) + }) + + when("#SetOSVersion #SetArchitecture", func() { + var repoName = newTestImageName() + + it.After(func() { + h.AssertNil(t, h.DockerRmi(dockerClient, repoName)) + }) + + it("sets the os.version/arch", func() { + img, err := local.NewImage(repoName, dockerClient) + h.AssertNil(t, err) + err = img.SetOSVersion("1.2.3.4") h.AssertNil(t, err) + err = img.SetArchitecture("arm64") h.AssertNil(t, err) @@ -730,7 +754,6 @@ func testImage(t *testing.T, when spec.G, it spec.S) { 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") })