Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: chiragkyal <[email protected]>
  • Loading branch information
chiragkyal committed Sep 25, 2024
1 parent 03dfa5c commit f413d74
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
92 changes: 87 additions & 5 deletions pkg/controllers/awsloadbalancercontroller/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ var platformStatus = &configv1.PlatformStatus{

func TestDesiredArgs(t *testing.T) {
for _, tc := range []struct {
name string
controller *albo.AWSLoadBalancerController
expectedArgs sets.Set[string]
name string
controller *albo.AWSLoadBalancerController
expectedArgs sets.Set[string]
platformStatus *configv1.PlatformStatus
}{
{
name: "non-default ingress class",
Expand Down Expand Up @@ -125,7 +126,7 @@ func TestDesiredArgs(t *testing.T) {
),
},
{
name: "resource tags specified",
name: "resource tags specified in the operator spec",
controller: &albo.AWSLoadBalancerController{
Spec: albo.AWSLoadBalancerControllerSpec{
AdditionalResourceTags: []albo.AWSResourceTag{
Expand All @@ -143,6 +144,84 @@ func TestDesiredArgs(t *testing.T) {
"--default-tags=test-key1=test-value1,test-key2=test-value2,test-key3=test-value3",
),
},
{
name: "resource tags specified in the platform status",
controller: &albo.AWSLoadBalancerController{
Spec: albo.AWSLoadBalancerControllerSpec{},
},
platformStatus: &configv1.PlatformStatus{
Type: configv1.AWSPlatformType,
AWS: &configv1.AWSPlatformStatus{
ResourceTags: []configv1.AWSResourceTag{
{Key: "key1", Value: "value1"},
{Key: "key2", Value: "value2"},
},
},
},
expectedArgs: sets.New[string](
"--enable-shield=false",
"--enable-waf=false",
"--enable-wafv2=false",
"--ingress-class=alb",
"--default-tags=key1=value1,key2=value2",
),
},
{
name: "conflicting resource tags specified in the platform status and operator spec",
controller: &albo.AWSLoadBalancerController{
Spec: albo.AWSLoadBalancerControllerSpec{
AdditionalResourceTags: []albo.AWSResourceTag{
{Key: "op-key1", Value: "op-value1"},
{Key: "conflict-key1", Value: "op-value2"},
{Key: "conflict-key2", Value: "op-value3"},
},
},
},
platformStatus: &configv1.PlatformStatus{
Type: configv1.AWSPlatformType,
AWS: &configv1.AWSPlatformStatus{
ResourceTags: []configv1.AWSResourceTag{
{Key: "plat-key1", Value: "plat-value1"},
{Key: "conflict-key1", Value: "plat-value2"},
{Key: "conflict-key2", Value: "plat-value3"},
},
},
},
expectedArgs: sets.New[string](
"--enable-shield=false",
"--enable-waf=false",
"--enable-wafv2=false",
"--ingress-class=alb",
"--default-tags=conflict-key1=op-value2,conflict-key2=op-value3,op-key1=op-value1,plat-key1=plat-value1",
),
},
{
name: "non-conflicting resource tags specified in the platform status and operator spec",
controller: &albo.AWSLoadBalancerController{
Spec: albo.AWSLoadBalancerControllerSpec{
AdditionalResourceTags: []albo.AWSResourceTag{
{Key: "op-key1", Value: "op-value1"},
{Key: "op-key2", Value: "op-value2"},
},
},
},
platformStatus: &configv1.PlatformStatus{
Type: configv1.AWSPlatformType,
AWS: &configv1.AWSPlatformStatus{
ResourceTags: []configv1.AWSResourceTag{
{Key: "plat-key1", Value: "plat-value1"},
{Key: "plat-key2", Value: "plat-value2"},
},
},
},
expectedArgs: sets.New[string](
"--enable-shield=false",
"--enable-waf=false",
"--enable-wafv2=false",
"--ingress-class=alb",
"--default-tags=op-key1=op-value1,op-key2=op-value2,plat-key1=plat-value1,plat-key2=plat-value2",
),
},
} {
t.Run(tc.name, func(t *testing.T) {
defaultArgs := sets.New[string](
Expand All @@ -157,7 +236,10 @@ func TestDesiredArgs(t *testing.T) {
if tc.controller.Spec.IngressClass == "" {
tc.controller.Spec.IngressClass = "alb"
}
args := desiredContainerArgs(tc.controller, platformStatus, "test-cluster", "test-vpc")
if tc.platformStatus == nil {
tc.platformStatus = &configv1.PlatformStatus{}
}
args := desiredContainerArgs(tc.controller, tc.platformStatus, "test-cluster", "test-vpc")

expected := sets.List(expectedArgs)
sort.Strings(expected)
Expand Down
5 changes: 5 additions & 0 deletions pkg/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

configv1 "github.com/openshift/api/config/v1"

albo "github.com/openshift/aws-load-balancer-operator/api/v1"
albc "github.com/openshift/aws-load-balancer-operator/pkg/controllers/awsloadbalancercontroller"
//+kubebuilder:scaffold:imports
Expand Down Expand Up @@ -82,6 +84,9 @@ var _ = BeforeSuite(func() {
err = cco.Install(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

err = configv1.Install(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expand Down

0 comments on commit f413d74

Please sign in to comment.