github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/broker/response_labels_test.go (about) 1 package broker 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/kyma-project/kyma-environment-broker/internal" 9 "github.com/kyma-project/kyma-environment-broker/internal/fixture" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestResponseLabels(t *testing.T) { 15 t.Run("return all labels", func(t *testing.T) { 16 // given 17 operation := internal.ProvisioningOperation{} 18 operation.ProvisioningParameters.Parameters.Name = "test" 19 20 instance := internal.Instance{InstanceID: "inst1234", DashboardURL: "https://console.dashbord.test"} 21 22 // when 23 labels := ResponseLabels(operation, instance, "https://example.com", true) 24 25 // then 26 require.Len(t, labels, 2) 27 require.Equal(t, "test", labels["Name"]) 28 require.Equal(t, "https://example.com/kubeconfig/inst1234", labels["KubeconfigURL"]) 29 }) 30 31 t.Run("disable kubeconfig URL label", func(t *testing.T) { 32 // given 33 operation := internal.ProvisioningOperation{} 34 operation.ProvisioningParameters.Parameters.Name = "test" 35 instance := internal.Instance{} 36 37 // when 38 labels := ResponseLabels(operation, instance, "https://console.example.com", false) 39 40 // then 41 require.Len(t, labels, 1) 42 require.Equal(t, "test", labels["Name"]) 43 }) 44 45 t.Run("should return labels with expire info for not expired instance", func(t *testing.T) { 46 // given 47 operation := internal.ProvisioningOperation{} 48 operation.ProvisioningParameters.Parameters.Name = "cluster-test" 49 50 instance := fixture.FixInstance("instanceID") 51 52 // when 53 labels := ResponseLabelsWithExpirationInfo(operation, instance, "https://example.com", "https://trial.docs.local", true) 54 55 // then 56 require.Len(t, labels, 3) 57 assert.Contains(t, labels, trialExpiryDetailsKey) 58 require.Equal(t, "cluster-test", labels["Name"]) 59 require.Equal(t, "https://example.com/kubeconfig/instanceID", labels["KubeconfigURL"]) 60 }) 61 62 t.Run("should return labels with expire info for instance soon to be expired", func(t *testing.T) { 63 // given 64 operation := internal.ProvisioningOperation{} 65 operation.ProvisioningParameters.Parameters.Name = "cluster-test" 66 67 instance := fixture.FixInstance("instanceID") 68 instance.CreatedAt = time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) 69 70 expectedMsg := fmt.Sprintf(notExpiredInfoFormat, "today") 71 72 // when 73 labels := ResponseLabelsWithExpirationInfo(operation, instance, "https://example.com", "https://trial.docs.local", true) 74 75 // then 76 require.Len(t, labels, 3) 77 assert.Contains(t, labels, trialExpiryDetailsKey) 78 assert.Contains(t, labels, kubeconfigURLKey) 79 require.Equal(t, "cluster-test", labels["Name"]) 80 assert.Equal(t, expectedMsg, labels[trialExpiryDetailsKey]) 81 }) 82 83 t.Run("should return labels with expire info for expired instance", func(t *testing.T) { 84 // given 85 operation := internal.ProvisioningOperation{} 86 operation.ProvisioningParameters.Parameters.Name = "cluster-test" 87 88 instance := fixture.FixInstance("instanceID") 89 instance.CreatedAt = time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC) 90 expiryDate := time.Date(2022, 1, 15, 0, 0, 0, 0, time.UTC) 91 instance.ExpiredAt = &expiryDate 92 93 // when 94 labels := ResponseLabelsWithExpirationInfo(operation, instance, "https://example.com", "https://trial.docs.local", true) 95 96 // then 97 require.Len(t, labels, 3) 98 assert.Contains(t, labels, trialExpiryDetailsKey) 99 assert.Contains(t, labels, trialDocsKey) 100 assert.NotContains(t, labels, kubeconfigURLKey) 101 require.Equal(t, "cluster-test", labels["Name"]) 102 }) 103 104 t.Run("should return labels for own cluster", func(t *testing.T) { 105 // given 106 operation := internal.ProvisioningOperation{} 107 operation.ProvisioningParameters.Parameters.Name = "cluster-test" 108 109 instance := fixture.FixInstance("instanceID") 110 instance.ServicePlanID = OwnClusterPlanID 111 112 // when 113 labels := ResponseLabelsWithExpirationInfo(operation, instance, "https://example.com", "https://trial.docs.local", true) 114 115 // then 116 require.Len(t, labels, 2) 117 assert.NotContains(t, labels, kubeconfigURLKey) 118 require.Equal(t, "cluster-test", labels["Name"]) 119 }) 120 }