@@ -19,6 +19,7 @@ package initializingworkspaces
1919import (
2020 "context"
2121 "encoding/json"
22+ "fmt"
2223 "math/rand"
2324 "sort"
2425 "strings"
@@ -27,6 +28,7 @@ import (
2728
2829 jsonpatch "github.com/evanphx/json-patch"
2930 "github.com/google/go-cmp/cmp"
31+ "github.com/stretchr/testify/assert"
3032 "github.com/stretchr/testify/require"
3133
3234 corev1 "k8s.io/api/core/v1"
@@ -356,10 +358,9 @@ func TestInitializingWorkspacesVirtualWorkspaceAccess(t *testing.T) {
356358
357359 t .Log ("Ensure that LIST calls through the virtual workspace eventually show the correct values" )
358360 for _ , wsName := range wsNames {
359- require .Eventually (t , func () bool {
361+ require .EventuallyWithT (t , func (c * assert. CollectT ) {
360362 _ , err := sourceKcpClusterClient .CoreV1alpha1 ().Cluster (wsPath .Join (wsName )).LogicalClusters ().Get (ctx , corev1alpha1 .LogicalClusterName , metav1.GetOptions {})
361- require .True (t , err == nil || errors .IsForbidden (err ), "got %#v error getting logicalcluster %q, expected unauthorized or success" , err , wsPath .Join (wsName ))
362- return err == nil
363+ require .NoError (c , err , "got %#v error getting logicalcluster %q, expected success" , err , wsPath .Join (wsName ))
363364 }, wait .ForeverTestTimeout , 100 * time .Millisecond )
364365 }
365366
@@ -522,12 +523,25 @@ func TestInitializingWorkspacesVirtualWorkspaceAccess(t *testing.T) {
522523
523524 t .Logf ("Attempt to do something more than just removing our initializer %q, get denied" , initializer )
524525 patchBytes := patchBytesFor (logicalCluster , func (workspace * corev1alpha1.LogicalCluster ) {
525- workspace .Status .Initializers = []corev1alpha1.LogicalClusterInitializer {"wrong" }
526+ workspace .Status .Initializers = []corev1alpha1.LogicalClusterInitializer {"wrong:wrong " }
526527 })
527528 _ , err = clusterClient .Cluster (wsClusterName .Path ()).Patch (ctx , corev1alpha1 .LogicalClusterName , types .MergePatchType , patchBytes , metav1.PatchOptions {}, "status" )
528529 if ! errors .IsInvalid (err ) {
529530 t .Fatalf ("got %#v error from patch, expected invalid" , err )
530531 }
532+ // Since Invalid is a generic error, which is not exclusive to an
533+ // initializer failing our custom updateValidation, we need to check for it
534+ // as well.
535+ // Unfortunately, it is not possible to make use of
536+ // field.Error.Origin to do so, as we convert our field.ErrorList into an
537+ // errors.StatusError, thus loosing this information. As a result, our only
538+ // option is to reconstruct the expected error.
539+ expErrMsg := fmt .Sprintf ("only removing the %q initializer is supported" , initialization .InitializerForType (workspacetypes [initializer ]))
540+ // for now using contains seems to strike the best balance between
541+ // identifying the error, while not making the test too brittle as
542+ // kubernetes statusError creation use a lot of squashing an string
543+ // manipulation to create the final exact message.
544+ require .Contains (t , err .Error (), expErrMsg )
531545
532546 t .Logf ("Remove just our initializer %q" , initializer )
533547 patchBytes = patchBytesFor (logicalCluster , func (workspace * corev1alpha1.LogicalCluster ) {
0 commit comments