Skip to content

Commit

Permalink
chore: introduce additional unit tests for api packages (#420)
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
Co-authored-by: Michael Beemer <beeme1mr@users.noreply.github.com>
  • Loading branch information
odubajDT and beeme1mr authored Mar 30, 2023
1 parent 4290978 commit 5ba5bc9
Show file tree
Hide file tree
Showing 10 changed files with 426 additions and 37 deletions.
18 changes: 3 additions & 15 deletions apis/core/v1alpha1/featureflagconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package v1alpha1

import (
"fmt"

"github.com/open-feature/open-feature-operator/pkg/utils"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -110,7 +108,7 @@ func init() {
SchemeBuilder.Register(&FeatureFlagConfiguration{}, &FeatureFlagConfigurationList{})
}

func GetFfReference(ff *FeatureFlagConfiguration) metav1.OwnerReference {
func (ff *FeatureFlagConfiguration) GetReference() metav1.OwnerReference {
return metav1.OwnerReference{
APIVersion: ff.APIVersion,
Kind: ff.Kind,
Expand All @@ -120,7 +118,7 @@ func GetFfReference(ff *FeatureFlagConfiguration) metav1.OwnerReference {
}
}

func GenerateFfConfigMap(name string, namespace string, references []metav1.OwnerReference, spec FeatureFlagConfigurationSpec) corev1.ConfigMap {
func (ff *FeatureFlagConfiguration) GenerateConfigMap(name string, namespace string, references []metav1.OwnerReference) corev1.ConfigMap {
return corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -131,21 +129,11 @@ func GenerateFfConfigMap(name string, namespace string, references []metav1.Owne
OwnerReferences: references,
},
Data: map[string]string{
FeatureFlagConfigurationConfigMapKey(namespace, name): spec.FeatureFlagSpec,
utils.FeatureFlagConfigurationConfigMapKey(namespace, name): ff.Spec.FeatureFlagSpec,
},
}
}

// unique string used to create unique volume mount and file name
func FeatureFlagConfigurationId(namespace, name string) string {
return fmt.Sprintf("%s_%s", namespace, name)
}

// unique key (and filename) for configMap data
func FeatureFlagConfigurationConfigMapKey(namespace, name string) string {
return fmt.Sprintf("%s.flagd.json", FeatureFlagConfigurationId(namespace, name))
}

func (p *FeatureFlagServiceProvider) IsSet() bool {
return p != nil && p.Name != ""
}
80 changes: 80 additions & 0 deletions apis/core/v1alpha1/featureflagconfiguration_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package v1alpha1

import (
"testing"

"github.com/open-feature/open-feature-operator/pkg/utils"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func Test_FeatureFlagConfiguration(t *testing.T) {
ffConfig := FeatureFlagConfiguration{
ObjectMeta: v1.ObjectMeta{
Name: "ffconf1",
Namespace: "test",
OwnerReferences: []v1.OwnerReference{
{
APIVersion: "ver",
Kind: "kind",
Name: "ffconf1",
UID: types.UID("5"),
Controller: utils.TrueVal(),
},
},
},
Spec: FeatureFlagConfigurationSpec{
FeatureFlagSpec: "spec",
},
}

name := "cmname"
namespace := "cmnamespace"
references := []v1.OwnerReference{
{
APIVersion: "ver",
Kind: "kind",
Name: "ffconf1",
UID: types.UID("5"),
Controller: utils.TrueVal(),
},
}

require.Equal(t, v1.OwnerReference{
APIVersion: ffConfig.APIVersion,
Kind: ffConfig.Kind,
Name: ffConfig.Name,
UID: ffConfig.UID,
Controller: utils.TrueVal(),
}, ffConfig.GetReference())

require.Equal(t, corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: map[string]string{
"openfeature.dev/featureflagconfiguration": name,
},
OwnerReferences: references,
},
Data: map[string]string{
"cmnamespace_cmname.flagd.json": "spec",
},
}, ffConfig.GenerateConfigMap(name, namespace, references))

require.False(t, ffConfig.Spec.ServiceProvider.IsSet())

ffConfig.Spec.ServiceProvider = &FeatureFlagServiceProvider{
Name: "",
}

require.False(t, ffConfig.Spec.ServiceProvider.IsSet())

ffConfig.Spec.ServiceProvider = &FeatureFlagServiceProvider{
Name: "prov",
}

require.True(t, ffConfig.Spec.ServiceProvider.IsSet())
}
4 changes: 2 additions & 2 deletions apis/core/v1alpha1/flagsourceconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,15 @@ func NewFlagSourceConfigurationSpec() (*FlagSourceConfigurationSpec, error) {
fsc.DefaultSyncProvider = SyncProviderType(syncProvider)
}

if logFormat := os.Getenv(fmt.Sprintf("%s_%s", InputConfigurationEnvVarPrefix, SidecarLogFormatEnvVar)); logFormat != "" {
if logFormat := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarLogFormatEnvVar)); logFormat != "" {
fsc.LogFormat = logFormat
}

if envVarPrefix := os.Getenv(SidecarEnvVarPrefix); envVarPrefix != "" {
fsc.EnvVarPrefix = envVarPrefix
}

if probesEnabled := os.Getenv(fmt.Sprintf("%s_%s", InputConfigurationEnvVarPrefix, SidecarProbesEnabledVar)); probesEnabled != "" {
if probesEnabled := os.Getenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarProbesEnabledVar)); probesEnabled != "" {
b, err := strconv.ParseBool(probesEnabled)
if err != nil {
return fsc, fmt.Errorf("unable to parse sidecar probes enabled %s to boolean: %w", probesEnabled, err)
Expand Down
Loading

0 comments on commit 5ba5bc9

Please sign in to comment.