Skip to content

Commit

Permalink
Merge pull request moby#46891 from thaJeztah/cleanup_api_versions
Browse files Browse the repository at this point in the history
integration / integration-cil: cleanup and fix API versions used in tests
  • Loading branch information
thaJeztah authored Dec 5, 2023
2 parents 7dc5f9d + 713c7d4 commit 2a38569
Show file tree
Hide file tree
Showing 34 changed files with 42 additions and 241 deletions.
3 changes: 1 addition & 2 deletions integration-cli/docker_api_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,7 @@ func (s *DockerAPISuite) TestBuildAddRemoteNoDecompress(c *testing.T) {
}

func (s *DockerAPISuite) TestBuildChownOnCopy(c *testing.T) {
// new feature added in 1.31 - https://github.com/moby/moby/pull/34263
testRequires(c, DaemonIsLinux, MinimumAPIVersion("1.31"))
testRequires(c, DaemonIsLinux)
dockerfile := `FROM busybox
RUN echo 'test1:x:1001:1001::/bin:/bin/false' >> /etc/passwd
RUN echo 'test1:x:1001:' >> /etc/group
Expand Down
78 changes: 15 additions & 63 deletions integration-cli/docker_api_containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
dconfig "github.com/docker/docker/daemon/config"
"github.com/docker/docker/errdefs"
Expand Down Expand Up @@ -633,30 +632,20 @@ func (s *DockerAPISuite) TestContainerAPIVerifyHeader(c *testing.T) {
// Try with no content-type
res, body, err := create("")
assert.NilError(c, err)
// todo: we need to figure out a better way to compare between dockerd versions
// comparing between daemon API version is not precise.
if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
} else {
assert.Assert(c, res.StatusCode != http.StatusOK)
}
body.Close()
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
_ = body.Close()

// Try with wrong content-type
res, body, err = create("application/xml")
assert.NilError(c, err)
if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
} else {
assert.Assert(c, res.StatusCode != http.StatusOK)
}
body.Close()
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
_ = body.Close()

// now application/json
res, body, err = create("application/json")
assert.NilError(c, err)
assert.Equal(c, res.StatusCode, http.StatusCreated)
body.Close()
_ = body.Close()
}

// Issue 14230. daemon should return 500 for invalid port syntax
Expand All @@ -675,11 +664,7 @@ func (s *DockerAPISuite) TestContainerAPIInvalidPortSyntax(c *testing.T) {

res, body, err := request.Post(testutil.GetContext(c), "/containers/create", request.RawString(config), request.JSON)
assert.NilError(c, err)
if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
} else {
assert.Assert(c, res.StatusCode != http.StatusOK)
}
assert.Equal(c, res.StatusCode, http.StatusBadRequest)

b, err := request.ReadBody(body)
assert.NilError(c, err)
Expand All @@ -699,11 +684,7 @@ func (s *DockerAPISuite) TestContainerAPIRestartPolicyInvalidPolicyName(c *testi

res, body, err := request.Post(testutil.GetContext(c), "/containers/create", request.RawString(config), request.JSON)
assert.NilError(c, err)
if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
} else {
assert.Assert(c, res.StatusCode != http.StatusOK)
}
assert.Equal(c, res.StatusCode, http.StatusBadRequest)

b, err := request.ReadBody(body)
assert.NilError(c, err)
Expand All @@ -723,11 +704,7 @@ func (s *DockerAPISuite) TestContainerAPIRestartPolicyRetryMismatch(c *testing.T

res, body, err := request.Post(testutil.GetContext(c), "/containers/create", request.RawString(config), request.JSON)
assert.NilError(c, err)
if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
} else {
assert.Assert(c, res.StatusCode != http.StatusOK)
}
assert.Equal(c, res.StatusCode, http.StatusBadRequest)

b, err := request.ReadBody(body)
assert.NilError(c, err)
Expand All @@ -747,11 +724,7 @@ func (s *DockerAPISuite) TestContainerAPIRestartPolicyNegativeRetryCount(c *test

res, body, err := request.Post(testutil.GetContext(c), "/containers/create", request.RawString(config), request.JSON)
assert.NilError(c, err)
if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
} else {
assert.Assert(c, res.StatusCode != http.StatusOK)
}
assert.Equal(c, res.StatusCode, http.StatusBadRequest)

b, err := request.ReadBody(body)
assert.NilError(c, err)
Expand Down Expand Up @@ -836,11 +809,7 @@ func (s *DockerAPISuite) TestCreateWithTooLowMemoryLimit(c *testing.T) {
b, err2 := request.ReadBody(body)
assert.Assert(c, err2 == nil)

if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
} else {
assert.Assert(c, res.StatusCode != http.StatusOK)
}
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
assert.Assert(c, strings.Contains(string(b), "Minimum memory limit allowed is 6MB"))
}

Expand Down Expand Up @@ -1030,11 +999,7 @@ func (s *DockerAPISuite) TestContainerAPICopyResourcePathEmptyPre124(c *testing.

res, body, err := request.Post(testutil.GetContext(c), "/v1.23/containers/"+name+"/copy", request.JSONBody(postData))
assert.NilError(c, err)
if versions.GreaterThanOrEqualTo(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
} else {
assert.Assert(c, res.StatusCode != http.StatusOK)
}
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Assert(c, is.Regexp("^Path cannot be empty\n$", string(b)))
Expand All @@ -1051,11 +1016,7 @@ func (s *DockerAPISuite) TestContainerAPICopyResourcePathNotFoundPre124(c *testi

res, body, err := request.Post(testutil.GetContext(c), "/v1.23/containers/"+name+"/copy", request.JSONBody(postData))
assert.NilError(c, err)
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusInternalServerError)
} else {
assert.Equal(c, res.StatusCode, http.StatusNotFound)
}
assert.Equal(c, res.StatusCode, http.StatusNotFound)
b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Assert(c, is.Regexp("^Could not find the file /notexist in container "+name+"\n$", string(b)))
Expand Down Expand Up @@ -1487,11 +1448,7 @@ func (s *DockerAPISuite) TestPostContainersCreateMemorySwappinessHostConfigOmitt
containerJSON, err := apiClient.ContainerInspect(testutil.GetContext(c), ctr.ID)
assert.NilError(c, err)

if versions.LessThan(testEnv.DaemonAPIVersion(), "1.31") {
assert.Equal(c, *containerJSON.HostConfig.MemorySwappiness, int64(-1))
} else {
assert.Assert(c, containerJSON.HostConfig.MemorySwappiness == nil)
}
assert.Assert(c, containerJSON.HostConfig.MemorySwappiness == nil)
}

// check validation is done daemon side and not only in cli
Expand Down Expand Up @@ -1954,13 +1911,8 @@ func (s *DockerAPISuite) TestContainersAPICreateMountsCreate(c *testing.T) {
}

var selinuxSharedLabel string
// this test label was added after a bug fix in 1.32, thus add requirements min API >= 1.32
// for the sake of making test pass in earlier versions
// bug fixed in https://github.com/moby/moby/pull/34684
if !versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
if runtime.GOOS == "linux" {
selinuxSharedLabel = "z"
}
if runtime.GOOS == "linux" {
selinuxSharedLabel = "z"
}

cases := []testCase{
Expand Down
7 changes: 1 addition & 6 deletions integration-cli/docker_api_exec_resize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"sync"
"testing"

"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/testutil"
"github.com/docker/docker/testutil/request"
Expand All @@ -26,11 +25,7 @@ func (s *DockerAPISuite) TestExecResizeAPIHeightWidthNoInt(c *testing.T) {
endpoint := "/exec/" + cleanedContainerID + "/resize?h=foo&w=bar"
res, _, err := request.Post(testutil.GetContext(c), endpoint)
assert.NilError(c, err)
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusInternalServerError)
} else {
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
}
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
}

// Part of #14845
Expand Down
19 changes: 3 additions & 16 deletions integration-cli/docker_api_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/client"
"github.com/docker/docker/integration-cli/checker"
"github.com/docker/docker/integration-cli/cli"
Expand All @@ -31,11 +30,7 @@ func (s *DockerAPISuite) TestExecAPICreateNoCmd(c *testing.T) {

res, body, err := request.Post(testutil.GetContext(c), fmt.Sprintf("/containers/%s/exec", name), request.JSONBody(map[string]interface{}{"Cmd": nil}))
assert.NilError(c, err)
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusInternalServerError)
} else {
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
}
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Assert(c, strings.Contains(getErrorMessage(c, b), "No exec command specified"), "Expected message when creating exec command with no Cmd specified")
Expand All @@ -52,11 +47,7 @@ func (s *DockerAPISuite) TestExecAPICreateNoValidContentType(c *testing.T) {

res, body, err := request.Post(testutil.GetContext(c), fmt.Sprintf("/containers/%s/exec", name), request.RawContent(io.NopCloser(jsonData)), request.ContentType("test/plain"))
assert.NilError(c, err)
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, res.StatusCode, http.StatusInternalServerError)
} else {
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
}
assert.Equal(c, res.StatusCode, http.StatusBadRequest)
b, err := request.ReadBody(body)
assert.NilError(c, err)
assert.Assert(c, is.Contains(getErrorMessage(c, b), "unsupported Content-Type header (test/plain): must be 'application/json'"))
Expand Down Expand Up @@ -198,11 +189,7 @@ func (s *DockerAPISuite) TestExecAPIStartInvalidCommand(c *testing.T) {
cli.DockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")

id := createExecCmd(c, name, "invalid")
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
startExec(c, id, http.StatusNotFound)
} else {
startExec(c, id, http.StatusBadRequest)
}
startExec(c, id, http.StatusBadRequest)
ctx := testutil.GetContext(c)
waitForExec(ctx, c, id)

Expand Down
1 change: 0 additions & 1 deletion integration-cli/docker_api_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ func (s *DockerAPISuite) TestLogsAPIUntilFutureFollow(c *testing.T) {
}

func (s *DockerAPISuite) TestLogsAPIUntil(c *testing.T) {
testRequires(c, MinimumAPIVersion("1.34"))
const name = "logsuntil"
cli.DockerCmd(c, "run", "--name", name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do echo log$i; sleep 1; done")

Expand Down
16 changes: 1 addition & 15 deletions integration-cli/docker_api_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/testutil"
"github.com/docker/docker/testutil/request"
Expand Down Expand Up @@ -162,11 +161,7 @@ func (s *DockerAPISuite) TestAPINetworkIPAMMultipleBridgeNetworks(c *testing.T)
IPAM: ipam1,
},
}
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
createNetwork(c, config1, http.StatusInternalServerError)
} else {
createNetwork(c, config1, http.StatusForbidden)
}
createNetwork(c, config1, http.StatusForbidden)
assert.Assert(c, !isNetworkAvailable(c, "test1"))

ipam2 := &network.IPAM{
Expand Down Expand Up @@ -213,15 +208,6 @@ func createDeletePredefinedNetwork(c *testing.T, name string) {
// Create pre-defined network
config := types.NetworkCreateRequest{Name: name}
expectedStatus := http.StatusForbidden
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.34") {
// In the early test code it uses bool value to represent
// whether createNetwork() is expected to fail or not.
// Therefore, we use negation to handle the same logic after
// the code was changed in https://github.com/moby/moby/pull/35030
// -http.StatusCreated will also be checked as NOT equal to
// http.StatusCreated in createNetwork() function.
expectedStatus = -http.StatusCreated
}
createNetwork(c, config, expectedStatus)
deleteNetwork(c, name, false)
}
Expand Down
27 changes: 7 additions & 20 deletions integration-cli/docker_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"strings"
"testing"

"github.com/docker/docker/api"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/runconfig"
"github.com/docker/docker/testutil"
"github.com/docker/docker/testutil/request"
Expand Down Expand Up @@ -52,21 +50,18 @@ func (s *DockerAPISuite) TestAPIClientVersionOldNotSupported(c *testing.T) {
if testEnv.DaemonInfo.OSType != runtime.GOOS {
c.Skip("Daemon platform doesn't match test platform")
}
if api.MinVersion == api.DefaultVersion {
c.Skip("API MinVersion==DefaultVersion")
}
v := strings.Split(api.MinVersion, ".")
vMinInt, err := strconv.Atoi(v[1])

major, minor, _ := strings.Cut(testEnv.DaemonVersion.MinAPIVersion, ".")
vMinInt, err := strconv.Atoi(minor)
assert.NilError(c, err)
vMinInt--
v[1] = strconv.Itoa(vMinInt)
version := strings.Join(v, ".")
version := fmt.Sprintf("%s.%d", major, vMinInt)

resp, body, err := request.Get(testutil.GetContext(c), "/v"+version+"/version")
assert.NilError(c, err)
defer body.Close()
assert.Equal(c, resp.StatusCode, http.StatusBadRequest)
expected := fmt.Sprintf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", version, api.MinVersion)
expected := fmt.Sprintf("client version %s is too old. Minimum supported API version is %s, please upgrade your client to a newer version", version, testEnv.DaemonVersion.MinAPIVersion)
content, err := io.ReadAll(body)
assert.NilError(c, err)
assert.Equal(c, strings.TrimSpace(string(content)), expected)
Expand All @@ -75,11 +70,7 @@ func (s *DockerAPISuite) TestAPIClientVersionOldNotSupported(c *testing.T) {
func (s *DockerAPISuite) TestAPIErrorJSON(c *testing.T) {
httpResp, body, err := request.Post(testutil.GetContext(c), "/containers/create", request.JSONBody(struct{}{}))
assert.NilError(c, err)
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, httpResp.StatusCode, http.StatusInternalServerError)
} else {
assert.Equal(c, httpResp.StatusCode, http.StatusBadRequest)
}
assert.Equal(c, httpResp.StatusCode, http.StatusBadRequest)
assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "application/json"))
b, err := request.ReadBody(body)
assert.NilError(c, err)
Expand All @@ -92,11 +83,7 @@ func (s *DockerAPISuite) TestAPIErrorPlainText(c *testing.T) {
testRequires(c, DaemonIsLinux)
httpResp, body, err := request.Post(testutil.GetContext(c), "/v1.23/containers/create", request.JSONBody(struct{}{}))
assert.NilError(c, err)
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
assert.Equal(c, httpResp.StatusCode, http.StatusInternalServerError)
} else {
assert.Equal(c, httpResp.StatusCode, http.StatusBadRequest)
}
assert.Equal(c, httpResp.StatusCode, http.StatusBadRequest)
assert.Assert(c, strings.Contains(httpResp.Header.Get("Content-Type"), "text/plain"))
b, err := request.ReadBody(body)
assert.NilError(c, err)
Expand Down
5 changes: 1 addition & 4 deletions integration-cli/docker_cli_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"
"testing"

"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/integration-cli/cli"
"gotest.tools/v3/assert"
"gotest.tools/v3/skip"
Expand Down Expand Up @@ -136,9 +135,7 @@ func (s *DockerCLICommitSuite) TestCommitChange(c *testing.T) {
imageID = strings.TrimSpace(imageID)

expectedEnv := "[DEBUG=true test=1 PATH=/foo]"
// bug fixed in 1.36, add min APi >= 1.36 requirement
// PR record https://github.com/moby/moby/pull/35582
if versions.GreaterThan(testEnv.DaemonAPIVersion(), "1.35") && testEnv.DaemonInfo.OSType != "windows" {
if testEnv.DaemonInfo.OSType != "windows" {
// The ordering here is due to `PATH` being overridden from the container's
// ENV. On windows, the container doesn't have a `PATH` ENV variable so
// the ordering is the same as the cli.
Expand Down
9 changes: 2 additions & 7 deletions integration-cli/docker_cli_ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"
"time"

"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/integration-cli/cli"
"github.com/docker/docker/integration-cli/cli/build"
"github.com/docker/docker/pkg/stringid"
Expand Down Expand Up @@ -223,13 +222,9 @@ func (s *DockerCLIPsSuite) TestPsListContainersFilterStatus(c *testing.T) {
assert.Equal(c, RemoveOutputForExistingElements(containerOut, existingContainers), secondID)

result := cli.Docker(cli.Args("ps", "-a", "-q", "--filter=status=rubbish"), cli.WithTimeout(time.Second*60))
err := "invalid filter 'status=rubbish'"
if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
err = "Unrecognised filter value for status: rubbish"
}
result.Assert(c, icmd.Expected{
ExitCode: 1,
Err: err,
Err: "invalid filter 'status=rubbish'",
})
// Windows doesn't support pausing of containers
if testEnv.DaemonInfo.OSType != "windows" {
Expand Down Expand Up @@ -858,7 +853,7 @@ func (s *DockerCLIPsSuite) TestPsListContainersFilterPorts(c *testing.T) {
}

func (s *DockerCLIPsSuite) TestPsNotShowLinknamesOfDeletedContainer(c *testing.T) {
testRequires(c, DaemonIsLinux, MinimumAPIVersion("1.31"))
testRequires(c, DaemonIsLinux)
existingContainers := ExistingContainerNames(c)

cli.DockerCmd(c, "create", "--name=aaa", "busybox", "top")
Expand Down
Loading

0 comments on commit 2a38569

Please sign in to comment.