-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add Konnect methods to KongConsumer
- Loading branch information
Showing
6 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package kongconsumer | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
|
||
configurationv1client "github.com/kong/kubernetes-configuration/pkg/clientset/typed/configuration/v1" | ||
"github.com/kong/kubernetes-configuration/test/crdsvalidation/kongconsumer/testcases" | ||
) | ||
|
||
func TestKongConsumer(t *testing.T) { | ||
ctx := context.Background() | ||
cfg, err := config.GetConfig() | ||
require.NoError(t, err, "error loading Kubernetes config") | ||
cl, err := configurationv1client.NewForConfig(cfg) | ||
require.NoError(t, err, "error creating configurationv1 client") | ||
|
||
for _, tcsGroup := range testcases.TestCases { | ||
tcsGroup := tcsGroup | ||
t.Run(tcsGroup.Name, func(t *testing.T) { | ||
for _, tc := range tcsGroup.TestCases { | ||
tc := tc | ||
t.Run(tc.Name, func(t *testing.T) { | ||
cl := cl.KongConsumers(tc.KongConsumer.Namespace) | ||
entity, err := cl.Create(ctx, &tc.KongConsumer, metav1.CreateOptions{}) | ||
if err == nil { | ||
t.Cleanup(func() { | ||
assert.NoError(t, client.IgnoreNotFound(cl.Delete(ctx, entity.Name, metav1.DeleteOptions{}))) | ||
}) | ||
// Create doesn't set the status, so we need to update it explicitly. | ||
entity.Status = tc.KongConsumer.Status | ||
entity, err = cl.UpdateStatus(ctx, entity, metav1.UpdateOptions{}) | ||
assert.NoError(t, err) | ||
} | ||
|
||
if tc.ExpectedErrorMessage == nil { | ||
assert.NoError(t, err) | ||
|
||
// Update the object and check if the update is allowed. | ||
if tc.Update != nil { | ||
tc.Update(entity) | ||
_, err := cl.Update(ctx, entity, metav1.UpdateOptions{}) | ||
if tc.ExpectedUpdateErrorMessage != nil { | ||
require.NotNil(t, err) | ||
assert.Contains(t, err.Error(), *tc.ExpectedUpdateErrorMessage) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
} | ||
} else { | ||
require.NotNil(t, err) | ||
assert.Contains(t, err.Error(), *tc.ExpectedErrorMessage) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package testcases | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" | ||
) | ||
|
||
// testCase is a test case related to KongConsumer validation. | ||
type testCase struct { | ||
Name string | ||
KongConsumer configurationv1.KongConsumer | ||
Update func(*configurationv1.KongConsumer) | ||
ExpectedErrorMessage *string | ||
ExpectedUpdateErrorMessage *string | ||
} | ||
|
||
// testCasesGroup is a group of test cases related to KongConsumer validation. | ||
// The grouping is done by a common name. | ||
type testCasesGroup struct { | ||
Name string | ||
TestCases []testCase | ||
} | ||
|
||
// TestCases is a collection of all test cases groups related to KongConsumer validation. | ||
var TestCases = []testCasesGroup{} | ||
|
||
func init() { | ||
TestCases = append(TestCases, | ||
requiredFields, | ||
) | ||
} | ||
|
||
var commonObjectMeta = metav1.ObjectMeta{ | ||
GenerateName: "test-kongconsumer-", | ||
Namespace: "default", | ||
} |
58 changes: 58 additions & 0 deletions
58
test/crdsvalidation/kongconsumer/testcases/required_fields.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package testcases | ||
|
||
import ( | ||
configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" | ||
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" | ||
"github.com/samber/lo" | ||
) | ||
|
||
var requiredFields = testCasesGroup{ | ||
Name: "consumer required fields", | ||
TestCases: []testCase{ | ||
{ | ||
Name: "username or custom_id required (username provided)", | ||
KongConsumer: configurationv1.KongConsumer{ | ||
ObjectMeta: commonObjectMeta, | ||
Spec: configurationv1.KongConsumerSpec{ | ||
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ | ||
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, | ||
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ | ||
Name: "test-konnect-control-plane", | ||
}, | ||
}, | ||
}, | ||
Username: "username-1", | ||
}, | ||
}, | ||
{ | ||
Name: "username or custom_id required (custom_id provided)", | ||
KongConsumer: configurationv1.KongConsumer{ | ||
ObjectMeta: commonObjectMeta, | ||
Spec: configurationv1.KongConsumerSpec{ | ||
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ | ||
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, | ||
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ | ||
Name: "test-konnect-control-plane", | ||
}, | ||
}, | ||
}, | ||
CustomID: "customid-1", | ||
}, | ||
}, | ||
{ | ||
Name: "username or custom_id required (none provided)", | ||
KongConsumer: configurationv1.KongConsumer{ | ||
ObjectMeta: commonObjectMeta, | ||
Spec: configurationv1.KongConsumerSpec{ | ||
ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ | ||
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, | ||
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ | ||
Name: "test-konnect-control-plane", | ||
}, | ||
}, | ||
}, | ||
}, | ||
ExpectedErrorMessage: lo.ToPtr("Need to provide either username or custom_id"), | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" | ||
) | ||
|
||
func TestKongConsumer(t *testing.T) { | ||
c := &configurationv1.KongConsumer{} | ||
|
||
require.Nil(t, c.GetKonnectStatus()) | ||
require.Empty(t, c.GetKonnectStatus().GetKonnectID()) | ||
require.Empty(t, c.GetKonnectStatus().GetOrgID()) | ||
require.Empty(t, c.GetKonnectStatus().GetServerURL()) | ||
|
||
require.Equal(t, "", c.GetControlPlaneID()) | ||
c.SetControlPlaneID("123") | ||
require.Equal(t, "123", c.GetControlPlaneID()) | ||
require.Equal(t, "123", c.Status.Konnect.ControlPlaneID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters