sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/rollout_test.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package client
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	. "github.com/onsi/gomega"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  
    26  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    27  	clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
    28  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
    29  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
    30  )
    31  
    32  func fakeClientForRollout() *fakeClient {
    33  	core := config.NewProvider("cluster-api", "https://somewhere.com", clusterctlv1.CoreProviderType)
    34  	infra := config.NewProvider("infra", "https://somewhere.com", clusterctlv1.InfrastructureProviderType)
    35  	md1 := &clusterv1.MachineDeployment{
    36  		TypeMeta: metav1.TypeMeta{
    37  			Kind:       "MachineDeployment",
    38  			APIVersion: "cluster.x-k8s.io/v1beta1",
    39  		},
    40  		ObjectMeta: metav1.ObjectMeta{
    41  			Namespace: "default",
    42  			Name:      "md-1",
    43  		},
    44  	}
    45  	md2 := &clusterv1.MachineDeployment{
    46  		TypeMeta: metav1.TypeMeta{
    47  			Kind:       "MachineDeployment",
    48  			APIVersion: "cluster.x-k8s.io/v1beta1",
    49  		},
    50  		ObjectMeta: metav1.ObjectMeta{
    51  			Namespace: "default",
    52  			Name:      "md-2",
    53  		},
    54  	}
    55  
    56  	ctx := context.Background()
    57  
    58  	config1 := newFakeConfig(ctx).
    59  		WithProvider(core).
    60  		WithProvider(infra)
    61  
    62  	cluster1 := newFakeCluster(cluster.Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"}, config1).
    63  		WithProviderInventory(core.Name(), core.Type(), "v1.0.0", "cluster-api-system").
    64  		WithProviderInventory(infra.Name(), infra.Type(), "v2.0.0", "infra-system").
    65  		WithObjs(md1).
    66  		WithObjs(md2)
    67  
    68  	client := newFakeClient(ctx, config1).
    69  		WithCluster(cluster1)
    70  
    71  	return client
    72  }
    73  
    74  func Test_clusterctlClient_RolloutRestart(t *testing.T) {
    75  	type fields struct {
    76  		client *fakeClient
    77  	}
    78  	type args struct {
    79  		options RolloutRestartOptions
    80  	}
    81  	tests := []struct {
    82  		name    string
    83  		fields  fields
    84  		args    args
    85  		wantErr bool
    86  	}{
    87  		{
    88  			name: "return an error if machinedeployment is not found",
    89  			fields: fields{
    90  				client: fakeClientForRollout(),
    91  			},
    92  			args: args{
    93  				options: RolloutRestartOptions{
    94  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
    95  					Resources:  []string{"machinedeployment/foo"},
    96  					Namespace:  "default",
    97  				},
    98  			},
    99  			wantErr: true,
   100  		},
   101  		{
   102  			name: "return error if one of the machinedeployments is not found",
   103  			fields: fields{
   104  				client: fakeClientForRollout(),
   105  			},
   106  			args: args{
   107  				options: RolloutRestartOptions{
   108  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   109  					Resources:  []string{"machinedeployment/md-1", "machinedeployment/md-does-not-exist"},
   110  					Namespace:  "default",
   111  				},
   112  			},
   113  			wantErr: true,
   114  		},
   115  		{
   116  			name: "return error if unknown resource specified",
   117  			fields: fields{
   118  				client: fakeClientForRollout(),
   119  			},
   120  			args: args{
   121  				options: RolloutRestartOptions{
   122  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   123  					Resources:  []string{"foo/bar"},
   124  					Namespace:  "default",
   125  				},
   126  			},
   127  			wantErr: true,
   128  		},
   129  		{
   130  			name: "return error if no resource specified",
   131  			fields: fields{
   132  				client: fakeClientForRollout(),
   133  			},
   134  			args: args{
   135  				options: RolloutRestartOptions{
   136  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   137  					Namespace:  "default",
   138  				},
   139  			},
   140  			wantErr: true,
   141  		},
   142  		{
   143  			name: "do not return error if machinedeployment found",
   144  			fields: fields{
   145  				client: fakeClientForRollout(),
   146  			},
   147  			args: args{
   148  				options: RolloutRestartOptions{
   149  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   150  					Resources:  []string{"machinedeployment/md-1"},
   151  					Namespace:  "default",
   152  				},
   153  			},
   154  			wantErr: false,
   155  		},
   156  		{
   157  			name: "do not return error if all machinedeployments found",
   158  			fields: fields{
   159  				client: fakeClientForRollout(),
   160  			},
   161  			args: args{
   162  				options: RolloutRestartOptions{
   163  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   164  					Resources:  []string{"machinedeployment/md-1", "machinedeployment/md-2"},
   165  					Namespace:  "default",
   166  				},
   167  			},
   168  			wantErr: false,
   169  		},
   170  	}
   171  
   172  	for _, tt := range tests {
   173  		t.Run(tt.name, func(t *testing.T) {
   174  			g := NewWithT(t)
   175  
   176  			ctx := context.Background()
   177  
   178  			err := tt.fields.client.RolloutRestart(ctx, tt.args.options)
   179  			if tt.wantErr {
   180  				g.Expect(err).To(HaveOccurred())
   181  				return
   182  			}
   183  			g.Expect(err).ToNot(HaveOccurred())
   184  		})
   185  	}
   186  }
   187  
   188  func Test_clusterctlClient_RolloutPause(t *testing.T) {
   189  	type fields struct {
   190  		client *fakeClient
   191  	}
   192  	type args struct {
   193  		options RolloutPauseOptions
   194  	}
   195  	tests := []struct {
   196  		name    string
   197  		fields  fields
   198  		args    args
   199  		wantErr bool
   200  	}{
   201  		{
   202  			name: "return an error if machinedeployment is not found",
   203  			fields: fields{
   204  				client: fakeClientForRollout(),
   205  			},
   206  			args: args{
   207  				options: RolloutPauseOptions{
   208  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   209  					Resources:  []string{"machinedeployment/foo"},
   210  					Namespace:  "default",
   211  				},
   212  			},
   213  			wantErr: true,
   214  		},
   215  		{
   216  			name: "return error if one of the machinedeployments is not found",
   217  			fields: fields{
   218  				client: fakeClientForRollout(),
   219  			},
   220  			args: args{
   221  				options: RolloutPauseOptions{
   222  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   223  					Resources:  []string{"machinedeployment/md-1", "machinedeployment/md-does-not-exist"},
   224  					Namespace:  "default",
   225  				},
   226  			},
   227  			wantErr: true,
   228  		},
   229  		{
   230  			name: "return error if unknown resource specified",
   231  			fields: fields{
   232  				client: fakeClientForRollout(),
   233  			},
   234  			args: args{
   235  				options: RolloutPauseOptions{
   236  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   237  					Resources:  []string{"foo/bar"},
   238  					Namespace:  "default",
   239  				},
   240  			},
   241  			wantErr: true,
   242  		},
   243  		{
   244  			name: "return error if no resource specified",
   245  			fields: fields{
   246  				client: fakeClientForRollout(),
   247  			},
   248  			args: args{
   249  				options: RolloutPauseOptions{
   250  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   251  					Namespace:  "default",
   252  				},
   253  			},
   254  			wantErr: true,
   255  		},
   256  	}
   257  	for _, tt := range tests {
   258  		t.Run(tt.name, func(t *testing.T) {
   259  			g := NewWithT(t)
   260  
   261  			ctx := context.Background()
   262  
   263  			err := tt.fields.client.RolloutPause(ctx, tt.args.options)
   264  			if tt.wantErr {
   265  				g.Expect(err).To(HaveOccurred())
   266  				return
   267  			}
   268  			g.Expect(err).ToNot(HaveOccurred())
   269  		})
   270  	}
   271  }
   272  
   273  func Test_clusterctlClient_RolloutResume(t *testing.T) {
   274  	type fields struct {
   275  		client *fakeClient
   276  	}
   277  	type args struct {
   278  		options RolloutResumeOptions
   279  	}
   280  	tests := []struct {
   281  		name    string
   282  		fields  fields
   283  		args    args
   284  		wantErr bool
   285  	}{
   286  		{
   287  			name: "return an error if machinedeployment is not found",
   288  			fields: fields{
   289  				client: fakeClientForRollout(),
   290  			},
   291  			args: args{
   292  				options: RolloutResumeOptions{
   293  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   294  					Resources:  []string{"machinedeployment/foo"},
   295  					Namespace:  "default",
   296  				},
   297  			},
   298  			wantErr: true,
   299  		},
   300  		{
   301  			name: "return error if one of the machinedeployments is not found",
   302  			fields: fields{
   303  				client: fakeClientForRollout(),
   304  			},
   305  			args: args{
   306  				options: RolloutResumeOptions{
   307  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   308  					Resources:  []string{"machinedeployment/md-1", "machinedeployment/md-does-not-exist"},
   309  					Namespace:  "default",
   310  				},
   311  			},
   312  			wantErr: true,
   313  		},
   314  		{
   315  			name: "return error if unknown resource specified",
   316  			fields: fields{
   317  				client: fakeClientForRollout(),
   318  			},
   319  			args: args{
   320  				options: RolloutResumeOptions{
   321  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   322  					Resources:  []string{"foo/bar"},
   323  					Namespace:  "default",
   324  				},
   325  			},
   326  			wantErr: true,
   327  		},
   328  		{
   329  			name: "return error if no resource specified",
   330  			fields: fields{
   331  				client: fakeClientForRollout(),
   332  			},
   333  			args: args{
   334  				options: RolloutResumeOptions{
   335  					Kubeconfig: Kubeconfig{Path: "kubeconfig", Context: "mgmt-context"},
   336  					Namespace:  "default",
   337  				},
   338  			},
   339  			wantErr: true,
   340  		},
   341  	}
   342  	for _, tt := range tests {
   343  		t.Run(tt.name, func(t *testing.T) {
   344  			g := NewWithT(t)
   345  
   346  			ctx := context.Background()
   347  
   348  			err := tt.fields.client.RolloutResume(ctx, tt.args.options)
   349  			if tt.wantErr {
   350  				g.Expect(err).To(HaveOccurred())
   351  				return
   352  			}
   353  			g.Expect(err).ToNot(HaveOccurred())
   354  		})
   355  	}
   356  }