github.com/verrazzano/verrazzano-monitoring-operator@v0.0.30/pkg/resources/helper_test.go (about)

     1  // Copyright (C) 2020, 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 resources
     5  
     6  import (
     7  	"fmt"
     8  	"reflect"
     9  	"testing"
    10  
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  
    15  	vmov1 "github.com/verrazzano/verrazzano-monitoring-operator/pkg/apis/vmcontroller/v1"
    16  )
    17  
    18  func createTestVMI() *vmov1.VerrazzanoMonitoringInstance {
    19  	return &vmov1.VerrazzanoMonitoringInstance{
    20  		ObjectMeta: metav1.ObjectMeta{
    21  			Name:      "system",
    22  			Namespace: "test",
    23  		},
    24  	}
    25  }
    26  
    27  func TestGetOpenSearchDashboardsHTTPEndpoint(t *testing.T) {
    28  	osdEndpoint := GetOpenSearchDashboardsHTTPEndpoint(createTestVMI())
    29  	assert.Equal(t, "http://vmi-system-osd.test.svc.cluster.local:5601", osdEndpoint)
    30  }
    31  
    32  func TestGetOpenSearchHTTPEndpoint(t *testing.T) {
    33  	osEndpoint := GetOpenSearchHTTPEndpoint(createTestVMI())
    34  	assert.Equal(t, "http://vmi-system-es-master-http.test.svc.cluster.local:9200", osEndpoint)
    35  }
    36  
    37  func TestConvertToRegexp(t *testing.T) {
    38  	var tests = []struct {
    39  		pattern string
    40  		regexp  string
    41  	}{
    42  		{
    43  			"verrazzano-*",
    44  			"^verrazzano-.*$",
    45  		},
    46  		{
    47  			"verrazzano-system",
    48  			"^verrazzano-system$",
    49  		},
    50  		{
    51  			"*",
    52  			"^.*$",
    53  		},
    54  	}
    55  
    56  	for _, tt := range tests {
    57  		t.Run(fmt.Sprintf("converting pattern '%s' to regexp", tt.pattern), func(t *testing.T) {
    58  			r := ConvertToRegexp(tt.pattern)
    59  			assert.Equal(t, tt.regexp, r)
    60  		})
    61  	}
    62  }
    63  
    64  func TestGetCompLabel(t *testing.T) {
    65  	var tests = []struct {
    66  		compName     string
    67  		expectedName string
    68  	}{
    69  		{
    70  			"es-master",
    71  			"opensearch",
    72  		},
    73  		{
    74  			"es-data",
    75  			"opensearch",
    76  		},
    77  		{
    78  			"es-ingest",
    79  			"opensearch",
    80  		},
    81  		{
    82  			"foo",
    83  			"foo",
    84  		},
    85  	}
    86  
    87  	for _, tt := range tests {
    88  		t.Run(fmt.Sprintf("component name '%s' to expectedName '%s'", tt.compName, tt.expectedName), func(t *testing.T) {
    89  			r := GetCompLabel(tt.compName)
    90  			assert.Equal(t, tt.expectedName, r)
    91  		})
    92  	}
    93  }
    94  
    95  func TestDeepCopyMap(t *testing.T) {
    96  	var tests = []struct {
    97  		srcMap map[string]string
    98  		dstMap map[string]string
    99  	}{
   100  		{
   101  			map[string]string{"foo": "bar"},
   102  			map[string]string{"foo": "bar"},
   103  		},
   104  	}
   105  
   106  	for _, tt := range tests {
   107  		t.Run("basic deepcopy test", func(t *testing.T) {
   108  			r := DeepCopyMap(tt.srcMap)
   109  			assert.Equal(t, tt.dstMap, r)
   110  		})
   111  	}
   112  }
   113  
   114  // GIVEN a string representing java options settings for an OpenSerach container
   115  // WHEN  CreateOpenSearchContainerCMD is invoked to get the command for the OpenSearch container
   116  // THEN the command contains a subcommand to disable the jvm heap settings, if input contains java heap settings
   117  func TestCreateOpenSearchContainerCMD(t *testing.T) {
   118  	containerCmdWithoutJavaOpts := fmt.Sprintf(containerCmdTmpl, "", "")
   119  	containerCmdWithJavaOpts := fmt.Sprintf(containerCmdTmpl, jvmOptsDisableCmd, "")
   120  	var tests = []struct {
   121  		description    string
   122  		javaOpts       string
   123  		expectedResult string
   124  	}{
   125  		{
   126  			"testCreateOpenSearchContainerCMD with empty jvmOpts",
   127  			"",
   128  			containerCmdWithoutJavaOpts,
   129  		},
   130  		{
   131  			"testCreateOpenSearchContainerCMD with jvmOpts not containing jvm memory settings",
   132  			"-Xsomething",
   133  			containerCmdWithoutJavaOpts,
   134  		},
   135  		{
   136  			"testCreateOpenSearchContainerCMD with jvmOpts containing jvm memory settings",
   137  			"-Xms1g -Xmx2g",
   138  			containerCmdWithJavaOpts,
   139  		},
   140  	}
   141  
   142  	for _, tt := range tests {
   143  		t.Run(tt.description, func(t *testing.T) {
   144  			r := CreateOpenSearchContainerCMD(tt.javaOpts, []string{})
   145  			assert.Equal(t, tt.expectedResult, r)
   146  		})
   147  	}
   148  }
   149  
   150  // TestGetOpenSearchPluginList tests the GetOpenSearchPluginList
   151  // GIVEN VMI CRD
   152  // WHEN GetOpenSearchPluginList is called
   153  // THEN returns the list of given OS plugins if there are plugins in VMI crd for OS, else empty list is returned
   154  func TestGetOpenSearchPluginList(t *testing.T) {
   155  	testPlugins := []string{"testPluginURL"}
   156  	tests := []struct {
   157  		name string
   158  		vmo  *vmov1.VerrazzanoMonitoringInstance
   159  		want []string
   160  	}{
   161  		{
   162  			"TestGetOpenSearchPluginList when plugins are provided in VMI CRD",
   163  			&vmov1.VerrazzanoMonitoringInstance{
   164  				Spec: vmov1.VerrazzanoMonitoringInstanceSpec{
   165  					Elasticsearch: vmov1.Elasticsearch{
   166  						Enabled: true,
   167  						Plugins: vmov1.OpenSearchPlugins{
   168  							InstallList: testPlugins,
   169  							Enabled:     true,
   170  						},
   171  					},
   172  				},
   173  			},
   174  			testPlugins,
   175  		},
   176  		{
   177  			"TestGetOpenSearchPluginList when plugins are not provided in VMI CRD",
   178  			&vmov1.VerrazzanoMonitoringInstance{
   179  				Spec: vmov1.VerrazzanoMonitoringInstanceSpec{
   180  					Elasticsearch: vmov1.Elasticsearch{
   181  						Enabled: true,
   182  						Plugins: vmov1.OpenSearchPlugins{
   183  							Enabled: false,
   184  						},
   185  					},
   186  				},
   187  			},
   188  			[]string{},
   189  		},
   190  	}
   191  	for _, tt := range tests {
   192  		t.Run(tt.name, func(t *testing.T) {
   193  			if got := GetOpenSearchPluginList(tt.vmo); !reflect.DeepEqual(got, tt.want) {
   194  				t.Errorf("GetOpenSearchPluginList() = %v, want %v", got, tt.want)
   195  			}
   196  		})
   197  	}
   198  }
   199  
   200  // TestGetOSDashboardPluginList tests the GetOSDashboardPluginList
   201  // GIVEN VMI CRD
   202  // WHEN GetOSDashboardPluginList is called
   203  // THEN returns the list of given OSD plugins if there are plugins provided in VMI crd for OSD, else empty list is returned
   204  func TestGetOSDashboardPluginList(t *testing.T) {
   205  	testPlugins := []string{"testOSDPluginURL"}
   206  	tests := []struct {
   207  		name string
   208  		vmo  *vmov1.VerrazzanoMonitoringInstance
   209  		want []string
   210  	}{
   211  		{
   212  			"TestGetOSDashboardPluginList when plugins are provided in VMI CRD",
   213  			&vmov1.VerrazzanoMonitoringInstance{
   214  				Spec: vmov1.VerrazzanoMonitoringInstanceSpec{
   215  					Kibana: vmov1.Kibana{
   216  						Enabled: true,
   217  						Plugins: vmov1.OpenSearchDashboardsPlugins{
   218  							InstallList: testPlugins,
   219  							Enabled:     true,
   220  						},
   221  					},
   222  				},
   223  			},
   224  			testPlugins,
   225  		},
   226  		{
   227  			"TestGetOSDashboardPluginList when plugins are not provided in VMI CRD",
   228  			&vmov1.VerrazzanoMonitoringInstance{
   229  				Spec: vmov1.VerrazzanoMonitoringInstanceSpec{
   230  					Kibana: vmov1.Kibana{
   231  						Enabled: true,
   232  						Plugins: vmov1.OpenSearchDashboardsPlugins{
   233  							Enabled: false,
   234  						},
   235  					},
   236  				},
   237  			},
   238  			[]string{},
   239  		},
   240  	}
   241  	for _, tt := range tests {
   242  		t.Run(tt.name, func(t *testing.T) {
   243  			if got := GetOSDashboardPluginList(tt.vmo); !reflect.DeepEqual(got, tt.want) {
   244  				t.Errorf("GetOSDashboardPluginList() = %v, want %v", got, tt.want)
   245  			}
   246  		})
   247  	}
   248  }
   249  
   250  // TestGetOSPluginsInstallTmpl tests GetOSPluginsInstallTmpl
   251  // GIVEN list of plugins name, URLs to plugins zip file or Maven coordinates.
   252  // WHEN GetOSPluginsInstallTmpl is called
   253  // THEN template is returned with updated plugins URls
   254  func TestGetOSPluginsInstallTmpl(t *testing.T) {
   255  	plugin := "testPluginsURL"
   256  	tests := []struct {
   257  		name    string
   258  		plugins []string
   259  		want    string
   260  	}{
   261  		{
   262  			"TestGetOSPluginsInstallTmpl when list of plugins is provided",
   263  			[]string{plugin},
   264  			fmt.Sprintf(OSPluginsInstallTmpl, fmt.Sprintf(OSPluginsInstallCmd, plugin)),
   265  		},
   266  		{
   267  			"TestGetOSPluginsInstallTmpl when no plugin is provided",
   268  			[]string{},
   269  			"",
   270  		},
   271  	}
   272  	for _, tt := range tests {
   273  		t.Run(tt.name, func(t *testing.T) {
   274  			if got := GetOSPluginsInstallTmpl(tt.plugins, OSPluginsInstallCmd); got != tt.want {
   275  				t.Errorf("GetOSPluginsInstallTmpl() = %v, want %v", got, tt.want)
   276  			}
   277  		})
   278  	}
   279  }