mirror of https://github.com/grafana/grafana.git
				
				
				
			Plugins: Make file system path handling in tests OS agnostic (#79651)
* make path handling OS agnostic * PR feedback * fix input for test case
This commit is contained in:
		
							parent
							
								
									8cb351e54a
								
							
						
					
					
						commit
						ef60c90dfa
					
				|  | @ -47,6 +47,8 @@ func TestHTTPLoggerMiddleware(t *testing.T) { | ||||||
| 		f, err := os.CreateTemp("", "example_*.har") | 		f, err := os.CreateTemp("", "example_*.har") | ||||||
| 		require.NoError(t, err) | 		require.NoError(t, err) | ||||||
| 		defer func() { | 		defer func() { | ||||||
|  | 			err = f.Close() | ||||||
|  | 			require.NoError(t, err) | ||||||
| 			err := os.Remove(f.Name()) | 			err := os.Remove(f.Name()) | ||||||
| 			require.NoError(t, err) | 			require.NoError(t, err) | ||||||
| 		}() | 		}() | ||||||
|  |  | ||||||
|  | @ -365,7 +365,7 @@ func TestFSPathSeparatorFiles(t *testing.T) { | ||||||
| 	} { | 	} { | ||||||
| 		t.Run(tc.name, func(t *testing.T) { | 		t.Run(tc.name, func(t *testing.T) { | ||||||
| 			pfs, err := newPathSeparatorOverrideFS( | 			pfs, err := newPathSeparatorOverrideFS( | ||||||
| 				"/", plugins.NewInMemoryFS( | 				tc.sep, plugins.NewInMemoryFS( | ||||||
| 					map[string][]byte{"a": nil, strings.Join([]string{"a", "b", "c"}, tc.sep): nil}, | 					map[string][]byte{"a": nil, strings.Join([]string{"a", "b", "c"}, tc.sep): nil}, | ||||||
| 				), | 				), | ||||||
| 			) | 			) | ||||||
|  |  | ||||||
|  | @ -2,6 +2,7 @@ package sources | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
|  | 	"path/filepath" | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/stretchr/testify/require" | 	"github.com/stretchr/testify/require" | ||||||
|  | @ -36,7 +37,7 @@ func TestSources_List(t *testing.T) { | ||||||
| 		require.Len(t, srcs, 3) | 		require.Len(t, srcs, 3) | ||||||
| 
 | 
 | ||||||
| 		require.Equal(t, srcs[0].PluginClass(ctx), plugins.ClassCore) | 		require.Equal(t, srcs[0].PluginClass(ctx), plugins.ClassCore) | ||||||
| 		require.Equal(t, srcs[0].PluginURIs(ctx), []string{"app/plugins/datasource", "app/plugins/panel"}) | 		require.Equal(t, srcs[0].PluginURIs(ctx), []string{filepath.Join("app", "plugins", "datasource"), filepath.Join("app", "plugins", "panel")}) | ||||||
| 		sig, exists := srcs[0].DefaultSignature(ctx) | 		sig, exists := srcs[0].DefaultSignature(ctx) | ||||||
| 		require.True(t, exists) | 		require.True(t, exists) | ||||||
| 		require.Equal(t, plugins.SignatureStatusInternal, sig.Status) | 		require.Equal(t, plugins.SignatureStatusInternal, sig.Status) | ||||||
|  |  | ||||||
|  | @ -147,7 +147,7 @@ func TestParsePluginTestdata(t *testing.T) { | ||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	staticRootPath, err := filepath.Abs("../manager/testdata") | 	staticRootPath, err := filepath.Abs(filepath.Join("..", "manager", "testdata")) | ||||||
| 	require.NoError(t, err) | 	require.NoError(t, err) | ||||||
| 	dfs := os.DirFS(staticRootPath) | 	dfs := os.DirFS(staticRootPath) | ||||||
| 	ents, err := fs.ReadDir(dfs, ".") | 	ents, err := fs.ReadDir(dfs, ".") | ||||||
|  | @ -240,7 +240,7 @@ func TestParseTreeZips(t *testing.T) { | ||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	staticRootPath, err := filepath.Abs("../storage/testdata") | 	staticRootPath, err := filepath.Abs(filepath.Join("..", "storage", "testdata")) | ||||||
| 	require.NoError(t, err) | 	require.NoError(t, err) | ||||||
| 	ents, err := os.ReadDir(staticRootPath) | 	ents, err := os.ReadDir(staticRootPath) | ||||||
| 	require.NoError(t, err) | 	require.NoError(t, err) | ||||||
|  |  | ||||||
|  | @ -119,7 +119,7 @@ func TestExtractFiles(t *testing.T) { | ||||||
| 		skipWindows(t) | 		skipWindows(t) | ||||||
| 
 | 
 | ||||||
| 		pluginID := "plugin-with-absolute-symlink" | 		pluginID := "plugin-with-absolute-symlink" | ||||||
| 		path, err := i.extractFiles(context.Background(), zipFile(t, "testdata/plugin-with-absolute-symlink.zip"), pluginID, SimpleDirNameGeneratorFunc) | 		path, err := i.extractFiles(context.Background(), zipFile(t, filepath.Join("testdata", "plugin-with-absolute-symlink.zip")), pluginID, SimpleDirNameGeneratorFunc) | ||||||
| 		require.Equal(t, filepath.Join(pluginsDir, pluginID), path) | 		require.Equal(t, filepath.Join(pluginsDir, pluginID), path) | ||||||
| 		require.NoError(t, err) | 		require.NoError(t, err) | ||||||
| 
 | 
 | ||||||
|  | @ -131,7 +131,7 @@ func TestExtractFiles(t *testing.T) { | ||||||
| 		skipWindows(t) | 		skipWindows(t) | ||||||
| 
 | 
 | ||||||
| 		pluginID := "plugin-with-absolute-symlink-dir" | 		pluginID := "plugin-with-absolute-symlink-dir" | ||||||
| 		path, err := i.extractFiles(context.Background(), zipFile(t, "testdata/plugin-with-absolute-symlink-dir.zip"), pluginID, SimpleDirNameGeneratorFunc) | 		path, err := i.extractFiles(context.Background(), zipFile(t, filepath.Join("testdata", "plugin-with-absolute-symlink-dir.zip")), pluginID, SimpleDirNameGeneratorFunc) | ||||||
| 		require.Equal(t, filepath.Join(pluginsDir, pluginID), path) | 		require.Equal(t, filepath.Join(pluginsDir, pluginID), path) | ||||||
| 		require.NoError(t, err) | 		require.NoError(t, err) | ||||||
| 
 | 
 | ||||||
|  | @ -140,7 +140,7 @@ func TestExtractFiles(t *testing.T) { | ||||||
| 	}) | 	}) | ||||||
| 
 | 
 | ||||||
| 	t.Run("Should detect if archive members point outside of the destination directory", func(t *testing.T) { | 	t.Run("Should detect if archive members point outside of the destination directory", func(t *testing.T) { | ||||||
| 		path, err := i.extractFiles(context.Background(), zipFile(t, "testdata/plugin-with-parent-member.zip"), "plugin-with-parent-member", SimpleDirNameGeneratorFunc) | 		path, err := i.extractFiles(context.Background(), zipFile(t, filepath.Join("testdata", "plugin-with-parent-member.zip")), "plugin-with-parent-member", SimpleDirNameGeneratorFunc) | ||||||
| 		require.Empty(t, path) | 		require.Empty(t, path) | ||||||
| 		require.EqualError(t, err, fmt.Sprintf( | 		require.EqualError(t, err, fmt.Sprintf( | ||||||
| 			`archive member "../member.txt" tries to write outside of plugin directory: %q, this can be a security risk`, | 			`archive member "../member.txt" tries to write outside of plugin directory: %q, this can be a security risk`, | ||||||
|  | @ -149,7 +149,7 @@ func TestExtractFiles(t *testing.T) { | ||||||
| 	}) | 	}) | ||||||
| 
 | 
 | ||||||
| 	t.Run("Should detect if archive members are absolute", func(t *testing.T) { | 	t.Run("Should detect if archive members are absolute", func(t *testing.T) { | ||||||
| 		path, err := i.extractFiles(context.Background(), zipFile(t, "testdata/plugin-with-absolute-member.zip"), "plugin-with-absolute-member", SimpleDirNameGeneratorFunc) | 		path, err := i.extractFiles(context.Background(), zipFile(t, filepath.Join("testdata", "plugin-with-absolute-member.zip")), "plugin-with-absolute-member", SimpleDirNameGeneratorFunc) | ||||||
| 		require.Empty(t, path) | 		require.Empty(t, path) | ||||||
| 		require.EqualError(t, err, fmt.Sprintf( | 		require.EqualError(t, err, fmt.Sprintf( | ||||||
| 			`archive member "/member.txt" tries to write outside of plugin directory: %q, this can be a security risk`, | 			`archive member "/member.txt" tries to write outside of plugin directory: %q, this can be a security risk`, | ||||||
|  |  | ||||||
|  | @ -1,6 +1,7 @@ | ||||||
| package util | package util | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"path/filepath" | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/stretchr/testify/assert" | 	"github.com/stretchr/testify/assert" | ||||||
|  | @ -16,16 +17,17 @@ func TestCleanRelativePath(t *testing.T) { | ||||||
| 			expectedPath: ".", | 			expectedPath: ".", | ||||||
| 		}, | 		}, | ||||||
| 		{ | 		{ | ||||||
| 			input:        "/test/test.txt", | 			input:        filepath.Join(string(filepath.Separator), "test", "test.txt"), | ||||||
| 			expectedPath: "test/test.txt", | 			expectedPath: filepath.Join("test", "test.txt"), | ||||||
| 		}, | 		}, | ||||||
| 		{ | 		{ | ||||||
| 			input:        "../../test/test.txt", | 			input:        filepath.Join("..", "..", "test", "test.txt"), | ||||||
| 			expectedPath: "test/test.txt", | 			expectedPath: filepath.Join("test", "test.txt"), | ||||||
| 		}, | 		}, | ||||||
| 		{ | 		{ | ||||||
| 			input:        "./../test/test.txt", | 			// since filepath.Join will remove the leading dot, we need to build the path manually
 | ||||||
| 			expectedPath: "test/test.txt", | 			input:        "." + string(filepath.Separator) + filepath.Join("..", "test", "test.txt"), | ||||||
|  | 			expectedPath: filepath.Join("test", "test.txt"), | ||||||
| 		}, | 		}, | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue