github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/k8s/commands/resource_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func Test_extractKindAndName(t *testing.T) {
    10  	tests := []struct {
    11  		name          string
    12  		args          []string
    13  		expectedKind  string
    14  		expectedName  string
    15  		expectedError string
    16  	}{
    17  		{
    18  			name:         "one argument only",
    19  			args:         []string{"deploy"},
    20  			expectedKind: "deploy",
    21  			expectedName: "",
    22  		},
    23  		{
    24  			name:         "one argument only, multiple targets",
    25  			args:         []string{"deploy,configmaps"},
    26  			expectedKind: "deploy,configmaps",
    27  			expectedName: "",
    28  		},
    29  		{
    30  			name:         "bar separated",
    31  			args:         []string{"deploy/orion"},
    32  			expectedKind: "deploy",
    33  			expectedName: "orion",
    34  		},
    35  		{
    36  			name:         "space separated",
    37  			args:         []string{"deploy", "lua"},
    38  			expectedKind: "deploy",
    39  			expectedName: "lua",
    40  		},
    41  		{
    42  			name:          "multiple arguments separated",
    43  			args:          []string{"test", "test", "test"},
    44  			expectedError: "can't parse arguments [test test test]. Please run `trivy k8s` for usage.",
    45  		},
    46  	}
    47  
    48  	for _, test := range tests {
    49  		t.Run(test.name, func(t *testing.T) {
    50  			kind, name, err := extractKindAndName(test.args)
    51  
    52  			if len(test.expectedError) > 0 {
    53  				assert.Error(t, err, test.expectedError)
    54  				return
    55  			}
    56  
    57  			assert.Equal(t, test.expectedKind, kind)
    58  			assert.Equal(t, test.expectedName, name)
    59  		})
    60  	}
    61  }