Skip to content

Commit

Permalink
Merge pull request PelicanPlatform#1517 from turetske/client_cleanup
Browse files Browse the repository at this point in the history
Client cleanup
  • Loading branch information
jhiemstrawisc authored Aug 12, 2024
2 parents 21ac212 + b855e53 commit ab8ac76
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
52 changes: 23 additions & 29 deletions client/fed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -488,7 +489,7 @@ func TestObjectStat(t *testing.T) {
require.NoError(t, err)
}
assert.Equal(t, int64(13), got.Size)
assert.Equal(t, "hello_world.txt", got.Name)
assert.Equal(t, fmt.Sprintf("%s/hello_world.txt", export.FederationPrefix), got.Name)
}
})

Expand Down Expand Up @@ -544,7 +545,7 @@ func TestObjectStat(t *testing.T) {
assert.NoError(t, err)
if err == nil {
assert.Equal(t, int64(17), int64(statInfo.Size))
assert.Equal(t, "test.txt", statInfo.Name)
assert.Equal(t, fmt.Sprintf("%s/%s", fed.Exports[0].FederationPrefix, fileName), statInfo.Name)
}
})

Expand Down Expand Up @@ -736,39 +737,18 @@ func TestObjectList(t *testing.T) {
// Other set-up items:
testFileContent := "test file content"
// Create the temporary file to upload
tempFile, err := os.CreateTemp(t.TempDir(), "test")
tempFileName := filepath.Join(t.TempDir(), "test")
tempFile, err := os.OpenFile(tempFileName, os.O_CREATE|os.O_RDWR, 0644)
assert.NoError(t, err, "Error creating temp file")
defer os.Remove(tempFile.Name())
_, err = tempFile.WriteString(testFileContent)
assert.NoError(t, err, "Error writing to temp file")
tempFile.Close()

issuer, err := config.GetServerIssuerURL()
require.NoError(t, err)

// Create a token file
tokenConfig := token.NewWLCGToken()
tokenConfig.Lifetime = time.Minute
tokenConfig.Issuer = issuer
tokenConfig.Subject = "origin"
tokenConfig.AddAudienceAny()

scopes := []token_scopes.TokenScope{}
readScope, err := token_scopes.Storage_Read.Path("/")
assert.NoError(t, err)
scopes = append(scopes, readScope)
modScope, err := token_scopes.Storage_Modify.Path("/")
assert.NoError(t, err)
scopes = append(scopes, modScope)
tokenConfig.AddScopes(scopes...)
token, err := tokenConfig.CreateToken()
assert.NoError(t, err)
tempToken, err := os.CreateTemp(t.TempDir(), "token")
assert.NoError(t, err, "Error creating temp token file")
// Get a temporary token file
tempToken, _ := getTempToken(t)
defer tempToken.Close()
defer os.Remove(tempToken.Name())
_, err = tempToken.WriteString(token)
assert.NoError(t, err, "Error writing to temp token file")
tempToken.Close()

// Disable progress bars to not reuse the same mpb instance
viper.Set("Logging.DisableProgressBars", true)

Expand All @@ -786,10 +766,24 @@ func TestObjectList(t *testing.T) {
get, err := client.DoList(fed.Ctx, listURL, client.WithTokenLocation(""))
require.NoError(t, err)
require.Len(t, get, 2)
var name string
if strings.Contains(get[0].Name, "hello_world.txt") {
name = get[0].Name
} else {
name = get[1].Name
}
require.Equal(t, fmt.Sprintf("%s/hello_world.txt", export.FederationPrefix), name)
} else {
get, err := client.DoList(fed.Ctx, listURL, client.WithTokenLocation(tempToken.Name()))
require.NoError(t, err)
require.Len(t, get, 2)
var name string
if strings.Contains(get[0].Name, "hello_world.txt") {
name = get[0].Name
} else {
name = get[1].Name
}
require.Equal(t, fmt.Sprintf("%s/hello_world.txt", export.FederationPrefix), name)
}
}
})
Expand Down
9 changes: 5 additions & 4 deletions client/handle_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2665,7 +2665,7 @@ func listHttp(ctx context.Context, remoteObjectUrl *url.URL, directorUrl string,
if !info.IsDir() {
// NOTE: we implement our own FileInfo here because the one we get back from stat() does not have a .name field for some reason
file := FileInfo{
Name: path.Base(remotePath),
Name: remotePath,
Size: info.Size(),
ModTime: info.ModTime(),
IsDir: false,
Expand All @@ -2682,9 +2682,10 @@ func listHttp(ctx context.Context, remoteObjectUrl *url.URL, directorUrl string,
}

for _, info := range infos {
jPath, _ := url.JoinPath(remotePath, info.Name())
// Create a FileInfo for the file and append it to the slice
file := FileInfo{
Name: info.Name(),
Name: jPath,
Size: info.Size(),
ModTime: info.ModTime(),
IsDir: info.IsDir(),
Expand Down Expand Up @@ -2762,7 +2763,7 @@ func statHttp(ctx context.Context, dest *url.URL, namespace namespaces.Namespace
fsinfo, err := client.Stat(endpoint.Path)
if err == nil {
info = FileInfo{
Name: path.Base(endpoint.Path),
Name: endpoint.Path,
Size: fsinfo.Size(),
IsDir: fsinfo.IsDir(),
ModTime: fsinfo.ModTime(),
Expand Down Expand Up @@ -2801,7 +2802,7 @@ func statHttp(ctx context.Context, dest *url.URL, namespace namespaces.Namespace
}

resultsChan <- statResults{FileInfo{
Name: path.Base(endpoint.Path),
Name: endpoint.Path,
Size: info.Size,
IsDir: info.IsDir,
ModTime: info.ModTime,
Expand Down
17 changes: 12 additions & 5 deletions cmd/object_ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"strconv"
"text/tabwriter"

Expand All @@ -44,12 +45,18 @@ var (
func init() {
flagSet := lsCmd.Flags()
flagSet.StringP("token", "t", "", "Token file to use for transfer")
flagSet.BoolP("long", "L", false, "Include extended information")
flagSet.BoolP("long", "L", false, "Include extended information - The '-L' for long output will be changed to '-l' in the 7.11.0 pelican release")
flagSet.BoolP("collectionOnly", "C", false, "List collections only")
flagSet.BoolP("objectonly", "O", false, "List objects only")
flagSet.BoolP("json", "j", false, "Print results in JSON format")

objectCmd.AddCommand(lsCmd)

lsCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
if cmd.Flags().Changed("long") {
log.Warningln("The '-L' for long output will be changed to '-l' in the 7.11.0 pelican release")
}
}
}

func listMain(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -122,7 +129,7 @@ func listMain(cmd *cobra.Command, args []string) error {
}

// Take our fileInfos and print them in a nice way
// if the -L flag was set, we print more information
// if the -l flag was set, we print more information
if long {
w := tabwriter.NewWriter(os.Stdout, 1, 2, 10, ' ', tabwriter.TabIndent|tabwriter.DiscardEmptyColumns)
// If we want JSON format, we append the file info to a slice of fileInfo structs so that we can marshal it
Expand All @@ -140,10 +147,10 @@ func listMain(cmd *cobra.Command, args []string) error {
}
w.Flush()
} else if asJSON {
// In this case, we are not using the long option (-L) and want a JSON format
// In this case, we are not using the long option (-l) and want a JSON format
jsonInfo := []string{}
for _, info := range filteredInfos {
jsonInfo = append(jsonInfo, info.Name)
jsonInfo = append(jsonInfo, path.Base(info.Name))
}
// Convert the FileInfo to JSON and print it
jsonData, err := json.Marshal(jsonInfo)
Expand All @@ -159,7 +166,7 @@ func listMain(cmd *cobra.Command, args []string) error {
w := tabwriter.NewWriter(os.Stdout, 1, 2, 10, ' ', tabwriter.TabIndent|tabwriter.DiscardEmptyColumns)
var line string
for _, info := range filteredInfos {
line += info.Name
line += path.Base(info.Name)
//increase our counter
column++

Expand Down
8 changes: 7 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func init() {
panic(err)
}

rootCmd.PersistentFlags().StringP("log", "l", "", "Specified log output file")
rootCmd.PersistentFlags().StringP("log", "l", "", "Specified log output file - The '-l' for logging will be changed to '-L' in the 7.11.0 pelican release")
if err := viper.BindPFlag("Logging.LogLocation", rootCmd.PersistentFlags().Lookup("log")); err != nil {
panic(err)
}
Expand All @@ -169,4 +169,10 @@ func init() {
if err := viper.BindPFlag("Server.WebPort", portFlag); err != nil {
panic(err)
}

rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
if cmd.Flags().Changed("log") {
log.Warningln("The '-l' for logging will be changed to '-L' in the 7.11.0 pelican release")
}
}
}

0 comments on commit ab8ac76

Please sign in to comment.