Skip to content

Commit

Permalink
update ut
Browse files Browse the repository at this point in the history
Signed-off-by: l1b0k <[email protected]>
  • Loading branch information
l1b0k committed Jan 3, 2025
1 parent e8c2532 commit d3483c4
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/ip/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package ip

import (
"net"
"net/netip"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_ipIntersect(t *testing.T) {
Expand Down Expand Up @@ -53,3 +56,23 @@ func Test_ipIntersect(t *testing.T) {
})
}
}

func TestIPAddrs2str_MultipleValidIPs_ReturnsCorrectStrings(t *testing.T) {
ip1, _ := netip.ParseAddr("192.0.2.1")
ip2, _ := netip.ParseAddr("192.0.2.2")
input := []netip.Addr{ip1, ip2}
expected := []string{"192.0.2.1", "192.0.2.2"}
result := IPAddrs2str(input)
if len(result) != len(expected) {
t.Errorf("Expected %v, got %v", expected, result)
}
for i := range expected {
if result[i] != expected[i] {
t.Errorf("Expected %v, got %v", expected, result)
}
}
}

func TestDeriveGatewayIP(t *testing.T) {
assert.Equal(t, "192.168.0.253", DeriveGatewayIP("192.168.0.0/24"))
}
60 changes: 60 additions & 0 deletions types/daemon/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/AliyunContainerService/terway/types"
)

func Test_MergeConfigAndUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -105,3 +107,61 @@ func TestGetAddonSecret(t *testing.T) {
assert.Equal(t, "key", ak)
assert.Equal(t, "secret", sk)
}

func TestGetVSwitchIDsReturnsAllVSwitchIDs(t *testing.T) {
cfg := &Config{
VSwitches: map[string][]string{
"zone-a": {"vsw-1", "vsw-2"},
"zone-b": {"vsw-3"},
},
}
vsws := cfg.GetVSwitchIDs()
assert.ElementsMatch(t, []string{"vsw-1", "vsw-2", "vsw-3"}, vsws)
}

func TestGetVSwitchIDsReturnsEmptyWhenNoVSwitches(t *testing.T) {
cfg := &Config{
VSwitches: map[string][]string{},
}
vsws := cfg.GetVSwitchIDs()
assert.Empty(t, vsws)
}

func TestGetExtraRoutesReturnsAllRoutes(t *testing.T) {
cfg := &Config{
VSwitches: map[string][]string{
"zone-a": {"vsw-1", "vsw-2"},
"zone-b": {"vsw-3"},
},
}
routes := cfg.GetExtraRoutes()
assert.ElementsMatch(t, []string{"vsw-1", "vsw-2", "vsw-3"}, routes)
}

func TestGetExtraRoutesReturnsEmptyWhenNoRoutes(t *testing.T) {
cfg := &Config{
VSwitches: map[string][]string{},
}
routes := cfg.GetExtraRoutes()
assert.Empty(t, routes)
}

func TestPopulateSetsDefaultValues(t *testing.T) {
cfg := &Config{}
cfg.Populate()
assert.Equal(t, 1.0, cfg.EniCapRatio)
assert.Equal(t, VSwitchSelectionPolicyRandom, cfg.VSwitchSelectionPolicy)
assert.Equal(t, string(types.IPStackIPv4), cfg.IPStack)
}

func TestPopulateDoesNotOverrideExistingValues(t *testing.T) {
cfg := &Config{
EniCapRatio: 0.5,
VSwitchSelectionPolicy: "custom",
IPStack: string(types.IPStackDual),
}
cfg.Populate()
assert.Equal(t, 0.5, cfg.EniCapRatio)
assert.Equal(t, "custom", cfg.VSwitchSelectionPolicy)
assert.Equal(t, string(types.IPStackDual), cfg.IPStack)
}
56 changes: 56 additions & 0 deletions types/daemon/dynamicconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package daemon

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestConfigFromConfigMapReturnsErrorWhenBaseConfigMapNotFound(t *testing.T) {
client := fake.NewFakeClient()
_, err := ConfigFromConfigMap(context.Background(), client, "")
assert.Error(t, err)
}

func TestConfigFromConfigMapReturnsErrorWhenBaseConfigIsEmpty(t *testing.T) {
client := fake.NewFakeClient(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "eni-config",
Namespace: "kube-system",
},
Data: map[string]string{"eni_conf": ""},
})
_, err := ConfigFromConfigMap(context.Background(), client, "")
assert.Error(t, err)
}

