Skip to content

Commit

Permalink
test: add more ut
Browse files Browse the repository at this point in the history
  • Loading branch information
vie-serendipity committed Apr 30, 2024
1 parent d1aefa5 commit 0abbe6a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
40 changes: 18 additions & 22 deletions pkg/yurthub/autonomymanager/autonomy_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,26 @@ func NewAutonomyManager(restConfigMgr *hubrest.RestConfigManager, cacheMgr cache
// utilize queue and wait
func (am *AutonomyManager) Run(ctx context.Context) {
// 1.Consistency Validate every 20 seconds
go func() {
checkTicker := time.NewTicker(time.Second * 20)
defer checkTicker.Stop()
for {
select {
case <-checkTicker.C:
config := am.restConfigMgr.GetRestConfig(true)
if config == nil {
return
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return
}
am.ConsistencyValidate(client)
case <-ctx.Done():
return
}
go wait.JitterUntil(func() {
if am.restConfigMgr == nil {
return
}
}()
config := am.restConfigMgr.GetRestConfig(true)
if config == nil {
return

Check warning on line 76 in pkg/yurthub/autonomymanager/autonomy_manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurthub/autonomymanager/autonomy_manager.go#L74-L76

Added lines #L74 - L76 were not covered by tests
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return

Check warning on line 80 in pkg/yurthub/autonomymanager/autonomy_manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurthub/autonomymanager/autonomy_manager.go#L78-L80

Added lines #L78 - L80 were not covered by tests
}
am.ConsistencyValidate(client)

Check warning on line 82 in pkg/yurthub/autonomymanager/autonomy_manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurthub/autonomymanager/autonomy_manager.go#L82

Added line #L82 was not covered by tests
}, 20*time.Second, 2, true, ctx.Done())

// 2. Sync
go wait.Until(func() {
go wait.JitterUntil(func() {
if am.restConfigMgr == nil {
return
}
config := am.restConfigMgr.GetRestConfig(true)
if config == nil {
return

Check warning on line 92 in pkg/yurthub/autonomymanager/autonomy_manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurthub/autonomymanager/autonomy_manager.go#L90-L92

Added lines #L90 - L92 were not covered by tests
Expand All @@ -99,7 +96,7 @@ func (am *AutonomyManager) Run(ctx context.Context) {
return

Check warning on line 96 in pkg/yurthub/autonomymanager/autonomy_manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurthub/autonomymanager/autonomy_manager.go#L94-L96

Added lines #L94 - L96 were not covered by tests
}
am.Sync(dynamicClient)

Check warning on line 98 in pkg/yurthub/autonomymanager/autonomy_manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurthub/autonomymanager/autonomy_manager.go#L98

Added line #L98 was not covered by tests
}, 20*time.Second, ctx.Done())
}, 20*time.Second, 3, true, ctx.Done())

// 3. Update errorKeys
go func() {
Expand Down Expand Up @@ -205,7 +202,6 @@ func EnsureAutonomyCondition(client kubernetes.Interface, node *v1.Node, oldCond

// Sync fetch object from api server and write it to cache manager
func (am *AutonomyManager) Sync(dynamicClient dynamic.Interface) {

errorKey, quit := am.queue.Get()
if quit {
return

Check warning on line 207 in pkg/yurthub/autonomymanager/autonomy_manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurthub/autonomymanager/autonomy_manager.go#L207

Added line #L207 was not covered by tests
Expand Down
34 changes: 34 additions & 0 deletions pkg/yurthub/autonomymanager/autonomy_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -278,3 +279,36 @@ func TestAutonomyManager_Sync(t *testing.T) {
})
}
}

func TestAutonomyManager_Run(t *testing.T) {
testcases := []struct {
name string
}{
{
name: "test1",
},
}
for _, tc := range testcases {
fakeClient := clientsetfake.NewSimpleClientset()
fakeSharedInformerFactory := informers.NewSharedInformerFactory(fakeClient, 0)
dStorage, err := disk.NewDiskStorage("/tmp/cache-manager")
if err != nil {
t.Errorf("failed to create disk storage, %v", err)
}
restRESTMapperMgr, err := hubmeta.NewRESTMapperManager("/tmp/cache-manager")
if err != nil {
t.Errorf("failed to create RESTMapper manager, %v", err)
}
sWrapper := cachemanager.NewStorageWrapper(dStorage)
serializerM := serializer.NewSerializerManager()
mgr := cachemanager.NewCacheManager(sWrapper, serializerM, restRESTMapperMgr, fakeSharedInformerFactory)
am, err := NewAutonomyManager(nil, mgr, tc.name)
if err != nil {
t.Errorf("failed to create autonomy manager, %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
am.Run(ctx)
time.Sleep(20 * time.Second)
cancel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import (
"github.com/openyurtio/openyurt/pkg/projectinfo"
)

func TestFormat(t *testing.T) {
assert.Equal(t, "autonomy-controller: autonomy-controller add controller Node", Format("autonomy-controller add controller %s", "Node"))
}

func TestReconcileAutonomy_Reconcile(t *testing.T) {
testcases := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/yurt/yurtstaticset.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (

var _ = Describe("yurtStaticSet Test", Ordered, func() {
ctx := context.Background()
timeout := 60 * time.Second
timeout := 90 * time.Second
k8sClient := ycfg.YurtE2eCfg.RuntimeClient
nodeToImageMap := make(map[string]string)

Expand Down

0 comments on commit 0abbe6a

Please sign in to comment.