...

Source file src/helm.sh/helm/v3/pkg/postrender/exec_test.go

Documentation: helm.sh/helm/v3/pkg/postrender

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package postrender
    18  
    19  import (
    20  	"bytes"
    21  	"os"
    22  	"path/filepath"
    23  	"runtime"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  const testingScript = `#!/bin/sh
    31  if [ $# -eq 0 ]; then
    32  sed s/FOOTEST/BARTEST/g <&0
    33  else
    34  sed s/FOOTEST/"$*"/g <&0
    35  fi
    36  `
    37  
    38  func TestGetFullPath(t *testing.T) {
    39  	is := assert.New(t)
    40  	t.Run("full path resolves correctly", func(t *testing.T) {
    41  		testpath := setupTestingScript(t)
    42  
    43  		fullPath, err := getFullPath(testpath)
    44  		is.NoError(err)
    45  		is.Equal(testpath, fullPath)
    46  	})
    47  
    48  	t.Run("relative path resolves correctly", func(t *testing.T) {
    49  		testpath := setupTestingScript(t)
    50  
    51  		currentDir, err := os.Getwd()
    52  		require.NoError(t, err)
    53  		relative, err := filepath.Rel(currentDir, testpath)
    54  		require.NoError(t, err)
    55  		fullPath, err := getFullPath(relative)
    56  		is.NoError(err)
    57  		is.Equal(testpath, fullPath)
    58  	})
    59  
    60  	t.Run("binary in PATH resolves correctly", func(t *testing.T) {
    61  		testpath := setupTestingScript(t)
    62  
    63  		realPath := os.Getenv("PATH")
    64  		os.Setenv("PATH", filepath.Dir(testpath))
    65  		defer func() {
    66  			os.Setenv("PATH", realPath)
    67  		}()
    68  
    69  		fullPath, err := getFullPath(filepath.Base(testpath))
    70  		is.NoError(err)
    71  		is.Equal(testpath, fullPath)
    72  	})
    73  
    74  	// NOTE(thomastaylor312): See note in getFullPath for more details why this
    75  	// is here
    76  
    77  	// t.Run("binary in plugin path resolves correctly", func(t *testing.T) {
    78  	// 	testpath, cleanup := setupTestingScript(t)
    79  	// 	defer cleanup()
    80  
    81  	// 	realPath := os.Getenv("HELM_PLUGINS")
    82  	// 	os.Setenv("HELM_PLUGINS", filepath.Dir(testpath))
    83  	// 	defer func() {
    84  	// 		os.Setenv("HELM_PLUGINS", realPath)
    85  	// 	}()
    86  
    87  	// 	fullPath, err := getFullPath(filepath.Base(testpath))
    88  	// 	is.NoError(err)
    89  	// 	is.Equal(testpath, fullPath)
    90  	// })
    91  
    92  	// t.Run("binary in multiple plugin paths resolves correctly", func(t *testing.T) {
    93  	// 	testpath, cleanup := setupTestingScript(t)
    94  	// 	defer cleanup()
    95  
    96  	// 	realPath := os.Getenv("HELM_PLUGINS")
    97  	// 	os.Setenv("HELM_PLUGINS", filepath.Dir(testpath)+string(os.PathListSeparator)+"/another/dir")
    98  	// 	defer func() {
    99  	// 		os.Setenv("HELM_PLUGINS", realPath)
   100  	// 	}()
   101  
   102  	// 	fullPath, err := getFullPath(filepath.Base(testpath))
   103  	// 	is.NoError(err)
   104  	// 	is.Equal(testpath, fullPath)
   105  	// })
   106  }
   107  
   108  func TestExecRun(t *testing.T) {
   109  	if runtime.GOOS == "windows" {
   110  		// the actual Run test uses a basic sed example, so skip this test on windows
   111  		t.Skip("skipping on windows")
   112  	}
   113  	is := assert.New(t)
   114  	testpath := setupTestingScript(t)
   115  
   116  	renderer, err := NewExec(testpath)
   117  	require.NoError(t, err)
   118  
   119  	output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
   120  	is.NoError(err)
   121  	is.Contains(output.String(), "BARTEST")
   122  }
   123  
   124  func TestNewExecWithOneArgsRun(t *testing.T) {
   125  	if runtime.GOOS == "windows" {
   126  		// the actual Run test uses a basic sed example, so skip this test on windows
   127  		t.Skip("skipping on windows")
   128  	}
   129  	is := assert.New(t)
   130  	testpath := setupTestingScript(t)
   131  
   132  	renderer, err := NewExec(testpath, "ARG1")
   133  	require.NoError(t, err)
   134  
   135  	output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
   136  	is.NoError(err)
   137  	is.Contains(output.String(), "ARG1")
   138  }
   139  
   140  func TestNewExecWithTwoArgsRun(t *testing.T) {
   141  	if runtime.GOOS == "windows" {
   142  		// the actual Run test uses a basic sed example, so skip this test on windows
   143  		t.Skip("skipping on windows")
   144  	}
   145  	is := assert.New(t)
   146  	testpath := setupTestingScript(t)
   147  
   148  	renderer, err := NewExec(testpath, "ARG1", "ARG2")
   149  	require.NoError(t, err)
   150  
   151  	output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
   152  	is.NoError(err)
   153  	is.Contains(output.String(), "ARG1 ARG2")
   154  }
   155  
   156  func setupTestingScript(t *testing.T) (filepath string) {
   157  	t.Helper()
   158  
   159  	tempdir := t.TempDir()
   160  
   161  	f, err := os.CreateTemp(tempdir, "post-render-test.sh")
   162  	if err != nil {
   163  		t.Fatalf("unable to create tempfile for testing: %s", err)
   164  	}
   165  
   166  	_, err = f.WriteString(testingScript)
   167  	if err != nil {
   168  		t.Fatalf("unable to write tempfile for testing: %s", err)
   169  	}
   170  
   171  	err = f.Chmod(0755)
   172  	if err != nil {
   173  		t.Fatalf("unable to make tempfile executable for testing: %s", err)
   174  	}
   175  
   176  	err = f.Close()
   177  	if err != nil {
   178  		t.Fatalf("unable to close tempfile after writing: %s", err)
   179  	}
   180  
   181  	return f.Name()
   182  }
   183  

View as plain text