Skip to content

Commit 5149507

Browse files
committed
update scan test cases for scans parameters
Signed-off-by: freedisch <freeproduc@gmail.com>
1 parent a5c69e3 commit 5149507

2 files changed

Lines changed: 23 additions & 19 deletions

File tree

scbctl/cmd/scans.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
v1 "github.com/secureCodeBox/secureCodeBox/operator/apis/execution/v1"
1313
kubernetes "github.com/secureCodeBox/secureCodeBox/scbctl/pkg"
1414
"github.com/spf13/cobra"
15+
metav2 "k8s.io/apimachinery/pkg/api/errors"
1516
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1617
"k8s.io/apimachinery/pkg/runtime"
1718
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -29,30 +30,26 @@ func init() {
2930
}
3031

3132
var ScanCmd = &cobra.Command{
32-
Use: "scan [name] [target]",
33+
Use: "scan [name] -- [parameters...]",
3334
Short: "Create a new scanner",
3435
Long: `Create a new execution (Scan) in the default namespace if no namespace is provided`,
3536
Example: `
3637
# Create a new scan
3738
scbctl scan nmap
3839
# Create in a different namespace
39-
scbctl scan nmap scanme.nmap.org --namespace foobar
40+
scbctl scan --namespace foobar nmap -- scanme.nmap.org -p 90
4041
`,
42+
SilenceUsage: true,
4143
RunE: func(cmd *cobra.Command, args []string) error {
42-
if len(args) < 2 {
43-
return errors.New("You must specify the name of the scan and the target")
44-
}
4544

4645
scanName := args[0]
4746
paramIndex := cmd.ArgsLenAtDash()
4847
if paramIndex == -1 {
4948
return errors.New("You must use '--' to separate scan parameters")
5049
}
5150

52-
5351
parameters := args[paramIndex:]
54-
55-
52+
5653
fmt.Println("🎬 Initializing Kubernetes client")
5754

5855
kubeclient, namespace, err := clientProvider.GetClient(kubeconfigArgs)
@@ -76,7 +73,7 @@ var ScanCmd = &cobra.Command{
7673
Namespace: namespace,
7774
},
7875
Spec: v1.ScanSpec{
79-
ScanType: scanName,
76+
ScanType: scanName,
8077
Parameters: parameters,
8178
},
8279
}
@@ -85,6 +82,9 @@ var ScanCmd = &cobra.Command{
8582

8683
err = kubeclient.Create(context.TODO(), scan)
8784
if err != nil {
85+
if metav2.IsNotFound(err) {
86+
return fmt.Errorf("failed to create Scan: namespace '%s' not found", namespace)
87+
}
8888
return fmt.Errorf("Failed to create Scan: %s", err)
8989
}
9090

scbctl/cmd/scans_test.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ func TestScanCommand(t *testing.T) {
3636
expectedError error
3737
}{
3838
{
39-
name: "Valid entry",
40-
args: []string{"nmap", "scanme.nmap.org"},
39+
name: "Valid entry ",
40+
args: []string{"nmap", "--", "scanme.nmap.org"},
4141
expectedError: nil,
4242
},
4343
{
44-
name: "Missing values",
45-
args: []string{},
46-
expectedError: errors.New("You must specify the name of the scan and the target"),
44+
name: "Valid entry with namespace",
45+
args: []string{"nmap", "--", "scanme.nmap.org"},
46+
expectedError: nil,
4747
},
4848
{
49-
name: "Missing target name",
49+
name: "No scan parameters provided",
5050
args: []string{"nmap"},
51-
expectedError: errors.New("You must specify the name of the scan and the target"),
51+
expectedError: errors.New("You must use '--' to separate scan parameters"),
5252
},
5353
}
5454

@@ -62,11 +62,15 @@ func TestScanCommand(t *testing.T) {
6262
}
6363

6464
cmd := &cobra.Command{
65-
RunE: func(cmd *cobra.Command, args []string) error {
66-
return ScanCmd.RunE(cmd, tc.args)
67-
},
65+
Use: ScanCmd.Use,
66+
Short: ScanCmd.Short,
67+
Long: ScanCmd.Long,
68+
Example: ScanCmd.Example,
69+
RunE: ScanCmd.RunE,
6870
}
6971

72+
cmd.SetArgs(tc.args)
73+
7074
err := cmd.Execute()
7175
if tc.expectedError != nil {
7276
if err == nil || err.Error() != tc.expectedError.Error() {

0 commit comments

Comments
 (0)