github.com/verrazzano/verrazzano@v1.7.0/platform-operator/internal/operatorinit/run_operator_test.go (about)

     1  // Copyright (c) 2023, 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 operatorinit
     5  
     6  import (
     7  	"context"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/verrazzano/verrazzano/platform-operator/constants"
    10  	"github.com/verrazzano/verrazzano/platform-operator/internal/config"
    11  	corev1 "k8s.io/api/core/v1"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  	"k8s.io/client-go/kubernetes/fake"
    14  	"os"
    15  	"testing"
    16  )
    17  
    18  // TestGenerateConfigMapFromHelmChartFiles tests the call to generateConfigMapFromHelmChartFiles
    19  // GIVEN the chart directory of the verrazzano-platform-operator
    20  //
    21  //	WHEN I call generateConfigMapFromHelmChartFiles
    22  //	THEN no error is returned and a config map is generated with expected results
    23  func TestGenerateConfigMapFromHelmChartFiles(t *testing.T) {
    24  	vpoHelmChartConfigMap := generateVPOConfigMap(t)
    25  	assert.Equal(t, vpoHelmChartConfigMapName, vpoHelmChartConfigMap.Name)
    26  	assert.Equal(t, constants.VerrazzanoInstallNamespace, vpoHelmChartConfigMap.Namespace)
    27  	assert.Equal(t, 14, len(vpoHelmChartConfigMap.Data))
    28  	assert.Contains(t, vpoHelmChartConfigMap.Data, "crds...install.verrazzano.io_verrazzanos.yaml")
    29  	assert.Contains(t, vpoHelmChartConfigMap.Data, "templates...clusterrole.yaml")
    30  	assert.Contains(t, vpoHelmChartConfigMap.Data, "templates...clusterrolebinding.yaml")
    31  	assert.Contains(t, vpoHelmChartConfigMap.Data, "templates...deployment.yaml")
    32  	assert.Contains(t, vpoHelmChartConfigMap.Data, "templates...mutatingWebHookConfiguration.yaml")
    33  	assert.Contains(t, vpoHelmChartConfigMap.Data, "templates...namespace.yaml")
    34  	assert.Contains(t, vpoHelmChartConfigMap.Data, "templates...service.yaml")
    35  	assert.Contains(t, vpoHelmChartConfigMap.Data, "templates...serviceaccount.yaml")
    36  	assert.Contains(t, vpoHelmChartConfigMap.Data, "templates...validatingwebhookconfiguration.yaml")
    37  	assert.Contains(t, vpoHelmChartConfigMap.Data, ".helmignore")
    38  	assert.Contains(t, vpoHelmChartConfigMap.Data, "Chart.yaml")
    39  	assert.Contains(t, vpoHelmChartConfigMap.Data, "NOTES.txt")
    40  	assert.Contains(t, vpoHelmChartConfigMap.Data, "values.yaml")
    41  }
    42  
    43  // TestCreateVPOHelmChartConfigMap tests the call to createVPOHelmChartConfigMap
    44  // GIVEN a config map with a helm chart
    45  //
    46  //	WHEN I call createVPOHelmChartConfigMap
    47  //	THEN no error is returned and a config map is created/updated with expected results
    48  func TestCreateVPOHelmChartConfigMap(t *testing.T) {
    49  	kubeclient := fake.NewSimpleClientset()
    50  
    51  	// Test the create of the VPO config map
    52  	configMap := generateVPOConfigMap(t)
    53  	err := createVPOHelmChartConfigMap(kubeclient, configMap)
    54  	assert.NoError(t, err)
    55  	create, err := kubeclient.CoreV1().ConfigMaps(configMap.Namespace).Get(context.TODO(), vpoHelmChartConfigMapName, metav1.GetOptions{})
    56  	assert.NoError(t, err)
    57  	assert.Contains(t, create.Data, "crds...install.verrazzano.io_verrazzanos.yaml")
    58  
    59  	// Test the update of the VPO config map by using the VMO helm chart
    60  	configMap = generateVPOConfigMapWithVMOChart(t)
    61  	err = createVPOHelmChartConfigMap(kubeclient, configMap)
    62  	assert.NoError(t, err)
    63  	update, err := kubeclient.CoreV1().ConfigMaps(configMap.Namespace).Get(context.TODO(), vpoHelmChartConfigMapName, metav1.GetOptions{})
    64  	assert.NoError(t, err)
    65  	assert.Contains(t, update.Data, "crds...verrazzano.io_verrazzanomonitoringinstances_crd.yaml")
    66  }
    67  
    68  func generateVPOConfigMap(t *testing.T) *corev1.ConfigMap {
    69  	config.TestHelmConfigDir = "../../helm_config"
    70  	chartDir := config.GetHelmVPOChartsDir()
    71  	files, err := os.ReadDir(chartDir)
    72  	assert.NoError(t, err)
    73  	vpoHelmChartConfigMap := &corev1.ConfigMap{
    74  		ObjectMeta: metav1.ObjectMeta{
    75  			Name:      vpoHelmChartConfigMapName,
    76  			Namespace: constants.VerrazzanoInstallNamespace,
    77  		},
    78  	}
    79  
    80  	err = generateConfigMapFromHelmChartFiles(chartDir, "", files, vpoHelmChartConfigMap)
    81  	assert.NoError(t, err)
    82  
    83  	return vpoHelmChartConfigMap
    84  }
    85  
    86  func generateVPOConfigMapWithVMOChart(t *testing.T) *corev1.ConfigMap {
    87  	config.TestHelmConfigDir = "../../helm_config"
    88  	chartDir := config.GetHelmVMOChartsDir()
    89  	files, err := os.ReadDir(chartDir)
    90  	assert.NoError(t, err)
    91  	vpoHelmChartConfigMap := &corev1.ConfigMap{
    92  		ObjectMeta: metav1.ObjectMeta{
    93  			Name:      vpoHelmChartConfigMapName,
    94  			Namespace: constants.VerrazzanoInstallNamespace,
    95  		},
    96  	}
    97  
    98  	err = generateConfigMapFromHelmChartFiles(chartDir, "", files, vpoHelmChartConfigMap)
    99  	assert.NoError(t, err)
   100  
   101  	return vpoHelmChartConfigMap
   102  }