mirror of
https://github.com/open-cluster-management-io/ocm.git
synced 2026-05-17 14:48:49 +00:00
89 lines
3.0 KiB
Go
89 lines
3.0 KiB
Go
package spokecluster
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"testing"
|
|
|
|
clusterfake "github.com/open-cluster-management/api/client/cluster/clientset/versioned/fake"
|
|
clusterv1 "github.com/open-cluster-management/api/cluster/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
clienttesting "k8s.io/client-go/testing"
|
|
)
|
|
|
|
const testSpokeExternalServerUrl = "https://192.168.3.77:32769"
|
|
|
|
func TestCreateSpokeCluster(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
startingObjects []runtime.Object
|
|
validateActions func(t *testing.T, actions []clienttesting.Action)
|
|
expectedErr string
|
|
}{
|
|
{
|
|
name: "create a new cluster",
|
|
startingObjects: []runtime.Object{},
|
|
validateActions: func(t *testing.T, actions []clienttesting.Action) {
|
|
assertActions(t, actions, "get", "create")
|
|
actual := actions[1].(clienttesting.CreateActionImpl).Object
|
|
assertSpokeExternalServerUrl(t, actual, testSpokeExternalServerUrl)
|
|
assertSpokeCABundle(t, actual, []byte("testcabundle"))
|
|
},
|
|
},
|
|
{
|
|
name: "create an existed cluster",
|
|
startingObjects: []runtime.Object{newSpokeCluster([]clusterv1.StatusCondition{})},
|
|
validateActions: func(t *testing.T, actions []clienttesting.Action) {
|
|
assertActions(t, actions, "get")
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
clusterClient := clusterfake.NewSimpleClientset(c.startingObjects...)
|
|
ctrl := spokeClusterCreatingController{
|
|
clusterName: testSpokeClusterName,
|
|
spokeExternalServerURLs: []string{testSpokeExternalServerUrl},
|
|
spokeCABundle: []byte("testcabundle"),
|
|
hubClusterClient: clusterClient,
|
|
}
|
|
|
|
syncErr := ctrl.sync(context.TODO(), newFakeSyncContext(t))
|
|
if len(c.expectedErr) > 0 && syncErr == nil {
|
|
t.Errorf("expected %q error", c.expectedErr)
|
|
return
|
|
}
|
|
if len(c.expectedErr) > 0 && syncErr != nil && syncErr.Error() != c.expectedErr {
|
|
t.Errorf("expected %q error, got %q", c.expectedErr, syncErr.Error())
|
|
return
|
|
}
|
|
if len(c.expectedErr) == 0 && syncErr != nil {
|
|
t.Errorf("unexpected err: %v", syncErr)
|
|
}
|
|
|
|
c.validateActions(t, clusterClient.Actions())
|
|
})
|
|
}
|
|
}
|
|
|
|
func assertSpokeExternalServerUrl(t *testing.T, actual runtime.Object, expected string) {
|
|
spokeCluster := actual.(*clusterv1.SpokeCluster)
|
|
if len(spokeCluster.Spec.SpokeClientConfigs) != 1 {
|
|
t.Errorf("expected one spoke client config, but got %v", spokeCluster.Spec.SpokeClientConfigs)
|
|
}
|
|
if spokeCluster.Spec.SpokeClientConfigs[0].URL != expected {
|
|
t.Errorf("expected %q error, but got %q", expected, spokeCluster.Spec.SpokeClientConfigs[0].URL)
|
|
}
|
|
}
|
|
|
|
func assertSpokeCABundle(t *testing.T, actual runtime.Object, expected []byte) {
|
|
spokeCluster := actual.(*clusterv1.SpokeCluster)
|
|
if len(spokeCluster.Spec.SpokeClientConfigs) != 1 {
|
|
t.Errorf("expected one spoke client config, but got %v", spokeCluster.Spec.SpokeClientConfigs)
|
|
}
|
|
if !bytes.Equal(spokeCluster.Spec.SpokeClientConfigs[0].CABundle, expected) {
|
|
t.Errorf("expected %q error, but got %q", expected, spokeCluster.Spec.SpokeClientConfigs[0].CABundle)
|
|
}
|
|
}
|