Skip to content

Commit bd60017

Browse files
committed
worked on PR comments
1 parent b7759e1 commit bd60017

File tree

3 files changed

+687
-43
lines changed

3 files changed

+687
-43
lines changed

internal/file/file_manager_service.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ import (
3737
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6@v6.8.1 -generate
3838
//counterfeiter:generate . fileManagerServiceInterface
3939

40+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6@v6.8.1 -generate
41+
//counterfeiter:generate . fileServiceOperatorInterface
42+
4043
const (
4144
maxAttempts = 5
4245
dirPerm = 0o755
@@ -967,7 +970,7 @@ func isDomainAllowed(downloadURL string, allowedDomains []string) bool {
967970
return true
968971
}
969972

970-
if isWildcardMatch(hostname, domain) {
973+
if isMatchesWildcardDomain(hostname, domain) {
971974
return true
972975
}
973976
}
@@ -1028,7 +1031,7 @@ func (fms *FileManagerService) addConditionalHeaders(ctx context.Context, req *h
10281031
}
10291032
}
10301033

1031-
func isWildcardMatch(hostname, pattern string) bool {
1034+
func isMatchesWildcardDomain(hostname, pattern string) bool {
10321035
if !strings.HasPrefix(pattern, "*.") {
10331036
return false
10341037
}

internal/file/file_manager_service_test.go

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"github.com/nginx/agent/v3/internal/config"
24+
"github.com/nginx/agent/v3/internal/file/filefakes"
2425
"github.com/nginx/agent/v3/internal/model"
2526

2627
"github.com/nginx/agent/v3/pkg/files"
@@ -35,39 +36,6 @@ import (
3536
"github.com/stretchr/testify/require"
3637
)
3738

38-
type fakeFSO struct {
39-
renameSrc, renameDst string
40-
renameExternalCalled bool
41-
}
42-
43-
func (f *fakeFSO) File(ctx context.Context, file *mpi.File, tempFilePath, expectedHash string) error {
44-
return nil
45-
}
46-
47-
func (f *fakeFSO) UpdateOverview(ctx context.Context, instanceID string, filesToUpdate []*mpi.File, configPath string,
48-
iteration int,
49-
) error {
50-
return nil
51-
}
52-
53-
func (f *fakeFSO) ChunkedFile(ctx context.Context, file *mpi.File, tempFilePath, expectedHash string) error {
54-
return nil
55-
}
56-
func (f *fakeFSO) IsConnected() bool { return true }
57-
func (f *fakeFSO) UpdateFile(ctx context.Context, instanceID string, fileToUpdate *mpi.File) error {
58-
return nil
59-
}
60-
func (f *fakeFSO) SetIsConnected(isConnected bool) {}
61-
func (f *fakeFSO) RenameFile(ctx context.Context, hash, fileName, tempDir string) error { return nil }
62-
func (f *fakeFSO) RenameExternalFile(ctx context.Context, fileName, tempDir string) error {
63-
f.renameExternalCalled = true
64-
f.renameSrc = fileName
65-
f.renameDst = tempDir
66-
67-
return nil
68-
}
69-
func (f *fakeFSO) UpdateClient(ctx context.Context, fileServiceClient mpi.FileServiceClient) {}
70-
7139
func TestFileManagerService_ConfigApply_Add(t *testing.T) {
7240
ctx := context.Background()
7341
tempDir := t.TempDir()
@@ -1438,8 +1406,13 @@ func TestMoveOrDeleteFiles_ExternalFileRenameCalled(t *testing.T) {
14381406
ctx := context.Background()
14391407
fms := NewFileManagerService(nil, types.AgentConfig(), &sync.RWMutex{})
14401408

1441-
fake := &fakeFSO{}
1442-
fms.fileServiceOperator = fake
1409+
fakeFSO := &filefakes.FakeFileServiceOperatorInterface{}
1410+
1411+
fakeFSO.RenameExternalFileStub = func(ctx context.Context, fileName, tempDir string) error {
1412+
return nil
1413+
}
1414+
1415+
fms.fileServiceOperator = fakeFSO
14431416

14441417
fileName := filepath.Join(t.TempDir(), "ext.conf")
14451418
fms.fileActions = map[string]*model.FileCache{
@@ -1456,9 +1429,13 @@ func TestMoveOrDeleteFiles_ExternalFileRenameCalled(t *testing.T) {
14561429

14571430
err := fms.moveOrDeleteFiles(ctx, nil)
14581431
require.NoError(t, err)
1459-
assert.True(t, fake.renameExternalCalled)
1460-
assert.Equal(t, tempPath, fake.renameSrc)
1461-
assert.Equal(t, fileName, fake.renameDst)
1432+
1433+
assert.Equal(t, 1, fakeFSO.RenameExternalFileCallCount(), "RenameExternalFile should be called once")
1434+
1435+
_, srcArg, dstArg := fakeFSO.RenameExternalFileArgsForCall(0)
1436+
1437+
assert.Equal(t, tempPath, srcArg, "RenameExternalFile source argument mismatch")
1438+
assert.Equal(t, fileName, dstArg, "RenameExternalFile destination argument mismatch")
14621439
}
14631440

14641441
func TestDownloadFileContent_MaxBytesLimit(t *testing.T) {
@@ -1533,7 +1510,7 @@ func TestIsDomainAllowed_EdgeCases(t *testing.T) {
15331510
ok = isDomainAllowed("http://sub.example.com/path", []string{"*.example.com"})
15341511
assert.True(t, ok)
15351512

1536-
assert.True(t, isWildcardMatch("example.com", "*.example.com"))
1537-
assert.True(t, isWildcardMatch("sub.example.com", "*.example.com"))
1538-
assert.False(t, isWildcardMatch("badexample.com", "*.example.com"))
1513+
assert.True(t, isMatchesWildcardDomain("example.com", "*.example.com"))
1514+
assert.True(t, isMatchesWildcardDomain("sub.example.com", "*.example.com"))
1515+
assert.False(t, isMatchesWildcardDomain("badexample.com", "*.example.com"))
15391516
}

0 commit comments

Comments
 (0)