func TestConfigFromConfigMapReturnsConfigWhenNodeNameIsNotEmpty(t *testing.T) {
client := fake.NewFakeClient(
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "eni-config",
Namespace: "kube-system",
},
Data: map[string]string{"eni_conf": `{"version": "1"}`},
},
)
cfg, err := ConfigFromConfigMap(context.Background(), client, "node-1")
assert.NoError(t, err)
assert.Equal(t, "1", cfg.Version)
}

func TestConfigFromConfigMapReturnsErrorWhenSecurityGroupsExceedLimit(t *testing.T) {
client := fake.NewFakeClient(&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "eni-config",
Namespace: "kube-system",
},
Data: map[string]string{"eni_conf": `{"security_groups": ["sg-1", "sg-2", "sg-3", "sg-4", "sg-5", "sg-6"]}`},
})
_, err := ConfigFromConfigMap(context.Background(), client, "")
assert.Error(t, err)
}
134 changes: 134 additions & 0 deletions types/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package types

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/AliyunContainerService/terway/rpc"
)

func TestBuildIPNet_EmptyInputs_ReturnsEmptyIPNetSet(t *testing.T) {
ipNetSet, err := BuildIPNet(nil, nil)
assert.NoError(t, err)
assert.NotNil(t, ipNetSet)
assert.Nil(t, ipNetSet.IPv4)
assert.Nil(t, ipNetSet.IPv6)
}

func TestBuildIPNet_PartiallyEmptyInputs_ReturnsEmptyIPNetSet(t *testing.T) {
ip := &rpc.IPSet{IPv4: "192.168.1.1", IPv6: "2001:db8::1"}
ipNetSet, err := BuildIPNet(ip, nil)
assert.NoError(t, err)
assert.NotNil(t, ipNetSet)
assert.Nil(t, ipNetSet.IPv4)
assert.Nil(t, ipNetSet.IPv6)

subnet := &rpc.IPSet{IPv4: "192.168.1.0/24", IPv6: "2001:db8::/64"}
ipNetSet, err = BuildIPNet(nil, subnet)
assert.NoError(t, err)
assert.NotNil(t, ipNetSet)
assert.Nil(t, ipNetSet.IPv4)
assert.Nil(t, ipNetSet.IPv6)
}

func TestBuildIPNet_ValidInputs_ReturnsCorrectIPNetSet(t *testing.T) {
ip := &rpc.IPSet{IPv4: "192.168.1.1", IPv6: "2001:db8::1"}
subnet := &rpc.IPSet{IPv4: "192.168.1.0/24", IPv6: "2001:db8::/64"}
ipNetSet, err := BuildIPNet(ip, subnet)
assert.NoError(t, err)
assert.NotNil(t, ipNetSet)
assert.NotNil(t, ipNetSet.IPv4)
assert.NotNil(t, ipNetSet.IPv6)
assert.Equal(t, "192.168.1.1/24", ipNetSet.IPv4.String())
assert.Equal(t, "2001:db8::1/64", ipNetSet.IPv6.String())
}

func TestBuildIPNet_InvalidIP_ReturnsError(t *testing.T) {
ip := &rpc.IPSet{IPv4: "invalid", IPv6: "2001:db8::1"}
subnet := &rpc.IPSet{IPv4: "192.168.1.0/24", IPv6: "2001:db8::/64"}
ipNetSet, err := BuildIPNet(ip, subnet)
assert.Error(t, err)
assert.Nil(t, ipNetSet)
}

func TestBuildIPNet_InvalidSubnet_ReturnsError(t *testing.T) {
ip := &rpc.IPSet{IPv4: "192.168.1.1", IPv6: "2001:db8::1"}
subnet := &rpc.IPSet{IPv4: "invalid", IPv6: "2001:db8::/64"}
ipNetSet, err := BuildIPNet(ip, subnet)
assert.Error(t, err)
assert.Nil(t, ipNetSet)
}

