get.porter.sh/porter@v1.3.0/pkg/storage/parameters_test.go (about)

     1  package storage
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"get.porter.sh/porter/pkg/secrets"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestParseVariableAssignments(t *testing.T) {
    15  	testcases := []struct {
    16  		Name, Raw, Variable, Value string
    17  	}{
    18  		{"simple", "a=b", "a", "b"},
    19  		{"multiple equal signs", "c=abc1232===", "c", "abc1232==="},
    20  		{"empty value", "d=", "d", ""},
    21  		{"extra whitespace", " a = b ", "a", "b"},
    22  	}
    23  
    24  	for _, tc := range testcases {
    25  		t.Run(tc.Name, func(t *testing.T) {
    26  
    27  			params := []string{tc.Raw}
    28  
    29  			got, err := ParseVariableAssignments(params)
    30  			if err != nil {
    31  				t.Fatal(err)
    32  			}
    33  
    34  			want := make(map[string]string)
    35  			want[tc.Variable] = tc.Value
    36  			if !reflect.DeepEqual(want, got) {
    37  				t.Fatalf("%s\nexpected:\n\t%v\ngot:\n\t%v\n", tc.Raw, want, got)
    38  			}
    39  		})
    40  	}
    41  }
    42  
    43  func TestParseVariableAssignments_MissingVariableName(t *testing.T) {
    44  	params := []string{"=b"}
    45  
    46  	_, err := ParseVariableAssignments(params)
    47  	if err == nil {
    48  		t.Fatal("should have failed due to a missing variable name")
    49  	}
    50  }
    51  
    52  func TestTestParameterProvider_Load(t *testing.T) {
    53  	p := NewTestParameterProvider(t)
    54  	defer p.Close()
    55  
    56  	t.Run("unsuccessful load", func(t *testing.T) {
    57  		_, err := p.Load("paramset.json")
    58  		require.Error(t, err)
    59  		assert.True(t, strings.Contains(err.Error(), "no such file or directory") || strings.Contains(err.Error(), "The system cannot find the file specified"))
    60  	})
    61  
    62  	t.Run("successful load, unsuccessful unmarshal", func(t *testing.T) {
    63  		_, err := p.Load("testdata/paramset_bad.json")
    64  		require.Error(t, err)
    65  		require.Contains(t, err.Error(), "error reading testdata/paramset_bad.json as a parameter set")
    66  	})
    67  
    68  	t.Run("successful load, successful unmarshal", func(t *testing.T) {
    69  		expected := NewParameterSet("", "mybun",
    70  			secrets.SourceMap{
    71  				Name: "param_env",
    72  				Source: secrets.Source{
    73  					Strategy: "env",
    74  					Hint:     "PARAM_ENV",
    75  				},
    76  			},
    77  			secrets.SourceMap{
    78  				Name: "param_value",
    79  				Source: secrets.Source{
    80  					Strategy: "value",
    81  					Hint:     "param_value",
    82  				},
    83  			},
    84  			secrets.SourceMap{
    85  				Name: "param_command",
    86  				Source: secrets.Source{
    87  					Strategy: "command",
    88  					Hint:     "echo hello world",
    89  				},
    90  			},
    91  			secrets.SourceMap{
    92  				Name: "param_path",
    93  				Source: secrets.Source{
    94  					Strategy: "path",
    95  					Hint:     "/path/to/param",
    96  				},
    97  			},
    98  			secrets.SourceMap{
    99  				Name: "param_secret",
   100  				Source: secrets.Source{
   101  					Strategy: "secret",
   102  					Hint:     "param_secret",
   103  				},
   104  			})
   105  		expected.SchemaVersion = "1.0.1" // It's an older code but it checks out
   106  		expected.Status.Created = time.Date(1983, time.April, 18, 1, 2, 3, 4, time.UTC)
   107  		expected.Status.Modified = expected.Status.Created
   108  
   109  		pset, err := p.Load("testdata/paramset.json")
   110  		require.NoError(t, err)
   111  		require.Equal(t, expected, pset)
   112  	})
   113  }