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

     1  package openshift
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/opendevstack/tailor/internal/test/helper"
     8  )
     9  
    10  type mockOcExportClient struct {
    11  	t       *testing.T
    12  	fixture string
    13  }
    14  
    15  func (c *mockOcExportClient) Export(target string, label string) ([]byte, error) {
    16  	return helper.ReadFixtureFile(c.t, "export/"+c.fixture), nil
    17  }
    18  
    19  func newResourceFilterOrFatal(t *testing.T, kindArg string, selectorFlag string, excludes []string) *ResourceFilter {
    20  	filter, err := NewResourceFilter(kindArg, selectorFlag, excludes)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	return filter
    25  }
    26  
    27  func TestExportAsTemplateFile(t *testing.T) {
    28  	tests := map[string]struct {
    29  		fixture                string
    30  		goldenTemplate         string
    31  		filter                 *ResourceFilter
    32  		withAnnotations        bool
    33  		trimAnnotations        []string
    34  		namespace              string
    35  		withHardcodedNamespace bool
    36  	}{
    37  		"Without all annotations": {
    38  			fixture:                "is.yml",
    39  			goldenTemplate:         "is.yml",
    40  			filter:                 newResourceFilterOrFatal(t, "is", "", []string{}),
    41  			withAnnotations:        false,
    42  			trimAnnotations:        []string{},
    43  			namespace:              "foo",
    44  			withHardcodedNamespace: true,
    45  		},
    46  		"With all annotations": {
    47  			fixture:                "is.yml",
    48  			goldenTemplate:         "is-annotation.yml",
    49  			filter:                 newResourceFilterOrFatal(t, "is", "", []string{}),
    50  			withAnnotations:        true,
    51  			trimAnnotations:        []string{},
    52  			namespace:              "foo",
    53  			withHardcodedNamespace: true,
    54  		},
    55  		"With trimmed annotation": {
    56  			fixture:                "is.yml",
    57  			goldenTemplate:         "is-trimmed-annotation.yml",
    58  			filter:                 newResourceFilterOrFatal(t, "is", "", []string{}),
    59  			withAnnotations:        false,
    60  			trimAnnotations:        []string{"description"},
    61  			namespace:              "foo",
    62  			withHardcodedNamespace: true,
    63  		},
    64  		"With trimmed annotation prefix": {
    65  			fixture:                "is.yml",
    66  			goldenTemplate:         "is-trimmed-annotation-prefix.yml",
    67  			filter:                 newResourceFilterOrFatal(t, "is", "", []string{}),
    68  			withAnnotations:        false,
    69  			trimAnnotations:        []string{"openshift.io/"},
    70  			namespace:              "foo",
    71  			withHardcodedNamespace: true,
    72  		},
    73  		"With TAILOR_NAMESPACE": {
    74  			fixture:                "bc.yml",
    75  			goldenTemplate:         "bc.yml",
    76  			filter:                 newResourceFilterOrFatal(t, "bc", "", []string{}),
    77  			withAnnotations:        false,
    78  			trimAnnotations:        []string{},
    79  			namespace:              "foo-dev",
    80  			withHardcodedNamespace: false,
    81  		},
    82  		"Works with generateName": {
    83  			fixture:                "rolebinding-generate-name.yml",
    84  			goldenTemplate:         "rolebinding-generate-name.yml",
    85  			filter:                 newResourceFilterOrFatal(t, "rolebinding", "", []string{}),
    86  			withAnnotations:        false,
    87  			trimAnnotations:        []string{},
    88  			namespace:              "foo",
    89  			withHardcodedNamespace: true,
    90  		},
    91  		"Respects filter": {
    92  			fixture:                "is.yml",
    93  			goldenTemplate:         "empty.yml",
    94  			filter:                 newResourceFilterOrFatal(t, "bc", "", []string{}),
    95  			withAnnotations:        false,
    96  			namespace:              "foo",
    97  			withHardcodedNamespace: true,
    98  		},
    99  	}
   100  
   101  	for name, tc := range tests {
   102  		t.Run(name, func(t *testing.T) {
   103  			c := &mockOcExportClient{t: t, fixture: tc.fixture}
   104  			actual, err := ExportAsTemplateFile(tc.filter, tc.withAnnotations, tc.namespace, tc.withHardcodedNamespace, tc.trimAnnotations, c)
   105  			if err != nil {
   106  				t.Fatal(err)
   107  			}
   108  
   109  			expected := string(helper.ReadGoldenFile(t, "export/"+tc.goldenTemplate))
   110  
   111  			if diff := cmp.Diff(expected, actual); diff != "" {
   112  				t.Fatalf("Expected template mismatch (-want +got):\n%s", diff)
   113  			}
   114  		})
   115  	}
   116  }