Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andfasano committed Dec 13, 2024
1 parent b4e9043 commit 78bb608
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions pkg/asset/agent/joiner/clusterinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ package joiner
import (
"context"
"encoding/json"
"fmt"
"testing"

ignutil "github.com/coreos/ignition/v2/config/util"
igntypes "github.com/coreos/ignition/v2/config/v3_2/types"
"github.com/coreos/stream-metadata-go/stream"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/fake"
faketesting "k8s.io/client-go/testing"
"k8s.io/utils/ptr"
"sigs.k8s.io/yaml"

configv1 "github.com/openshift/api/config/v1"
machineconfigv1 "github.com/openshift/api/machineconfiguration/v1"
Expand All @@ -27,13 +32,21 @@ import (
)

func TestClusterInfo_Generate(t *testing.T) {
type fakeClientErr struct {
verb string
resource string
name string
err error
}

cases := []struct {
name string
workflow workflow.AgentWorkflowType
nodesConfig AddNodesConfig
objs func(t *testing.T) ([]runtime.Object, []runtime.Object, []runtime.Object)
overrideExpectedClusterInfo func(clusterInfo ClusterInfo) ClusterInfo
expectedError string
fakeClientError *fakeClientErr
}{
{
name: "skip if not add-nodes workflow",
Expand Down Expand Up @@ -150,6 +163,59 @@ storage:
return clusterInfo
},
},
{
name: "fallback for icsp",
workflow: workflow.AgentWorkflowTypeAddNodes,
objs: defaultObjects(),
fakeClientError: &fakeClientErr{
verb: "list",
resource: "imagecontentpolicies",
err: errors.NewForbidden(schema.GroupResource{}, "", nil),
},
},
{
name: "fallback for idms",
workflow: workflow.AgentWorkflowTypeAddNodes,
objs: defaultObjects(),
fakeClientError: &fakeClientErr{
verb: "list",
resource: "imagedigestmirrorsets",
err: errors.NewForbidden(schema.GroupResource{}, "", nil),
},
},
{
name: "fallback for chrony (skipped)",
workflow: workflow.AgentWorkflowTypeAddNodes,
objs: defaultObjects(),
fakeClientError: &fakeClientErr{
verb: "get",
resource: "machineconfigs",
name: "50-workers-chrony-configuration",
err: errors.NewForbidden(schema.GroupResource{}, "", nil),
},
},
{
name: "fallback for fips",
workflow: workflow.AgentWorkflowTypeAddNodes,
objs: defaultObjects(),
fakeClientError: &fakeClientErr{
verb: "get",
resource: "machineconfigs",
name: "99-worker-fips",
err: errors.NewForbidden(schema.GroupResource{}, "", nil),
},
},
{
name: "fallback for ssh",
workflow: workflow.AgentWorkflowTypeAddNodes,
objs: defaultObjects(),
fakeClientError: &fakeClientErr{
verb: "get",
resource: "machineconfigs",
name: "99-worker-ssh",
err: errors.NewForbidden(schema.GroupResource{}, "", nil),
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -167,6 +233,35 @@ storage:
fakeOCClient := fakeclientconfig.NewSimpleClientset(openshiftObjects...)
fakeOCMachineConfigClient := fakeclientmachineconfig.NewSimpleClientset(openshiftMachineConfigObjects...)

if tc.fakeClientError != nil {
switch tc.fakeClientError.resource {
case "machineconfigs":
fakeOCMachineConfigClient.PrependReactor(tc.fakeClientError.verb, tc.fakeClientError.resource, func(action faketesting.Action) (handled bool, ret runtime.Object, err error) {
getAction, ok := action.(faketesting.GetAction)
if ok && getAction.GetName() == tc.fakeClientError.name {
return true, nil, errors.NewForbidden(
schema.GroupResource{Group: "", Resource: tc.fakeClientError.resource},
tc.fakeClientError.name,
fmt.Errorf("access denied"),
)
}
return false, nil, nil
})
case "imagedigestmirrorsets", "imagecontentpolicies":
fakeOCClient.PrependReactor(tc.fakeClientError.verb, tc.fakeClientError.resource, func(action faketesting.Action) (handled bool, ret runtime.Object, err error) {
listAction, ok := action.(faketesting.ListAction)
if ok && listAction.GetResource().Resource == tc.fakeClientError.resource {
return true, nil, errors.NewForbidden(
schema.GroupResource{Group: "", Resource: tc.fakeClientError.resource},
tc.fakeClientError.name,
fmt.Errorf("access denied"),
)
}
return false, nil, nil
})
}
}

clusterInfo := ClusterInfo{
Client: fakeClient,
OpenshiftClient: fakeOCClient,
Expand Down Expand Up @@ -233,6 +328,32 @@ func makeCoreOsBootImages(t *testing.T, st *stream.Stream) string {
return string(data)
}

func makeInstallConfig(t *testing.T) string {
t.Helper()
ic := &types.InstallConfig{
ObjectMeta: v1.ObjectMeta{
Name: "ostest",
},
BaseDomain: "test.metalkube.org",
ImageDigestSources: []types.ImageDigestSource{
{
Source: "quay.io/openshift-release-dev/ocp-v4.0-art-dev",
Mirrors: []string{
"registry.example.com:5000/ocp4/openshift4",
},
},
},
SSHKey: "my-ssh-key",
FIPS: true,
}
data, err := yaml.Marshal(ic)
if err != nil {
t.Error(err)
}

return string(data)
}

func defaultObjects() func(t *testing.T) ([]runtime.Object, []runtime.Object, []runtime.Object) {
return func(t *testing.T) ([]runtime.Object, []runtime.Object, []runtime.Object) {
t.Helper()
Expand Down Expand Up @@ -267,6 +388,15 @@ func defaultObjects() func(t *testing.T) ([]runtime.Object, []runtime.Object, []
},
},
},
&corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Name: "cluster-config-v1",
Namespace: "kube-system",
},
Data: map[string]string{
"install-config": makeInstallConfig(t),
},
},
&corev1.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Name: "coreos-bootimages",
Expand Down

0 comments on commit 78bb608

Please sign in to comment.