sigs.k8s.io/cluster-api@v1.6.3/bootstrap/kubeadm/internal/cloudinit/cloudinit_test.go (about) 1 /* 2 Copyright 2019 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 cloudinit 18 19 import ( 20 "testing" 21 22 . "github.com/onsi/gomega" 23 "k8s.io/utils/pointer" 24 25 bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" 26 "sigs.k8s.io/cluster-api/util/certs" 27 "sigs.k8s.io/cluster-api/util/secret" 28 ) 29 30 func TestNewInitControlPlaneAdditionalFileEncodings(t *testing.T) { 31 g := NewWithT(t) 32 33 cpinput := &ControlPlaneInput{ 34 BaseUserData: BaseUserData{ 35 Header: "test", 36 PreKubeadmCommands: nil, 37 PostKubeadmCommands: nil, 38 AdditionalFiles: []bootstrapv1.File{ 39 { 40 Path: "/tmp/my-path", 41 Encoding: bootstrapv1.Base64, 42 Content: "aGk=", 43 }, 44 { 45 Path: "/tmp/my-other-path", 46 Content: "hi", 47 }, 48 { 49 Path: "/tmp/existing-path", 50 Append: true, 51 Content: "hi", 52 }, 53 }, 54 WriteFiles: nil, 55 Users: nil, 56 NTP: nil, 57 }, 58 Certificates: secret.Certificates{}, 59 ClusterConfiguration: "my-cluster-config", 60 InitConfiguration: "my-init-config", 61 } 62 63 for _, certificate := range cpinput.Certificates { 64 certificate.KeyPair = &certs.KeyPair{ 65 Cert: []byte("some certificate"), 66 Key: []byte("some key"), 67 } 68 } 69 70 out, err := NewInitControlPlane(cpinput) 71 g.Expect(err).ToNot(HaveOccurred()) 72 73 expectedFiles := []string{ 74 `- path: /tmp/my-path 75 encoding: "base64" 76 content: | 77 aGk=`, 78 `- path: /tmp/my-other-path 79 content: | 80 hi`, 81 `- path: /tmp/existing-path 82 append: true 83 content: | 84 hi`, 85 } 86 for _, f := range expectedFiles { 87 g.Expect(out).To(ContainSubstring(f)) 88 } 89 } 90 91 func TestNewInitControlPlaneCommands(t *testing.T) { 92 g := NewWithT(t) 93 94 cpinput := &ControlPlaneInput{ 95 BaseUserData: BaseUserData{ 96 Header: "test", 97 PreKubeadmCommands: []string{`"echo $(date) ': hello world!'"`}, 98 PostKubeadmCommands: []string{"echo $(date) ': hello world!'"}, 99 AdditionalFiles: nil, 100 WriteFiles: nil, 101 Users: nil, 102 NTP: nil, 103 }, 104 Certificates: secret.Certificates{}, 105 ClusterConfiguration: "my-cluster-config", 106 InitConfiguration: "my-init-config", 107 } 108 109 for _, certificate := range cpinput.Certificates { 110 certificate.KeyPair = &certs.KeyPair{ 111 Cert: []byte("some certificate"), 112 Key: []byte("some key"), 113 } 114 } 115 116 out, err := NewInitControlPlane(cpinput) 117 g.Expect(err).ToNot(HaveOccurred()) 118 119 expectedCommands := []string{ 120 `"\"echo $(date) ': hello world!'\""`, 121 `"echo $(date) ': hello world!'"`, 122 } 123 for _, f := range expectedCommands { 124 g.Expect(out).To(ContainSubstring(f)) 125 } 126 } 127 128 func TestNewInitControlPlaneDiskMounts(t *testing.T) { 129 g := NewWithT(t) 130 131 cpinput := &ControlPlaneInput{ 132 BaseUserData: BaseUserData{ 133 Header: "test", 134 PreKubeadmCommands: nil, 135 PostKubeadmCommands: nil, 136 WriteFiles: nil, 137 Users: nil, 138 NTP: nil, 139 DiskSetup: &bootstrapv1.DiskSetup{ 140 Partitions: []bootstrapv1.Partition{ 141 { 142 Device: "test-device", 143 Layout: true, 144 Overwrite: pointer.Bool(false), 145 TableType: pointer.String("gpt"), 146 }, 147 }, 148 Filesystems: []bootstrapv1.Filesystem{ 149 { 150 Device: "test-device", 151 Filesystem: "ext4", 152 Label: "test_disk", 153 ExtraOpts: []string{"-F", "-E", "lazy_itable_init=1,lazy_journal_init=1"}, 154 }, 155 }, 156 }, 157 Mounts: []bootstrapv1.MountPoints{ 158 {"test_disk", "/var/lib/testdir"}, 159 }, 160 }, 161 Certificates: secret.Certificates{}, 162 ClusterConfiguration: "my-cluster-config", 163 InitConfiguration: "my-init-config", 164 } 165 166 out, err := NewInitControlPlane(cpinput) 167 g.Expect(err).ToNot(HaveOccurred()) 168 169 expectedDiskSetup := `disk_setup: 170 test-device: 171 table_type: gpt 172 layout: true 173 overwrite: false` 174 expectedFSSetup := `fs_setup: 175 - label: test_disk 176 filesystem: ext4 177 device: test-device 178 extra_opts: 179 - -F 180 - -E 181 - lazy_itable_init=1,lazy_journal_init=1` 182 expectedMounts := `mounts: 183 - - test_disk 184 - /var/lib/testdir` 185 186 g.Expect(string(out)).To(ContainSubstring(expectedDiskSetup)) 187 g.Expect(string(out)).To(ContainSubstring(expectedFSSetup)) 188 g.Expect(string(out)).To(ContainSubstring(expectedMounts)) 189 } 190 191 func TestNewJoinControlPlaneAdditionalFileEncodings(t *testing.T) { 192 g := NewWithT(t) 193 194 cpinput := &ControlPlaneJoinInput{ 195 BaseUserData: BaseUserData{ 196 Header: "test", 197 PreKubeadmCommands: nil, 198 PostKubeadmCommands: nil, 199 AdditionalFiles: []bootstrapv1.File{ 200 { 201 Path: "/tmp/my-path", 202 Encoding: bootstrapv1.Base64, 203 Content: "aGk=", 204 }, 205 { 206 Path: "/tmp/my-other-path", 207 Content: "hi", 208 }, 209 }, 210 WriteFiles: nil, 211 Users: nil, 212 NTP: nil, 213 }, 214 Certificates: secret.Certificates{}, 215 BootstrapToken: "my-bootstrap-token", 216 JoinConfiguration: "my-join-config", 217 } 218 219 for _, certificate := range cpinput.Certificates { 220 certificate.KeyPair = &certs.KeyPair{ 221 Cert: []byte("some certificate"), 222 Key: []byte("some key"), 223 } 224 } 225 226 out, err := NewJoinControlPlane(cpinput) 227 g.Expect(err).ToNot(HaveOccurred()) 228 229 expectedFiles := []string{ 230 `- path: /tmp/my-path 231 encoding: "base64" 232 content: | 233 aGk=`, 234 `- path: /tmp/my-other-path 235 content: | 236 hi`, 237 } 238 for _, f := range expectedFiles { 239 g.Expect(out).To(ContainSubstring(f)) 240 } 241 } 242 243 func TestNewJoinControlPlaneExperimentalRetry(t *testing.T) { 244 g := NewWithT(t) 245 246 cpinput := &ControlPlaneJoinInput{ 247 BaseUserData: BaseUserData{ 248 Header: "test", 249 PreKubeadmCommands: nil, 250 PostKubeadmCommands: nil, 251 UseExperimentalRetry: true, 252 WriteFiles: nil, 253 Users: nil, 254 NTP: nil, 255 }, 256 Certificates: secret.Certificates{}, 257 BootstrapToken: "my-bootstrap-token", 258 JoinConfiguration: "my-join-config", 259 } 260 261 for _, certificate := range cpinput.Certificates { 262 certificate.KeyPair = &certs.KeyPair{ 263 Cert: []byte("some certificate"), 264 Key: []byte("some key"), 265 } 266 } 267 268 out, err := NewJoinControlPlane(cpinput) 269 g.Expect(err).ToNot(HaveOccurred()) 270 271 expectedFiles := []string{ 272 `- path: ` + retriableJoinScriptName + ` 273 owner: ` + retriableJoinScriptOwner + ` 274 permissions: '` + retriableJoinScriptPermissions + `' 275 `, 276 } 277 for _, f := range expectedFiles { 278 g.Expect(out).To(ContainSubstring(f)) 279 } 280 }