github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/openshift/params_test.go (about)

     1  package openshift
     2  
     3  import (
     4  	"io/ioutil"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestDecryptedParams(t *testing.T) {
    10  	input := readFileContent(t, "test-encrypted.env")
    11  	t.Logf("Read input: %s", input)
    12  	expected := readFileContent(t, "test-cleartext.env")
    13  	t.Logf("Read expected: %s", expected)
    14  	actual, err := DecryptedParams(input, "test-private.key", "")
    15  	if err != nil {
    16  		t.Error(err)
    17  	}
    18  	if actual != expected {
    19  		t.Errorf("Mismatch, got: %v, want: %v.", actual, expected)
    20  	}
    21  }
    22  
    23  func TestEncodedParams(t *testing.T) {
    24  	input := readFileContent(t, "test-encrypted.env")
    25  	t.Logf("Read input: %s", input)
    26  	expected := readFileContent(t, "test-encoded.env")
    27  	t.Logf("Read expected: %s", expected)
    28  	actual, err := EncodedParams(input, "test-private.key", "")
    29  	if err != nil {
    30  		t.Error(err)
    31  	}
    32  	if actual != expected {
    33  		t.Errorf("Mismatch, got: %v, want: %v.", actual, expected)
    34  	}
    35  }
    36  
    37  func TestEncryptedParams(t *testing.T) {
    38  	previous := readFileContent(t, "test-encrypted.env")
    39  	t.Logf("Read previous: %s", previous)
    40  	input := readFileContent(t, "test-cleartext.env")
    41  	// Add one additional line ...
    42  	input = input + "BAZ=baz\n"
    43  	t.Logf("Read input: %s", input)
    44  	actual, err := EncryptedParams(input, previous, ".", "test-private.key", "")
    45  	if err != nil {
    46  		t.Error(err)
    47  	}
    48  	// The expected output is the first line of the previous file
    49  	// plus one additional line (added above)
    50  	expectedText := strings.TrimSuffix(previous, "\n")
    51  	expectedLines := strings.Split(expectedText, "\n")
    52  	actualText := strings.TrimSuffix(actual, "\n")
    53  	actualLines := strings.Split(actualText, "\n")
    54  	if actualLines[0] != expectedLines[0] {
    55  		t.Errorf("Mismatch, got: %v, want: %v.", actualLines[0], expectedLines[0])
    56  	}
    57  	if actualLines[1] != expectedLines[1] {
    58  		t.Errorf("Mismatch, got: %v, want: %v.", actualLines[1], expectedLines[1])
    59  	}
    60  	if !strings.HasPrefix(actualLines[2], "BAZ=") {
    61  		t.Errorf("Mismatch, got: %v, want: %v.", actualLines[2], "BAZ=")
    62  	}
    63  }
    64  
    65  func readFileContent(t *testing.T, filename string) string {
    66  	bytes, err := ioutil.ReadFile(filename)
    67  	if err != nil {
    68  		t.Error(err)
    69  	}
    70  	return string(bytes)
    71  }