Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardChen820 committed Mar 4, 2024
1 parent f07faa3 commit ef366bf
Showing 1 changed file with 287 additions and 0 deletions.
287 changes: 287 additions & 0 deletions internal/controller/appconfigurationprovider_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,293 @@ var _ = Describe("AppConfiguationProvider controller", func() {
Expect(configmap.Data["filestyle.json"]).Should(Equal("{\"testKey\":\"testValue\",\"feature_management\":{\"feature_flags\":[{\"id\": \"testFeatureFlag\",\"enabled\": true,\"conditions\": {\"client_filters\": []}}]}}"))
Expect(len(configmap.Data)).Should(Equal(1))
})

It("Should refresh configMap", func() {
By("By updating the provider and trigger reconciliation")
mapResult := make(map[string]string)
mapResult["testKey"] = "testValue"
mapResult["testKey2"] = "testValue2"
mapResult["testKey3"] = "testValue3"

allSettings := &loader.TargetKeyValueSettings{
ConfigMapSettings: mapResult,
}

mockConfigurationSettings.EXPECT().CreateTargetSettings(gomock.Any(), gomock.Any()).Return(allSettings, nil)

ctx := context.Background()
providerName := "refresh-appconfigurationprovider-1"
configMapName := "configmap-to-be-refresh-1"
configProvider := &acpv1.AzureAppConfigurationProvider{
TypeMeta: metav1.TypeMeta{
APIVersion: "appconfig.kubernetes.config/v1",
Kind: "AzureAppConfigurationProvider",
},
ObjectMeta: metav1.ObjectMeta{
Name: providerName,
Namespace: ProviderNamespace,
Labels: map[string]string{"foo": "fooValue", "bar": "barValue"},
},
Spec: acpv1.AzureAppConfigurationProviderSpec{
Endpoint: &EndpointName,
Target: acpv1.ConfigurationGenerationParameters{
ConfigMapName: configMapName,
},
},
}
Expect(k8sClient.Create(ctx, configProvider)).Should(Succeed())
configmapLookupKey := types.NamespacedName{Name: configMapName, Namespace: ProviderNamespace}
configmap := &corev1.ConfigMap{}

Eventually(func() bool {
err := k8sClient.Get(ctx, configmapLookupKey, configmap)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(configmap.Name).Should(Equal(configMapName))
Expect(configmap.Namespace).Should(Equal(ProviderNamespace))
Expect(configmap.Labels["foo"]).Should(Equal("fooValue"))
Expect(configmap.Labels["bar"]).Should(Equal("barValue"))
Expect(configmap.Data["testKey"]).Should(Equal("testValue"))
Expect(configmap.Data["testKey2"]).Should(Equal("testValue2"))
Expect(configmap.Data["testKey3"]).Should(Equal("testValue3"))

newEndpoint := "https://fake-endpoint-2"

mapResult2 := make(map[string]string)
mapResult2["testKey"] = "newtestValue"
mapResult2["testKey2"] = "newtestValue2"
mapResult2["testKey3"] = "newtestValue3"

allSettings2 := &loader.TargetKeyValueSettings{
ConfigMapSettings: mapResult2,
}

mockConfigurationSettings.EXPECT().CreateTargetSettings(gomock.Any(), gomock.Any()).Return(allSettings2, nil)

k8sClient.Get(ctx, types.NamespacedName{Name: providerName, Namespace: ProviderNamespace}, configProvider)
configProvider.Spec.Endpoint = &newEndpoint

Expect(k8sClient.Update(ctx, configProvider)).Should(Succeed())

time.Sleep(5 * time.Second)

Eventually(func() bool {
err := k8sClient.Get(ctx, configmapLookupKey, configmap)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(configmap.Data["testKey"]).Should(Equal("newtestValue"))
Expect(configmap.Data["testKey2"]).Should(Equal("newtestValue2"))
Expect(configmap.Data["testKey3"]).Should(Equal("newtestValue3"))

})

It("Should refresh configMap", func() {
By("By sentinel value updated in Azure App Configuration")
mapResult := make(map[string]string)
mapResult["testKey"] = "testValue"
mapResult["testKey2"] = "testValue2"
mapResult["testKey3"] = "testValue3"

allSettings := &loader.TargetKeyValueSettings{
ConfigMapSettings: mapResult,
}

mapResult2 := make(map[string]string)
mapResult2["testKey"] = "newtestValue"
mapResult2["testKey2"] = "newtestValue2"
mapResult2["testKey3"] = "newtestValue3"

allSettings2 := &loader.TargetKeyValueSettings{
ConfigMapSettings: mapResult2,
}

mockConfigurationSettings.EXPECT().CreateTargetSettings(gomock.Any(), gomock.Any()).Return(allSettings, nil)
mockConfigurationSettings.EXPECT().CheckAndRefreshSentinels(gomock.Any(), gomock.Any(), gomock.Any()).Return(true, nil, nil)
mockConfigurationSettings.EXPECT().RefreshKeyValueSettings(gomock.Any(), gomock.Any(), gomock.Any()).Return(allSettings2, nil)

ctx := context.Background()
providerName := "refresh-appconfigurationprovider-2"
configMapName := "configmap-to-be-refresh-2"
configProvider := &acpv1.AzureAppConfigurationProvider{
TypeMeta: metav1.TypeMeta{
APIVersion: "appconfig.kubernetes.config/v1",
Kind: "AzureAppConfigurationProvider",
},
ObjectMeta: metav1.ObjectMeta{
Name: providerName,
Namespace: ProviderNamespace,
Labels: map[string]string{"foo": "fooValue", "bar": "barValue"},
},
Spec: acpv1.AzureAppConfigurationProviderSpec{
Endpoint: &EndpointName,
Target: acpv1.ConfigurationGenerationParameters{
ConfigMapName: configMapName,
},
Configuration: acpv1.AzureAppConfigurationKeyValueOptions{
Refresh: &acpv1.DynamicConfigurationRefreshParameters{
Interval: "5s",
Enabled: true,
Monitoring: &acpv1.RefreshMonitoring{
Sentinels: []acpv1.Sentinel{
{Key: "testKey", Label: "testLabel"},
},
},
},
},
},
}
Expect(k8sClient.Create(ctx, configProvider)).Should(Succeed())
configmapLookupKey := types.NamespacedName{Name: configMapName, Namespace: ProviderNamespace}
configmap := &corev1.ConfigMap{}

Eventually(func() bool {
err := k8sClient.Get(ctx, configmapLookupKey, configmap)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(configmap.Name).Should(Equal(configMapName))
Expect(configmap.Namespace).Should(Equal(ProviderNamespace))
Expect(configmap.Labels["foo"]).Should(Equal("fooValue"))
Expect(configmap.Labels["bar"]).Should(Equal("barValue"))
Expect(configmap.Data["testKey"]).Should(Equal("testValue"))
Expect(configmap.Data["testKey2"]).Should(Equal("testValue2"))
Expect(configmap.Data["testKey3"]).Should(Equal("testValue3"))

time.Sleep(6 * time.Second)

Eventually(func() bool {
err := k8sClient.Get(ctx, configmapLookupKey, configmap)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(configmap.Data["testKey"]).Should(Equal("newtestValue"))
Expect(configmap.Data["testKey2"]).Should(Equal("newtestValue2"))
Expect(configmap.Data["testKey3"]).Should(Equal("newtestValue3"))

k8sClient.Delete(ctx, configProvider)
})

It("Should refresh secret", func() {
By("By enabling refresh on secret")
configMapResult := make(map[string]string)
configMapResult["testKey"] = "testValue"
configMapResult["testKey2"] = "testValue2"
configMapResult["testKey3"] = "testValue3"

secretResult := make(map[string][]byte)
secretResult["testSecretKey"] = []byte("testSecretValue")
secretResult["testSecretKey2"] = []byte("testSecretValue2")
secretResult["testSecretKey3"] = []byte("testSecretValue3")

secretName := "secret-to-be-refreshed-3"
allSettings := &loader.TargetKeyValueSettings{
SecretSettings: map[string]corev1.Secret{
secretName: {
Data: secretResult,
Type: corev1.SecretType("Opaque"),
},
},
ConfigMapSettings: configMapResult,
SecretReferences: map[string]*loader.TargetSecretReference{
secretName: {
Type: corev1.SecretType("Opaque"),
UriSegments: make(map[string]loader.KeyVaultSecretUriSegment),
},
},
}

mockConfigurationSettings.EXPECT().CreateTargetSettings(gomock.Any(), gomock.Any()).Return(allSettings, nil)

ctx := context.Background()
providerName := "refresh-appconfigurationprovider-3"
configMapName := "configmap-to-be-refreshed-3"

configProvider := &acpv1.AzureAppConfigurationProvider{
TypeMeta: metav1.TypeMeta{
APIVersion: "appconfig.kubernetes.config/v1",
Kind: "AppConfigurationProvider",
},
ObjectMeta: metav1.ObjectMeta{
Name: providerName,
Namespace: ProviderNamespace,
},
Spec: acpv1.AzureAppConfigurationProviderSpec{
Endpoint: &EndpointName,
Target: acpv1.ConfigurationGenerationParameters{
ConfigMapName: configMapName,
},
Secret: &acpv1.SecretReference{
Target: acpv1.SecretGenerationParameters{
SecretName: secretName,
},
Refresh: &acpv1.RefreshSettings{
Interval: "1m",
Enabled: true,
},
},
},
}
Expect(k8sClient.Create(ctx, configProvider)).Should(Succeed())
configmapLookupKey := types.NamespacedName{Name: configMapName, Namespace: ProviderNamespace}
configmap := &corev1.ConfigMap{}

Eventually(func() bool {
err := k8sClient.Get(ctx, configmapLookupKey, configmap)
if err != nil {
fmt.Print(err.Error())
}
return err == nil
}, timeout, interval).Should(BeTrue())

secretLookupKey := types.NamespacedName{Name: secretName, Namespace: ProviderNamespace}
secret := &corev1.Secret{}

Eventually(func() bool {
err := k8sClient.Get(ctx, secretLookupKey, secret)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(configmap.Name).Should(Equal(configMapName))
Expect(configmap.Namespace).Should(Equal(ProviderNamespace))
Expect(configmap.Data["testKey"]).Should(Equal("testValue"))
Expect(configmap.Data["testKey2"]).Should(Equal("testValue2"))
Expect(configmap.Data["testKey3"]).Should(Equal("testValue3"))

Expect(secret.Namespace).Should(Equal(ProviderNamespace))
Expect(string(secret.Data["testSecretKey"])).Should(Equal("testSecretValue"))
Expect(string(secret.Data["testSecretKey2"])).Should(Equal("testSecretValue2"))
Expect(string(secret.Data["testSecretKey3"])).Should(Equal("testSecretValue3"))
Expect(secret.Type).Should(Equal(corev1.SecretType("Opaque")))

newSecretResult := make(map[string][]byte)
newSecretResult["testSecretKey"] = []byte("newTestSecretValue")
newSecretResult["testSecretKey2"] = []byte("newTestSecretValue2")
newSecretResult["testSecretKey3"] = []byte("newTestSecretValue3")

newResolvedSecret := map[string]corev1.Secret{
secretName: {
Data: newSecretResult,
Type: corev1.SecretType("Opaque"),
},
}

mockConfigurationSettings.EXPECT().ResolveSecretReferences(gomock.Any(), gomock.Any(), gomock.Any()).Return(newResolvedSecret, nil)
// Refresh interval is 1 minute, wait for 65 seconds to make sure the refresh is triggered
time.Sleep(65 * time.Second)

Eventually(func() bool {
err := k8sClient.Get(ctx, secretLookupKey, secret)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(secret.Namespace).Should(Equal(ProviderNamespace))
Expect(string(secret.Data["testSecretKey"])).Should(Equal("newTestSecretValue"))
Expect(string(secret.Data["testSecretKey2"])).Should(Equal("newTestSecretValue2"))
Expect(string(secret.Data["testSecretKey3"])).Should(Equal("newTestSecretValue3"))
Expect(secret.Type).Should(Equal(corev1.SecretType("Opaque")))
})
})

Context("Verify exist non escaped value in label", func() {
Expand Down

0 comments on commit ef366bf

Please sign in to comment.