Skip to content

Commit

Permalink
feat(torch): add validations - fix issues
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Ramon Mañes <[email protected]>
  • Loading branch information
tty47 committed Oct 28, 2023
1 parent 8606336 commit d87a637
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
28 changes: 27 additions & 1 deletion pkg/nodes/da.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (

const (
errRemoteCommand = "Error executing remote command: "
timeoutDuration = 30 * time.Second // timeoutDuration we specify the max time to run the func.
timeoutDuration = 60 * time.Second // timeoutDuration we specify the max time to run the func.
nodeIdMaxLength = 52 // nodeIdMaxLength Specify the max length for the nodes ids.
)

var (
Expand Down Expand Up @@ -187,8 +188,18 @@ func GenerateNodeIdAndSaveIt(
return "", err
}

// if the output of the generation is not empty, that means that we could generate the node id successfully, so let's
// store it into the DB.
if output != "" {
log.Info("Adding pod id to Redis: ", connNode, " [", output, "] ")

// check that the node id generate has the right length
output, err = TruncateString(output, nodeIdMaxLength)
if err != nil {
log.Error("Error TruncateString: ", err)
return "", err
}

// save node in redis
err = redis.SetNodeId(connNode, red, ctx, output)
if err != nil {
Expand All @@ -202,3 +213,18 @@ func GenerateNodeIdAndSaveIt(

return output, nil
}

// TruncateString receives and input and a maxLength and returns a string with the size specified.
func TruncateString(input string, maxLength int) (string, error) {
if len(input) == maxLength {
return input, nil
}
if len(input) < maxLength {
log.Error("Error: The node id received is not valid, too short: , ", " - [", len(input), "]", " - [", input, "]")
return input, errors.New("error: The node id received is not valid")
}

log.Info("Node ID with bigger size found: ", input)

return input[:maxLength], nil
}
56 changes: 56 additions & 0 deletions pkg/nodes/da_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,59 @@ func TestSetDaNodeDefault(t *testing.T) {
})
}
}

func TestTruncateString(t *testing.T) {
type args struct {
input string
maxLength int
}
tests := []struct {
name string
args args
want string
}{
{
name: "Case 1: Tests valid node ID: 12D3KooWPB3thXCYyr6Jid49d5DDaRL63inzVagaQswCcgUARg5W - 52",
args: args{
input: "12D3KooWPB3thXCYyr6Jid49d5DDaRL63inzVagaQswCcgUARg5W",
maxLength: 52,
},
want: "12D3KooWPB3thXCYyr6Jid49d5DDaRL63inzVagaQswCcgUARg5W",
},
{
name: "Case 2: Tests not valid node ID.",
args: args{
input: "12D3KooWPB3thXCYyr6Jid49d5DDaRL63inzVagaQswCcgUARg5W12D3KooWPB3thXCYyr6Jid49d5DDaRL63inzVagaQswCcgUARg5W",
maxLength: 52,
},
want: "12D3KooWPB3thXCYyr6Jid49d5DDaRL63inzVagaQswCcgUARg5W",
},
{
name: "Case 3: Tests valid node ID: 12D3KooWMGSh8pLvQYn5zYcdRhVfNAcMZrDt71iyq6eSVtrgjKb8 - 52",
args: args{
input: "12D3KooWMGSh8pLvQYn5zYcdRhVfNAcMZrDt71iyq6eSVtrgjKb8",
maxLength: 52,
},
want: "12D3KooWMGSh8pLvQYn5zYcdRhVfNAcMZrDt71iyq6eSVtrgjKb8",
},
{
name: "Case 4: Tests not valid node ID.",
args: args{
input: "12D3KooWMGSh8pLvQYn5zYcdRhVfNAcMZrDt71iyq6eSVtrgjKb812D3KooWMGSh8pLvQYn5zYcdRhVfNAcMZrDt71iyq6eSVtrgjKb8",
maxLength: 52,
},
want: "12D3KooWMGSh8pLvQYn5zYcdRhVfNAcMZrDt71iyq6eSVtrgjKb8",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got, err := TruncateString(tt.args.input, tt.args.maxLength); got != tt.want {
if err != nil {
t.Errorf("ERROR: TruncateString() = %v, want %v", got, tt.want)
}
t.Errorf("TruncateString() = %v, want %v", got, tt.want)

}
})
}
}

0 comments on commit d87a637

Please sign in to comment.