github.com/verrazzano/verrazzano@v1.7.0/application-operator/controllers/loggingtrait/loggingtrait_controller_helper_test.go (about)

     1  // Copyright (c) 2021, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package loggingtrait
     5  
     6  import (
     7  	"reflect"
     8  	"testing"
     9  
    10  	corev1 "k8s.io/api/core/v1"
    11  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    12  )
    13  
    14  func Test_struct2Unmarshal(t *testing.T) {
    15  	type args struct {
    16  		obj interface{}
    17  	}
    18  	tests := []struct {
    19  		name    string
    20  		args    args
    21  		want    unstructured.Unstructured
    22  		wantErr bool
    23  	}{
    24  		{
    25  			name: "volumeMountJSON",
    26  			args: args{
    27  				obj: &corev1.VolumeMount{
    28  					MountPath: loggingMountPath,
    29  					Name:      "loggingVolume",
    30  					SubPath:   loggingKey,
    31  					ReadOnly:  true,
    32  				},
    33  			},
    34  			want: unstructured.Unstructured{
    35  				Object: map[string]interface{}{
    36  					"mountPath": loggingMountPath,
    37  					"name":      "loggingVolume",
    38  					"subPath":   loggingKey,
    39  					"readOnly":  true,
    40  				},
    41  			},
    42  			wantErr: false,
    43  		},
    44  		{
    45  			name: "nilJSON",
    46  			args: args{
    47  				obj: nil,
    48  			},
    49  			want: unstructured.Unstructured{
    50  				Object: nil,
    51  			},
    52  			wantErr: false,
    53  		},
    54  	}
    55  	for _, tt := range tests {
    56  		t.Run(tt.name, func(t *testing.T) {
    57  			got, err := struct2Unmarshal(tt.args.obj)
    58  			if (err != nil) != tt.wantErr {
    59  				t.Errorf("struct2Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
    60  				return
    61  			}
    62  			if !reflect.DeepEqual(got, tt.want) {
    63  				t.Errorf("struct2Unmarshal() = %v, want %v", got, tt.want)
    64  			}
    65  		})
    66  	}
    67  }
    68  
    69  func Test_appendSliceOfInterface(t *testing.T) {
    70  	type args struct {
    71  		aSlice []interface{}
    72  		bSlice []interface{}
    73  	}
    74  
    75  	var empty []interface{}
    76  	var noDuplicateMounts = []corev1.VolumeMount{
    77  		{
    78  			Name:      "test-volume-1",
    79  			MountPath: "test/mount/path/1",
    80  		},
    81  		{
    82  			Name:      "test-volume-2",
    83  			MountPath: "test/mount/path/2",
    84  		},
    85  	}
    86  	var noDuplicates = make([]interface{}, len(noDuplicateMounts))
    87  	for _, v := range noDuplicateMounts {
    88  		noDuplicates = append(noDuplicates, v)
    89  	}
    90  
    91  	var duplicateMounts1 = []corev1.VolumeMount{
    92  		{
    93  			Name:      "test-volume-1",
    94  			MountPath: "test/mount/path/1",
    95  		},
    96  		{
    97  			Name:      "test-volume-2",
    98  			MountPath: "test/mount/path/2",
    99  		},
   100  	}
   101  	var duplicates1 = make([]interface{}, 0)
   102  	for _, v := range duplicateMounts1 {
   103  		duplicates1 = append(duplicates1, v)
   104  	}
   105  
   106  	var duplicateMounts2 = []corev1.VolumeMount{
   107  		{
   108  			Name:      "test-volume-2",
   109  			MountPath: "test/mount/path/2",
   110  		},
   111  		{
   112  			Name:      "test-volume-3",
   113  			MountPath: "test/mount/path/3",
   114  		},
   115  	}
   116  	var duplicates2 = make([]interface{}, 0)
   117  	for _, v := range duplicateMounts2 {
   118  		duplicates2 = append(duplicates2, v)
   119  	}
   120  
   121  	var duplicateMountsWant = []corev1.VolumeMount{
   122  		{
   123  			Name:      "test-volume-2",
   124  			MountPath: "test/mount/path/2",
   125  		},
   126  		{
   127  			Name:      "test-volume-3",
   128  			MountPath: "test/mount/path/3",
   129  		},
   130  		{
   131  			Name:      "test-volume-1",
   132  			MountPath: "test/mount/path/1",
   133  		},
   134  	}
   135  	var duplicatesWant = make([]interface{}, 0)
   136  	for _, v := range duplicateMountsWant {
   137  		duplicatesWant = append(duplicatesWant, v)
   138  	}
   139  
   140  	tests := []struct {
   141  		name string
   142  		args args
   143  		want []interface{}
   144  	}{
   145  		{
   146  			name: "append slice both empty",
   147  			args: args{
   148  				aSlice: empty,
   149  				bSlice: empty,
   150  			},
   151  			want: make([]interface{}, 0),
   152  		},
   153  		{
   154  			name: "append slice no duplicates",
   155  			args: args{
   156  				aSlice: empty,
   157  				bSlice: noDuplicates,
   158  			},
   159  			want: noDuplicates,
   160  		},
   161  		{
   162  			name: "append slice with duplicates",
   163  			args: args{
   164  				aSlice: duplicates1,
   165  				bSlice: duplicates2,
   166  			},
   167  			want: duplicatesWant,
   168  		},
   169  	}
   170  	for _, tt := range tests {
   171  		t.Run(tt.name, func(t *testing.T) {
   172  			if got := appendSliceOfInterface(tt.args.aSlice, tt.args.bSlice); !reflect.DeepEqual(got, tt.want) {
   173  				t.Errorf("appendSliceOfInterface() = %v, want %v", got, tt.want)
   174  			}
   175  		})
   176  	}
   177  }
   178  
   179  var (
   180  	wantHalfContainers = []string{"spec", "containers"}
   181  	wantFullContainers = []string{"spec", "template", "spec", "containers"}
   182  	wantHalfVolumes    = []string{"spec", "volumes"}
   183  	wantFullVolumes    = []string{"spec", "template", "spec", "volumes"}
   184  )
   185  
   186  func Test_locateContainersField(t *testing.T) {
   187  
   188  	tests := []struct {
   189  		name  string
   190  		res   *unstructured.Unstructured
   191  		want  bool
   192  		want1 []string
   193  	}{
   194  		{
   195  			name:  "deployment_test",
   196  			res:   getResource("Deployment"),
   197  			want:  true,
   198  			want1: wantFullContainers,
   199  		},
   200  		{
   201  			name:  "pod_test",
   202  			res:   getResource("Pod"),
   203  			want:  true,
   204  			want1: wantHalfContainers,
   205  		},
   206  		{
   207  			name:  "containerizedWorkload_test",
   208  			res:   getResource("ContainerizedWorkload"),
   209  			want:  true,
   210  			want1: wantHalfContainers,
   211  		},
   212  		{
   213  			name:  "statefulSet_test",
   214  			res:   getResource("StatefulState"),
   215  			want:  true,
   216  			want1: wantFullContainers,
   217  		},
   218  		{
   219  			name:  "daemonSet_test",
   220  			res:   getResource("DaemonSet"),
   221  			want:  true,
   222  			want1: wantFullContainers,
   223  		},
   224  		{
   225  			name:  "secret_test",
   226  			res:   getResource("Secret"),
   227  			want:  false,
   228  			want1: nil,
   229  		},
   230  	}
   231  	for _, tt := range tests {
   232  		t.Run(tt.name, func(t *testing.T) {
   233  			got, got1 := locateContainersField(tt.res)
   234  			if got != tt.want {
   235  				t.Errorf("locateContainersField() got = %v, want %v", got, tt.want)
   236  			}
   237  			if !reflect.DeepEqual(got1, tt.want1) {
   238  				t.Errorf("locateContainersField() got1 = %v, want %v", got1, tt.want1)
   239  			}
   240  		})
   241  	}
   242  }
   243  
   244  func Test_locateVolumesField(t *testing.T) {
   245  	tests := []struct {
   246  		name  string
   247  		res   *unstructured.Unstructured
   248  		want  bool
   249  		want1 []string
   250  	}{
   251  		{
   252  			name:  "deployment_test",
   253  			res:   getResource("Deployment"),
   254  			want:  true,
   255  			want1: wantFullVolumes,
   256  		},
   257  		{
   258  			name:  "pod_test",
   259  			res:   getResource("Pod"),
   260  			want:  true,
   261  			want1: wantHalfVolumes,
   262  		},
   263  		{
   264  			name:  "containerizedWorkload_test",
   265  			res:   getResource("ContainerizedWorkload"),
   266  			want:  true,
   267  			want1: wantHalfVolumes,
   268  		},
   269  		{
   270  			name:  "statefulSet_test",
   271  			res:   getResource("StatefulState"),
   272  			want:  true,
   273  			want1: wantFullVolumes,
   274  		},
   275  		{
   276  			name:  "daemonSet_test",
   277  			res:   getResource("DaemonSet"),
   278  			want:  true,
   279  			want1: wantFullVolumes,
   280  		},
   281  		{
   282  			name:  "secret_test",
   283  			res:   getResource("Secret"),
   284  			want:  false,
   285  			want1: nil,
   286  		},
   287  	}
   288  	for _, tt := range tests {
   289  		t.Run(tt.name, func(t *testing.T) {
   290  			got, got1 := locateVolumesField(tt.res)
   291  			if got != tt.want {
   292  				t.Errorf("locateVolumesField() got = %v, want %v", got, tt.want)
   293  			}
   294  			if !reflect.DeepEqual(got1, tt.want1) {
   295  				t.Errorf("locateVolumesField() got1 = %v, want %v", got1, tt.want1)
   296  			}
   297  		})
   298  	}
   299  }
   300  
   301  func getResource(resource string) *unstructured.Unstructured {
   302  	res := unstructured.Unstructured{}
   303  	appsv1 := "apps/v1"
   304  	v1 := "v1"
   305  
   306  	switch resource {
   307  	case "Deployment":
   308  		res.SetAPIVersion(appsv1)
   309  		res.SetKind("Deployment")
   310  	case "Pod":
   311  		res.SetAPIVersion(v1)
   312  		res.SetKind("Pod")
   313  	case "ContainerizedWorkload":
   314  		res.SetAPIVersion(v1)
   315  		res.SetKind("ContainerizedWorkload")
   316  	case "StatefulState":
   317  		res.SetAPIVersion(appsv1)
   318  		res.SetKind("StatefulSet")
   319  	case "DaemonSet":
   320  		res.SetAPIVersion(appsv1)
   321  		res.SetKind("DaemonSet")
   322  	case "Secret":
   323  		res.SetAPIVersion(v1)
   324  		res.SetKind("Secret")
   325  	}
   326  
   327  	return &res
   328  }