-
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.
- Loading branch information
Showing
8 changed files
with
210 additions
and
11 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
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
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
56 changes: 56 additions & 0 deletions
56
test/crdsvalidation/konnectcontrolplane/konnectcontrolplane_test.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,56 @@ | ||
package kongpluginbindings | ||
|
||
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" | ||
|
||
konnectv1alpha1client "github.com/kong/kubernetes-configuration/pkg/clientset/typed/konnect/v1alpha1" | ||
"github.com/kong/kubernetes-configuration/test/crdsvalidation/konnectcontrolplane/testcases" | ||
) | ||
|
||
func TestKonnectControlPlane(t *testing.T) { | ||
ctx := context.Background() | ||
cfg, err := config.GetConfig() | ||
require.NoError(t, err, "error loading Kubernetes config") | ||
cl, err := konnectv1alpha1client.NewForConfig(cfg) | ||
require.NoError(t, err, "error creating konnectv1alpha1client 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.KonnectControlPlanes(tc.KonnectControlPlane.Namespace) | ||
kcp, err := cl.Create(ctx, &tc.KonnectControlPlane, metav1.CreateOptions{}) | ||
if tc.ExpectedErrorMessage == nil { | ||
assert.NoError(t, err) | ||
t.Cleanup(func() { | ||
assert.NoError(t, client.IgnoreNotFound(cl.Delete(ctx, kcp.Name, metav1.DeleteOptions{}))) | ||
}) | ||
|
||
// Update the object and check if the update is allowed. | ||
if tc.Update != nil { | ||
tc.Update(kcp) | ||
|
||
_, err := cl.Update(ctx, kcp, metav1.UpdateOptions{}) | ||
if tc.ExpectedUpdateErrorMessage != nil { | ||
require.NotNil(t, err) | ||
assert.Contains(t, err.Error(), *tc.ExpectedUpdateErrorMessage) | ||
} | ||
} | ||
} else { | ||
require.NotNil(t, err) | ||
assert.Contains(t, err.Error(), *tc.ExpectedErrorMessage) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
test/crdsvalidation/konnectcontrolplane/testcases/common.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,37 @@ | ||
package testcases | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1" | ||
) | ||
|
||
// kcpTestCase is a test case related to KonnectControlPlane validation. | ||
type kcpTestCase struct { | ||
Name string | ||
KonnectControlPlane konnectv1alpha1.KonnectControlPlane | ||
Update func(*konnectv1alpha1.KonnectControlPlane) | ||
ExpectedErrorMessage *string | ||
ExpectedUpdateErrorMessage *string | ||
} | ||
|
||
// kcpTestCasesGroup is a group of test cases related to KonnectControlPlane validation. | ||
// The grouping is done by a common name. | ||
type kcpTestCasesGroup struct { | ||
Name string | ||
TestCases []kcpTestCase | ||
} | ||
|
||
// TestCases is a collection of all test cases groups related to KonnectControlPlane validation. | ||
var TestCases = []kcpTestCasesGroup{} | ||
|
||
func init() { | ||
TestCases = append(TestCases, | ||
updatesNotAllowedForStatus, | ||
) | ||
} | ||
|
||
var commonObjectMeta = metav1.ObjectMeta{ | ||
GenerateName: "test-konnect-control-plane", | ||
Namespace: "default", | ||
} |
107 changes: 107 additions & 0 deletions
107
test/crdsvalidation/konnectcontrolplane/testcases/update-not-allowed-for-status.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,107 @@ | ||
package testcases | ||
|
||
import ( | ||
"github.com/Kong/sdk-konnect-go/models/components" | ||
"github.com/samber/lo" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1" | ||
) | ||
|
||
// updatesNotAllowedForStatus are test cases checking if updates to konnect.authRef | ||
// are indeed blocked for some status conditions. | ||
var updatesNotAllowedForStatus = kcpTestCasesGroup{ | ||
Name: "updates not allowed for status conditions", | ||
TestCases: []kcpTestCase{ | ||
{ | ||
Name: "konnect.authRef change is not allowed for Programmed=True", | ||
KonnectControlPlane: konnectv1alpha1.KonnectControlPlane{ | ||
ObjectMeta: commonObjectMeta, | ||
Spec: konnectv1alpha1.KonnectControlPlaneSpec{ | ||
CreateControlPlaneRequest: components.CreateControlPlaneRequest{ | ||
Name: "cp-1", | ||
ClusterType: lo.ToPtr(components.ClusterTypeClusterTypeControlPlane), | ||
}, | ||
KonnectConfiguration: konnectv1alpha1.KonnectConfiguration{ | ||
APIAuthConfigurationRef: konnectv1alpha1.KonnectAPIAuthConfigurationRef{ | ||
Name: "name-1", | ||
}, | ||
}, | ||
}, | ||
Status: konnectv1alpha1.KonnectControlPlaneStatus{ | ||
Conditions: []metav1.Condition{ | ||
{ | ||
Type: "Programmed", | ||
Status: metav1.ConditionTrue, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Update: func(kcp *konnectv1alpha1.KonnectControlPlane) { | ||
kcp.Spec.KonnectConfiguration.APIAuthConfigurationRef.Name = "name-2" | ||
}, | ||
ExpectedUpdateErrorMessage: lo.ToPtr("spec.konnect.authRef is immutable when entity is already Programme"), | ||
}, | ||
{ | ||
Name: "konnect.authRef change is not allowed for APIAuthValid=True", | ||
KonnectControlPlane: konnectv1alpha1.KonnectControlPlane{ | ||
ObjectMeta: commonObjectMeta, | ||
Spec: konnectv1alpha1.KonnectControlPlaneSpec{ | ||
CreateControlPlaneRequest: components.CreateControlPlaneRequest{ | ||
Name: "cp-1", | ||
ClusterType: lo.ToPtr(components.ClusterTypeClusterTypeControlPlane), | ||
}, | ||
KonnectConfiguration: konnectv1alpha1.KonnectConfiguration{ | ||
APIAuthConfigurationRef: konnectv1alpha1.KonnectAPIAuthConfigurationRef{ | ||
Name: "name-1", | ||
}, | ||
}, | ||
}, | ||
Status: konnectv1alpha1.KonnectControlPlaneStatus{ | ||
Conditions: []metav1.Condition{ | ||
{ | ||
Type: "APIAuthValid", | ||
Status: metav1.ConditionTrue, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Update: func(kcp *konnectv1alpha1.KonnectControlPlane) { | ||
kcp.Spec.KonnectConfiguration.APIAuthConfigurationRef.Name = "name-2" | ||
}, | ||
ExpectedUpdateErrorMessage: lo.ToPtr("spec.konnect.authRef is immutable when entity is already Programme"), | ||
}, | ||
{ | ||
Name: "konnect.authRef change is allowed when cp is not Programmed=True nor APIAuthValid=True", | ||
KonnectControlPlane: konnectv1alpha1.KonnectControlPlane{ | ||
ObjectMeta: commonObjectMeta, | ||
Spec: konnectv1alpha1.KonnectControlPlaneSpec{ | ||
CreateControlPlaneRequest: components.CreateControlPlaneRequest{ | ||
Name: "cp-1", | ||
ClusterType: lo.ToPtr(components.ClusterTypeClusterTypeControlPlane), | ||
}, | ||
KonnectConfiguration: konnectv1alpha1.KonnectConfiguration{ | ||
APIAuthConfigurationRef: konnectv1alpha1.KonnectAPIAuthConfigurationRef{ | ||
Name: "name-1", | ||
}, | ||
}, | ||
}, | ||
Status: konnectv1alpha1.KonnectControlPlaneStatus{ | ||
Conditions: []metav1.Condition{ | ||
{ | ||
Type: "APIAuthValid", | ||
Status: metav1.ConditionFalse, | ||
}, | ||
{ | ||
Type: "Programmed", | ||
Status: metav1.ConditionFalse, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Update: func(kcp *konnectv1alpha1.KonnectControlPlane) { | ||
kcp.Spec.KonnectConfiguration.APIAuthConfigurationRef.Name = "name-2" | ||
}, | ||
}, | ||
}, | ||
} |