github.com/argoproj/argo-cd/v3@v3.2.1/cmpserver/plugin/config_test.go (about)

     1  package plugin
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  
    13  	"github.com/argoproj/argo-cd/v3/common"
    14  )
    15  
    16  func Test_IsDefined(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	testCases := []struct {
    20  		name     string
    21  		discover Discover
    22  		expected bool
    23  	}{
    24  		{
    25  			name:     "empty discover",
    26  			discover: Discover{},
    27  			expected: false,
    28  		},
    29  		{
    30  			name: "discover with find",
    31  			discover: Discover{
    32  				Find: Find{
    33  					Glob: "glob",
    34  				},
    35  			},
    36  			expected: true,
    37  		},
    38  		{
    39  			name: "discover with fileName",
    40  			discover: Discover{
    41  				FileName: "fileName",
    42  			},
    43  			expected: true,
    44  		},
    45  		{
    46  			name: "discover with empty command",
    47  			discover: Discover{
    48  				Find: Find{
    49  					Command: Command{
    50  						Command: []string{},
    51  					},
    52  				},
    53  			},
    54  			expected: false,
    55  		},
    56  		{
    57  			name: "discover with command",
    58  			discover: Discover{
    59  				Find: Find{
    60  					Command: Command{
    61  						Command: []string{"command"},
    62  					},
    63  				},
    64  			},
    65  			expected: true,
    66  		},
    67  	}
    68  
    69  	for _, tc := range testCases {
    70  		tcc := tc
    71  		t.Run(tcc.name, func(t *testing.T) {
    72  			t.Parallel()
    73  
    74  			actual := tcc.discover.IsDefined()
    75  			assert.Equal(t, tcc.expected, actual)
    76  		})
    77  	}
    78  }
    79  
    80  func Test_ReadPluginConfig(t *testing.T) {
    81  	t.Parallel()
    82  
    83  	testCases := []struct {
    84  		name         string
    85  		fileContents string
    86  		expected     *PluginConfig
    87  		expectedErr  string
    88  	}{
    89  		{
    90  			name: "empty metadata",
    91  			fileContents: `
    92  metadata:
    93  `,
    94  			expected:    nil,
    95  			expectedErr: "invalid plugin configuration file. metadata.name should be non-empty",
    96  		},
    97  		{
    98  			name: "empty metadata name",
    99  			fileContents: `
   100  metadata:
   101    name: ""
   102  `,
   103  			expected:    nil,
   104  			expectedErr: "invalid plugin configuration file. metadata.name should be non-empty",
   105  		},
   106  		{
   107  			name: "invalid kind",
   108  			fileContents: `
   109  kind: invalid
   110  metadata:	
   111    name: name
   112  `,
   113  			expected:    nil,
   114  			expectedErr: "invalid plugin configuration file. kind should be ConfigManagementPlugin, found invalid",
   115  		},
   116  		{
   117  			name: "empty generate command",
   118  			fileContents: `
   119  kind: ConfigManagementPlugin
   120  metadata:
   121    name: name
   122  `,
   123  			expected:    nil,
   124  			expectedErr: "invalid plugin configuration file. spec.generate command should be non-empty",
   125  		},
   126  		{
   127  			name: "valid config",
   128  			fileContents: `
   129  kind: ConfigManagementPlugin
   130  metadata:
   131    name: name
   132  spec:
   133    generate:
   134      command: [command]
   135  `,
   136  			expected: &PluginConfig{
   137  				TypeMeta: metav1.TypeMeta{
   138  					Kind: ConfigManagementPluginKind,
   139  				},
   140  				Metadata: metav1.ObjectMeta{
   141  					Name: "name",
   142  				},
   143  				Spec: PluginConfigSpec{
   144  					Generate: Command{
   145  						Command: []string{"command"},
   146  					},
   147  				},
   148  			},
   149  		},
   150  	}
   151  
   152  	for _, tc := range testCases {
   153  		tcc := tc
   154  		t.Run(tcc.name, func(t *testing.T) {
   155  			t.Parallel()
   156  			// write test string to temporary file
   157  			tempDir := t.TempDir()
   158  			tempFile, err := os.Create(filepath.Join(tempDir, "plugin.yaml"))
   159  			require.NoError(t, err)
   160  			err = tempFile.Close()
   161  			require.NoError(t, err)
   162  			err = os.WriteFile(tempFile.Name(), []byte(tcc.fileContents), 0o644)
   163  			require.NoError(t, err)
   164  			config, err := ReadPluginConfig(tempDir)
   165  			if tcc.expectedErr != "" {
   166  				require.EqualError(t, err, tcc.expectedErr)
   167  			} else {
   168  				require.NoError(t, err)
   169  			}
   170  			assert.Equal(t, tcc.expected, config)
   171  		})
   172  	}
   173  }
   174  
   175  func Test_PluginConfig_Address(t *testing.T) {
   176  	t.Parallel()
   177  
   178  	testCases := []struct {
   179  		name     string
   180  		config   *PluginConfig
   181  		expected string
   182  	}{
   183  		{
   184  			name: "no version specified",
   185  			config: &PluginConfig{
   186  				TypeMeta: metav1.TypeMeta{
   187  					Kind: ConfigManagementPluginKind,
   188  				},
   189  				Metadata: metav1.ObjectMeta{
   190  					Name: "name",
   191  				},
   192  			},
   193  			expected: "name",
   194  		},
   195  		{
   196  			name: "version specified",
   197  			config: &PluginConfig{
   198  				TypeMeta: metav1.TypeMeta{
   199  					Kind: ConfigManagementPluginKind,
   200  				},
   201  				Metadata: metav1.ObjectMeta{
   202  					Name: "name",
   203  				},
   204  				Spec: PluginConfigSpec{
   205  					Version: "version",
   206  				},
   207  			},
   208  			expected: "name-version",
   209  		},
   210  	}
   211  
   212  	for _, tc := range testCases {
   213  		tcc := tc
   214  		t.Run(tcc.name, func(t *testing.T) {
   215  			t.Parallel()
   216  			actual := tcc.config.Address()
   217  			expectedAddress := fmt.Sprintf("%s/%s.sock", common.GetPluginSockFilePath(), tcc.expected)
   218  			assert.Equal(t, expectedAddress, actual)
   219  		})
   220  	}
   221  }