github.com/grafana/tanka@v0.26.1-0.20240506093700-c22cfc35c21a/pkg/process/resourceDefaults_test.go (about)

     1  package process
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  
     8  	"github.com/grafana/tanka/pkg/kubernetes/manifest"
     9  	"github.com/grafana/tanka/pkg/spec/v1alpha1"
    10  )
    11  
    12  func TestResourceDefaults(t *testing.T) {
    13  	cases := []struct {
    14  		name                string
    15  		beforeAnnotations   map[string]string
    16  		beforeLabels        map[string]string
    17  		specAnnotations     map[string]string
    18  		specLabels          map[string]string
    19  		expectedAnnotations map[string]string
    20  		expectedLabels      map[string]string
    21  	}{
    22  		// resource without a namespace: set it
    23  		{
    24  			name: "No change",
    25  		},
    26  		{
    27  			name:                "Add annotation",
    28  			specAnnotations:     map[string]string{"a": "b"},
    29  			expectedAnnotations: map[string]string{"a": "b"},
    30  		},
    31  		{
    32  			name:           "Add Label",
    33  			specLabels:     map[string]string{"a": "b"},
    34  			expectedLabels: map[string]string{"a": "b"},
    35  		},
    36  		{
    37  			name:                "Add leaves existing",
    38  			beforeAnnotations:   map[string]string{"1": "2"},
    39  			beforeLabels:        map[string]string{"1": "2"},
    40  			specAnnotations:     map[string]string{"a": "b"},
    41  			specLabels:          map[string]string{"a": "b"},
    42  			expectedAnnotations: map[string]string{"a": "b", "1": "2"},
    43  			expectedLabels:      map[string]string{"a": "b", "1": "2"},
    44  		},
    45  		{
    46  			name:                "Existing overrides spec",
    47  			beforeAnnotations:   map[string]string{"a": "c", "1": "2"},
    48  			beforeLabels:        map[string]string{"a": "c", "1": "2"},
    49  			specAnnotations:     map[string]string{"a": "b"},
    50  			specLabels:          map[string]string{"a": "b"},
    51  			expectedAnnotations: map[string]string{"a": "c", "1": "2"},
    52  			expectedLabels:      map[string]string{"a": "c", "1": "2"},
    53  		},
    54  	}
    55  
    56  	for _, c := range cases {
    57  		t.Run(c.name, func(t *testing.T) {
    58  			cfg := v1alpha1.Environment{
    59  				Spec: v1alpha1.Spec{
    60  					ResourceDefaults: v1alpha1.ResourceDefaults{
    61  						Annotations: c.specAnnotations,
    62  						Labels:      c.specLabels,
    63  					},
    64  				},
    65  			}
    66  
    67  			before := manifest.Manifest{
    68  				"kind": "Deployment",
    69  			}
    70  			for k, v := range c.beforeAnnotations {
    71  				before.Metadata().Annotations()[k] = v
    72  			}
    73  			for k, v := range c.beforeLabels {
    74  				before.Metadata().Labels()[k] = v
    75  			}
    76  
    77  			expected := manifest.Metadata{}
    78  			for k, v := range c.expectedAnnotations {
    79  				expected.Annotations()[k] = v
    80  			}
    81  			for k, v := range c.expectedLabels {
    82  				expected.Labels()[k] = v
    83  			}
    84  
    85  			result := ResourceDefaults(manifest.List{before}, cfg)
    86  			actual := result[0]
    87  			if diff := cmp.Diff(expected, actual.Metadata()); diff != "" {
    88  				t.Error(diff)
    89  			}
    90  		})
    91  	}
    92  }