Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some unit tests for yurt tunnel #470

Merged
merged 1 commit into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions pkg/profile/profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2021 The OpenYurt Authors.

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 profile

import (
"net/http"
"testing"

"github.com/gorilla/mux"
)

func fakeServer(h http.Handler) error {
err := http.ListenAndServe(":9090", h)
return err
}

func TestInstall(t *testing.T) {
m := mux.NewRouter()
Install(m)
go fakeServer(m)
r, err := http.Get("http://localhost:9090/debug/pprof/")
if err != nil {
t.Error(" failed to send request to fake server")
}

if r.StatusCode != http.StatusOK {
t.Error(err)
}
}
4 changes: 2 additions & 2 deletions pkg/yurttunnel/dns/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func getNodeHostIP(node *v1.Node) (string, error) {

func removeRecordByHostname(records []string, hostname string) (result []string, changed bool) {
result = make([]string, 0, len(records))
for _, v := range result {
for _, v := range records {
if !strings.HasSuffix(v, hostname) {
result = append(result, v)
}
}
return result, len(records) == len(result)
return result, len(records) != len(result)
}

func parseHostnameFromDNSRecord(record string) (string, error) {
Expand Down
285 changes: 285 additions & 0 deletions pkg/yurttunnel/dns/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
/*
Copyright 2021 The OpenYurt Authors.

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 dns

import (
"reflect"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestIsEdgeNode(t *testing.T) {
tests := []struct {
desc string
node *corev1.Node
expect bool
}{
{
desc: "node has edge worker label which equals true",
node: &corev1.Node{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"openyurt.io/is-edge-worker": "true",
},
},
Spec: corev1.NodeSpec{},
Status: corev1.NodeStatus{},
},
expect: true,
},
{
desc: "node has edge worker label which equals false",
node: &corev1.Node{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"openyurt.io/is-edge-worker": "false",
},
},
Spec: corev1.NodeSpec{},
Status: corev1.NodeStatus{},
},
expect: false,
},
{
desc: "node dose not has edge worker label",
node: &corev1.Node{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"yin": "ruixing",
},
},
Spec: corev1.NodeSpec{},
Status: corev1.NodeStatus{},
},
expect: false,
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
act := isEdgeNode(tt.node)
if act != tt.expect {
t.Errorf("the result we want is: %v, but the actual result is: %v\n", tt.expect, act)
}
})
}
}

func TestFormatDnsRecord(t *testing.T) {
var (
ip = "10.10.102.60"
host = "k8s-xing-master"
expect = ip + "\t" + host
)

act := formatDNSRecord(ip, host)
if act != expect {
t.Errorf("the result we want is: %v, but the actual result is: %v\n", expect, act)
}

}

func TestGetNodeHostIP(t *testing.T) {
tests := []struct {
desc string
node *corev1.Node
expect string
}{
{
desc: "get node primary host ip",
node: &corev1.Node{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: corev1.NodeSpec{},
Status: corev1.NodeStatus{
Addresses: []corev1.NodeAddress{
{
Type: corev1.NodeExternalIP,
Address: "205.20.20.2",
},
{
Type: corev1.NodeInternalIP,
Address: "102.10.10.60",
},
{
Type: corev1.NodeHostName,
Address: "k8s-edge-node",
},
},
},
},
expect: "102.10.10.60",
},
{
desc: "get node primary host ip",
node: &corev1.Node{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: corev1.NodeSpec{},
Status: corev1.NodeStatus{
Addresses: []corev1.NodeAddress{
{
Type: corev1.NodeExternalIP,
Address: "205.20.20.2",
},
{
Type: corev1.NodeHostName,
Address: "k8s-edge-node",
},
},
},
},
expect: "205.20.20.2",
},
{
desc: "get node primary host ip",
node: &corev1.Node{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: corev1.NodeSpec{},
Status: corev1.NodeStatus{
Addresses: []corev1.NodeAddress{
{
Type: corev1.NodeHostName,
Address: "k8s-edge-node",
},
},
},
},
expect: "",
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
act, _ := getNodeHostIP(tt.node)
if act != tt.expect {
t.Errorf("the result we want is: %v, but the actual result is: %v\n", tt.expect, act)
}
})
}
}

func TestRemoveRecordByHostname(t *testing.T) {
var (
records = []string{"10.1.218.68\tk8s-xing-61", "10.10.102.60\tk8s-xing-master"}
hostname = "k8s-xing-61"
expect = []string{"10.10.102.60\tk8s-xing-master"}
)

act, changed := removeRecordByHostname(records, hostname)

if !changed && !reflect.DeepEqual(act, records) {
t.Errorf("the result we want is: %v, but the actual result is: %v\n", records, act)
} else if !reflect.DeepEqual(act, expect) {
t.Errorf("the result we want is: %v, but the actual result is: %v\n", expect, act)
}
}

func TestParseHostnameFromDNSRecord(t *testing.T) {
tests := []struct {
desc string
record string
expect string
}{
{
desc: "parse host name from dns record",
record: "10.1.218.68\tk8s-xing-61",
expect: "k8s-xing-61",
},

{
desc: "parse invalid dns recode",
record: "10.10.102.2invalid dns record",
expect: "",
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
act, _ := parseHostnameFromDNSRecord(tt.record)
if act != tt.expect {
t.Errorf("the result we want is: %v, but the actual result is: %v\n", tt.expect, act)
}
})
}
}

func TestAddOrUpdateRecord(t *testing.T) {
tests := []struct {
desc string
records []string
record string
expect []string
}{
//{
// desc: "test add record",
// records: []string{"10.1.218.68\tk8s-xing-61"},
// record: "10.1.10.62\tk8s-xing-62",
// expect: []string{"10.1.218.68\tk8s-xing-61","10.1.10.62\tk8s-xing-62"},
//},
//
//{
// desc: "test update record",
// records: []string{"10.1.218.68\tk8s-xing-61"},
// record: "10.1.10.62\tk8s-xing-61",
// expect: []string{"10.1.10.62\tk8s-xing-61"},
//},

{
desc: "test idempotence",
records: []string{"10.1.218.68\tk8s-xing-61"},
record: "10.1.10.62\tk8s-xing-61",
expect: []string{"10.1.218.68\tk8s-xing-61"},
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
act, _, err := addOrUpdateRecord(tt.records, tt.record)
if err != nil {
t.Error(err)
}
if stringSliceEqual(act, tt.expect) {
t.Errorf("the result we want is: %v, but the actual result is: %v\n", tt.expect, act)
}
})
}
}

func stringSliceEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}

if (a == nil) != (b == nil) {
return false
}

for i, v := range a {
if v != b[i] {
return false
}
}

return true
}
Loading