File: info_linux_test.go

package info (click to toggle)
docker.io 28.5.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 69,048 kB
  • sloc: sh: 5,867; makefile: 863; ansic: 184; python: 162; asm: 159
file content (106 lines) | stat: -rw-r--r-- 3,373 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//go:build !windows

package system

import (
	"encoding/json"
	"io"
	"net/http"
	"testing"

	"github.com/docker/docker/client"
	"github.com/docker/docker/testutil/request"
	"gotest.tools/v3/assert"
	is "gotest.tools/v3/assert/cmp"
)

func TestInfoBinaryCommits(t *testing.T) {
	ctx := setupTest(t)

	t.Run("current", func(t *testing.T) {
		apiClient := testEnv.APIClient()

		info, err := apiClient.Info(ctx)
		assert.NilError(t, err)

		assert.Check(t, info.ContainerdCommit.ID != "N/A")
		assert.Check(t, is.Equal(info.ContainerdCommit.Expected, "")) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.49.

		assert.Check(t, info.InitCommit.ID != "N/A")
		assert.Check(t, is.Equal(info.InitCommit.Expected, "")) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.49.

		assert.Check(t, info.RuncCommit.ID != "N/A")
		assert.Check(t, is.Equal(info.RuncCommit.Expected, "")) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.49.
	})

	// Expected commits are omitted in API 1.49, but should still be included in older versions.
	t.Run("1.48", func(t *testing.T) {
		apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.48"))
		assert.NilError(t, err)

		info, err := apiClient.Info(ctx)
		assert.NilError(t, err)

		assert.Check(t, info.ContainerdCommit.ID != "N/A")
		assert.Check(t, is.Equal(info.ContainerdCommit.Expected, info.ContainerdCommit.ID)) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.49.

		assert.Check(t, info.InitCommit.ID != "N/A")
		assert.Check(t, is.Equal(info.InitCommit.Expected, info.InitCommit.ID)) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.49.

		assert.Check(t, info.RuncCommit.ID != "N/A")
		assert.Check(t, is.Equal(info.RuncCommit.Expected, info.RuncCommit.ID)) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.49.
	})
}

func TestInfoLegacyFields(t *testing.T) {
	ctx := setupTest(t)

	const notPresent = "expected field to not be present"

	tests := []struct {
		name           string
		url            string
		expectedFields map[string]any
	}{
		{
			name: "api v1.49 legacy bridge-nftables",
			url:  "/v1.49/info",
			expectedFields: map[string]any{
				"BridgeNfIp6tables": false,
				"BridgeNfIptables":  false,
			},
		},
		{
			name: "api v1.50 legacy bridge-nftables",
			url:  "/v1.50/info",
			expectedFields: map[string]any{
				"BridgeNfIp6tables": notPresent,
				"BridgeNfIptables":  notPresent,
			},
		},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			res, _, err := request.Get(ctx, tc.url)
			assert.NilError(t, err)
			assert.Equal(t, res.StatusCode, http.StatusOK)
			body, err := io.ReadAll(res.Body)
			assert.NilError(t, err)

			actual := map[string]any{}
			err = json.Unmarshal(body, &actual)
			assert.NilError(t, err, string(body))

			for field, expectedValue := range tc.expectedFields {
				if expectedValue == notPresent {
					_, found := actual[field]
					assert.Assert(t, !found, "field %s should not be present", field)
				} else {
					_, found := actual[field]
					assert.Assert(t, found, "field %s should be present", field)
					assert.Check(t, is.DeepEqual(actual[field], expectedValue))
				}
			}
		})
	}
}