get.porter.sh/porter@v1.3.0/pkg/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"sort"
     8  	"testing"
     9  
    10  	"get.porter.sh/porter/pkg/experimental"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestConfig_GetHomeDir(t *testing.T) {
    16  	c := NewTestConfig(t)
    17  
    18  	home, err := c.GetHomeDir()
    19  	require.NoError(t, err)
    20  
    21  	assert.Equal(t, filepath.FromSlash("/home/myuser/.porter"), home)
    22  }
    23  
    24  func TestConfig_GetHomeDirFromSymlink(t *testing.T) {
    25  	c := NewTestConfig(t)
    26  
    27  	// Set up no PORTER_HOME, and /usr/local/bin/porter -> ~/.porter/porter
    28  	c.Unsetenv(EnvHOME)
    29  	getExecutable = func() (string, error) {
    30  		return "/usr/local/bin/porter", nil
    31  	}
    32  	evalSymlinks = func(path string) (string, error) {
    33  		return "/home/myuser/.porter/porter", nil
    34  	}
    35  
    36  	home, err := c.GetHomeDir()
    37  	require.NoError(t, err)
    38  
    39  	// The reason why we do filepath.join here and not above is because resolving symlinks gets the OS involved
    40  	// and on Windows, that means flipping the afero `/` to `\`.
    41  	assert.Equal(t, filepath.Join("/home/myuser", ".porter"), home)
    42  }
    43  
    44  func TestConfig_GetFeatureFlags(t *testing.T) {
    45  	t.Parallel()
    46  
    47  	t.Run("feature defaulted to disabled", func(t *testing.T) {
    48  		c := Config{}
    49  		assert.False(t, c.IsFeatureEnabled(experimental.FlagNoopFeature))
    50  	})
    51  
    52  	t.Run("feature enabled", func(t *testing.T) {
    53  		c := Config{}
    54  		c.Data.ExperimentalFlags = []string{experimental.NoopFeature}
    55  		assert.True(t, c.IsFeatureEnabled(experimental.FlagNoopFeature))
    56  	})
    57  }
    58  
    59  func TestConfigExperimentalFlags(t *testing.T) {
    60  	// Do not run in parallel since we are using os.Setenv
    61  
    62  	t.Run("default off", func(t *testing.T) {
    63  		c := New()
    64  		defer c.Close()
    65  
    66  		assert.False(t, c.IsFeatureEnabled(experimental.FlagNoopFeature))
    67  	})
    68  
    69  	t.Run("user configuration", func(t *testing.T) {
    70  		os.Setenv("PORTER_EXPERIMENTAL", experimental.NoopFeature)
    71  		defer os.Unsetenv("PORTER_EXPERIMENTAL")
    72  
    73  		c := New()
    74  		defer c.Close()
    75  		c.DataLoader = LoadFromEnvironment()
    76  
    77  		_, err := c.Load(context.Background(), nil)
    78  		require.NoError(t, err, "Load failed")
    79  		assert.True(t, c.IsFeatureEnabled(experimental.FlagNoopFeature))
    80  	})
    81  
    82  	t.Run("programmatically enabled", func(t *testing.T) {
    83  		c := New()
    84  		defer c.Close()
    85  
    86  		c.Data.ExperimentalFlags = nil
    87  		c.SetExperimentalFlags(experimental.FlagNoopFeature)
    88  		assert.True(t, c.IsFeatureEnabled(experimental.FlagNoopFeature))
    89  	})
    90  }
    91  
    92  func TestConfig_GetBuildDriver(t *testing.T) {
    93  	c := NewTestConfig(t)
    94  	c.Data.BuildDriver = "special"
    95  	require.Equal(t, BuildDriverBuildkit, c.GetBuildDriver(), "Default to docker when experimental is false, even when a build driver is set")
    96  }
    97  
    98  func TestConfig_ExportRemoteConfigAsEnvironmentVariables(t *testing.T) {
    99  	ctx := context.Background()
   100  
   101  	c := NewTestConfig(t)
   102  	c.DataLoader = LoadFromEnvironment()
   103  	c.TestContext.AddTestFile("testdata/config.toml", "/home/myuser/.porter/config.toml")
   104  
   105  	_, err := c.Load(ctx, nil)
   106  	require.NoError(t, err, "Config.Load failed")
   107  
   108  	gotEnvVars := c.ExportRemoteConfigAsEnvironmentVariables()
   109  	sort.Strings(gotEnvVars)
   110  	wantEnvVars := []string{
   111  		"PORTER_LOGS_LEVEL=info",
   112  		"PORTER_LOGS_LOG_TO_FILE=true",
   113  		"PORTER_LOGS_STRUCTURED=true",
   114  		"PORTER_TELEMETRY_ENABLED=true",
   115  		"PORTER_TELEMETRY_REDIRECT_TO_FILE=true",
   116  		"PORTER_VERBOSITY=warn",
   117  	}
   118  	assert.Equal(t, wantEnvVars, gotEnvVars)
   119  }