Skip to content

Commit cdd900d

Browse files
committed
Add more unit tests and comments
1 parent 3ea83ca commit cdd900d

File tree

9 files changed

+145
-34
lines changed

9 files changed

+145
-34
lines changed

ecs-agent/daemonimages/csidriver/Makefile

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ GO111MODULE=on
33
GOPATH=$(shell go env GOPATH)
44
BUILD_DATE=$(shell date -u -Iseconds)
55

6-
SOURCEDIR=./
7-
CSI_DRIVER_SOURCES=$(shell find $(SOURCEDIR) -name '*.go')
8-
96
OS?=linux
107
ARCH?=amd64
118

12-
# Build binary for linux only
139
.PHONY: bin/csi-driver
14-
bin/csi-driver: $(CSI_DRIVER_SOURCES)
10+
bin/csi-driver:
1511
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go build -ldflags "\
1612
-X \"github.com/aws/amazon-ecs-agent/ecs-agent/daemon_images/csi-driver/version.version=$(CSI_DRIVER_VERSION)\" \
1713
-X \"github.com/aws/amazon-ecs-agent/ecs-agent/daemon_images/csi-driver/version.buildDate=$(BUILD_DATE)\"" \

ecs-agent/daemonimages/csidriver/driver/driver.go

+4
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ import (
2525
"github.com/aws/amazon-ecs-agent/ecs-agent/daemonimages/csidriver/version"
2626
)
2727

28+
// Driver encapsulates the GRPC server and the node service which implements all necessary interfaces, such as
29+
// the EBS volume stats interface.
2830
type Driver struct {
2931
nodeService
3032

3133
srv *grpc.Server
3234
options *DriverOptions
3335
}
3436

37+
// DriverOptions supports to custom the endpoint of the GRPC server.
3538
type DriverOptions struct {
3639
endpoint string
3740
}
3841

