@@ -20,7 +20,6 @@ import (
2020 "os"
2121 "path/filepath"
2222 "reflect"
23- "regexp"
2423 "strings"
2524 "testing"
2625
@@ -35,7 +34,6 @@ import (
3534 kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
3635 "k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
3736 "k8s.io/kubernetes/cmd/kubeadm/app/constants"
38- "k8s.io/kubernetes/cmd/kubeadm/app/preflight"
3937 testutil "k8s.io/kubernetes/cmd/kubeadm/test"
4038)
4139
@@ -114,26 +112,15 @@ func TestRollbackFiles(t *testing.T) {
114112}
115113
116114func 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 ()
123116 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
129121 }{
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.
134122 {
135- name : "write kubelet config file successfully" ,
136- dryrun : true ,
123+ name : "write kubelet config file successfully" ,
137124 cfg : & kubeadmapi.InitConfiguration {
138125 ClusterConfiguration : kubeadmapi.ClusterConfiguration {
139126 ComponentConfigs : kubeadmapi.ComponentConfigMap {
@@ -143,16 +130,14 @@ func TestWriteKubeletConfigFiles(t *testing.T) {
143130 },
144131 },
145132 {
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 {},
150136 },
151137 {
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 ,
156141 cfg : & kubeadmapi.InitConfiguration {
157142 ClusterConfiguration : kubeadmapi.ClusterConfiguration {
158143 ComponentConfigs : kubeadmapi.ComponentConfigMap {
@@ -163,14 +148,9 @@ func TestWriteKubeletConfigFiles(t *testing.T) {
163148 },
164149 }
165150 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 )
174154 }
175155 }
176156}
0 commit comments