From 50de4adf50e5e956f177512db0bed8e9c9272f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Fri, 10 May 2024 11:30:20 +0200 Subject: [PATCH 1/5] Add unmarshal unit test for default.yaml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders F Björklund --- pkg/limayaml/limayaml_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 pkg/limayaml/limayaml_test.go diff --git a/pkg/limayaml/limayaml_test.go b/pkg/limayaml/limayaml_test.go new file mode 100644 index 00000000000..37f1b7631e4 --- /dev/null +++ b/pkg/limayaml/limayaml_test.go @@ -0,0 +1,16 @@ +package limayaml + +import ( + "os" + "testing" + + "gotest.tools/v3/assert" +) + +func TestDefaultYAML(t *testing.T) { + bytes, err := os.ReadFile("default.yaml") + assert.NilError(t, err) + var y LimaYAML + err = unmarshalYAML(bytes, &y, "") + assert.NilError(t, err) +} From a5f790c3440e9e676e5b3378d08bf76ccb51a045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Fri, 10 May 2024 11:31:17 +0200 Subject: [PATCH 2/5] Add marshal unit test for default.yaml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders F Björklund --- pkg/limayaml/limayaml_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/limayaml/limayaml_test.go b/pkg/limayaml/limayaml_test.go index 37f1b7631e4..a69fe6883f5 100644 --- a/pkg/limayaml/limayaml_test.go +++ b/pkg/limayaml/limayaml_test.go @@ -1,16 +1,46 @@ package limayaml import ( + "encoding/json" "os" "testing" "gotest.tools/v3/assert" ) +func dumpJSON(d interface{}) string { + b, err := json.Marshal(d) + if err != nil { + return "ERROR" + } + return string(b) +} + +const emptyYAML = "images: []\n" + +func TestEmptyYAML(t *testing.T) { + var y LimaYAML + t.Log(dumpJSON(y)) + b, err := marshalYAML(y) + assert.NilError(t, err) + assert.Equal(t, string(b), emptyYAML) +} + +const defaultYAML = `images: [] +ssh: + localPort: 0 +` + func TestDefaultYAML(t *testing.T) { bytes, err := os.ReadFile("default.yaml") assert.NilError(t, err) var y LimaYAML err = unmarshalYAML(bytes, &y, "") assert.NilError(t, err) + y.Images = nil // remove default images + y.Mounts = nil // remove default mounts + t.Log(dumpJSON(y)) + b, err := marshalYAML(y) + assert.NilError(t, err) + assert.Equal(t, string(b), defaultYAML) } From e20f87902f9e8858120286af810c55de01d622dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Fri, 10 May 2024 11:37:25 +0200 Subject: [PATCH 3/5] Use builtin default for ssh.localPort MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders F Björklund --- examples/default.yaml | 2 +- pkg/limayaml/limayaml_test.go | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/default.yaml b/examples/default.yaml index b471f4fed14..36d04ac95f5 100644 --- a/examples/default.yaml +++ b/examples/default.yaml @@ -125,7 +125,7 @@ ssh: # 🟢 Builtin default: 0 (automatically assigned to a free port) # NOTE: when the instance name is "default", the builtin default value is set to # 60022 for backward compatibility. - localPort: 0 + localPort: null # Load ~/.ssh/*.pub in addition to $LIMA_HOME/_config/user.pub . # This option is useful when you want to use other SSH-based # applications such as rsync with the Lima instance. diff --git a/pkg/limayaml/limayaml_test.go b/pkg/limayaml/limayaml_test.go index a69fe6883f5..6692b7402b8 100644 --- a/pkg/limayaml/limayaml_test.go +++ b/pkg/limayaml/limayaml_test.go @@ -26,10 +26,7 @@ func TestEmptyYAML(t *testing.T) { assert.Equal(t, string(b), emptyYAML) } -const defaultYAML = `images: [] -ssh: - localPort: 0 -` +const defaultYAML = "images: []\n" func TestDefaultYAML(t *testing.T) { bytes, err := os.ReadFile("default.yaml") From 1a101d209286b2e2701f75e2afaa3a9b0bab235d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Fri, 10 May 2024 11:38:17 +0200 Subject: [PATCH 4/5] Omit empty values for default rosetta MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Anders F Björklund --- pkg/limayaml/limayaml.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/limayaml/limayaml.go b/pkg/limayaml/limayaml.go index 1a2e5ce5a00..a6761b6cf4d 100644 --- a/pkg/limayaml/limayaml.go +++ b/pkg/limayaml/limayaml.go @@ -72,8 +72,8 @@ const ( ) type Rosetta struct { - Enabled *bool `yaml:"enabled" json:"enabled"` - BinFmt *bool `yaml:"binfmt" json:"binfmt"` + Enabled *bool `yaml:"enabled,omitempty" json:"enabled,omitempty"` + BinFmt *bool `yaml:"binfmt,omitempty" json:"binfmt,omitempty"` } type File struct { From b84dd658c6fb9a6688e336b8ef6edc9aafea9ff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20F=20Bj=C3=B6rklund?= Date: Wed, 17 Jul 2024 12:20:22 +0200 Subject: [PATCH 5/5] Update pkg/limayaml/limayaml_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Oleksandr Redko Signed-off-by: Anders F Björklund --- pkg/limayaml/limayaml_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/limayaml/limayaml_test.go b/pkg/limayaml/limayaml_test.go index 6692b7402b8..b75e9e0b0b5 100644 --- a/pkg/limayaml/limayaml_test.go +++ b/pkg/limayaml/limayaml_test.go @@ -8,10 +8,10 @@ import ( "gotest.tools/v3/assert" ) -func dumpJSON(d interface{}) string { +func dumpJSON(t *testing.T, d interface{}) string { b, err := json.Marshal(d) if err != nil { - return "ERROR" + t.Fatal(err) } return string(b) } @@ -20,7 +20,7 @@ const emptyYAML = "images: []\n" func TestEmptyYAML(t *testing.T) { var y LimaYAML - t.Log(dumpJSON(y)) + t.Log(dumpJSON(t, y)) b, err := marshalYAML(y) assert.NilError(t, err) assert.Equal(t, string(b), emptyYAML) @@ -36,7 +36,7 @@ func TestDefaultYAML(t *testing.T) { assert.NilError(t, err) y.Images = nil // remove default images y.Mounts = nil // remove default mounts - t.Log(dumpJSON(y)) + t.Log(dumpJSON(t, y)) b, err := marshalYAML(y) assert.NilError(t, err) assert.Equal(t, string(b), defaultYAML)