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 test unit cases for YurtManage and YurtHub modules #2128

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ limitations under the License.

package kubeletcertificate

import "testing"
import (
"crypto/tls"
"crypto/x509"
"testing"
"time"

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

func TestNewKubeletCertManager(t *testing.T) {
testcases := map[string]struct {
Expand Down Expand Up @@ -57,3 +64,62 @@ func TestNewKubeletCertManager(t *testing.T) {
})
}
}

// TestStart tests the Start method of kubeletCertManager
func TestStart(t *testing.T) {
kcm := &kubeletCertManager{}
kcm.Start()
// No assertion needed as Start method does nothing
}

// TestStop tests the Stop method of kubeletCertManager
func TestStop(t *testing.T) {
kcm := &kubeletCertManager{}
kcm.Stop()
// No assertion needed as Stop method does nothing
}

// TestUpdateBootstrapConf tests the UpdateBootstrapConf method of kubeletCertManager
func TestUpdateBootstrapConf(t *testing.T) {
kcm := &kubeletCertManager{}
err := kcm.UpdateBootstrapConf("test")
assert.NoError(t, err)
}

// TestGetHubConfFile tests the GetHubConfFile method of kubeletCertManager
func TestGetHubConfFile(t *testing.T) {
expectedFile := "test.conf"
kcm := &kubeletCertManager{kubeConfFile: expectedFile}
file := kcm.GetHubConfFile()
assert.Equal(t, expectedFile, file)
}

// TestGetCAData tests the GetCAData method of kubeletCertManager
func TestGetCAData(t *testing.T) {
expectedData := []byte("test")
kcm := &kubeletCertManager{caData: expectedData}
data := kcm.GetCAData()
assert.Equal(t, expectedData, data)
}

// TestGetCaFile tests the GetCaFile method of kubeletCertManager
func TestGetCaFile(t *testing.T) {
expectedFile := "test.ca"
kcm := &kubeletCertManager{kubeletCAFile: expectedFile}
file := kcm.GetCaFile()
assert.Equal(t, expectedFile, file)
}

// TestGetAPIServerClientCert tests the GetAPIServerClientCert method of kubeletCertManager
func TestGetAPIServerClientCert(t *testing.T) {
kcm := &kubeletCertManager{
kubeletPemFile: "test.pem",
cert: &tls.Certificate{
Leaf: &x509.Certificate{
NotAfter: time.Now().Add(time.Hour),
},
},
}
cert := kcm.GetAPIServerClientCert()
assert.NotNil(t, cert)
}
126 changes: 126 additions & 0 deletions pkg/yurthub/network/dummyif_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package network

import (
"net"
"testing"
)

func TestEnsureDummyInterface(t *testing.T) {
// Create a new instance of the dummyInterfaceController
dic := NewDummyInterfaceController()

// Define the test input values
ifName := "dummy0"
ifIP := net.ParseIP("192.168.0.1")

// Call the EnsureDummyInterface function
err := dic.EnsureDummyInterface(ifName, ifIP)

// Check if the function returned an error
if err != nil {
t.Errorf("EnsureDummyInterface returned an error: %v", err)
}

// TODO: Add additional assertions to verify the behavior of EnsureDummyInterface
}
func TestEnsureDummyInterface_ExistingInterfaceAndMatchingIP(t *testing.T) {
dic := NewDummyInterfaceController()

// Create a dummy interface with the specified name and IP
ifName := "dummy0"
ifIP := net.ParseIP("192.168.0.1")
err := dic.EnsureDummyInterface(ifName, ifIP)
if err != nil {
t.Fatalf("Failed to create dummy interface: %v", err)
}

// Call the EnsureDummyInterface function
err = dic.EnsureDummyInterface(ifName, ifIP)

// Check if the function returned nil error
if err != nil {
t.Errorf("EnsureDummyInterface returned an error: %v", err)
}
}

