Skip to content

Commit bc73249

Browse files
committed
Fix create-workspace tests
Signed-off-by: Nelo-T. Wallus <[email protected]>
1 parent fc9ccbb commit bc73249

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

cli/pkg/workspace/plugin/create.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ type CreateWorkspaceOptions struct {
6363
kcpClusterClient kcpclientset.ClusterInterface
6464

6565
// for testing - passed to UseWorkspaceOptions
66-
modifyConfig func(configAccess clientcmd.ConfigAccess, newConfig *clientcmdapi.Config) error
66+
newKCPClusterClient func(config clientcmd.ClientConfig) (kcpclientset.ClusterInterface, error)
67+
modifyConfig func(configAccess clientcmd.ConfigAccess, newConfig *clientcmdapi.Config) error
6768
}
6869

6970
// NewCreateWorkspaceOptions returns a new CreateWorkspaceOptions.
@@ -233,10 +234,22 @@ func (o *CreateWorkspaceOptions) Run(ctx context.Context) error {
233234
if o.EnterAfterCreate {
234235
useOptions := NewUseWorkspaceOptions(o.IOStreams)
235236
useOptions.Name = ws.Name
237+
useOptions.ClientConfig = o.ClientConfig
238+
239+
startingConfig, err := o.ClientConfig.RawConfig()
240+
if err != nil {
241+
return fmt.Errorf("error getting rawconfig for use: %w", err)
242+
}
243+
useOptions.startingConfig = &startingConfig
244+
236245
// only for unit test needs
237246
if o.modifyConfig != nil {
238247
useOptions.modifyConfig = o.modifyConfig
239248
}
249+
if o.newKCPClusterClient != nil {
250+
useOptions.newKCPClusterClient = o.newKCPClusterClient
251+
}
252+
240253
if err := useOptions.Complete(nil); err != nil {
241254
return err
242255
}

cli/pkg/workspace/plugin/create_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838

3939
corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
4040
tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1"
41+
kcpclientset "github.com/kcp-dev/kcp/sdk/client/clientset/versioned/cluster"
4142
kcpfakeclient "github.com/kcp-dev/kcp/sdk/client/clientset/versioned/cluster/fake"
4243
)
4344

@@ -64,6 +65,7 @@ func TestCreate(t *testing.T) {
6465
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test": {Token: "test"}},
6566
},
6667
newWorkspaceName: "bar",
68+
markReady: true,
6769
},
6870
{
6971
name: "create, use after creation, but not ready",
@@ -193,6 +195,9 @@ func TestCreate(t *testing.T) {
193195
for _, name := range tt.existingWorkspaces {
194196
objects = append(objects, &tenancyv1alpha1.Workspace{
195197
ObjectMeta: metav1.ObjectMeta{
198+
Annotations: map[string]string{
199+
logicalcluster.AnnotationKey: currentClusterName.String(),
200+
},
196201
Name: name,
197202
},
198203
Spec: tenancyv1alpha1.WorkspaceSpec{
@@ -209,6 +214,24 @@ func TestCreate(t *testing.T) {
209214
}
210215
client := kcpfakeclient.NewSimpleClientset(objects...)
211216

217+
// Fill up the resources map for the discovery client
218+
for _, name := range append(tt.existingWorkspaces, tt.newWorkspaceName) {
219+
if client.Resources == nil {
220+
client.Resources = map[logicalcluster.Path][]*metav1.APIResourceList{}
221+
}
222+
client.Resources[logicalcluster.NewPath(currentClusterName.String()).Join(name)] = []*metav1.APIResourceList{
223+
{
224+
GroupVersion: tenancyv1alpha1.SchemeGroupVersion.String(),
225+
APIResources: []metav1.APIResource{
226+
{
227+
Name: "workspaces",
228+
SingularName: "workspace",
229+
},
230+
},
231+
},
232+
}
233+
}
234+
212235
workspaceType := tt.newWorkspaceType
213236
if tt.newWorkspaceType == nil {
214237
workspaceType = &tenancyv1alpha1.WorkspaceTypeReference{
@@ -243,6 +266,9 @@ func TestCreate(t *testing.T) {
243266
return nil
244267
}
245268
opts.kcpClusterClient = client
269+
opts.newKCPClusterClient = func(config clientcmd.ClientConfig) (kcpclientset.ClusterInterface, error) {
270+
return client, nil
271+
}
246272
opts.ClientConfig = clientcmd.NewDefaultClientConfig(*tt.config.DeepCopy(), nil)
247273
err := opts.Run(context.Background())
248274
if tt.wantErr {

cli/pkg/workspace/plugin/use.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,18 @@ type UseWorkspaceOptions struct {
6363
startingConfig *clientcmdapi.Config
6464

6565
// for testing
66-
modifyConfig func(configAccess clientcmd.ConfigAccess, newConfig *clientcmdapi.Config) error
67-
getAPIBindings func(ctx context.Context, kcpClusterClient kcpclientset.ClusterInterface, host string) ([]apisv1alpha2.APIBinding, error)
68-
userHomeDir func() (string, error)
66+
newKCPClusterClient func(config clientcmd.ClientConfig) (kcpclientset.ClusterInterface, error)
67+
modifyConfig func(configAccess clientcmd.ConfigAccess, newConfig *clientcmdapi.Config) error
68+
getAPIBindings func(ctx context.Context, kcpClusterClient kcpclientset.ClusterInterface, host string) ([]apisv1alpha2.APIBinding, error)
69+
userHomeDir func() (string, error)
6970
}
7071

7172
// NewUseWorkspaceOptions returns a new UseWorkspaceOptions.
7273
func NewUseWorkspaceOptions(streams genericclioptions.IOStreams) *UseWorkspaceOptions {
7374
return &UseWorkspaceOptions{
7475
Options: base.NewOptions(streams),
7576

77+
newKCPClusterClient: newKCPClusterClient,
7678
modifyConfig: func(configAccess clientcmd.ConfigAccess, newConfig *clientcmdapi.Config) error {
7779
return clientcmd.ModifyConfig(configAccess, *newConfig, true)
7880
},
@@ -91,13 +93,15 @@ func (o *UseWorkspaceOptions) Complete(args []string) error {
9193
o.Name = args[0]
9294
}
9395

94-
var err error
95-
o.startingConfig, err = o.ClientConfig.ConfigAccess().GetStartingConfig()
96-
if err != nil {
97-
return err
96+
if o.startingConfig == nil {
97+
var err error
98+
o.startingConfig, err = o.ClientConfig.ConfigAccess().GetStartingConfig()
99+
if err != nil {
100+
return err
101+
}
98102
}
99103

100-
kcpClusterClient, err := newKCPClusterClient(o.ClientConfig)
104+
kcpClusterClient, err := o.newKCPClusterClient(o.ClientConfig)
101105
if err != nil {
102106
return err
103107
}

0 commit comments

Comments
 (0)