Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fstab mount with hyphens #1993

Merged
merged 10 commits into from
Jun 19, 2024
23 changes: 18 additions & 5 deletions tools/mount_gcsfuse/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func makeGcsfuseArgs(
opts map[string]string) (args []string, err error) {
// Deal with options.
for name, value := range opts {
switch name {
switch strings.ReplaceAll(name, "-", "_") {
// Don't pass through options that are relevant to mount(8) but not to
// gcsfuse, and that fusermount chokes on with "Invalid argument" on Linux.
case "user", "nouser", "auto", "noauto", "_netdev", "no_netdev":
Expand All @@ -85,11 +85,15 @@ func makeGcsfuseArgs(
"experimental_local_file_cache",
"reuse_token_from_url",
"enable_nonexistent_type_cache",
"experimental_enable_json_read":
"experimental_enable_json_read",
"enable_hns",
"ignore_interrupts",
"anonymous_access",
"log_rotate_compress":
if value == "" {
value = "true"
}
args = append(args, "--"+strings.Replace(name, "_", "-", -1)+"="+value)
args = append(args, "--"+strings.ReplaceAll(name, "_", "-")+"="+value)

// Special case: support mount-like formatting for gcsfuse string flags.
case "dir_mode",
Expand Down Expand Up @@ -123,8 +127,17 @@ func makeGcsfuseArgs(
"custom_endpoint",
"config_file",
"experimental_metadata_prefetch_on_mount",
"kernel_list_cache_ttl_secs":
args = append(args, "--"+strings.Replace(name, "_", "-", -1), value)
"kernel_list_cache_ttl_secs",
"stat_cache_max_size_mb",
"type_cache_max_size_mb",
"metadata_cache_ttl_secs",
"log_severity",
"log_rotate_max_file_size_mb",
"log_rotate_backup_file_count",
"file_cache_max_size_mb",
ashmeenkaur marked this conversation as resolved.
Show resolved Hide resolved
"experimental_grpc_conn_pool_size":

args = append(args, "--"+strings.ReplaceAll(name, "_", "-"), value)

// Special case: support mount-like formatting for gcsfuse debug flags.
case "debug_fuse",
Expand Down
143 changes: 143 additions & 0 deletions tools/mount_gcsfuse/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright 2024 Google Inc. All Rights Reserved.
//
// 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 main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMakeGcsfuseArgs(t *testing.T) {
testCases := []struct {
name string
opts map[string]string
expectedFlags []string
}{
{
name: "TestMakeGcsfuseArgs with NoOptions",
opts: map[string]string{},
expectedFlags: []string{},
},

{
name: "TestMakeGcsfuseArgs for BooleanFlags with underscore",
opts: map[string]string{"implicit_dirs": "",
"foreground": "true",
"experimental_local_file_cache": "",
"reuse_token_from_url": "false",
"enable_nonexistent_type_cache": "",
"experimental_enable_json_read": "true",
"enable_hns": "",
"ignore_interrupts": "",
"anonymous_access": "false",
"log_rotate_compress": "false"},
expectedFlags: []string{"--implicit-dirs=true",
"--foreground=true",
"--experimental-local-file-cache=true",
"--reuse-token-from-url=false",
"--enable-nonexistent-type-cache=true",
"--experimental-enable-json-read=true",
"--enable-hns=true",
"--ignore-interrupts=true",
"--anonymous-access=false",
"--log-rotate-compress=false"},
},

{
name: "TestMakeGcsfuseArgs for BooleanFlags with hyphens",
opts: map[string]string{"implicit_dirs": "",
"foreground": "true",
"experimental-local-file-cache": "",
"reuse-token-from-url": "false",
"enable-nonexistent-type-cache": "",
"experimental-enable-json-read": "true",
"enable-hns": "",
"ignore-interrupts": "",
"anonymous-access": "false",
"log_rotate-compress": "false"},
expectedFlags: []string{"--implicit-dirs=true",
"--foreground=true",
"--experimental-local-file-cache=true",
"--reuse-token-from-url=false",
"--enable-nonexistent-type-cache=true",
"--experimental-enable-json-read=true",
"--enable-hns=true",
"--ignore-interrupts=true",
"--anonymous-access=false",
"--log-rotate-compress=false"},
},

{
name: "TestMakeGcsfuseArgs for StringFlags with underscore",
opts: map[string]string{
"dir_mode": "0755",
"key_file": "/path/to/key",
"log_rotate_backup_file_count": "2",
},
expectedFlags: []string{"--dir-mode", "0755", "--key-file", "/path/to/key", "--log-rotate-backup-file-count", "2"},
},

{
name: "TestMakeGcsfuseArgs for StringFlags with hyphen",
opts: map[string]string{
"dir-mode": "0755",
"key-file": "/path/to/key",
"log-rotate-backup-file-count": "2",
},
expectedFlags: []string{"--dir-mode", "0755", "--key-file", "/path/to/key", "--log-rotate-backup-file-count", "2"},
},

{
name: "TestMakeGcsfuseArgs with DebugFlags",
opts: map[string]string{"debug_fuse": "", "debug_gcs": ""},
expectedFlags: []string{"--debug_fuse", "--debug_gcs"},
},

// Test ignored options
{
name: "TestMakeGcsfuseArgs with IgnoredOptions",
opts: map[string]string{"user": "nobody", "_netdev": ""},
expectedFlags: []string{},
},

{
name: "TestMakeGcsfuseArgs with RegularOptions",
opts: map[string]string{"allow_other": "", "ro": ""},
expectedFlags: []string{"-o", "allow_other", "-o", "ro"},
},

{
name: "TestMakeGcsfuseArgs with MixedOptions",
opts: map[string]string{
"implicit_dirs": "", "file_mode": "0644", "debug_fuse": "", "allow_other": "",
},
expectedFlags: []string{"--implicit-dirs=true", "--file-mode", "0644", "--debug_fuse", "-o", "allow_other"},
},
}
device := "gcsfuse"
mountPoint := "/mnt/gcs"

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
args, err := makeGcsfuseArgs(device, mountPoint, tc.opts)

if assert.Nil(t, err) {
assert.ElementsMatch(t, args[:len(args)-2], tc.expectedFlags)
assert.Equal(t, args[len(args)-2:], []string{device, mountPoint})
}
})
}
}
Loading