diff --git a/Makefile b/Makefile index dd694c7b0..0f87fa84c 100644 --- a/Makefile +++ b/Makefile @@ -233,7 +233,7 @@ test: unittest e2etest ## unittest Run all unit tests .PHONY: unittest -unittest: test_unikontainers test_metrics test_network test_hypervisors +unittest: test_unikontainers test_metrics test_network test_hypervisors test_unikernels ## e2etest Run all end-to-end tests .PHONY: e2etest @@ -263,6 +263,12 @@ test_hypervisors: @GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./pkg/unikontainers/hypervisors -v @echo " " +## test_unikernels Run unit tests for unikernels package +test_unikernels: + @echo "Unit testing in unikernels" + @GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./pkg/unikontainers/unikernels -v + @echo " " + ## test_nerdctl Run all end-to-end tests with nerdctl .PHONY: test_nerdctl test_nerdctl: diff --git a/pkg/unikontainers/unikernels/mewz.go b/pkg/unikontainers/unikernels/mewz.go index 5286ee428..cd512de36 100644 --- a/pkg/unikontainers/unikernels/mewz.go +++ b/pkg/unikontainers/unikernels/mewz.go @@ -36,8 +36,10 @@ type MewzNet struct { } func (m *Mewz) CommandString() (string, error) { - return fmt.Sprintf("ip=%s/%d gateway=%s ", m.Net.Address, m.Net.Mask, - m.Net.Gateway), nil + if m.Net.Address != "" { + return fmt.Sprintf("ip=%s/%d gateway=%s", m.Net.Address, m.Net.Mask, m.Net.Gateway), nil + } + return "", nil } func (m *Mewz) SupportsBlock() bool { diff --git a/pkg/unikontainers/unikernels/mewz_test.go b/pkg/unikontainers/unikernels/mewz_test.go new file mode 100644 index 000000000..c4ac12a07 --- /dev/null +++ b/pkg/unikontainers/unikernels/mewz_test.go @@ -0,0 +1,58 @@ +// Copyright (c) 2023-2026, Nubificus LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package unikernels + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMewzCommandString(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + mewz *Mewz + expected string + }{ + { + name: "no network configured", + mewz: &Mewz{}, + expected: "", + }, + { + name: "with network configured", + mewz: &Mewz{ + Net: MewzNet{ + Address: "10.0.0.2", + Mask: 24, + Gateway: "10.0.0.1", + }, + }, + expected: "ip=10.0.0.2/24 gateway=10.0.0.1", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + result, err := tc.mewz.CommandString() + require.NoError(t, err) + assert.Equal(t, tc.expected, result) + }) + } +}