github.com/hashicorp/packer@v1.14.3/hcl2template/function/filebase64_test.go (about)

     1  package function
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/zclconf/go-cty/cty"
     8  )
     9  
    10  func TestFilebase64(t *testing.T) {
    11  	tests := []struct {
    12  		name           string
    13  		file           string
    14  		expectedOutput string
    15  		expectError    bool
    16  	}{
    17  		{
    18  			"file exists, return base64'd contents, no error",
    19  			"./testdata/list.tmpl",
    20  			"JXsgZm9yIHggaW4gbGlzdCB+fQotICR7eH0KJXsgZW5kZm9yIH59Cg==",
    21  			false,
    22  		},
    23  		{
    24  			"file doesn't exist, return nilval and an error",
    25  			"./testdata/no_file",
    26  			"",
    27  			true,
    28  		},
    29  		{
    30  			"directory passed as arg, should error",
    31  			"./testdata",
    32  			"",
    33  			true,
    34  		},
    35  	}
    36  
    37  	for _, tt := range tests {
    38  		t.Run(tt.name, func(t *testing.T) {
    39  			res, err := Filebase64.Call([]cty.Value{
    40  				cty.StringVal(tt.file),
    41  			})
    42  
    43  			if tt.expectError && err == nil {
    44  				t.Fatal("succeeded; want error")
    45  			}
    46  
    47  			if !tt.expectError && err != nil {
    48  				t.Fatalf("unexpected error: %s", err)
    49  			}
    50  
    51  			if err != nil {
    52  				return
    53  			}
    54  
    55  			retVal := res.AsString()
    56  			diff := cmp.Diff(retVal, tt.expectedOutput)
    57  			if diff != "" {
    58  				t.Errorf("expected output and returned are different: %s", diff)
    59  			}
    60  		})
    61  	}
    62  }