Skip to content

Commit

Permalink
Operator flag tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Kostyukov committed Feb 15, 2022
1 parent 10fde74 commit bf5ce15
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 11 deletions.
52 changes: 46 additions & 6 deletions pkg/operator/controllers/storageaccounts/storageaccounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package storageaccounts
import (
"context"
"testing"
"strconv"

mgmtstorage "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
"github.com/Azure/go-autorest/autorest/to"
Expand Down Expand Up @@ -39,13 +40,13 @@ var (
resourceIdWorker = "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameWorker
)

func getValidClusterInstance() *arov1alpha1.Cluster {
func getValidClusterInstance(operatorFlag bool) *arov1alpha1.Cluster {
return &arov1alpha1.Cluster{
Spec: arov1alpha1.ClusterSpec{
ClusterResourceGroupID: clusterResourceGroupId,
StorageSuffix: storageSuffix,
OperatorFlags: arov1alpha1.OperatorFlags{
ENABLED: "false",
ENABLED: strconv.FormatBool(operatorFlag),
},
},
}
Expand Down Expand Up @@ -76,12 +77,50 @@ func TestReconcileManager(t *testing.T) {
name string
mocks func(*mock_storage.MockAccountsClient, *mock_subnet.MockKubeManager)
imageregistryclient imageregistryclient.Interface

instance func(*arov1alpha1.Cluster)
operatorFlag bool
wantErr error
}{
{
name: "nothing to do",
name: "Operator Flag enabled - nothing to do",
operatorFlag: true,
mocks: func(storage *mock_storage.MockAccountsClient, kubeSubnet *mock_subnet.MockKubeManager) {
// cluster subnets
kubeSubnet.EXPECT().List(gomock.Any()).Return([]subnet.Subnet{
{
ResourceID: resourceIdMaster,
IsMaster: true,
},
{
ResourceID: resourceIdWorker,
IsMaster: false,
},
}, nil)

// storage objects in azure
result := getValidAccount([]string{resourceIdMaster, resourceIdWorker})
storage.EXPECT().GetProperties(gomock.Any(), clusterResourceGroupName, clusterStorageAccountName, gomock.Any()).Return(*result, nil)
storage.EXPECT().GetProperties(gomock.Any(), clusterResourceGroupName, registryStorageAccountName, gomock.Any()).Return(*result, nil)

},
imageregistryclient: imageregistryfake.NewSimpleClientset(
&imageregistryv1.Config{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
},
Spec: imageregistryv1.ImageRegistrySpec{
Storage: imageregistryv1.ImageRegistryConfigStorage{
Azure: &imageregistryv1.ImageRegistryConfigStorageAzure{
AccountName: registryStorageAccountName,
},
},
},
},
),
},
{
name: "Operator Flag disabled - nothing to do",
operatorFlag: false,
mocks: func(storage *mock_storage.MockAccountsClient, kubeSubnet *mock_subnet.MockKubeManager) {
// cluster subnets
kubeSubnet.EXPECT().List(gomock.Any()).Return([]subnet.Subnet{
Expand Down Expand Up @@ -117,7 +156,8 @@ func TestReconcileManager(t *testing.T) {
),
},
{
name: "all rules to all accounts",
name: "Operator Flag enabled - all rules to all accounts",
operatorFlag: true,
mocks: func(storage *mock_storage.MockAccountsClient, kubeSubnet *mock_subnet.MockKubeManager) {

// cluster subnets
Expand Down Expand Up @@ -182,7 +222,7 @@ func TestReconcileManager(t *testing.T) {
tt.mocks(storage, kubeSubnet)
}

instance := getValidClusterInstance()
instance := getValidClusterInstance(tt.operatorFlag)
if tt.instance != nil {
tt.instance(instance)
}
Expand Down
57 changes: 52 additions & 5 deletions pkg/operator/controllers/subnets/subnet_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package subnets
import (
"context"
"testing"
"strconv"

mgmtnetwork "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-08-01/network"
"github.com/Azure/go-autorest/autorest/to"
Expand Down Expand Up @@ -35,16 +36,16 @@ var (
subnetResourceIdWorker = "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameWorker
)

func getValidClusterInstance() *arov1alpha1.Cluster {
func getValidClusterInstance(operatorFlagEnabled bool, operatorFlagNSG bool, operatorFlagServiceEndpoint bool) *arov1alpha1.Cluster {
return &arov1alpha1.Cluster{
Spec: arov1alpha1.ClusterSpec{
ArchitectureVersion: 0,
ClusterResourceGroupID: clusterResourceGroupId,
InfraID: infraId,
OperatorFlags: arov1alpha1.OperatorFlags{
ENABLED: "true",
NSG_MANAGED: "true",
SERVICE_ENDPOINT_MANAGED: "true",
ENABLED: strconv.FormatBool(operatorFlagEnabled),
NSG_MANAGED: strconv.FormatBool(operatorFlagNSG),
SERVICE_ENDPOINT_MANAGED: strconv.FormatBool(operatorFlagServiceEndpoint),
},
},
}
Expand Down Expand Up @@ -75,10 +76,41 @@ func TestReconcileManager(t *testing.T) {
name string
subnetMock func(*mock_subnet.MockManager, *mock_subnet.MockKubeManager)
instance func(*arov1alpha1.Cluster)
operatorFlagEnabled bool
operatorFlagNSG bool
operatorFlagServiceEndpoint bool
wantErr error
}{
{
name: "Operator Disabled - no change",
operatorFlagEnabled: false,
operatorFlagNSG: false,
operatorFlagServiceEndpoint: false,
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {
kmock.EXPECT().List(gomock.Any()).Return([]subnet.Subnet{
{
ResourceID: subnetResourceIdMaster,
IsMaster: true,
},
{
ResourceID: subnetResourceIdWorker,
IsMaster: false,
},
}, nil)

subnetObjectMaster := getValidSubnet()
mock.EXPECT().Get(gomock.Any(), subnetResourceIdMaster).Return(subnetObjectMaster, nil).MaxTimes(2)

subnetObjectWorker := getValidSubnet()
subnetObjectWorker.NetworkSecurityGroup.ID = to.StringPtr(nsgv1NodeResourceId)
mock.EXPECT().Get(gomock.Any(), subnetResourceIdWorker).Return(subnetObjectWorker, nil).MaxTimes(2)
},
},
{
name: "Architecture V1 - no change",
operatorFlagEnabled: true,
operatorFlagNSG: true,
operatorFlagServiceEndpoint: true,
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {
kmock.EXPECT().List(gomock.Any()).Return([]subnet.Subnet{
{
Expand All @@ -101,6 +133,9 @@ func TestReconcileManager(t *testing.T) {
},
{
name: "Architecture V1 - all fixup",
operatorFlagEnabled: true,
operatorFlagNSG: true,
operatorFlagServiceEndpoint: true,
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {

kmock.EXPECT().List(gomock.Any()).Return([]subnet.Subnet{
Expand Down Expand Up @@ -133,6 +168,9 @@ func TestReconcileManager(t *testing.T) {
},
{
name: "Architecture V1 - node only fixup",
operatorFlagEnabled: true,
operatorFlagNSG: true,
operatorFlagServiceEndpoint: true,
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {

kmock.EXPECT().List(gomock.Any()).Return([]subnet.Subnet{
Expand Down Expand Up @@ -160,6 +198,9 @@ func TestReconcileManager(t *testing.T) {
},
{
name: "Architecture V2 - no fixups",
operatorFlagEnabled: true,
operatorFlagNSG: true,
operatorFlagServiceEndpoint: true,
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {

kmock.EXPECT().List(gomock.Any()).Return([]subnet.Subnet{
Expand Down Expand Up @@ -187,6 +228,9 @@ func TestReconcileManager(t *testing.T) {
},
{
name: "Architecture V2 - all nodes fixup",
operatorFlagEnabled: true,
operatorFlagNSG: true,
operatorFlagServiceEndpoint: true,
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {

kmock.EXPECT().List(gomock.Any()).Return([]subnet.Subnet{
Expand Down Expand Up @@ -222,6 +266,9 @@ func TestReconcileManager(t *testing.T) {
},
{
name: "Architecture V2 - endpoint fixup",
operatorFlagEnabled: true,
operatorFlagNSG: true,
operatorFlagServiceEndpoint: true,
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {

kmock.EXPECT().List(gomock.Any()).Return([]subnet.Subnet{
Expand Down Expand Up @@ -280,7 +327,7 @@ func TestReconcileManager(t *testing.T) {
tt.subnetMock(subnets, kubeSubnets)
}

instance := getValidClusterInstance()
instance := getValidClusterInstance(tt.operatorFlagEnabled, tt.operatorFlagNSG, tt.operatorFlagServiceEndpoint)
if tt.instance != nil {
tt.instance(instance)
}
Expand Down

0 comments on commit bf5ce15

Please sign in to comment.