Repository: containernetworking/cni
Pull request #847 (2021-06-23 15:58:44)

types: ensure empty CNIVersion always creates/converts to 0.1.0

coveralls: 
[![Coverage Status](https://coveralls.io/builds/40820946/badge)](https://coveralls.io/builds/40820946)

Coverage increased (+0.7%) to 71.45% when pulling **83a9ff8a0ba2c225735df2a88a2f198e6b5d31ce on dcbw:empty-cniversion-is-010** into **b4e078af7a216436b2f4ddbd70434e8f3e23b550 on containernetworking:master**.

squeed: Seems reasonable. @mars1024 , want to look again?
mars1024: > Seems reasonable. @mars1024 , want to look again?

Also look good to me, approved already :)

Patch:
diff --git a/pkg/types/020/types.go b/pkg/types/020/types.go
index 6d4878c..99b151f 100644
--- a/pkg/types/020/types.go
+++ b/pkg/types/020/types.go
@@ -49,6 +49,9 @@ func NewResult(data []byte) (types.Result, error) {
 	}
 	for _, v := range supportedVersions {
 		if result.CNIVersion == v {
+			if result.CNIVersion == "" {
+				result.CNIVersion = "0.1.0"
+			}
 			return result, nil
 		}
 	}
diff --git a/pkg/types/internal/convert.go b/pkg/types/internal/convert.go
index 11009b4..bdbe4b0 100644
--- a/pkg/types/internal/convert.go
+++ b/pkg/types/internal/convert.go
@@ -53,6 +53,10 @@ func findConverter(fromVersion, toVersion string) *converter {
 // Convert converts a CNI Result to the requested CNI specification version,
 // or returns an error if the conversion could not be performed or failed
 func Convert(from types.Result, toVersion string) (types.Result, error) {
+	if toVersion == "" {
+		toVersion = "0.1.0"
+	}
+
 	fromVersion := from.Version()
 
 	// Shortcut for same version
diff --git a/pkg/version/legacy_examples/examples.go b/pkg/version/legacy_examples/examples.go
index c60a4be..0916e90 100644
--- a/pkg/version/legacy_examples/examples.go
+++ b/pkg/version/legacy_examples/examples.go
@@ -120,6 +120,7 @@ func main() { skel.PluginMain(c, c) }
 // As we change the CNI spec, the Result type and this value may change.
 // The text of the example plugins should not.
 var ExpectedResult = &types020.Result{
+	CNIVersion: "0.1.0",
 	IP4: &types020.IPConfig{
 		IP: net.IPNet{
 			IP:   net.ParseIP("10.1.2.3"),


Test patch:
diff --git a/pkg/types/020/types_test.go b/pkg/types/020/types_test.go
index 36aaadf..48f7e8f 100644
--- a/pkg/types/020/types_test.go
+++ b/pkg/types/020/types_test.go
@@ -21,6 +21,7 @@ import (
 
 	"github.com/containernetworking/cni/pkg/types"
 	"github.com/containernetworking/cni/pkg/types/020"
+	"github.com/containernetworking/cni/pkg/types/create"
 
 	. "github.com/onsi/ginkgo"
 	. "github.com/onsi/gomega"
@@ -144,4 +145,13 @@ var _ = Describe("Ensures compatibility with the 0.1.0/0.2.0 spec", func() {
 		Expect(err).NotTo(HaveOccurred())
 		Expect(out).To(MatchJSON(expectedJSON))
 	})
+
+	It("creates a 0.1.0 result passing CNIVersion ''", func() {
+		_, expectedJSON := testResult("", "")
+		resT, err := create.Create("", []byte(expectedJSON))
+		Expect(err).NotTo(HaveOccurred())
+		res010, ok := resT.(*types020.Result)
+		Expect(ok).To(BeTrue())
+		Expect(res010.CNIVersion).To(Equal("0.1.0"))
+	})
 })
diff --git a/pkg/types/types_test.go b/pkg/types/types_test.go
index 894cdb7..2fb46b4 100644
--- a/pkg/types/types_test.go
+++ b/pkg/types/types_test.go
@@ -19,6 +19,7 @@ import (
 	"net"
 
 	"github.com/containernetworking/cni/pkg/types"
+	current "github.com/containernetworking/cni/pkg/types/100"
 	. "github.com/onsi/ginkgo"
 	. "github.com/onsi/ginkgo/extensions/table"
 	. "github.com/onsi/gomega"
@@ -143,4 +144,52 @@ var _ = Describe("Types", func() {
 			Expect(err).To(Equal(example))
 		})
 	})
+
+	Describe("Result conversion", func() {
+		var result *current.Result
+		BeforeEach(func() {
+			ipv4, err := types.ParseCIDR("1.2.3.30/24")
+			Expect(err).NotTo(HaveOccurred())
+			Expect(ipv4).NotTo(BeNil())
+
+			ipv6, err := types.ParseCIDR("abcd:1234:ffff::cdde/64")
+			Expect(err).NotTo(HaveOccurred())
+			Expect(ipv6).NotTo(BeNil())
+
+			result = &current.Result{
+				CNIVersion: "1.0.0",
+				Interfaces: []*current.Interface{
+					{
+						Name:    "eth0",
+						Mac:     "00:11:22:33:44:55",
+						Sandbox: "/proc/3553/ns/net",
+					},
+				},
+				IPs: []*current.IPConfig{
+					{
+						Interface: current.Int(0),
+						Address:   *ipv4,
+						Gateway:   net.ParseIP("1.2.3.1"),
+					},
+					{
+						Interface: current.Int(0),
+						Address:   *ipv6,
+						Gateway:   net.ParseIP("abcd:1234:ffff::1"),
+					},
+				},
+				DNS: types.DNS{
+					Nameservers: []string{"1.2.3.4", "1::cafe"},
+					Domain:      "acompany.com",
+					Search:      []string{"somedomain.com", "otherdomain.net"},
+					Options:     []string{"foo", "bar"},
+				},
+			}
+		})
+
+		It("can create a CNIVersion '' (0.1.0) result", func() {
+			newResult, err := result.GetAsVersion("")
+			Expect(err).NotTo(HaveOccurred())
+			Expect(newResult.Version()).To(Equal("0.1.0"))
+		})
+	})
 })