forked from AliyunContainerService/terway
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: l1b0k <[email protected]>
- Loading branch information
Showing
5 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |