Skip to content

Commit

Permalink
test: add test cases about toCNIPortMappings
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <[email protected]>
  • Loading branch information
starnop committed Aug 6, 2018
1 parent 3afedd0 commit 175bc4a
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
86 changes: 86 additions & 0 deletions cri/v1alpha1/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/utils"

"github.com/cri-o/ocicni/pkg/ocicni"
"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
)
Expand Down Expand Up @@ -1585,3 +1586,88 @@ func Test_modifySandboxNamespaceOptions(t *testing.T) {
})
}
}

// CNI Network related unit tests.
func Test_toCNIPortMappings(t *testing.T) {
criNormalTCP := &runtime.PortMapping{
Protocol: runtime.Protocol_TCP,
ContainerPort: 8080,
HostPort: 80,
HostIp: "192.168.1.101",
}
pouchNormalTCP := ocicni.PortMapping{
Protocol: "tcp",
ContainerPort: 8080,
HostPort: 80,
HostIP: "192.168.1.101",
}
criNormalUDP := &runtime.PortMapping{
Protocol: runtime.Protocol_UDP,
ContainerPort: 8080,
HostPort: 80,
HostIp: "192.168.1.102",
}
pouchNormalUDP := ocicni.PortMapping{
Protocol: "udp",
ContainerPort: 8080,
HostPort: 80,
HostIP: "192.168.1.102",
}
criHostPortLEZero := &runtime.PortMapping{
Protocol: runtime.Protocol_TCP,
ContainerPort: 8080,
HostPort: 0,
HostIp: "192.168.1.100",
}

type args struct {
criPortMappings []*runtime.PortMapping
}
tests := []struct {
name string
args args
want []ocicni.PortMapping
}{
{
name: "Normal Test",
args: args{
criPortMappings: []*runtime.PortMapping{
criNormalTCP,
criNormalUDP,
},
},
want: []ocicni.PortMapping{
pouchNormalTCP,
pouchNormalUDP,
},
},
{
name: "HostPort LE Zero Test",
args: args{
criPortMappings: []*runtime.PortMapping{
criNormalTCP,
criNormalUDP,
criHostPortLEZero,
},
},
want: []ocicni.PortMapping{
pouchNormalTCP,
pouchNormalUDP,
},
},
{
name: "Nil Test",
args: args{
criPortMappings: []*runtime.PortMapping{},
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := toCNIPortMappings(tt.args.criPortMappings); !reflect.DeepEqual(got, tt.want) {
t.Errorf("toCNIPortMappings() = %v, want %v", got, tt.want)
}
})
}
}
86 changes: 86 additions & 0 deletions cri/v1alpha2/cri_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/utils"

"github.com/cri-o/ocicni/pkg/ocicni"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -1455,3 +1456,88 @@ func Test_modifySandboxNamespaceOptions(t *testing.T) {
})
}
}

// CNI Network related unit tests.
func Test_toCNIPortMappings(t *testing.T) {
criNormalTCP := &runtime.PortMapping{
Protocol: runtime.Protocol_TCP,
ContainerPort: 8080,
HostPort: 80,
HostIp: "192.168.1.101",
}
pouchNormalTCP := ocicni.PortMapping{
Protocol: "tcp",
ContainerPort: 8080,
HostPort: 80,
HostIP: "192.168.1.101",
}
criNormalUDP := &runtime.PortMapping{
Protocol: runtime.Protocol_UDP,
ContainerPort: 8080,
HostPort: 80,
HostIp: "192.168.1.102",
}
pouchNormalUDP := ocicni.PortMapping{
Protocol: "udp",
ContainerPort: 8080,
HostPort: 80,
HostIP: "192.168.1.102",
}
criHostPortLEZero := &runtime.PortMapping{
Protocol: runtime.Protocol_TCP,
ContainerPort: 8080,
HostPort: 0,
HostIp: "192.168.1.100",
}

type args struct {
criPortMappings []*runtime.PortMapping
}
tests := []struct {
name string
args args
want []ocicni.PortMapping
}{
{
name: "Normal Test",
args: args{
criPortMappings: []*runtime.PortMapping{
criNormalTCP,
criNormalUDP,
},
},
want: []ocicni.PortMapping{
pouchNormalTCP,
pouchNormalUDP,
},
},
{
name: "HostPort LE Zero Test",
args: args{
criPortMappings: []*runtime.PortMapping{
criNormalTCP,
criNormalUDP,
criHostPortLEZero,
},
},
want: []ocicni.PortMapping{
pouchNormalTCP,
pouchNormalUDP,
},
},
{
name: "Nil Test",
args: args{
criPortMappings: []*runtime.PortMapping{},
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := toCNIPortMappings(tt.args.criPortMappings); !reflect.DeepEqual(got, tt.want) {
t.Errorf("toCNIPortMappings() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 175bc4a

Please sign in to comment.