Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Zhang Kang <kang.zhang@intel.com>
  • Loading branch information
kangclzjc committed Apr 12, 2024
1 parent 82f324e commit 1782aef
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/koordlet/runtimehooks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func NewDefaultConfig() *Config {
RuntimeHookHostEndpoint: "/var/run/koordlet/koordlet.sock",
RuntimeHookDisableStages: []string{},
RuntimeHooksNRI: true,
RuntimeHooksNRIReconnectInitInterval: 10 * time.Second,
RuntimeHooksNRIReconnectMaxInterval: 5 * time.Minute,
RuntimeHooksNRIReconnectInitInterval: 1 * time.Second,
RuntimeHooksNRIReconnectMaxInterval: 1 * time.Minute,
RuntimeHooksNRIReconnectLimit: 100,
RuntimeHooksNRIReconnectMul: 2,
RuntimeHooksNRISocketPath: "nri/nri.sock",
Expand Down
16 changes: 16 additions & 0 deletions pkg/koordlet/runtimehooks/nri/backoff.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2022 The Koordinator 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 nri

import "time"
Expand Down
95 changes: 95 additions & 0 deletions pkg/koordlet/runtimehooks/nri/backoff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2022 The Koordinator 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 nri

import (
"reflect"
"testing"
"time"
)

func TestBackoff_NextInterval(t *testing.T) {
type fields struct {
initInterval time.Duration
maxInterval time.Duration
multiplier float64
}
type args struct {
attempt int
}
tests := []struct {
name string
fields fields
args args
want time.Duration
}{
{
name: "Test NextInterval backoff smaller than maxInterval",
fields: fields{
initInterval: 10,
maxInterval: 100,
multiplier: 2,
},
args: args{attempt: 2},
want: time.Duration(40),
},
{
name: "Test NextInterval backoff larger than maxInterval",
fields: fields{
initInterval: 10,
maxInterval: 100,
multiplier: 2,
},
args: args{attempt: 10},
want: time.Duration(100),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := Backoff{
initInterval: tt.fields.initInterval,
maxInterval: tt.fields.maxInterval,
multiplier: tt.fields.multiplier,
}
if got := b.NextInterval(tt.args.attempt); got != tt.want {
t.Errorf("NextInterval() = %v, want %v", got, tt.want)
}
})
}
}

func TestNewBackOff(t *testing.T) {
type args struct {
initInterval time.Duration
maxInterval time.Duration
mul float64
}
tests := []struct {
name string
args args
want Backoff
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewBackOff(tt.args.initInterval, tt.args.maxInterval, tt.args.mul); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewBackOff() = %v, want %v", got, tt.want)
}
})
}
}
10 changes: 6 additions & 4 deletions pkg/koordlet/runtimehooks/nri/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package nri
import (
"context"
"fmt"
"github.com/containerd/nri/pkg/api"
"github.com/containerd/nri/pkg/stub"
"k8s.io/klog/v2"
"path/filepath"
"sigs.k8s.io/yaml"
"strings"
"time"

"k8s.io/klog/v2"
"sigs.k8s.io/yaml"

"github.com/containerd/nri/pkg/api"
"github.com/containerd/nri/pkg/stub"

Check failure on line 30 in pkg/koordlet/runtimehooks/nri/server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `goimports`-ed with -local github.com/koordinator-sh/koordinator (goimports)
"github.com/koordinator-sh/koordinator/pkg/koordlet/resourceexecutor"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/hooks"
"github.com/koordinator-sh/koordinator/pkg/koordlet/runtimehooks/protocol"
Expand Down Expand Up @@ -235,6 +236,7 @@ func (p *NriServer) onClose() {
p.stub = stub
err = p.Start()
if err != nil {
klog.Infof("NRI reconnect after %v seconds", p.options.ReconnectionOption.Backoff.NextInterval(attempt).Seconds())
time.Sleep(p.options.ReconnectionOption.Backoff.NextInterval(attempt))
} else {
klog.V(5).Infof("nri server restart success")
Expand Down

0 comments on commit 1782aef

Please sign in to comment.