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

     1  package openshift
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/ghodss/yaml"
     8  )
     9  
    10  func TestNewResourceFilter(t *testing.T) {
    11  	actual, err := NewResourceFilter("pvc", "", []string{})
    12  	expected := &ResourceFilter{
    13  		Kinds: []string{"PersistentVolumeClaim"},
    14  		Name:  "",
    15  		Label: "",
    16  	}
    17  	if err != nil || !reflect.DeepEqual(actual, expected) {
    18  		t.Errorf("Kinds incorrect, got: %v, want: %v.", actual, expected)
    19  	}
    20  
    21  	actual, err = NewResourceFilter("pvc,dc", "", []string{})
    22  	expected = &ResourceFilter{
    23  		Kinds: []string{"DeploymentConfig", "PersistentVolumeClaim"},
    24  		Name:  "",
    25  		Label: "",
    26  	}
    27  	if err != nil || !reflect.DeepEqual(actual, expected) {
    28  		t.Errorf("Kinds incorrect, got: %v, want: %v.", actual, expected)
    29  	}
    30  
    31  	actual, err = NewResourceFilter("pvc,persistentvolumeclaim,PersistentVolumeClaim", "", []string{})
    32  	expected = &ResourceFilter{
    33  		Kinds: []string{"PersistentVolumeClaim"},
    34  		Name:  "",
    35  		Label: "",
    36  	}
    37  	if err != nil || !reflect.DeepEqual(actual, expected) {
    38  		t.Errorf("Kinds incorrect, got: %v, want: %v.", actual, expected)
    39  	}
    40  
    41  	_, err = NewResourceFilter("pvb", "", []string{})
    42  	if err == nil {
    43  		t.Errorf("Expected to detect unknown kind pvb.")
    44  	}
    45  
    46  	actual, err = NewResourceFilter("dc/foo", "", []string{})
    47  	expected = &ResourceFilter{
    48  		Kinds: []string{},
    49  		Name:  "DeploymentConfig/foo",
    50  		Label: "",
    51  	}
    52  	if err != nil || !reflect.DeepEqual(actual, expected) {
    53  		t.Errorf("Kinds incorrect, got: %v, want: %v.", actual, expected)
    54  	}
    55  
    56  	actual, err = NewResourceFilter("pvc", "name=foo", []string{})
    57  	expected = &ResourceFilter{
    58  		Kinds: []string{"PersistentVolumeClaim"},
    59  		Name:  "",
    60  		Label: "name=foo",
    61  	}
    62  	if err != nil || !reflect.DeepEqual(actual, expected) {
    63  		t.Errorf("Kinds incorrect, got: %v, want: %v.", actual, expected)
    64  	}
    65  
    66  	actual, err = NewResourceFilter("pvc,dc", "name=foo", []string{})
    67  	expected = &ResourceFilter{
    68  		Kinds: []string{"DeploymentConfig", "PersistentVolumeClaim"},
    69  		Name:  "",
    70  		Label: "name=foo",
    71  	}
    72  	if err != nil || !reflect.DeepEqual(actual, expected) {
    73  		t.Errorf("Kinds incorrect, got: %v, want: %v.", actual, expected)
    74  	}
    75  
    76  	actual, err = NewResourceFilter("hpa", "", []string{})
    77  	expected = &ResourceFilter{
    78  		Kinds: []string{"HorizontalPodAutoscaler"},
    79  		Name:  "",
    80  		Label: "",
    81  	}
    82  	if err != nil || !reflect.DeepEqual(actual, expected) {
    83  		t.Errorf("Kinds incorrect, got: %v, want: %v.", actual, expected)
    84  	}
    85  
    86  	actual, err = NewResourceFilter("statefulset", "", []string{})
    87  	expected = &ResourceFilter{
    88  		Kinds: []string{"StatefulSet"},
    89  		Name:  "",
    90  		Label: "",
    91  	}
    92  	if err != nil || !reflect.DeepEqual(actual, expected) {
    93  		t.Errorf("Kinds incorrect, got: %v, want: %v.", actual, expected)
    94  	}
    95  
    96  	actual, err = NewResourceFilter("", "", []string{"rolebinding", "serviceaccount"})
    97  	expected = &ResourceFilter{
    98  		ExcludedKinds: []string{"RoleBinding", "ServiceAccount"},
    99  		Kinds:         []string{},
   100  		Name:          "",
   101  		Label:         "",
   102  	}
   103  	if err != nil || !reflect.DeepEqual(actual, expected) {
   104  		t.Errorf("Kinds incorrect, got: %v, want: %v.", actual, expected)
   105  	}
   106  
   107  }
   108  
   109  func TestConvertToKinds(t *testing.T) {
   110  	tests := map[string]struct {
   111  		filter *ResourceFilter
   112  		want   string
   113  	}{
   114  		"kinds": {
   115  			filter: &ResourceFilter{
   116  				ExcludedKinds: []string{},
   117  				Kinds:         []string{"RoleBinding", "ServiceAccount"},
   118  				Name:          "",
   119  				Label:         "",
   120  			},
   121  			want: "RoleBinding,ServiceAccount",
   122  		},
   123  		"excluded kinds": {
   124  			filter: &ResourceFilter{
   125  				ExcludedKinds: []string{"RoleBinding", "ServiceAccount"},
   126  				Kinds:         []string{},
   127  				Name:          "",
   128  				Label:         "",
   129  			},
   130  			want: "Service,Route,DeploymentConfig,Deployment,BuildConfig,ImageStream,PersistentVolumeClaim,Template,ConfigMap,Secret,CronJob,Job,LimitRange,ResourceQuota,HorizontalPodAutoscaler,StatefulSet",
   131  		},
   132  		"kinds and excluded kinds": {
   133  			filter: &ResourceFilter{
   134  				ExcludedKinds: []string{"RoleBinding", "Route"},
   135  				Kinds:         []string{"Service", "Route", "DeploymentConfig"},
   136  				Name:          "",
   137  				Label:         "",
   138  			},
   139  			want: "Service,DeploymentConfig",
   140  		},
   141  	}
   142  
   143  	for name, tc := range tests {
   144  		t.Run(name, func(t *testing.T) {
   145  			got := tc.filter.ConvertToKinds()
   146  			if got != tc.want {
   147  				t.Errorf("Got: %+v, want: %+v. Filter is: %+v", got, tc.want, tc.filter)
   148  			}
   149  		})
   150  	}
   151  }
   152  
   153  func TestSatisfiedBy(t *testing.T) {
   154  	bc := []byte(
   155  		`kind: BuildConfig
   156  metadata:
   157    labels:
   158      app: foo
   159    name: foo`)
   160  	tests := map[string]struct {
   161  		kindArg      string
   162  		selectorFlag string
   163  		excludes     []string
   164  		config       []byte
   165  		expected     bool
   166  	}{
   167  		"item is included when no constraints are specified": {
   168  			kindArg:      "",
   169  			selectorFlag: "",
   170  			excludes:     []string{},
   171  			config:       bc,
   172  			expected:     true,
   173  		},
   174  		"item is included when kind is specified": {
   175  			kindArg:      "bc",
   176  			selectorFlag: "",
   177  			excludes:     []string{},
   178  			config:       bc,
   179  			expected:     true,
   180  		},
   181  		"item is included when name is specified": {
   182  			kindArg:      "bc/foo",
   183  			selectorFlag: "",
   184  			excludes:     []string{},
   185  			config:       bc,
   186  			expected:     true,
   187  		},
   188  		"item is included when label is specified": {
   189  			kindArg:      "",
   190  			selectorFlag: "app=foo",
   191  			excludes:     []string{},
   192  			config:       bc,
   193  			expected:     true,
   194  		},
   195  		"item is excluded when only some other kind is specified": {
   196  			kindArg:      "is",
   197  			selectorFlag: "",
   198  			excludes:     []string{},
   199  			config:       bc,
   200  			expected:     false,
   201  		},
   202  		"item is excluded when kind is excluded": {
   203  			kindArg:      "",
   204  			selectorFlag: "",
   205  			excludes:     []string{"bc"},
   206  			config:       bc,
   207  			expected:     false,
   208  		},
   209  		"item is excluded when name is excluded": {
   210  			kindArg:      "",
   211  			selectorFlag: "",
   212  			excludes:     []string{"bc/foo"},
   213  			config:       bc,
   214  			expected:     false,
   215  		},
   216  		"item is excluded when label is excluded": {
   217  			kindArg:      "",
   218  			selectorFlag: "",
   219  			excludes:     []string{"app=foo"},
   220  			config:       bc,
   221  			expected:     false,
   222  		},
   223  		"item is excluded when multiple excludes are given that match": {
   224  			kindArg:      "",
   225  			selectorFlag: "",
   226  			excludes:     []string{"app=foo", "bc/foo"},
   227  			config:       bc,
   228  			expected:     false,
   229  		},
   230  		"item is excluded when multiple excludes are given that partially match": {
   231  			kindArg:      "",
   232  			selectorFlag: "",
   233  			excludes:     []string{"app=foobar", "bc/foo"},
   234  			config:       bc,
   235  			expected:     false,
   236  		},
   237  		"item is not excluded when multiple excludes are given that do not match": {
   238  			kindArg:      "",
   239  			selectorFlag: "",
   240  			excludes:     []string{"app=foobar", "dc/foo"},
   241  			config:       bc,
   242  			expected:     true,
   243  		},
   244  	}
   245  
   246  	for name, tc := range tests {
   247  		t.Run(name, func(t *testing.T) {
   248  			item, err := makeItem(tc.config)
   249  			if err != nil {
   250  				t.Fatal(err)
   251  			}
   252  			filter, err := NewResourceFilter(tc.kindArg, tc.selectorFlag, tc.excludes)
   253  			if err != nil {
   254  				t.Fatal(err)
   255  			}
   256  			actual := filter.SatisfiedBy(item)
   257  			if actual != tc.expected {
   258  				t.Errorf("Got: %+v, want: %+v. Filter is: %+v", actual, tc.expected, filter)
   259  			}
   260  		})
   261  	}
   262  }
   263  
   264  func makeItem(config []byte) (*ResourceItem, error) {
   265  	var f interface{}
   266  	err := yaml.Unmarshal(config, &f)
   267  	if err != nil {
   268  		return nil, err
   269  	}
   270  	m := f.(map[string]interface{})
   271  	return NewResourceItem(m, "template")
   272  }