|
| 1 | +// Copyright 2025 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//go:build unit |
| 16 | + |
| 17 | +package project |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/golang/mock/gomock" |
| 24 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/kubernetes/operator/features" |
| 25 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/kubernetes/operator/resources" |
| 26 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks" |
| 27 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" |
| 28 | + "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" |
| 29 | + akoapi "github.com/mongodb/mongodb-atlas-kubernetes/v2/api" |
| 30 | + akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1" |
| 31 | + akov2common "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/common" |
| 32 | + akov2status "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1/status" |
| 33 | + "github.com/stretchr/testify/assert" |
| 34 | + "github.com/stretchr/testify/require" |
| 35 | + "go.mongodb.org/atlas-sdk/v20241113004/admin" |
| 36 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 37 | +) |
| 38 | + |
| 39 | +func TestBuildIPAccessList(t *testing.T) { |
| 40 | + projectID := "project-ial-id" |
| 41 | + projectName := "projectName-ial" |
| 42 | + targetNamespace := "ialNamespace" |
| 43 | + credentialName := "ial-creds" |
| 44 | + deleteAfter := metav1.NewTime(time.Now().Add(time.Hour)) |
| 45 | + |
| 46 | + tests := map[string]struct { |
| 47 | + ipAccessList []admin.NetworkPermissionEntry |
| 48 | + independentResource bool |
| 49 | + expectedEmpty bool |
| 50 | + expectedResource *akov2.AtlasIPAccessList |
| 51 | + }{ |
| 52 | + "ip access list is empty": { |
| 53 | + ipAccessList: []admin.NetworkPermissionEntry{}, |
| 54 | + expectedEmpty: true, |
| 55 | + }, |
| 56 | + "generate ip access list with kubernetes project reference": { |
| 57 | + ipAccessList: []admin.NetworkPermissionEntry{ |
| 58 | + { |
| 59 | + IpAddress: pointer.Get("192.168.100.233"), |
| 60 | + Comment: pointer.Get("My private access"), |
| 61 | + }, |
| 62 | + { |
| 63 | + CidrBlock: pointer.Get("10.1.1.0/24"), |
| 64 | + Comment: pointer.Get("Company network"), |
| 65 | + }, |
| 66 | + { |
| 67 | + AwsSecurityGroup: pointer.Get("sg-123456"), |
| 68 | + Comment: pointer.Get("Cloud network"), |
| 69 | + }, |
| 70 | + { |
| 71 | + IpAddress: pointer.Get("172.16.100.10"), |
| 72 | + DeleteAfterDate: pointer.Get(deleteAfter.Time), |
| 73 | + Comment: pointer.Get("Third party temporary access"), |
| 74 | + }, |
| 75 | + }, |
| 76 | + expectedResource: &akov2.AtlasIPAccessList{ |
| 77 | + TypeMeta: metav1.TypeMeta{ |
| 78 | + Kind: "AtlasIPAccessList", |
| 79 | + APIVersion: "atlas.mongodb.com/v1", |
| 80 | + }, |
| 81 | + ObjectMeta: metav1.ObjectMeta{ |
| 82 | + Name: "projectname-ial-ip-access-list", |
| 83 | + Namespace: targetNamespace, |
| 84 | + Labels: map[string]string{ |
| 85 | + features.ResourceVersion: "2.7.0", |
| 86 | + }, |
| 87 | + }, |
| 88 | + Spec: akov2.AtlasIPAccessListSpec{ |
| 89 | + ProjectDualReference: akov2.ProjectDualReference{ |
| 90 | + ProjectRef: &akov2common.ResourceRefNamespaced{ |
| 91 | + Name: projectName, |
| 92 | + Namespace: targetNamespace, |
| 93 | + }, |
| 94 | + }, |
| 95 | + Entries: []akov2.IPAccessEntry{ |
| 96 | + { |
| 97 | + IPAddress: "192.168.100.233", |
| 98 | + Comment: "My private access", |
| 99 | + }, |
| 100 | + { |
| 101 | + CIDRBlock: "10.1.1.0/24", |
| 102 | + Comment: "Company network", |
| 103 | + }, |
| 104 | + { |
| 105 | + AwsSecurityGroup: "sg-123456", |
| 106 | + Comment: "Cloud network", |
| 107 | + }, |
| 108 | + { |
| 109 | + IPAddress: "172.16.100.10", |
| 110 | + DeleteAfterDate: pointer.Get(deleteAfter), |
| 111 | + Comment: "Third party temporary access", |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + Status: akov2status.AtlasIPAccessListStatus{ |
| 116 | + Common: akoapi.Common{ |
| 117 | + Conditions: []akoapi.Condition{}, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + "generate ip access list with external project reference": { |
| 123 | + ipAccessList: []admin.NetworkPermissionEntry{ |
| 124 | + { |
| 125 | + IpAddress: pointer.Get("192.168.100.233"), |
| 126 | + Comment: pointer.Get("My private access"), |
| 127 | + }, |
| 128 | + { |
| 129 | + CidrBlock: pointer.Get("10.1.1.0/24"), |
| 130 | + Comment: pointer.Get("Company network"), |
| 131 | + }, |
| 132 | + }, |
| 133 | + independentResource: true, |
| 134 | + expectedResource: &akov2.AtlasIPAccessList{ |
| 135 | + TypeMeta: metav1.TypeMeta{ |
| 136 | + Kind: "AtlasIPAccessList", |
| 137 | + APIVersion: "atlas.mongodb.com/v1", |
| 138 | + }, |
| 139 | + ObjectMeta: metav1.ObjectMeta{ |
| 140 | + Name: "projectname-ial-ip-access-list", |
| 141 | + Namespace: targetNamespace, |
| 142 | + Labels: map[string]string{ |
| 143 | + features.ResourceVersion: "2.7.0", |
| 144 | + }, |
| 145 | + }, |
| 146 | + Spec: akov2.AtlasIPAccessListSpec{ |
| 147 | + ProjectDualReference: akov2.ProjectDualReference{ |
| 148 | + ExternalProjectRef: &akov2.ExternalProjectReference{ |
| 149 | + ID: projectID, |
| 150 | + }, |
| 151 | + ConnectionSecret: &akoapi.LocalObjectReference{ |
| 152 | + Name: credentialName, |
| 153 | + }, |
| 154 | + }, |
| 155 | + Entries: []akov2.IPAccessEntry{ |
| 156 | + { |
| 157 | + IPAddress: "192.168.100.233", |
| 158 | + Comment: "My private access", |
| 159 | + }, |
| 160 | + { |
| 161 | + CIDRBlock: "10.1.1.0/24", |
| 162 | + Comment: "Company network", |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + Status: akov2status.AtlasIPAccessListStatus{ |
| 167 | + Common: akoapi.Common{ |
| 168 | + Conditions: []akoapi.Condition{}, |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + } |
| 174 | + for name, tt := range tests { |
| 175 | + t.Run(name, func(t *testing.T) { |
| 176 | + ctl := gomock.NewController(t) |
| 177 | + ialStore := mocks.NewMockProjectIPAccessListLister(ctl) |
| 178 | + dictionary := resources.AtlasNameToKubernetesName() |
| 179 | + |
| 180 | + ialStore.EXPECT().ProjectIPAccessLists(projectID, &store.ListOptions{ItemsPerPage: MaxItems}). |
| 181 | + Return(&admin.PaginatedNetworkAccess{Results: &tt.ipAccessList}, nil) |
| 182 | + |
| 183 | + atlasIPAccessList, isEmpty, err := BuildIPAccessList( |
| 184 | + ialStore, |
| 185 | + IPAccessListRequest{ |
| 186 | + ProjectName: projectName, |
| 187 | + ProjectID: projectID, |
| 188 | + TargetNamespace: targetNamespace, |
| 189 | + Version: "2.7.0", |
| 190 | + Credentials: credentialName, |
| 191 | + IndependentResource: tt.independentResource, |
| 192 | + Dictionary: dictionary, |
| 193 | + }, |
| 194 | + ) |
| 195 | + require.NoError(t, err) |
| 196 | + assert.Equal(t, tt.expectedEmpty, isEmpty) |
| 197 | + assert.Equal(t, tt.expectedResource, atlasIPAccessList) |
| 198 | + }) |
| 199 | + } |
| 200 | +} |
0 commit comments