github.com/verrazzano/verrazzano@v1.7.0/cluster-operator/controllers/quickcreate/controller/ocne/versions_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 ocne
     5  
     6  import (
     7  	ctx "context"
     8  	_ "embed"
     9  	"github.com/stretchr/testify/assert"
    10  	corev1 "k8s.io/api/core/v1"
    11  	"k8s.io/client-go/kubernetes/scheme"
    12  	clipkg "sigs.k8s.io/controller-runtime/pkg/client"
    13  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    14  	"sigs.k8s.io/yaml"
    15  	"testing"
    16  )
    17  
    18  var (
    19  	//go:embed testdata/ocne-versions.yaml
    20  	testConfigMapBytes []byte
    21  	testOCNEVersion    = "1.6"
    22  )
    23  
    24  func TestGetVersionDefaults(t *testing.T) {
    25  	cm := &corev1.ConfigMap{}
    26  	_ = yaml.Unmarshal(testConfigMapBytes, cm)
    27  	cli := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(cm).Build()
    28  
    29  	cmNoData := cm.DeepCopy()
    30  	cmNoData.Data = nil
    31  	cmNoMapping := cmNoData.DeepCopy()
    32  	cmNoData.Data = map[string]string{
    33  		"invalid": "x",
    34  	}
    35  	cmNoVersions := cmNoData.DeepCopy()
    36  	cmNoVersions.Data = map[string]string{
    37  		"mapping": "{}",
    38  	}
    39  
    40  	var tests = []struct {
    41  		name        string
    42  		ocneVersion string
    43  		cli         clipkg.Client
    44  		hasError    bool
    45  	}{
    46  		{
    47  			"no error for valid OCNE Version",
    48  			testOCNEVersion,
    49  			cli,
    50  			false,
    51  		},
    52  		{
    53  
    54  			"error when invalid OCNE Version",
    55  			"invalid version",
    56  			cli,
    57  			true,
    58  		},
    59  		{
    60  			"error when no ocne metadata",
    61  			testOCNEVersion,
    62  			fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
    63  			true,
    64  		},
    65  		{
    66  			"error when no ocne data in configmap",
    67  			testOCNEVersion,
    68  			fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(cmNoData).Build(),
    69  			true,
    70  		},
    71  		{
    72  			"error when no ocne mapping in configmap",
    73  			testOCNEVersion,
    74  			fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(cmNoMapping).Build(),
    75  			true,
    76  		},
    77  		{
    78  			"error when no ocne versions in configmap",
    79  			testOCNEVersion,
    80  			fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects(cmNoVersions).Build(),
    81  			true,
    82  		},
    83  	}
    84  	for _, tt := range tests {
    85  		t.Run(tt.name, func(t *testing.T) {
    86  			versions, err := GetVersionDefaults(ctx.TODO(), tt.cli, tt.ocneVersion)
    87  			if tt.hasError {
    88  				assert.Error(t, err)
    89  			} else {
    90  				assert.NoError(t, err)
    91  				assert.Equal(t, tt.ocneVersion, versions.Release)
    92  			}
    93  		})
    94  	}
    95  }