github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/postrender/exec_test.go (about)

     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  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"runtime"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  
    30  	"github.com/stefanmcshane/helm/internal/test/ensure"
    31  )
    32  
    33  const testingScript = `#!/bin/sh
    34  if [ $# -eq 0 ]; then
    35  sed s/FOOTEST/BARTEST/g <&0
    36  else
    37  sed s/FOOTEST/"$*"/g <&0
    38  fi
    39  `
    40  
    41  func TestGetFullPath(t *testing.T) {
    42  	is := assert.New(t)
    43  	t.Run("full path resolves correctly", func(t *testing.T) {
    44  		testpath, cleanup := setupTestingScript(t)
    45  		defer cleanup()
    46  
    47  		fullPath, err := getFullPath(testpath)
    48  		is.NoError(err)
    49  		is.Equal(testpath, fullPath)
    50  	})
    51  
    52  	t.Run("relative path resolves correctly", func(t *testing.T) {
    53  		testpath, cleanup := setupTestingScript(t)
    54  		defer cleanup()
    55  
    56  		currentDir, err := os.Getwd()
    57  		require.NoError(t, err)
    58  		relative, err := filepath.Rel(currentDir, testpath)
    59  		require.NoError(t, err)
    60  		fullPath, err := getFullPath(relative)
    61  		is.NoError(err)
    62  		is.Equal(testpath, fullPath)
    63  	})
    64  
    65  	t.Run("binary in PATH resolves correctly", func(t *testing.T) {
    66  		testpath, cleanup := setupTestingScript(t)
    67  		defer cleanup()
    68  
    69  		realPath := os.Getenv("PATH")
    70  		os.Setenv("PATH", filepath.Dir(testpath))
    71  		defer func() {
    72  			os.Setenv("PATH", realPath)
    73  		}()
    74  
    75  		fullPath, err := getFullPath(filepath.Base(testpath))
    76  		is.NoError(err)
    77  		is.Equal(testpath, fullPath)
    78  	})
    79  
    80  	// NOTE(thomastaylor312): See note in getFullPath for more details why this
    81  	// is here
    82  
    83  	// t.Run("binary in plugin path resolves correctly", func(t *testing.T) {
    84  	// 	testpath, cleanup := setupTestingScript(t)
    85  	// 	defer cleanup()
    86  
    87  	// 	realPath := os.Getenv("HELM_PLUGINS")
    88  	// 	os.Setenv("HELM_PLUGINS", filepath.Dir(testpath))
    89  	// 	defer func() {
    90  	// 		os.Setenv("HELM_PLUGINS", realPath)
    91  	// 	}()
    92  
    93  	// 	fullPath, err := getFullPath(filepath.Base(testpath))
    94  	// 	is.NoError(err)
    95  	// 	is.Equal(testpath, fullPath)
    96  	// })
    97  
    98  	// t.Run("binary in multiple plugin paths resolves correctly", func(t *testing.T) {
    99  	// 	testpath, cleanup := setupTestingScript(t)
   100  	// 	defer cleanup()
   101  
   102  	// 	realPath := os.Getenv("HELM_PLUGINS")
   103  	// 	os.Setenv("HELM_PLUGINS", filepath.Dir(testpath)+string(os.PathListSeparator)+"/another/dir")
   104  	// 	defer func() {
   105  	// 		os.Setenv("HELM_PLUGINS", realPath)
   106  	// 	}()
   107  
   108  	// 	fullPath, err := getFullPath(filepath.Base(testpath))
   109  	// 	is.NoError(err)
   110  	// 	is.Equal(testpath, fullPath)
   111  	// })
   112  }
   113  
   114  func TestExecRun(t *testing.T) {
   115  	if runtime.GOOS == "windows" {
   116  		// the actual Run test uses a basic sed example, so skip this test on windows
   117  		t.Skip("skipping on windows")
   118  	}
   119  	is := assert.New(t)
   120  	testpath, cleanup := setupTestingScript(t)
   121  	defer cleanup()
   122  
   123  	renderer, err := NewExec(testpath)
   124  	require.NoError(t, err)
   125  
   126  	output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
   127  	is.NoError(err)
   128  	is.Contains(output.String(), "BARTEST")
   129  }
   130  
   131  func TestNewExecWithOneArgsRun(t *testing.T) {
   132  	if runtime.GOOS == "windows" {
   133  		// the actual Run test uses a basic sed example, so skip this test on windows
   134  		t.Skip("skipping on windows")
   135  	}
   136  	is := assert.New(t)
   137  	testpath, cleanup := setupTestingScript(t)
   138  	defer cleanup()
   139  
   140  	renderer, err := NewExec(testpath, "ARG1")
   141  	require.NoError(t, err)
   142  
   143  	output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
   144  	is.NoError(err)
   145  	is.Contains(output.String(), "ARG1")
   146  }
   147  
   148  func TestNewExecWithTwoArgsRun(t *testing.T) {
   149  	if runtime.GOOS == "windows" {
   150  		// the actual Run test uses a basic sed example, so skip this test on windows
   151  		t.Skip("skipping on windows")
   152  	}
   153  	is := assert.New(t)
   154  	testpath, cleanup := setupTestingScript(t)
   155  	defer cleanup()
   156  
   157  	renderer, err := NewExec(testpath, "ARG1", "ARG2")
   158  	require.NoError(t, err)
   159  
   160  	output, err := renderer.Run(bytes.NewBufferString("FOOTEST"))
   161  	is.NoError(err)
   162  	is.Contains(output.String(), "ARG1 ARG2")
   163  }
   164  
   165  func setupTestingScript(t *testing.T) (filepath string, cleanup func()) {
   166  	t.Helper()
   167  
   168  	tempdir := ensure.TempDir(t)
   169  
   170  	f, err := ioutil.TempFile(tempdir, "post-render-test.sh")
   171  	if err != nil {
   172  		t.Fatalf("unable to create tempfile for testing: %s", err)
   173  	}
   174  
   175  	_, err = f.WriteString(testingScript)
   176  	if err != nil {
   177  		t.Fatalf("unable to write tempfile for testing: %s", err)
   178  	}
   179  
   180  	err = f.Chmod(0755)
   181  	if err != nil {
   182  		t.Fatalf("unable to make tempfile executable for testing: %s", err)
   183  	}
   184  
   185  	err = f.Close()
   186  	if err != nil {
   187  		t.Fatalf("unable to close tempfile after writing: %s", err)
   188  	}
   189  
   190  	return f.Name(), func() {
   191  		os.RemoveAll(tempdir)
   192  	}
   193  }