func TestBuildIPNet_OnlyIPv4_ReturnsCorrectIPNetSet(t *testing.T) {
ip := &rpc.IPSet{IPv4: "192.168.1.1"}
subnet := &rpc.IPSet{IPv4: "192.168.1.0/24"}
ipNetSet, err := BuildIPNet(ip, subnet)
assert.NoError(t, err)
assert.NotNil(t, ipNetSet)
assert.NotNil(t, ipNetSet.IPv4)
assert.Nil(t, ipNetSet.IPv6)
assert.Equal(t, "192.168.1.1/24", ipNetSet.IPv4.String())
}

func TestBuildIPNet_OnlyIPv6_ReturnsCorrectIPNetSet(t *testing.T) {
ip := &rpc.IPSet{IPv6: "2001:db8::1"}
subnet := &rpc.IPSet{IPv6: "2001:db8::/64"}
ipNetSet, err := BuildIPNet(ip, subnet)
assert.NoError(t, err)
assert.NotNil(t, ipNetSet)
assert.Nil(t, ipNetSet.IPv4)
assert.NotNil(t, ipNetSet.IPv6)
assert.Equal(t, "2001:db8::1/64", ipNetSet.IPv6.String())
}

func TestToIPNetSetReturnsErrorWhenIPIsNil(t *testing.T) {
ipNetSet, err := ToIPNetSet(nil)
assert.Error(t, err)
assert.Nil(t, ipNetSet)
}

func TestToIPNetSetReturnsCorrectIPNetSetWhenValidIPv4(t *testing.T) {
ip := &rpc.IPSet{IPv4: "192.168.1.0/24"}
ipNetSet, err := ToIPNetSet(ip)
assert.NoError(t, err)
assert.NotNil(t, ipNetSet)
assert.NotNil(t, ipNetSet.IPv4)
assert.Nil(t, ipNetSet.IPv6)
assert.Equal(t, "192.168.1.0/24", ipNetSet.IPv4.String())
}

func TestToIPNetSetReturnsCorrectIPNetSetWhenValidIPv6(t *testing.T) {
ip := &rpc.IPSet{IPv6: "2001:db8::/64"}
ipNetSet, err := ToIPNetSet(ip)
assert.NoError(t, err)
assert.NotNil(t, ipNetSet)
assert.Nil(t, ipNetSet.IPv4)
assert.NotNil(t, ipNetSet.IPv6)
assert.Equal(t, "2001:db8::/64", ipNetSet.IPv6.String())
}

func TestToIPNetSetReturnsCorrectIPNetSetWhenValidIPv4AndIPv6(t *testing.T) {
ip := &rpc.IPSet{IPv4: "192.168.1.0/24", IPv6: "2001:db8::/64"}
ipNetSet, err := ToIPNetSet(ip)
assert.NoError(t, err)
assert.NotNil(t, ipNetSet)
assert.NotNil(t, ipNetSet.IPv4)
assert.NotNil(t, ipNetSet.IPv6)
assert.Equal(t, "192.168.1.0/24", ipNetSet.IPv4.String())
assert.Equal(t, "2001:db8::/64", ipNetSet.IPv6.String())
}

func TestToIPNetSetReturnsErrorWhenInvalidIPv4(t *testing.T) {
ip := &rpc.IPSet{IPv4: "invalid"}
ipNetSet, err := ToIPNetSet(ip)
assert.Error(t, err)
assert.Nil(t, ipNetSet)
}

func TestToIPNetSetReturnsErrorWhenInvalidIPv6(t *testing.T) {
ip := &rpc.IPSet{IPv6: "invalid"}
ipNetSet, err := ToIPNetSet(ip)
assert.Error(t, err)
assert.Nil(t, ipNetSet)
}
32 changes: 32 additions & 0 deletions types/secret/secret_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package secret

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestStringReturnsMaskedValue(t *testing.T) {
s := Secret("mysecret")
assert.Equal(t, "******", s.String())
assert.Equal(t, "mysecret", string((s)))
}

func TestGoStringReturnsMaskedValue(t *testing.T) {
s := Secret("mysecret")
assert.Equal(t, "******", s.GoString())
}

func TestMarshalJSONReturnsMaskedValue(t *testing.T) {
s := Secret("mysecret")
json, err := s.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, `"******"`, string(json))
}

func TestMarshalJSONHandlesEmptySecret(t *testing.T) {
s := Secret("")
json, err := s.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, `"******"`, string(json))
}

0 comments on commit d3483c4

Please sign in to comment.