@@ -20,7 +20,6 @@ import (
20
20
"os"
21
21
"path/filepath"
22
22
"reflect"
23
- "regexp"
24
23
"strings"
25
24
"testing"
26
25
@@ -35,7 +34,6 @@ import (
35
34
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
36
35
"k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
37
36
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
38
- "k8s.io/kubernetes/cmd/kubeadm/app/preflight"
39
37
testutil "k8s.io/kubernetes/cmd/kubeadm/test"
40
38
)
41
39
@@ -114,26 +112,15 @@ func TestRollbackFiles(t *testing.T) {
114
112
}
115
113
116
114
func TestWriteKubeletConfigFiles (t * testing.T ) {
117
- // exit early if the user doesn't have root permission as the test needs to create /etc/kubernetes directory
118
- // while the permission should be granted to the user.
119
- isPrivileged := preflight.IsPrivilegedUserCheck {}
120
- if _ , err := isPrivileged .Check (); err != nil {
121
- return
122
- }
115
+ tempDir := t .TempDir ()
123
116
testCases := []struct {
124
- name string
125
- dryrun bool
126
- patchesDir string
127
- errPattern string
128
- cfg * kubeadmapi.InitConfiguration
117
+ name string
118
+ patchesDir string
119
+ expectedError bool
120
+ cfg * kubeadmapi.InitConfiguration
129
121
}{
130
- // Be careful that if the dryrun is set to false and the test is run on a live cluster, the kubelet config file might be overwritten.
131
- // However, you should be able to find the original config file in /etc/kubernetes/tmp/kubeadm-kubelet-configxxx folder.
132
- // The test haven't clean up the temporary file created under /etc/kubernetes/tmp/ as that could be accidentally delete other files in
133
- // that folder as well which might be unexpected.
134
122
{
135
- name : "write kubelet config file successfully" ,
136
- dryrun : true ,
123
+ name : "write kubelet config file successfully" ,
137
124
cfg : & kubeadmapi.InitConfiguration {
138
125
ClusterConfiguration : kubeadmapi.ClusterConfiguration {
139
126
ComponentConfigs : kubeadmapi.ComponentConfigMap {
@@ -143,16 +130,14 @@ func TestWriteKubeletConfigFiles(t *testing.T) {
143
130
},
144
131
},
145
132
{
146
- name : "aggregate errs: no kubelet config file and cannot read config file" ,
147
- dryrun : true ,
148
- errPattern : missingKubeletConfig ,
149
- cfg : & kubeadmapi.InitConfiguration {},
133
+ name : "aggregate errs: no kubelet config file and cannot read config file" ,
134
+ expectedError : true ,
135
+ cfg : & kubeadmapi.InitConfiguration {},
150
136
},
151
137
{
152
- name : "only one err: patch dir does not exist" ,
153
- dryrun : true ,
154
- patchesDir : "Bogus" ,
155
- errPattern : "could not list patch files for path \" Bogus\" " ,
138
+ name : "only one err: patch dir does not exist" ,
139
+ patchesDir : "Bogus" ,
140
+ expectedError : true ,
156
141
cfg : & kubeadmapi.InitConfiguration {
157
142
ClusterConfiguration : kubeadmapi.ClusterConfiguration {
158
143
ComponentConfigs : kubeadmapi.ComponentConfigMap {
@@ -163,14 +148,9 @@ func TestWriteKubeletConfigFiles(t *testing.T) {
163
148
},
164
149
}
165
150
for _ , tc := range testCases {
166
- err := WriteKubeletConfigFiles (tc .cfg , tc .patchesDir , tc .dryrun , os .Stdout )
167
- if err != nil && tc .errPattern != "" {
168
- if match , _ := regexp .MatchString (tc .errPattern , err .Error ()); ! match {
169
- t .Fatalf ("Expected error contains %q, got %v" , tc .errPattern , err .Error ())
170
- }
171
- }
172
- if err == nil && len (tc .errPattern ) != 0 {
173
- t .Fatalf ("WriteKubeletConfigFiles didn't return error expected %s" , tc .errPattern )
151
+ err := WriteKubeletConfigFiles (tc .cfg , tempDir , tc .patchesDir , true , os .Stdout )
152
+ if (err != nil ) != tc .expectedError {
153
+ t .Fatalf ("expected error: %v, got: %v, error: %v" , tc .expectedError , err != nil , err )
174
154
}
175
155
}
176
156
}
0 commit comments