Skip to content

Commit

Permalink
Fix user tests since rebase (invalid users are now rejected), add lic…
Browse files Browse the repository at this point in the history
…ense headers
  • Loading branch information
hugoShaka committed Jul 16, 2022
1 parent c61ce1c commit f5d23e4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
32 changes: 19 additions & 13 deletions operator/controllers/resources/role_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package resources

import (
"context"
"github.com/gravitational/teleport/lib/auth"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -143,19 +144,7 @@ func TestRoleUpdate(t *testing.T) {
}, &r)
require.True(t, kerrors.IsNotFound(err))

// The role is created in Teleport
tRole, err := types.NewRole(roleName, types.RoleSpecV5{
Allow: types.RoleConditions{
Logins: []string{"a", "b"},
},
})
require.NoError(t, err)
metadata := tRole.GetMetadata()
metadata.Labels = map[string]string{types.OriginLabel: types.OriginKubernetes}
tRole.SetMetadata(metadata)

err = tClient.UpsertRole(ctx, tRole)
require.NoError(t, err)
err = teleportCreateDummyRole(t, roleName, tClient, ctx)

// The role is created in K8S
k8sRole := resourcesv5.TeleportRole{
Expand Down Expand Up @@ -202,6 +191,23 @@ func TestRoleUpdate(t *testing.T) {
})
}

func teleportCreateDummyRole(t *testing.T, roleName string, tClient auth.ClientI, ctx context.Context) error {
// The role is created in Teleport
tRole, err := types.NewRole(roleName, types.RoleSpecV5{
Allow: types.RoleConditions{
Logins: []string{"a", "b"},
},
})
require.NoError(t, err)
metadata := tRole.GetMetadata()
metadata.Labels = map[string]string{types.OriginLabel: types.OriginKubernetes}
tRole.SetMetadata(metadata)

err = tClient.UpsertRole(ctx, tRole)
require.NoError(t, err)
return err
}

func k8sCreateDummyRole(ctx context.Context, t *testing.T, kc kclient.Client, namespace, roleName string) {
role := resourcesv5.TeleportRole{
ObjectMeta: metav1.ObjectMeta{
Expand Down
15 changes: 13 additions & 2 deletions operator/controllers/resources/user_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func TestUserCreation(t *testing.T) {
ns := createNamespaceForTest(t, k8sClient)
userName := validRandomResourceName("user-")

teleportCreateDummyRole(t, "a", tClient, ctx)
teleportCreateDummyRole(t, "b", tClient, ctx)
// The user is created in K8S
k8sCreateDummyUser(ctx, t, k8sClient, ns.Name, userName)

Expand Down Expand Up @@ -84,6 +86,9 @@ func TestUserDeletionDrift(t *testing.T) {
ns := createNamespaceForTest(t, k8sClient)
userName := validRandomResourceName("user-")

teleportCreateDummyRole(t, "a", tClient, ctx)
teleportCreateDummyRole(t, "b", tClient, ctx)

// The user is created in K8S
k8sCreateDummyUser(ctx, t, k8sClient, ns.Name, userName)

Expand Down Expand Up @@ -132,6 +137,12 @@ func TestUserUpdate(t *testing.T) {
tClient := clientForTeleport(t, teleportServer, operatorName)
k8sClient := startKubernetesOperator(t, tClient)

teleportCreateDummyRole(t, "a", tClient, ctx)
teleportCreateDummyRole(t, "b", tClient, ctx)
teleportCreateDummyRole(t, "x", tClient, ctx)
teleportCreateDummyRole(t, "y", tClient, ctx)
teleportCreateDummyRole(t, "z", tClient, ctx)

ns := createNamespaceForTest(t, k8sClient)
userName := validRandomResourceName("user-")

Expand Down Expand Up @@ -183,7 +194,7 @@ func TestUserUpdate(t *testing.T) {
}, &k8sUserNewVersion)
require.NoError(t, err)

k8sUserNewVersion.Spec.Roles = append(k8sUserNewVersion.Spec.Roles, "admin", "root")
k8sUserNewVersion.Spec.Roles = append(k8sUserNewVersion.Spec.Roles, "y")
err = k8sClient.Update(ctx, &k8sUserNewVersion)
require.NoError(t, err)

Expand All @@ -193,7 +204,7 @@ func TestUserUpdate(t *testing.T) {
require.NoError(t, err)

// TeleportUser updated with new roles
return assert.ElementsMatch(t, tUser.GetRoles(), []string{"x", "z", "admin", "root"})
return assert.ElementsMatch(t, tUser.GetRoles(), []string{"x", "z", "y"})
})
}

Expand Down
16 changes: 16 additions & 0 deletions operator/controllers/resources/utils.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2022 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
Expand Down
16 changes: 16 additions & 0 deletions operator/controllers/resources/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2022 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package resources

import (
Expand Down
16 changes: 16 additions & 0 deletions operator/sidecar/tbot.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2022 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package sidecar

import (
Expand Down

0 comments on commit f5d23e4

Please sign in to comment.