Skip to content

Commit 3b48270

Browse files
committed
add validations and test
Signed-off-by: Yaroslav Pershin <[email protected]>
1 parent 5aea1c8 commit 3b48270

2 files changed

Lines changed: 60 additions & 20 deletions

File tree

client/cmd/trdl/add.go

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,17 @@ func addCmd() *cobra.Command {
1616
Short: "Add a software repository",
1717
DisableFlagsInUseLine: true,
1818
RunE: func(cmd *cobra.Command, args []string) error {
19-
if err := cobra.ExactArgs(4)(cmd, args); err != nil {
20-
PrintHelp(cmd)
21-
return err
19+
addArgs, err := parseAddCommandArgs(cmd, args)
20+
if err != nil {
21+
return fmt.Errorf("unable to parse command arguments: %w", err)
2222
}
2323

24-
repoName := args[0]
25-
repoUrl := args[1]
26-
rootVersionArg := args[2]
27-
rootSha512 := args[3]
28-
29-
if repoName == trdl.SelfUpdateDefaultRepo {
24+
if addArgs.repoName == trdl.SelfUpdateDefaultRepo {
3025
PrintHelp(cmd)
3126
return fmt.Errorf("reserved repository name %q cannot be used", trdl.SelfUpdateDefaultRepo)
3227
}
3328

34-
rootVersion, err := parseRootVersionArgument(rootVersionArg)
29+
rootVersion, err := parseRootVersionArgument(addArgs.rootVersionArg)
3530
if err != nil {
3631
PrintHelp(cmd)
3732
return fmt.Errorf("unable to parse required argument \"ROOT_VERSION\": %w", err)
@@ -42,7 +37,7 @@ func addCmd() *cobra.Command {
4237
return fmt.Errorf("unable to initialize trdl client: %w", err)
4338
}
4439

45-
if err := c.AddRepo(repoName, repoUrl, rootVersion, rootSha512); err != nil {
40+
if err := c.AddRepo(addArgs.repoName, addArgs.repoUrl, rootVersion, addArgs.rootSha512); err != nil {
4641
return err
4742
}
4843

@@ -53,6 +48,47 @@ func addCmd() *cobra.Command {
5348
return cmd
5449
}
5550

51+
type addCommandArgs struct {
52+
repoName string
53+
repoUrl string
54+
rootVersionArg string
55+
rootSha512 string
56+
}
57+
58+
func parseAddCommandArgs(cmd *cobra.Command, args []string) (*addCommandArgs, error) {
59+
if len(args) != 2 && len(args) != 4 {
60+
PrintHelp(cmd)
61+
return nil, fmt.Errorf("expected 2 or 4 arguments: REPO URL [ROOT_VERSION ROOT_SHA512], got %d", len(args))
62+
}
63+
64+
repoName := args[0]
65+
repoUrl := args[1]
66+
67+
var rootVersionArg, rootSha512 string
68+
69+
if len(args) == 4 {
70+
rootVersionArg = args[2]
71+
rootSha512 = args[3]
72+
73+
if !govalidator.IsNumeric(rootVersionArg) {
74+
PrintHelp(cmd)
75+
return nil, fmt.Errorf("ROOT_VERSION must be numeric: %q", rootVersionArg)
76+
}
77+
78+
if rootSha512 == "" {
79+
PrintHelp(cmd)
80+
return nil, fmt.Errorf("ROOT_SHA512 must not be empty")
81+
}
82+
}
83+
84+
return &addCommandArgs{
85+
repoName: repoName,
86+
repoUrl: repoUrl,
87+
rootVersionArg: rootVersionArg,
88+
rootSha512: rootSha512,
89+
}, nil
90+
}
91+
5692
func parseRootVersionArgument(arg string) (int64, error) {
5793
if arg == "" {
5894
return 0, nil

e2e/tests/flow/complete_cycle_test.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
_ "embed"
66
"fmt"
7+
"io"
78
"io/ioutil"
89
"net/http"
910
"os/exec"
@@ -250,18 +251,21 @@ var _ = Describe("Complete cycle", func() {
250251
}
251252

252253
clientAdd := func(repo string, rootVersion int) {
253-
resp, err := http.Get(minioRepoAddress + fmt.Sprintf("/%d.root.json", rootVersion))
254-
Ω(err).ShouldNot(HaveOccurred())
255-
defer func() { _ = resp.Body.Close() }()
256-
257-
data, err := ioutil.ReadAll(resp.Body)
258-
Ω(err).ShouldNot(HaveOccurred())
259-
rootRoleSha512 := clientUtil.Sha512Checksum(data)
254+
cmdArgs := []string{"add", repo, minioRepoAddress}
255+
if rootVersion > 0 {
256+
resp, err := http.Get(minioRepoAddress + fmt.Sprintf("/%d.root.json", rootVersion))
257+
Ω(err).ShouldNot(HaveOccurred())
258+
defer func() { _ = resp.Body.Close() }()
259+
data, err := io.ReadAll(resp.Body)
260+
Ω(err).ShouldNot(HaveOccurred())
261+
rootRoleSha512 := clientUtil.Sha512Checksum(data)
262+
cmdArgs = append(cmdArgs, fmt.Sprintf("%d", rootVersion), rootRoleSha512)
263+
}
260264

261265
testutil.RunSucceedCommand(
262266
testDir,
263267
trdlBinPath,
264-
"add", repo, minioRepoAddress, fmt.Sprintf("%d", rootVersion), rootRoleSha512,
268+
cmdArgs...,
265269
)
266270
}
267271

@@ -434,7 +438,7 @@ script.sh
434438
}
435439

436440
By("[client] Adding repo ...")
437-
clientAdd(repo, 1)
441+
clientAdd(repo, 0)
438442

439443
By("[server] Publishing channels ...")
440444
{

0 commit comments

Comments
 (0)