github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/broker/instance_get_test.go (about) 1 package broker_test 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "net/http" 8 "testing" 9 "time" 10 11 "github.com/kyma-project/kyma-environment-broker/internal/euaccess" 12 13 "github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema" 14 "github.com/kyma-project/kyma-environment-broker/common/gardener" 15 "github.com/kyma-project/kyma-environment-broker/internal" 16 "github.com/kyma-project/kyma-environment-broker/internal/broker" 17 "github.com/kyma-project/kyma-environment-broker/internal/broker/automock" 18 "github.com/kyma-project/kyma-environment-broker/internal/fixture" 19 "github.com/kyma-project/kyma-environment-broker/internal/storage" 20 "github.com/pivotal-cf/brokerapi/v8/domain" 21 "github.com/pivotal-cf/brokerapi/v8/domain/apiresponses" 22 "github.com/sirupsen/logrus" 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/mock" 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestGetEndpoint_GetNonExistingInstance(t *testing.T) { 29 // given 30 st := storage.NewMemoryStorage() 31 svc := broker.NewGetInstance(broker.Config{}, st.Instances(), st.Operations(), logrus.New()) 32 33 // when 34 _, err := svc.GetInstance(context.Background(), instanceID, domain.FetchInstanceDetails{}) 35 36 // then 37 assert.IsType(t, err, &apiresponses.FailureResponse{}, "Get returned error of unexpected type") 38 apierr := err.(*apiresponses.FailureResponse) 39 assert.Equal(t, http.StatusNotFound, apierr.ValidatedStatusCode(nil), "Get status code not matching") 40 } 41 42 func TestGetEndpoint_GetProvisioningInstance(t *testing.T) { 43 // given 44 st := storage.NewMemoryStorage() 45 queue := &automock.Queue{} 46 queue.On("Add", mock.AnythingOfType("string")) 47 48 factoryBuilder := &automock.PlanValidator{} 49 factoryBuilder.On("IsPlanSupport", planID).Return(true) 50 51 planDefaults := func(planID string, platformProvider internal.CloudProvider, provider *internal.CloudProvider) (*gqlschema.ClusterConfigInput, error) { 52 return &gqlschema.ClusterConfigInput{}, nil 53 } 54 createSvc := broker.NewProvision( 55 broker.Config{EnablePlans: []string{"gcp", "azure"}, OnlySingleTrialPerGA: true}, 56 gardener.Config{Project: "test", ShootDomain: "example.com"}, 57 st.Operations(), 58 st.Instances(), 59 queue, 60 factoryBuilder, 61 broker.PlansConfig{}, 62 false, 63 planDefaults, 64 euaccess.WhitelistSet{}, 65 "request rejected, your globalAccountId is not whitelisted", 66 logrus.StandardLogger(), 67 dashboardConfig, 68 ) 69 getSvc := broker.NewGetInstance(broker.Config{EnableKubeconfigURLLabel: true}, st.Instances(), st.Operations(), logrus.New()) 70 71 // when 72 createSvc.Provision(fixRequestContext(t, "req-region"), instanceID, domain.ProvisionDetails{ 73 ServiceID: serviceID, 74 PlanID: planID, 75 RawParameters: json.RawMessage(fmt.Sprintf(`{"name": "%s"}`, clusterName)), 76 RawContext: json.RawMessage(fmt.Sprintf(`{"globalaccount_id": "%s", "subaccount_id": "%s", "user_id": "%s"}`, globalAccountID, subAccountID, userID)), 77 }, true) 78 79 // then 80 _, err := getSvc.GetInstance(context.Background(), instanceID, domain.FetchInstanceDetails{}) 81 assert.IsType(t, err, &apiresponses.FailureResponse{}, "Get returned error of unexpected type") 82 apierr := err.(*apiresponses.FailureResponse) 83 assert.Equal(t, http.StatusNotFound, apierr.ValidatedStatusCode(nil), "Get status code not matching") 84 assert.Equal(t, "provisioning of instanceID d3d5dca4-5dc8-44ee-a825-755c2a3fb839 in progress", apierr.Error()) 85 86 // when 87 op, _ := st.Operations().GetProvisioningOperationByInstanceID(instanceID) 88 op.State = domain.Succeeded 89 st.Operations().UpdateProvisioningOperation(*op) 90 91 // then 92 response, err := getSvc.GetInstance(context.Background(), instanceID, domain.FetchInstanceDetails{}) 93 assert.Equal(t, nil, err, "Get returned error when expected to pass") 94 assert.Len(t, response.Metadata.Labels, 2) 95 } 96 97 func TestGetEndpoint_DoNotReturnInstanceWhereDeletedAtIsNotZero(t *testing.T) { 98 // given 99 st := storage.NewMemoryStorage() 100 cfg := broker.Config{ 101 URL: "https://test-broker.local", 102 EnableKubeconfigURLLabel: true, 103 ShowTrialExpirationInfo: true, 104 SubaccountsIdsToShowTrialExpirationInfo: "test-saID", 105 } 106 107 const ( 108 instanceID = "cluster-test" 109 operationID = "operationID" 110 ) 111 op := fixture.FixProvisioningOperation(operationID, instanceID) 112 113 instance := fixture.FixInstance(instanceID) 114 instance.DeletedAt = time.Now() 115 116 err := st.Operations().InsertOperation(op) 117 require.NoError(t, err) 118 119 err = st.Instances().Insert(instance) 120 require.NoError(t, err) 121 122 svc := broker.NewGetInstance(cfg, st.Instances(), st.Operations(), logrus.New()) 123 124 // when 125 _, err = svc.GetInstance(context.Background(), instanceID, domain.FetchInstanceDetails{}) 126 127 // then 128 assert.IsType(t, err, &apiresponses.FailureResponse{}, "Get request returned error of unexpected type") 129 apierr := err.(*apiresponses.FailureResponse) 130 assert.Equal(t, http.StatusNotFound, apierr.ValidatedStatusCode(nil), "Get request status code not matching") 131 } 132 133 func TestGetEndpoint_GetExpiredInstanceWithExpirationDetails(t *testing.T) { 134 // given 135 st := storage.NewMemoryStorage() 136 cfg := broker.Config{ 137 URL: "https://test-broker.local", 138 EnableKubeconfigURLLabel: true, 139 ShowTrialExpirationInfo: true, 140 SubaccountsIdsToShowTrialExpirationInfo: "test-saID", 141 } 142 143 const ( 144 instanceID = "cluster-test" 145 operationID = "operationID" 146 ) 147 op := fixture.FixProvisioningOperation(operationID, instanceID) 148 149 instance := fixture.FixInstance(instanceID) 150 instance.SubAccountID = cfg.SubaccountsIdsToShowTrialExpirationInfo 151 instance.ServicePlanID = broker.TrialPlanID 152 instance.CreatedAt = time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) 153 expireTime := instance.CreatedAt.Add(time.Hour * 24 * 14) 154 instance.ExpiredAt = &expireTime 155 156 err := st.Operations().InsertOperation(op) 157 require.NoError(t, err) 158 159 err = st.Instances().Insert(instance) 160 require.NoError(t, err) 161 162 svc := broker.NewGetInstance(cfg, st.Instances(), st.Operations(), logrus.New()) 163 164 // when 165 response, err := svc.GetInstance(context.Background(), instanceID, domain.FetchInstanceDetails{}) 166 167 // then 168 require.NoError(t, err) 169 assert.True(t, instance.IsExpired()) 170 assert.Equal(t, instance.ServiceID, response.ServiceID) 171 assert.NotContains(t, response.Metadata.Labels, "KubeconfigURL") 172 assert.Contains(t, response.Metadata.Labels, "Trial expiration details") 173 assert.Contains(t, response.Metadata.Labels, "Trial documentation") 174 } 175 176 func TestGetEndpoint_GetExpiredInstanceWithExpirationDetailsAllSubaccountsIDs(t *testing.T) { 177 // given 178 st := storage.NewMemoryStorage() 179 cfg := broker.Config{ 180 URL: "https://test-broker.local", 181 EnableKubeconfigURLLabel: true, 182 ShowTrialExpirationInfo: true, 183 SubaccountsIdsToShowTrialExpirationInfo: "all", 184 } 185 186 const ( 187 instanceID = "cluster-test" 188 operationID = "operationID" 189 ) 190 op := fixture.FixProvisioningOperation(operationID, instanceID) 191 192 instance := fixture.FixInstance(instanceID) 193 instance.SubAccountID = "test-subaccount-id" 194 instance.ServicePlanID = broker.TrialPlanID 195 instance.CreatedAt = time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) 196 expireTime := instance.CreatedAt.Add(time.Hour * 24 * 14) 197 instance.ExpiredAt = &expireTime 198 199 err := st.Operations().InsertOperation(op) 200 require.NoError(t, err) 201 202 err = st.Instances().Insert(instance) 203 require.NoError(t, err) 204 205 svc := broker.NewGetInstance(cfg, st.Instances(), st.Operations(), logrus.New()) 206 207 // when 208 response, err := svc.GetInstance(context.Background(), instanceID, domain.FetchInstanceDetails{}) 209 210 // then 211 require.NoError(t, err) 212 assert.True(t, instance.IsExpired()) 213 assert.Equal(t, instance.ServiceID, response.ServiceID) 214 assert.NotContains(t, response.Metadata.Labels, "KubeconfigURL") 215 assert.Contains(t, response.Metadata.Labels, "Trial expiration details") 216 assert.Contains(t, response.Metadata.Labels, "Trial documentation") 217 } 218 219 func TestGetEndpoint_GetExpiredInstanceWithoutExpirationInfo(t *testing.T) { 220 // given 221 st := storage.NewMemoryStorage() 222 cfg := broker.Config{ 223 URL: "https://test-broker.local", 224 EnableKubeconfigURLLabel: true, 225 ShowTrialExpirationInfo: true, 226 SubaccountsIdsToShowTrialExpirationInfo: "subaccount-id1,subaccount-id2", 227 } 228 229 const ( 230 instanceID = "cluster-test" 231 operationID = "operationID" 232 ) 233 op := fixture.FixProvisioningOperation(operationID, instanceID) 234 235 instance := fixture.FixInstance(instanceID) 236 instance.SubAccountID = "subaccount-id3" 237 instance.ServicePlanID = broker.TrialPlanID 238 instance.CreatedAt = time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) 239 expireTime := instance.CreatedAt.Add(time.Hour * 24 * 14) 240 instance.ExpiredAt = &expireTime 241 242 err := st.Operations().InsertOperation(op) 243 require.NoError(t, err) 244 245 err = st.Instances().Insert(instance) 246 require.NoError(t, err) 247 248 svc := broker.NewGetInstance(cfg, st.Instances(), st.Operations(), logrus.New()) 249 250 // when 251 response, err := svc.GetInstance(context.Background(), instanceID, domain.FetchInstanceDetails{}) 252 253 // then 254 require.NoError(t, err) 255 assert.True(t, instance.IsExpired()) 256 assert.Equal(t, instance.ServiceID, response.ServiceID) 257 assert.Contains(t, response.Metadata.Labels, "KubeconfigURL") 258 assert.NotContains(t, response.Metadata.Labels, "Trial expiration details") 259 assert.NotContains(t, response.Metadata.Labels, "Trial documentation") 260 }