Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli/compose/loader: use gotest.tools/v3/golden #4610

Merged
merged 2 commits into from
Oct 20, 2023

Conversation

thaJeztah
Copy link
Member

use the golden utility instead of self-crafting expected output, this allows automaticaly updating the expected output.

- What I did

- How I did it

- How to verify it

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah
Copy link
Member Author

Let me double-check if there was a reason we were using a template for this (to substitute paths); was this for Mac/Linux/Windows?

diff --git a/cli/compose/loader/testdata/full-example.json.golden b/cli/compose/loader/testdata/full-example.json.golden
index cea8ac5ef..3c61d6458 100644
--- a/cli/compose/loader/testdata/full-example.json.golden
+++ b/cli/compose/loader/testdata/full-example.json.golden
@@ -1,7 +1,7 @@
 {
   "configs": {
     "config1": {
-      "file": "/foo/config_data",
+      "file": "%s/config_data",
       "external": false,
       "labels": {
         "foo": "bar"
@@ -17,7 +17,7 @@
     },
     "config4": {
       "name": "foo",
-      "file": "/foo",
+      "file": "%s",
       "external": false
     }
   },
@@ -61,7 +61,7 @@
   },
   "secrets": {
     "secret1": {
-      "file": "/foo/secret_data",
+      "file": "%s/secret_data",
       "external": false,
       "labels": {
         "foo": "bar"
@@ -77,7 +77,7 @@
     },
     "secret4": {
       "name": "bar",
-      "file": "/foo",
+      "file": "%s",
       "external": false
     }
   },
@@ -472,7 +472,7 @@
         },
         {
           "type": "bind",
-          "source": "/foo/static",
+          "source": "%s",
           "target": "/var/www/html"
         },
         {
@@ -488,7 +488,7 @@
         },
         {
           "type": "bind",
-          "source": "/foo/opt",
+          "source": "%s",
           "target": "/opt",
           "consistency": "cached"
         },
diff --git a/cli/compose/loader/testdata/full-example.yaml.golden b/cli/compose/loader/testdata/full-example.yaml.golden
index d131dd816..aa2470e02 100644
--- a/cli/compose/loader/testdata/full-example.yaml.golden
+++ b/cli/compose/loader/testdata/full-example.yaml.golden
@@ -274,7 +274,7 @@ services:
       source: /foo
       target: /code
     - type: bind
-      source: /foo/static
+      source: %s
       target: /var/www/html
     - type: bind
       source: /bar/configs
@@ -284,7 +284,7 @@ services:
       source: datavolume
       target: /var/lib/mysql
     - type: bind
-      source: /foo/opt
+      source: %s
       target: /opt
       consistency: cached
     - type: tmpfs
@@ -376,7 +376,7 @@ volumes:
   some-volume: {}
 secrets:
   secret1:
-    file: /foo/secret_data
+    file: %s/secret_data
     labels:
       foo: bar
   secret2:
@@ -387,12 +387,12 @@ secrets:
     external: true
   secret4:
     name: bar
-    file: /foo
+    file: %s
     x-bar: baz
     x-foo: bar
 configs:
   config1:
-    file: /foo/config_data
+    file: %s/config_data
     labels:
       foo: bar
   config2:
@@ -403,7 +403,7 @@ configs:
     external: true
   config4:
     name: foo
-    file: /foo
+    file: %s
     x-bar: baz
     x-foo: bar
 x-bar: baz

Comment on lines -971 to -976
filepath.Join(workingDir, "static"),
filepath.Join(workingDir, "opt"),
workingDir,
workingDir,
workingDir,
workingDir)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right; perhaps this was for running the tests on Windows? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks to be indeed; 0cf2e63 (#905)

However, I don't think any of the code is converting these paths; these paths are also "daemon-side", so no conversion is done; there's also other paths in the same file that are unix-style (forward slashes), so I think we should be fine here.

Would be good perhaps if someone could double-check the unit-tests in this package on a Windows machine.

@codecov-commenter
Copy link

codecov-commenter commented Oct 19, 2023

Codecov Report

Merging #4610 (f2fced4) into master (4d6cf13) will not change coverage.
The diff coverage is n/a.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4610   +/-   ##
=======================================
  Coverage   59.72%   59.72%           
=======================================
  Files         288      288           
  Lines       24846    24846           
=======================================
  Hits        14839    14839           
  Misses       9120     9120           
  Partials      887      887           

Comment on lines -375 to +373
{Source: filepath.Join(workingDir, "static"), Target: "/var/www/html", Type: "bind"},
{Source: workingDir + "/static", Target: "/var/www/html", Type: "bind"},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub is hiding changes in this file 😞

@vvoland
Copy link
Contributor

vvoland commented Oct 20, 2023

The tests you modified seems to pass on Windows (they fail before this PR):

PS C:\Users\Pawel\Desktop\cli\cli\compose\loader> go test -v . -run '(TestJSONMarshallConfig|TestMarshallConfig)'
=== RUN   TestMarshallConfig
--- PASS: TestMarshallConfig (0.01s)
=== RUN   TestJSONMarshallConfig
--- PASS: TestJSONMarshallConfig (0.01s)
PASS
ok      github.com/docker/cli/cli/compose/loader        0.282s
Failures before this PR
PS C:\Users\Pawel\Desktop\cli\cli\compose\loader> go test .
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:8001:8001/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5000:5000/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5001:5001/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5002:5002/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5003:5003/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5004:5004/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5005:5005/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5006:5006/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5007:5007/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5008:5008/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5009:5009/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5010:5010/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:34:11+02:00" level=warning msg="network other-external-network: network.external.name is deprecated in favor of network.name"
time="2023-10-20T09:34:11+02:00" level=warning msg="volume other-external-volume: volume.external.name is deprecated in favor of volume.name"
time="2023-10-20T09:34:11+02:00" level=warning msg="secret secret2: secret.external.name is deprecated in favor of secret.name"
time="2023-10-20T09:34:11+02:00" level=warning msg="config config2: config.external.name is deprecated in favor of config.name"
--- FAIL: TestMarshallConfig (0.01s)
    types_test.go:20: assertion failed:
        --- expected
        +++ →
        @@ -377,5 +377,5 @@
         secrets:
           secret1:
        -    file: /foo/secret_data
        +    file: \foo\secret_data
             labels:
               foo: bar
        @@ -393,5 +393,5 @@
         configs:
           config1:
        -    file: /foo/config_data
        +    file: \foo\config_data
             labels:
               foo: bar

--- FAIL: TestJSONMarshallConfig (0.00s)
    types_test.go:37: assertion failed:
        --- expected
        +++ →
        @@ -2,5 +2,5 @@
           "configs": {
             "config1": {
        -      "file": "/foo/config_data",
        +      "file": "\\foo\\config_data",
               "external": false,
               "labels": {
        @@ -62,5 +62,5 @@
           "secrets": {
             "secret1": {
        -      "file": "/foo/secret_data",
        +      "file": "\\foo\\secret_data",
               "external": false,
               "labels": {
        @@ -473,5 +473,5 @@
                 {
                   "type": "bind",
        -          "source": "\foo\static",
        +          "source": "\\foo\\static",
                   "target": "/var/www/html"
                 },
        @@ -489,5 +489,5 @@
                 {
                   "type": "bind",
        -          "source": "\foo\opt",
        +          "source": "\\foo\\opt",
                   "target": "/opt",
                   "consistency": "cached"

    types_test.go:40: assertion failed: error is not nil: yaml: line 475: found unknown escape character
FAIL
FAIL    github.com/docker/cli/cli/compose/loader        0.422s
FAIL

however, TestFullExample fails (this one was passing before this PR):

=== RUN   TestFullExample
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:8001:8001/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5000:5000/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5001:5001/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5002:5002/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5003:5003/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5004:5004/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5005:5005/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5006:5006/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5007:5007/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5008:5008/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5009:5009/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="ignoring IP-address (127.0.0.1:5010:5010/tcp) service will listen on '0.0.0.0'"
time="2023-10-20T09:26:14+02:00" level=warning msg="network other-external-network: network.external.name is deprecated in favor of network.name"
time="2023-10-20T09:26:14+02:00" level=warning msg="volume other-external-volume: volume.external.name is deprecated in favor of volume.name"
time="2023-10-20T09:26:14+02:00" level=warning msg="secret secret2: secret.external.name is deprecated in favor of secret.name"
time="2023-10-20T09:26:14+02:00" level=warning msg="config config2: config.external.name is deprecated in favor of config.name"
    loader_test.go:980: assertion failed:
        --- expected.Services
        +++ config.Services
          types.Services{
                {
                        ... // 49 identical fields
                        User:       "someone",
                        UserNSMode: "",
                        Volumes: []types.ServiceVolumeConfig{
                                {Type: "volume", Target: "/var/lib/mysql"},
                                {Type: "bind", Source: "/opt/data", Target: "/var/lib/mysql"},
                                {Type: "bind", Source: `C:\Users\Pawel\Desktop\cli\cli\compose\loader`, Target: "/code"},
                                {
                                        Type: "bind",
                                        Source: strings.Join({
                                                `C:\Users\Pawel\Desktop\cli\cli\compose\loader`,
        -                                       "/",
        +                                       `\`,
                                                "static",
                                        }, ""),
                                        Target:   "/var/www/html",
                                        ReadOnly: false,
                                        ... // 5 identical fields
                                },
                                {Type: "bind", Source: "/home/foo/configs", Target: "/etc/configs/", ReadOnly: true, ...},
                                {Type: "volume", Source: "datavolume", Target: "/var/lib/mysql"},
                                {
                                        Type: "bind",
                                        Source: strings.Join({
                                                `C:\Users\Pawel\Desktop\cli\cli\compose\loader`,
        -                                       "/",
        +                                       `\`,
                                                "opt",
                                        }, ""),
                                        Target:   "/opt",
                                        ReadOnly: false,
                                        ... // 5 identical fields
                                },
                                {Type: "tmpfs", Target: "/opt", Tmpfs: &{Size: 10000}},
                                {Type: "cluster", Source: "group:mygroup", Target: "/srv"},
                        },
                        WorkingDir: "/code",
                        Extras:     {"x-bar": string("baz"), "x-foo": string("bar")},
                },
          }

    loader_test.go:983: assertion failed:
        --- expected.Secrets
        +++ config.Secrets
          map[string]types.SecretConfig{
                "secret1": {
                        Name: "",
                        File: strings.Join({
                                `C:\Users\Pawel\Desktop\cli\cli\compose\loader`,
        -                       "/",
        +                       `\`,
                                "secret_data",
                        }, ""),
                        External: {},
                        Labels:   {"foo": "bar"},
                        ... // 4 identical fields
                },
                "secret2": {Name: "my_secret", External: {External: true}},
                "secret3": {Name: "secret3", External: {External: true}},
                "secret4": {Name: "bar", File: `C:\Users\Pawel\Desktop\cli\cli\compose\loader`, Extras: {"x-bar": string("baz"), "x-foo": string("bar")}},
          }

    loader_test.go:984: assertion failed:
        --- expected.Configs
        +++ config.Configs
          map[string]types.ConfigObjConfig{
                "config1": {
                        Name: "",
                        File: strings.Join({
                                `C:\Users\Pawel\Desktop\cli\cli\compose\loader`,
        -                       "/",
        +                       `\`,
                                "config_data",
                        }, ""),
                        External: {},
                        Labels:   {"foo": "bar"},
                        ... // 4 identical fields
                },
                "config2": {Name: "my_config", External: {External: true}},
                "config3": {Name: "config3", External: {External: true}},
                "config4": {Name: "foo", File: `C:\Users\Pawel\Desktop\cli\cli\compose\loader`, Extras: {"x-bar": string("baz"), "x-foo": string("bar")}},
          }

--- FAIL: TestFullExample (0.02s)

@thaJeztah
Copy link
Member Author

The tests you modified seems to pass on Windows (they fail before this PR):

NIce, even better!

I'm guessing the original PR either assumed that these paths were converted, or was previously calling code that assumed that "client" and "daemon" always were the same platform (not taking windows client <--> linux server cases into account)

@thaJeztah
Copy link
Member Author

I guess I could reverse the order of the commits, and update the test "before moving to use the golden package". Not sure if worth the effort; WDYT?

@vvoland
Copy link
Contributor

vvoland commented Oct 20, 2023

I would say it's not worth the effort and it's fine to fix it after the move to the golden package 😉

@thaJeztah
Copy link
Member Author

@vvoland is that a "LGTM"? 😅

@thaJeztah
Copy link
Member Author

Nevermind; I somehow glanced over that the second test failed after this and thought the comment described the "before this PR";

however, TestFullExample fails (this one was passing before this PR):

use the golden utility instead of self-crafting expected output,
this allows automaticaly updating the expected output.

This change does break this specific test on Windows due to platform-
specific paths. Other tests already have this issue on Windows, so
skipping the test for now.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Paths in the advanced / compose-file format are not converted
to be platform-specific, so for these tests, it should not be
needed to convert the paths to be Windows-paths.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah
Copy link
Member Author

I updated TestFullExample to skip the test on Windows (for now); we're not currently running this test on Windows in CI, and having the golden files reduces effort on maintaining the test, so is probably an "ok" trade-off for now.

Copy link
Contributor

@vvoland vvoland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thaJeztah thaJeztah merged commit 3e5f6ba into docker:master Oct 20, 2023
74 checks passed
@thaJeztah thaJeztah deleted the compose_golden branch October 20, 2023 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants