Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Nov 8, 2023
1 parent f0657ec commit d6ec78b
Show file tree
Hide file tree
Showing 10 changed files with 468 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ jobs:
IMG=open-feature-operator-local:${{ github.sha }} make deploy-operator
IMG=open-feature-operator-local:${{ github.sha }} make e2e-test-kuttl
- name: Create reports
if: always()
if: failure()
working-directory: ./.github/scripts
run: ./create-reports.sh

- name: Upload cluster logs
if: always()
if: failure()
uses: actions/upload-artifact@v3
with:
name: e2e-tests
Expand Down
70 changes: 70 additions & 0 deletions apis/core/v1beta1/featureflag_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package v1beta1

import (
"testing"

"github.com/open-feature/open-feature-operator/common/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_FeatureFlag(t *testing.T) {
ff := FeatureFlag{
ObjectMeta: v1.ObjectMeta{
Name: "ffconf1",
Namespace: "test",
OwnerReferences: []v1.OwnerReference{
{
APIVersion: "ver",
Kind: "kind",
Name: "ffconf1",
UID: types.UID("5"),
Controller: utils.TrueVal(),
},
},
},
Spec: FeatureFlagSpec{
FlagSpec: FlagSpec{
Flags: map[string]Flag{},
},
},
}

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

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

cm, _ := ff.GenerateConfigMap(name, namespace, references)

require.Equal(t, corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: map[string]string{
"openfeature.dev/featureflag": name,
},
OwnerReferences: references,
},
Data: map[string]string{
"cmnamespace_cmname.flagd.json": "{\"flags\":{}}",
},
}, *cm)
}
279 changes: 279 additions & 0 deletions apis/core/v1beta1/featureflagsource_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package v1beta1
import (
"testing"

"github.com/open-feature/open-feature-operator/common/utils"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
)

func Test_FeatureFlagSource_SyncProvider(t *testing.T) {
Expand All @@ -22,3 +24,280 @@ func Test_FeatureFlagSource_SyncProvider(t *testing.T) {
require.False(t, k.IsGrpc())
require.False(t, g.IsHttp())
}

func Test_FLagSourceConfiguration_envVarKey(t *testing.T) {
require.Equal(t, "pre_suf", envVarKey("pre", "suf"))
}

func Test_FLagSourceConfiguration_ToEnvVars(t *testing.T) {
ff := FeatureFlagSource{
Spec: FeatureFlagSourceSpec{
EnvVars: []v1.EnvVar{
{
Name: "env1",
Value: "val1",
},
{
Name: "env2",
Value: "val2",
},
},
EnvVarPrefix: "PRE",
MetricsPort: 22,
Port: 33,
Evaluator: "evaluator",
SocketPath: "socket-path",
LogFormat: "log",
},
}
expected := []v1.EnvVar{
{
Name: "PRE_env1",
Value: "val1",
},
{
Name: "PRE_env2",
Value: "val2",
},
{
Name: "PRE_METRICS_PORT",
Value: "22",
},
{
Name: "PRE_PORT",
Value: "33",
},
{
Name: "PRE_EVALUATOR",
Value: "evaluator",
},
{
Name: "PRE_SOCKET_PATH",
Value: "socket-path",
},
{
Name: "PRE_LOG_FORMAT",
Value: "log",
},
}
require.Equal(t, expected, ff.Spec.ToEnvVars())
}

func Test_FLagSourceConfiguration_Merge(t *testing.T) {
ff_old := &FeatureFlagSource{
Spec: FeatureFlagSourceSpec{
EnvVars: []v1.EnvVar{
{
Name: "env1",
Value: "val1",
},
{
Name: "env2",
Value: "val2",
},
},
EnvVarPrefix: "PRE",
MetricsPort: 22,
Port: 33,
Evaluator: "evaluator",
SocketPath: "socket-path",
LogFormat: "log",
Image: "img",
Tag: "tag",
Sources: []Source{
{
Source: "src1",
Provider: SyncProviderGrpc,
TLS: true,
CertPath: "etc/cert.ca",
ProviderID: "app",
Selector: "source=database",
},
},
SyncProviderArgs: []string{"arg1", "arg2"},
DefaultSyncProvider: SyncProviderKubernetes,
RolloutOnChange: utils.TrueVal(),
ProbesEnabled: utils.TrueVal(),
DebugLogging: utils.TrueVal(),
OtelCollectorUri: "",
},
}

ff_old.Spec.Merge(nil)

require.Equal(t, &FeatureFlagSource{
Spec: FeatureFlagSourceSpec{
EnvVars: []v1.EnvVar{
{
Name: "env1",
Value: "val1",
},
{
Name: "env2",
Value: "val2",
},
},
EnvVarPrefix: "PRE",
MetricsPort: 22,
Port: 33,
Evaluator: "evaluator",
SocketPath: "socket-path",
LogFormat: "log",
Image: "img",
Tag: "tag",
Sources: []Source{
{
Source: "src1",
Provider: SyncProviderGrpc,
TLS: true,
CertPath: "etc/cert.ca",
ProviderID: "app",
Selector: "source=database",
},
},
SyncProviderArgs: []string{"arg1", "arg2"},
DefaultSyncProvider: SyncProviderKubernetes,
RolloutOnChange: utils.TrueVal(),
ProbesEnabled: utils.TrueVal(),
DebugLogging: utils.TrueVal(),
OtelCollectorUri: "",
},
}, ff_old)

ff_new := &FeatureFlagSource{
Spec: FeatureFlagSourceSpec{
EnvVars: []v1.EnvVar{
{
Name: "env3",
Value: "val3",
},
{
Name: "env4",
Value: "val4",
},
},
EnvVarPrefix: "PREFIX",
MetricsPort: 221,
Port: 331,
Evaluator: "evaluator1",
SocketPath: "socket-path1",
LogFormat: "log1",
Image: "img1",
Tag: "tag1",
Sources: []Source{
{
Source: "src2",
Provider: SyncProviderFilepath,
},
},
SyncProviderArgs: []string{"arg3", "arg4"},
DefaultSyncProvider: SyncProviderFilepath,
RolloutOnChange: utils.FalseVal(),
ProbesEnabled: utils.FalseVal(),
DebugLogging: utils.FalseVal(),
OtelCollectorUri: "",
},
}

ff_old.Spec.Merge(&ff_new.Spec)

require.Equal(t, &FeatureFlagSource{
Spec: FeatureFlagSourceSpec{
EnvVars: []v1.EnvVar{
{
Name: "env1",
Value: "val1",
},
{
Name: "env2",
Value: "val2",
},
{
Name: "env3",
Value: "val3",
},
{
Name: "env4",
Value: "val4",
},
},
EnvVarPrefix: "PREFIX",
MetricsPort: 221,
Port: 331,
Evaluator: "evaluator1",
SocketPath: "socket-path1",
LogFormat: "log1",
Image: "img1",
Tag: "tag1",
Sources: []Source{
{
Source: "src1",
Provider: SyncProviderGrpc,
TLS: true,
CertPath: "etc/cert.ca",
ProviderID: "app",
Selector: "source=database",
},
{
Source: "src2",
Provider: SyncProviderFilepath,
},
},
SyncProviderArgs: []string{"arg1", "arg2", "arg3", "arg4"},
DefaultSyncProvider: SyncProviderFilepath,
RolloutOnChange: utils.FalseVal(),
ProbesEnabled: utils.FalseVal(),
DebugLogging: utils.FalseVal(),
OtelCollectorUri: "",
},
}, ff_old)
}

func Test_FLagSourceConfiguration_NewFeatureFlagSourceSpec(t *testing.T) {
//happy path
t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarMetricPortEnvVar), "22")
t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarPortEnvVar), "33")
t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarSocketPathEnvVar), "val1")
t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarEvaluatorEnvVar), "val2")
t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarImageEnvVar), "val3")
t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarVersionEnvVar), "val4")
t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarProviderArgsEnvVar), "val11,val22")
t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarDefaultSyncProviderEnvVar), "kubernetes")
t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarLogFormatEnvVar), "val5")
t.Setenv(SidecarEnvVarPrefix, "val6")
t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarProbesEnabledVar), "true")

fs, err := NewFeatureFlagSourceSpec()

require.Nil(t, err)
require.Equal(t, &FeatureFlagSourceSpec{
MetricsPort: 22,
Port: 33,
SocketPath: "val1",
Evaluator: "val2",
Image: "val3",
Tag: "val4",
Sources: []Source{},
EnvVars: []v1.EnvVar{},
SyncProviderArgs: []string{"val11", "val22"},
DefaultSyncProvider: SyncProviderKubernetes,
EnvVarPrefix: "val6",
LogFormat: "val5",
ProbesEnabled: utils.TrueVal(),
DebugLogging: utils.FalseVal(),
OtelCollectorUri: "",
}, fs)

//error paths
t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarProbesEnabledVar), "blah")
_, err = NewFeatureFlagSourceSpec()
require.NotNil(t, err)

t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarPortEnvVar), "blah")
_, err = NewFeatureFlagSourceSpec()
require.NotNil(t, err)

t.Setenv(envVarKey(InputConfigurationEnvVarPrefix, SidecarMetricPortEnvVar), "blah")
_, err = NewFeatureFlagSourceSpec()
require.NotNil(t, err)
}
Loading

0 comments on commit d6ec78b

Please sign in to comment.