github.com/verrazzano/verrazzano@v1.7.0/cluster-operator/controllers/quickcreate/controller/oci/network/network_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 network_test
     5  
     6  import (
     7  	"context"
     8  	_ "embed"
     9  	"github.com/stretchr/testify/assert"
    10  	vmcv1alpha1 "github.com/verrazzano/verrazzano/cluster-operator/apis/clusters/v1alpha1"
    11  	"github.com/verrazzano/verrazzano/cluster-operator/controllers/quickcreate/controller/oci/network"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    14  	"k8s.io/apimachinery/pkg/runtime"
    15  	apiyaml "k8s.io/apimachinery/pkg/util/yaml"
    16  	"sigs.k8s.io/cluster-api/api/v1beta1"
    17  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    18  	"testing"
    19  )
    20  
    21  var (
    22  	scheme *runtime.Scheme
    23  	//go:embed testdata/ocicluster.yaml
    24  	ocicluster []byte
    25  	testName   = "test"
    26  )
    27  
    28  func init() {
    29  	scheme = runtime.NewScheme()
    30  }
    31  
    32  func NewOCICluster() runtime.Object {
    33  	j, _ := apiyaml.ToJSON(ocicluster)
    34  	obj, _ := runtime.Decode(unstructured.UnstructuredJSONScheme, j)
    35  	return obj
    36  }
    37  
    38  func TestLoadNetwork(t *testing.T) {
    39  	cluster := &v1beta1.Cluster{
    40  		ObjectMeta: metav1.ObjectMeta{
    41  			Name:      testName,
    42  			Namespace: testName,
    43  		},
    44  	}
    45  	obj := NewOCICluster()
    46  
    47  	cli := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(obj).Build()
    48  	net, err := network.GetNetwork(context.TODO(), cli, cluster, network.GVKOCICluster)
    49  	assert.NoError(t, err)
    50  	assert.NotNil(t, net)
    51  	assert.Len(t, net.Subnets, 3)
    52  	assert.Equal(t, net.VCN, testName)
    53  }
    54  
    55  func TestGetLoadBalancerSubnet(t *testing.T) {
    56  	var tests = []struct {
    57  		n   *vmcv1alpha1.Network
    58  		res string
    59  	}{
    60  		{
    61  			nil,
    62  			"",
    63  		},
    64  		{
    65  			&vmcv1alpha1.Network{
    66  				Subnets: []vmcv1alpha1.Subnet{
    67  					{
    68  						Role: vmcv1alpha1.SubnetRoleServiceLB,
    69  						ID:   "foo",
    70  					},
    71  					{
    72  						Role: vmcv1alpha1.SubnetRoleWorker,
    73  						ID:   "foo",
    74  					},
    75  				},
    76  			},
    77  			"foo",
    78  		},
    79  		{
    80  			&vmcv1alpha1.Network{
    81  				Subnets: []vmcv1alpha1.Subnet{
    82  					{
    83  						Role: vmcv1alpha1.SubnetRoleWorker,
    84  						ID:   "foo",
    85  					},
    86  				},
    87  			},
    88  			"",
    89  		},
    90  	}
    91  
    92  	for _, tt := range tests {
    93  		res := network.GetLoadBalancerSubnet(tt.n)
    94  		assert.Equal(t, tt.res, res)
    95  	}
    96  }