func TestEnsureDummyInterface_ExistingInterfaceAndDifferentIP(t *testing.T) {
dic := NewDummyInterfaceController()

// Create a dummy interface with the specified name and IP
ifName := "dummy0"
ifIP := net.ParseIP("192.168.0.1")
err := dic.EnsureDummyInterface(ifName, ifIP)
if err != nil {
t.Fatalf("Failed to create dummy interface: %v", err)
}

// Call the EnsureDummyInterface function with a different IP
newIP := net.ParseIP("192.168.0.2")
err = dic.EnsureDummyInterface(ifName, newIP)

// Check if the function returned nil error
if err != nil {
t.Errorf("EnsureDummyInterface returned an error: %v", err)
}
}

func TestEnsureDummyInterface_NonExistingInterface(t *testing.T) {
dic := NewDummyInterfaceController()

// Define the test input values
ifName := "dummy0"
ifIP := net.ParseIP("192.168.0.1")

// Call the EnsureDummyInterface function
err := dic.EnsureDummyInterface(ifName, ifIP)

// Check if the function returned nil error
if err != nil {
t.Errorf("EnsureDummyInterface returned an error: %v", err)
}
}

func TestDeleteDummyInterface(t *testing.T) {
dic := NewDummyInterfaceController()

// Create a dummy interface with the specified name and IP
ifName := "dummy0"
ifIP := net.ParseIP("192.168.0.1")
err := dic.EnsureDummyInterface(ifName, ifIP)
if err != nil {
t.Fatalf("Failed to create dummy interface: %v", err)
}

// Call the DeleteDummyInterface function
err = dic.DeleteDummyInterface(ifName)

// Check if the function returned nil error
if err != nil {
t.Errorf("DeleteDummyInterface returned an error: %v", err)
}
}

func TestListDummyInterface(t *testing.T) {
dic := NewDummyInterfaceController()

// Create a dummy interface with the specified name and IP
ifName := "dummy0"
ifIP := net.ParseIP("192.168.0.1")
err := dic.EnsureDummyInterface(ifName, ifIP)
if err != nil {
t.Fatalf("Failed to create dummy interface: %v", err)
}

// Call the ListDummyInterface function
ips, err := dic.ListDummyInterface(ifName)

// Check if the function returned nil error
if err != nil {
t.Errorf("ListDummyInterface returned an error: %v", err)
}

// Check if the returned IPs match the expected IP
if len(ips) != 1 || !ips[0].Equal(ifIP) {
t.Errorf("ListDummyInterface returned unexpected IPs: %v", ips)
}
}
39 changes: 39 additions & 0 deletions pkg/yurthub/network/iptables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package network

import (
"testing"

"github.com/openyurtio/openyurt/pkg/util/iptables"
"github.com/stretchr/testify/assert"
)

func TestNewIptablesManager(t *testing.T) {
// Test the creation of a new IptablesManager
dummyIP := "169.254.2.1"
dummyPort := "10261"
manager := NewIptablesManager(dummyIP, dummyPort)
assert.NotNil(t, manager)
}
func TestMakeupIptablesRules(t *testing.T) {
ifIP := "169.254.2.1"
ifPort := "10261"

expectedRules := []iptablesRule{
{iptables.Prepend, iptables.TableFilter, iptables.ChainInput, []string{"-p", "tcp", "-m", "comment", "--comment", "for container access hub agent", "--dport", ifPort, "--destination", ifIP, "-j", "ACCEPT"}},
{iptables.Prepend, iptables.TableFilter, iptables.ChainOutput, []string{"-p", "tcp", "--sport", ifPort, "-s", ifIP, "-j", "ACCEPT"}},
}

actualRules := makeupIptablesRules(ifIP, ifPort)

assert.Equal(t, expectedRules, actualRules)
}
func TestEnsureIptablesRules(t *testing.T) {
manager := NewIptablesManager("169.254.2.1", "10261")
err := manager.EnsureIptablesRules()
assert.NoError(t, err)
}
func TestCleanUpIptablesRules(t *testing.T) {
manager := NewIptablesManager("169.254.2.1", "10261")
err := manager.CleanUpIptablesRules()
assert.NoError(t, err)
}
66 changes: 66 additions & 0 deletions pkg/yurthub/network/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package network

