Skip to content

Commit 53c9795

Browse files
committed
add some ut
1 parent 87805e0 commit 53c9795

File tree

4 files changed

+94
-36
lines changed

4 files changed

+94
-36
lines changed

pkg/blobfuse-proxy/main.go

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ import (
2323

2424
"k8s.io/klog/v2"
2525

26-
"github.com/go-ini/ini"
27-
"github.com/pkg/errors"
28-
server "sigs.k8s.io/blob-csi-driver/pkg/blobfuse-proxy/server"
26+
"sigs.k8s.io/blob-csi-driver/pkg/blobfuse-proxy/server"
2927
csicommon "sigs.k8s.io/blob-csi-driver/pkg/csi-common"
3028
)
3129

@@ -57,38 +55,10 @@ func main() {
5755
klog.Fatal("cannot start server:", err)
5856
}
5957

60-
mountServer := server.NewMountServiceServer(getBlobfuseVersion())
58+
mountServer := server.NewMountServiceServer()
6159

6260
klog.V(2).Info("Listening for connections on address: %v\n", listener.Addr())
6361
if err = server.RunGRPCServer(mountServer, false, listener); err != nil {
6462
klog.Fatalf("Error running grpc server. Error: %v", listener.Addr(), err)
6563
}
6664
}
67-
68-
func getOSInfo() (map[string]string, error) {
69-
f := "/etc/lsb-release"
70-
cfg, err := ini.Load(f)
71-
if err != nil {
72-
return nil, errors.Wrapf(err, "failed to read %q", f)
73-
}
74-
75-
OSInfo := make(map[string]string)
76-
OSInfo["DISTRIB_ID"] = cfg.Section("").Key("DISTRIB_ID").String()
77-
OSInfo["DISTRIB_RELEASE"] = cfg.Section("").Key("DISTRIB_RELEASE").String()
78-
79-
return OSInfo, nil
80-
}
81-
82-
func getBlobfuseVersion() server.BlobfuseVersion {
83-
OSInfo, err := getOSInfo()
84-
if err != nil {
85-
klog.Errorf("failed to get OS info: %v, default using blobfuse v1", err)
86-
return server.BlobfuseV1
87-
}
88-
klog.Infof("OS info: %v", OSInfo)
89-
90-
if OSInfo["DISTRIB_ID"] == "Ubuntu" && OSInfo["DISTRIB_RELEASE"] >= "22.04" {
91-
return server.BlobfuseV2
92-
}
93-
return server.BlobfuseV1
94-
}

pkg/blobfuse-proxy/server/server.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"google.golang.org/grpc"
2828
"k8s.io/klog/v2"
2929
mount_azure_blob "sigs.k8s.io/blob-csi-driver/pkg/blobfuse-proxy/pb"
30+
"sigs.k8s.io/blob-csi-driver/pkg/util"
3031
)
3132

3233
var (
@@ -46,10 +47,10 @@ type MountServer struct {
4647
}
4748

4849
// NewMountServer returns a new Mountserver
49-
func NewMountServiceServer(ver BlobfuseVersion) *MountServer {
50-
return &MountServer{
51-
blobfuseVersion: ver,
52-
}
50+
func NewMountServiceServer() *MountServer {
51+
mountServer := &MountServer{}
52+
mountServer.blobfuseVersion = getBlobfuseVersion()
53+
return mountServer
5354
}
5455

5556
// MountAzureBlob mounts an azure blob container to given location
@@ -101,3 +102,17 @@ func RunGRPCServer(
101102
klog.V(2).Infof("Start GRPC server at %s, TLS = %t", listener.Addr().String(), enableTLS)
102103
return grpcServer.Serve(listener)
103104
}
105+
106+
func getBlobfuseVersion() BlobfuseVersion {
107+
osinfo, err := util.GetOSInfo("/etc/lsb-release")
108+
if err != nil {
109+
klog.Warningf("failed to get OS info: %v, default using blobfuse v1", err)
110+
return BlobfuseV1
111+
}
112+
113+
if osinfo.Distro == "Ubuntu" && osinfo.Version >= "22.04" {
114+
return BlobfuseV2
115+
}
116+
117+
return BlobfuseV1
118+
}

pkg/util/util.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import (
2121
"os"
2222
"strings"
2323
"sync"
24+
25+
"github.com/go-ini/ini"
26+
"github.com/pkg/errors"
27+
"k8s.io/klog/v2"
2428
)
2529

2630
const (
@@ -155,3 +159,27 @@ func ConvertTagsToMap(tags string) (map[string]string, error) {
155159
}
156160
return m, nil
157161
}
162+
163+
type OsInfo struct {
164+
Distro string
165+
Version string
166+
}
167+
168+
const (
169+
keyDistribID = "DISTRIB_ID"
170+
keyDistribRelease = "DISTRIB_RELEASE"
171+
)
172+
173+
func GetOSInfo(f interface{}) (*OsInfo, error) {
174+
cfg, err := ini.Load(f)
175+
if err != nil {
176+
return nil, errors.Wrapf(err, "failed to read %q", f)
177+
}
178+
179+
oi := &OsInfo{}
180+
oi.Distro = cfg.Section("").Key(keyDistribID).String()
181+
oi.Version = cfg.Section("").Key(keyDistribRelease).String()
182+
183+
klog.V(2).Infof("get OS info: %v", oi)
184+
return oi, nil
185+
}

pkg/util/util_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package util
1919
import (
2020
"fmt"
2121
"os"
22+
"reflect"
2223
"testing"
2324
"time"
2425

@@ -248,3 +249,47 @@ func TestConvertTagsToMap2(t *testing.T) {
248249
assert.Equal(t, result, test.expected)
249250
}
250251
}
252+
253+
func TestGetOSInfo(t *testing.T) {
254+
type args struct {
255+
f interface{}
256+
}
257+
tests := []struct {
258+
name string
259+
args args
260+
want *OsInfo
261+
wantErr bool
262+
}{
263+
{
264+
name: "file not exist",
265+
args: args{
266+
f: "/not-exist/not-exist",
267+
},
268+
want: nil,
269+
wantErr: true,
270+
},
271+
{
272+
name: "parse os info correctly",
273+
args: args{
274+
f: []byte("DISTRIB_ID=Ubuntu\nDISTRIB_RELEASE=22.04"),
275+
},
276+
want: &OsInfo{
277+
Distro: "Ubuntu",
278+
Version: "22.04",
279+
},
280+
wantErr: false,
281+
},
282+
}
283+
for _, tt := range tests {
284+
t.Run(tt.name, func(t *testing.T) {
285+
got, err := GetOSInfo(tt.args.f)
286+
if (err != nil) != tt.wantErr {
287+
t.Errorf("GetOSInfo() error = %v, wantErr %v", err, tt.wantErr)
288+
return
289+
}
290+
if !reflect.DeepEqual(got, tt.want) {
291+
t.Errorf("GetOSInfo() = %v, want %v", got, tt.want)
292+
}
293+
})
294+
}
295+
}

0 commit comments

Comments
 (0)