-
Notifications
You must be signed in to change notification settings - Fork 23
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
9 changed files
with
341 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package fullnode | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
rbacv1 "k8s.io/api/rbac/v1" | ||
) | ||
|
||
func TestBuildRBAC(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("build rbac", func(t *testing.T) { | ||
crd := defaultCRD() | ||
crd.Spec.Replicas = 3 | ||
crd.Name = "hub" | ||
crd.Namespace = "test" | ||
crd.Spec.ChainSpec.Network = "testnet" | ||
crd.Spec.PodTemplate.Image = "gaia:v6.0.0" | ||
|
||
sas := BuildServiceAccounts(&crd) | ||
|
||
require.Len(t, sas, 1) // 1 svc account in the namespace | ||
|
||
sa := sas[0].Object() | ||
|
||
require.Equal(t, "hub-vc-sa", sa.Name) | ||
require.Equal(t, "test", sa.Namespace) | ||
|
||
wantLabels := map[string]string{ | ||
"app.kubernetes.io/created-by": "cosmos-operator", | ||
"app.kubernetes.io/name": "hub", | ||
"app.kubernetes.io/component": "vc", | ||
"app.kubernetes.io/version": "v6.0.0", | ||
"cosmos.strange.love/network": "testnet", | ||
"cosmos.strange.love/type": "FullNode", | ||
} | ||
require.Equal(t, wantLabels, sa.Labels) | ||
|
||
roles := BuildRoles(&crd) | ||
|
||
require.Len(t, roles, 1) // 1 role in the namespace | ||
|
||
role := roles[0].Object() | ||
|
||
require.Equal(t, "hub-vc-r", role.Name) | ||
require.Equal(t, "test", role.Namespace) | ||
|
||
wantLabels = map[string]string{ | ||
"app.kubernetes.io/created-by": "cosmos-operator", | ||
"app.kubernetes.io/name": "hub", | ||
"app.kubernetes.io/component": "vc", | ||
"app.kubernetes.io/version": "v6.0.0", | ||
"cosmos.strange.love/network": "testnet", | ||
"cosmos.strange.love/type": "FullNode", | ||
} | ||
require.Equal(t, wantLabels, role.Labels) | ||
|
||
require.Equal(t, []rbacv1.PolicyRule{ | ||
{ | ||
APIGroups: []string{""}, // core API group | ||
Resources: []string{"namespaces", "pods"}, | ||
Verbs: []string{"get", "list"}, | ||
}, | ||
{ | ||
APIGroups: []string{"cosmos.strange.love"}, | ||
Resources: []string{"cosmosfullnodes"}, | ||
Verbs: []string{"get"}, | ||
}, | ||
{ | ||
APIGroups: []string{"cosmos.strange.love"}, | ||
Resources: []string{"cosmosfullnodes/status"}, | ||
Verbs: []string{"update"}, | ||
}, | ||
}, role.Rules) | ||
|
||
rbs := BuildRoleBindings(&crd) | ||
|
||
require.Len(t, rbs, 1) // 1 role in the namespace | ||
|
||
rb := rbs[0].Object() | ||
|
||
require.Equal(t, "hub-vc-rb", rb.Name) | ||
require.Equal(t, "test", rb.Namespace) | ||
|
||
wantLabels = map[string]string{ | ||
"app.kubernetes.io/created-by": "cosmos-operator", | ||
"app.kubernetes.io/name": "hub", | ||
"app.kubernetes.io/component": "vc", | ||
"app.kubernetes.io/version": "v6.0.0", | ||
"cosmos.strange.love/network": "testnet", | ||
"cosmos.strange.love/type": "FullNode", | ||
} | ||
require.Equal(t, wantLabels, rb.Labels) | ||
|
||
require.Len(t, rb.Subjects, 1) | ||
require.Equal(t, rb.Subjects[0].Name, "hub-vc-sa") | ||
|
||
require.Equal(t, rb.RoleRef.Name, "hub-vc-r") | ||
}) | ||
} |
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,70 @@ | ||
package fullnode | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func TestRoleBindingControl_Reconcile(t *testing.T) { | ||
t.Parallel() | ||
|
||
type mockRbClient = mockClient[*rbacv1.RoleBinding] | ||
|
||
ctx := context.Background() | ||
|
||
t.Run("happy path", func(t *testing.T) { | ||
crd := defaultCRD() | ||
crd.Namespace = "test" | ||
crd.Spec.Replicas = 3 | ||
|
||
var mClient mockRbClient | ||
|
||
control := NewRoleBindingControl(&mClient) | ||
err := control.Reconcile(ctx, nopReporter, &crd) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, mClient.GotListOpts, 2) | ||
var listOpt client.ListOptions | ||
for _, opt := range mClient.GotListOpts { | ||
opt.ApplyToList(&listOpt) | ||
} | ||
require.Equal(t, "test", listOpt.Namespace) | ||
require.Zero(t, listOpt.Limit) | ||
|
||
require.Equal(t, 1, mClient.CreateCount) // Created 1 role binding. | ||
require.Equal(t, "osmosis-vc-rb", mClient.LastCreateObject.Name) | ||
require.NotEmpty(t, mClient.LastCreateObject.OwnerReferences) | ||
require.Equal(t, crd.Name, mClient.LastCreateObject.OwnerReferences[0].Name) | ||
require.Equal(t, "CosmosFullNode", mClient.LastCreateObject.OwnerReferences[0].Kind) | ||
require.True(t, *mClient.LastCreateObject.OwnerReferences[0].Controller) | ||
|
||
mClient.ObjectList = rbacv1.RoleBindingList{Items: []rbacv1.RoleBinding{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "osmosis-vc-rb", Namespace: crd.Namespace}, | ||
Subjects: nil, // different to force update | ||
}, | ||
}} | ||
|
||
mClient.GotListOpts = nil // reset for next reconcile | ||
|
||
err = control.Reconcile(ctx, nopReporter, &crd) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, mClient.GotListOpts, 2) | ||
listOpt = client.ListOptions{} | ||
for _, opt := range mClient.GotListOpts { | ||
opt.ApplyToList(&listOpt) | ||
} | ||
require.Equal(t, "test", listOpt.Namespace) | ||
require.Zero(t, listOpt.Limit) | ||
|
||
require.Equal(t, 1, mClient.UpdateCount) // Updated 1 role binding. | ||
|
||
require.Zero(t, mClient.DeleteCount) // Role bindings are never deleted. | ||
}) | ||
} |
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,70 @@ | ||
package fullnode | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func TestRoleControl_Reconcile(t *testing.T) { | ||
t.Parallel() | ||
|
||
type mockRoleClient = mockClient[*rbacv1.Role] | ||
|
||
ctx := context.Background() | ||
|
||
t.Run("happy path", func(t *testing.T) { | ||
crd := defaultCRD() | ||
crd.Namespace = "test" | ||
crd.Spec.Replicas = 3 | ||
|
||
var mClient mockRoleClient | ||
|
||
control := NewRoleControl(&mClient) | ||
err := control.Reconcile(ctx, nopReporter, &crd) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, mClient.GotListOpts, 2) | ||
var listOpt client.ListOptions | ||
for _, opt := range mClient.GotListOpts { | ||
opt.ApplyToList(&listOpt) | ||
} | ||
require.Equal(t, "test", listOpt.Namespace) | ||
require.Zero(t, listOpt.Limit) | ||
|
||
require.Equal(t, 1, mClient.CreateCount) // Created 1 role. | ||
require.Equal(t, "osmosis-vc-r", mClient.LastCreateObject.Name) | ||
require.NotEmpty(t, mClient.LastCreateObject.OwnerReferences) | ||
require.Equal(t, crd.Name, mClient.LastCreateObject.OwnerReferences[0].Name) | ||
require.Equal(t, "CosmosFullNode", mClient.LastCreateObject.OwnerReferences[0].Kind) | ||
require.True(t, *mClient.LastCreateObject.OwnerReferences[0].Controller) | ||
|
||
mClient.ObjectList = rbacv1.RoleList{Items: []rbacv1.Role{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "osmosis-vc-r", Namespace: crd.Namespace}, | ||
Rules: nil, // added to force update | ||
}, | ||
}} | ||
|
||
mClient.GotListOpts = nil // reset for next reconcile | ||
|
||
err = control.Reconcile(ctx, nopReporter, &crd) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, mClient.GotListOpts, 2) | ||
listOpt = client.ListOptions{} | ||
for _, opt := range mClient.GotListOpts { | ||
opt.ApplyToList(&listOpt) | ||
} | ||
require.Equal(t, "test", listOpt.Namespace) | ||
require.Zero(t, listOpt.Limit) | ||
|
||
require.Equal(t, 1, mClient.UpdateCount) // Updated 1 role. | ||
|
||
require.Zero(t, mClient.DeleteCount) // Roles are never deleted. | ||
}) | ||
} |
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
Oops, something went wrong.