import (
"testing"
"time"

"github.com/openyurtio/openyurt/cmd/yurthub/app/options"
)

// TestNewNetworkManager tests the creation of a new NetworkManager.
func TestNewNetworkManager(t *testing.T) {
opts := &options.YurtHubOptions{
HubAgentDummyIfIP: "127.0.0.1",
YurtHubProxyPort: 10261,
YurtHubProxySecurePort: 10262,
HubAgentDummyIfName: "dummy0",
EnableIptables: true,
}
nm, err := NewNetworkManager(opts)
if err != nil {
t.Fatalf("Failed to create NetworkManager: %v", err)
}
if nm.dummyIfIP.String() != opts.HubAgentDummyIfIP {
t.Errorf("Expected dummyIfIP %s, got %s", opts.HubAgentDummyIfIP, nm.dummyIfIP.String())
}
if nm.dummyIfName != opts.HubAgentDummyIfName {
t.Errorf("Expected dummyIfName %s, got %s", opts.HubAgentDummyIfName, nm.dummyIfName)
}
if nm.enableIptables != opts.EnableIptables {
t.Errorf("Expected enableIptables %t, got %t", opts.EnableIptables, nm.enableIptables)
}
}

// TestRun tests the Run method of NetworkManager.
func TestRun(t *testing.T) {
opts := &options.YurtHubOptions{
HubAgentDummyIfIP: "127.0.0.1",
YurtHubProxyPort: 10261,
YurtHubProxySecurePort: 10262,
HubAgentDummyIfName: "dummy0",
EnableIptables: true,
}
nm, _ := NewNetworkManager(opts)
stopCh := make(chan struct{})
go nm.Run(stopCh)
time.Sleep(1 * time.Second) // Wait for the goroutine to start
close(stopCh) // Send stop signal
// Test is successful if it doesn't hang and exits correctly
}

// TestConfigureNetwork tests the configureNetwork method.
func TestConfigureNetwork(t *testing.T) {
opts := &options.YurtHubOptions{
HubAgentDummyIfIP: "127.0.0.1",
YurtHubProxyPort: 10261,
YurtHubProxySecurePort: 10262,
HubAgentDummyIfName: "dummy0",
EnableIptables: true,
}
nm, _ := NewNetworkManager(opts)
err := nm.configureNetwork()
if err != nil {
t.Errorf("Failed to configure network: %v", err)
}
// Further checks can be added to verify the configuration
}
40 changes: 40 additions & 0 deletions pkg/yurthub/otaupdate/upgrader/static_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package upgrader

import (
"context"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -96,3 +97,42 @@ func Test_genUpgradeManifest(t *testing.T) {
}

}

func TestPreCheck(t *testing.T) {
fakeClientset := fake.NewSimpleClientset()

// Test Case 1: Successful PreCheck
t.Run("success", func(t *testing.T) {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: metav1.NamespaceDefault,
Name: spctrlutil.WithConfigMapPrefix("nginx"),
},
}
_, err := fakeClientset.CoreV1().ConfigMaps(metav1.NamespaceDefault).Create(context.TODO(), cm, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Failed to create configmap: %v", err)
}

ok, staticName, err := PreCheck("nginx-node", "node", metav1.NamespaceDefault, fakeClientset)
if err != nil || !ok || staticName != "nginx" {
t.Errorf("Expected success, got error: %v, ok: %v, staticName: %s", err, ok, staticName)
}
})

// Test Case 2: Pod Name Format Error
t.Run("pod_name_format_error", func(t *testing.T) {
ok, _, err := PreCheck("wrongformat", "node", metav1.NamespaceDefault, fakeClientset)
if err == nil || ok {
t.Errorf("Expected error due to wrong pod name format, got ok: %v, error: %v", ok, err)
}
})

// Test Case 3: ConfigMap NotFound Error
t.Run("configmap_not_found", func(t *testing.T) {
ok, _, err := PreCheck("missingpod-node", "node", metav1.NamespaceDefault, fakeClientset)
if err != nil || ok {
t.Errorf("Expected not found error, got ok: %v, error: %v", ok, err)
}
})
}
Loading