42+
// NewDriver creates a new driver.
3943
func NewDriver(options ...func(*DriverOptions)) (*Driver, error) {
4044
driverInfo := version.GetVersionInfo()
4145
klog.InfoS("Driver Information",

ecs-agent/daemonimages/csidriver/driver/mount.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@
1313

1414
package driver
1515

16-
// Mounter is the interface implemented by NodeMounter.
1716
type Mounter interface {
1817
PathExists(path string) (bool, error)
1918
}
2019

2120
// NodeMounter implements Mounter.
2221
type NodeMounter struct {
22+
// TODO
2323
}
2424

2525
func (nm NodeMounter) PathExists(path string) (bool, error) {
26+
// TODO
2627
return false, nil
2728
}
2829

2930
func newNodeMounter() (Mounter, error) {
31+
// TODO
3032
return NodeMounter{}, nil
3133
}

ecs-agent/daemonimages/csidriver/driver/node.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import (
1717
"github.com/container-storage-interface/spec/lib/go/csi"
1818
)
1919

20-
// nodeService represents the node service of CSI driver
20+
// nodeService represents the node service of CSI driver.
2121
type nodeService struct {
2222
mounter Mounter
23+
// UnimplementedNodeServer implements all interfaces with empty implementation. As one mini version of csi driver,
24+
// we only need to override the necessary interfaces.
2325
csi.UnimplementedNodeServer
2426
}
2527

ecs-agent/daemonimages/csidriver/options.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"os"
2020
)
2121

22-
const EmptyCSIEndpoint = ""
22+
const emptyCSIEndpoint = ""
2323

2424
type ServerOptions struct {
2525
// Endpoint is the endpoint that the driver server should listen on.
@@ -28,14 +28,18 @@ type ServerOptions struct {
2828

2929
func GetServerOptions(fs *flag.FlagSet) (*ServerOptions, error) {
3030
serverOptions := &ServerOptions{}
31-
fs.StringVar(&serverOptions.Endpoint, "endpoint", EmptyCSIEndpoint, "Endpoint for the CSI driver server")
31+
fs.StringVar(&serverOptions.Endpoint, "endpoint", emptyCSIEndpoint, "Endpoint for the CSI driver server")
3232

3333
args := os.Args[1:]
3434
if err := fs.Parse(args); err != nil {
3535
return nil, err
3636
}
3737

38-
if serverOptions.Endpoint == EmptyCSIEndpoint {
38+
if len(args) == 0 {
39+
return nil, errors.New("no argument is provided")
40+
}
41+
42+
if serverOptions.Endpoint == emptyCSIEndpoint {
3943
return nil, errors.New("no endpoint is provided")
4044
}
4145

ecs-agent/daemonimages/csidriver/options_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestGetServerOptions(t *testing.T) {
5555
name: "No argument is given",
5656
testFunc: func(t *testing.T) {
5757
_, err := testFunc(t, nil)
58-
assert.NotNil(t, err)
58+
assert.EqualError(t, err, "no argument is provided")
5959
},
6060
},
6161
}

ecs-agent/daemonimages/csidriver/util/fs/fs.go

+23-15
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,33 @@ type UsageInfo struct {
2727

2828
// Info linux returns (available bytes, byte capacity, byte usage, total inodes, inodes free, inode usage, error)
2929
// for the filesystem that path resides upon.
30-
func Info(path string) (int64, int64, int64, int64, int64, int64, error) {
30+
func Info(path string) (
31+
available int64, capacity int64, used int64,
32+
totalInodes int64, freeInodes int64, usedInodes int64,
33+
err error,
34+
) {
3135
statfs := &unix.Statfs_t{}
32-
err := unix.Statfs(path, statfs)
36+
err = unix.Statfs(path, statfs)
3337
if err != nil {
34-
return 0, 0, 0, 0, 0, 0, err
38+
available = 0
39+
capacity = 0
40+
used = 0
41+
42+
totalInodes = 0
43+
freeInodes = 0
44+
usedInodes = 0
45+
return
3546
}
3647

3748
// Available is blocks available * fragment size
38-
available := int64(statfs.Bavail) * int64(statfs.Bsize)
39-
49+
available = int64(statfs.Bavail) * int64(statfs.Bsize)
4050
// Capacity is total block count * fragment size
41-
capacity := int64(statfs.Blocks) * int64(statfs.Bsize)
42-
43-
// Usage is block being used * fragment size (aka block size).
44-
usage := (int64(statfs.Blocks) - int64(statfs.Bfree)) * int64(statfs.Bsize)
45-
46-
inodes := int64(statfs.Files)
47-
inodesFree := int64(statfs.Ffree)
48-
inodesUsed := inodes - inodesFree
49-
50-
return available, capacity, usage, inodes, inodesFree, inodesUsed, nil
51+
capacity = int64(statfs.Blocks) * int64(statfs.Bsize)
52+
// Used is block being used * fragment size (aka block size).
53+
used = (int64(statfs.Blocks) - int64(statfs.Bfree)) * int64(statfs.Bsize)
54+
55+
totalInodes = int64(statfs.Files)
56+
freeInodes = int64(statfs.Ffree)
57+
usedInodes = totalInodes - freeInodes
58+
return
5159
}

ecs-agent/daemonimages/csidriver/util/fs/fs_windows.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ type UsageInfo struct {
3636

3737
// Info returns (available bytes, byte capacity, byte usage, total inodes, inodes free, inode usage, error)
3838
// for the filesystem that path resides upon.
39-
func Info(path string) (int64, int64, int64, int64, int64, int64, error) {
40-
var freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes int64
41-
var err error
42-
39+
func Info(path string) (
40+
available int64, capacity int64, used int64,
41+
totalInodes int64, freeInodes int64, usedInodes int64,
42+
err error) {
43+
var totalNumberOfFreeBytes int64
4344
// The equivalent linux call supports calls against files but the syscall for windows
4445
// fails for files with error code: The directory name is invalid. (#99173)
4546
// https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
@@ -49,15 +50,27 @@ func Info(path string) (int64, int64, int64, int64, int64, int64, error) {
4950
procGetDiskFreeSpaceEx.Addr(),
5051
4,
5152
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
52-
uintptr(unsafe.Pointer(&freeBytesAvailable)),
53-
uintptr(unsafe.Pointer(&totalNumberOfBytes)),
53+
uintptr(unsafe.Pointer(&available)),
54+
uintptr(unsafe.Pointer(&capacity)),
5455
uintptr(unsafe.Pointer(&totalNumberOfFreeBytes)),
5556
0,
5657
0,
5758
)
5859
if ret == 0 {
59-
return 0, 0, 0, 0, 0, 0, err
60+
available = 0
61+
capacity = 0
62+
used = 0
63+
64+
totalInodes = 0
65+
freeInodes = 0
66+
usedInodes = 0
67+
68+
return
6069
}
6170

62-
return freeBytesAvailable, totalNumberOfBytes, totalNumberOfBytes - freeBytesAvailable, 0, 0, 0, nil
71+
used = capacity - available
72+
totalInodes = 0
73+
freeInodes = 0
74+
usedInodes = 0
75+
return
6376
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package util
15+
16+
import (
17+
"fmt"
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
)
22+
23+
func TestParseEndpoint(t *testing.T) {
24+
testCases := []struct {
25+
name string
26+
endpoint string
27+
expScheme string
28+
expAddr string
29+
expErr error
30+
}{
31+
{
32+
name: "valid unix endpoint 1",
33+
endpoint: "unix:///csi/csi.sock",
34+
expScheme: "unix",
35+
expAddr: "/csi/csi.sock",
36+
},
37+
{
38+
name: "valid unix endpoint 2",
39+
endpoint: "unix://csi/csi.sock",
40+
expScheme: "unix",
41+
expAddr: "/csi/csi.sock",
42+
},
43+
{
44+
name: "valid unix endpoint 3",
45+
endpoint: "unix:/csi/csi.sock",
46+
expScheme: "unix",
47+
expAddr: "/csi/csi.sock",
48+
},
49+
{
50+
name: "valid tcp endpoint",
51+
endpoint: "tcp:///127.0.0.1/",
52+
expScheme: "tcp",
53+
expAddr: "/127.0.0.1",
54+
},
55+
{
56+
name: "valid tcp endpoint",
57+
endpoint: "tcp:///127.0.0.1",
58+
expScheme: "tcp",
59+
expAddr: "/127.0.0.1",
60+
},
61+
{
62+
name: "invalid endpoint",
63+
endpoint: "http://127.0.0.1",
64+
expErr: fmt.Errorf("unsupported protocol: http"),
65+
},
66+
}
67+
68+
for _, tc := range testCases {
69+
t.Run(tc.name, func(t *testing.T) {
70+
scheme, addr, err := ParseEndpoint(tc.endpoint)
71+
72+
if tc.expErr != nil {
73+
assert.EqualError(t, err, tc.expErr.Error())
74+
} else {
75+
assert.Nil(t, err)
76+
assert.Equal(t, scheme, tc.expScheme, "scheme mismatches")
77+
assert.Equal(t, addr, tc.expAddr, "address mismatches")
78+
}
79+
})
80+
}
81+
82+
}

0 commit comments

Comments
 (0)