utils.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package utils
  14. import (
  15. "crypto/sha256"
  16. "encoding/base64"
  17. "fmt"
  18. "math/rand"
  19. "path/filepath"
  20. "strings"
  21. "time"
  22. "github.com/onsi/ginkgo"
  23. "github.com/onsi/gomega"
  24. v1 "k8s.io/api/core/v1"
  25. rbacv1 "k8s.io/api/rbac/v1"
  26. apierrs "k8s.io/apimachinery/pkg/api/errors"
  27. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  28. "k8s.io/apimachinery/pkg/util/wait"
  29. clientset "k8s.io/client-go/kubernetes"
  30. "k8s.io/kubernetes/test/e2e/framework"
  31. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  32. e2essh "k8s.io/kubernetes/test/e2e/framework/ssh"
  33. imageutils "k8s.io/kubernetes/test/utils/image"
  34. uexec "k8s.io/utils/exec"
  35. )
  36. type KubeletOpt string
  37. const (
  38. NodeStateTimeout = 1 * time.Minute
  39. KStart KubeletOpt = "start"
  40. KStop KubeletOpt = "stop"
  41. KRestart KubeletOpt = "restart"
  42. )
  43. const (
  44. // ClusterRole name for e2e test Priveledged Pod Security Policy User
  45. podSecurityPolicyPrivilegedClusterRoleName = "e2e-test-privileged-psp"
  46. )
  47. // PodExec wraps RunKubectl to execute a bash cmd in target pod
  48. func PodExec(pod *v1.Pod, bashExec string) (string, error) {
  49. return framework.RunKubectl("exec", fmt.Sprintf("--namespace=%s", pod.Namespace), pod.Name, "--", "/bin/sh", "-c", bashExec)
  50. }
  51. // VerifyExecInPodSucceed verifies bash cmd in target pod succeed
  52. func VerifyExecInPodSucceed(pod *v1.Pod, bashExec string) {
  53. _, err := PodExec(pod, bashExec)
  54. if err != nil {
  55. if err, ok := err.(uexec.CodeExitError); ok {
  56. exitCode := err.ExitStatus()
  57. framework.ExpectNoError(err,
  58. "%q should succeed, but failed with exit code %d and error message %q",
  59. bashExec, exitCode, err)
  60. } else {
  61. framework.ExpectNoError(err,
  62. "%q should succeed, but failed with error message %q",
  63. bashExec, err)
  64. }
  65. }
  66. }
  67. // VerifyExecInPodFail verifies bash cmd in target pod fail with certain exit code
  68. func VerifyExecInPodFail(pod *v1.Pod, bashExec string, exitCode int) {
  69. _, err := PodExec(pod, bashExec)
  70. if err != nil {
  71. if err, ok := err.(uexec.CodeExitError); ok {
  72. actualExitCode := err.ExitStatus()
  73. gomega.Expect(actualExitCode).To(gomega.Equal(exitCode),
  74. "%q should fail with exit code %d, but failed with exit code %d and error message %q",
  75. bashExec, exitCode, actualExitCode, err)
  76. } else {
  77. framework.ExpectNoError(err,
  78. "%q should fail with exit code %d, but failed with error message %q",
  79. bashExec, exitCode, err)
  80. }
  81. }
  82. framework.ExpectError(err, "%q should fail with exit code %d, but exit without error", bashExec, exitCode)
  83. }
  84. // KubeletCommand performs `start`, `restart`, or `stop` on the kubelet running on the node of the target pod and waits
  85. // for the desired statues..
  86. // - First issues the command via `systemctl`
  87. // - If `systemctl` returns stderr "command not found, issues the command via `service`
  88. // - If `service` also returns stderr "command not found", the test is aborted.
  89. // Allowed kubeletOps are `KStart`, `KStop`, and `KRestart`
  90. func KubeletCommand(kOp KubeletOpt, c clientset.Interface, pod *v1.Pod) {
  91. command := ""
  92. sudoPresent := false
  93. systemctlPresent := false
  94. kubeletPid := ""
  95. nodeIP, err := framework.GetHostExternalAddress(c, pod)
  96. framework.ExpectNoError(err)
  97. nodeIP = nodeIP + ":22"
  98. e2elog.Logf("Checking if sudo command is present")
  99. sshResult, err := e2essh.SSH("sudo --version", nodeIP, framework.TestContext.Provider)
  100. framework.ExpectNoError(err, fmt.Sprintf("SSH to Node %q errored.", pod.Spec.NodeName))
  101. if !strings.Contains(sshResult.Stderr, "command not found") {
  102. sudoPresent = true
  103. }
  104. e2elog.Logf("Checking if systemctl command is present")
  105. sshResult, err = e2essh.SSH("systemctl --version", nodeIP, framework.TestContext.Provider)
  106. framework.ExpectNoError(err, fmt.Sprintf("SSH to Node %q errored.", pod.Spec.NodeName))
  107. if !strings.Contains(sshResult.Stderr, "command not found") {
  108. command = fmt.Sprintf("systemctl %s kubelet", string(kOp))
  109. systemctlPresent = true
  110. } else {
  111. command = fmt.Sprintf("service kubelet %s", string(kOp))
  112. }
  113. if sudoPresent {
  114. command = fmt.Sprintf("sudo %s", command)
  115. }
  116. if kOp == KRestart {
  117. kubeletPid = getKubeletMainPid(nodeIP, sudoPresent, systemctlPresent)
  118. }
  119. e2elog.Logf("Attempting `%s`", command)
  120. sshResult, err = e2essh.SSH(command, nodeIP, framework.TestContext.Provider)
  121. framework.ExpectNoError(err, fmt.Sprintf("SSH to Node %q errored.", pod.Spec.NodeName))
  122. e2essh.LogResult(sshResult)
  123. gomega.Expect(sshResult.Code).To(gomega.BeZero(), "Failed to [%s] kubelet:\n%#v", string(kOp), sshResult)
  124. if kOp == KStop {
  125. if ok := framework.WaitForNodeToBeNotReady(c, pod.Spec.NodeName, NodeStateTimeout); !ok {
  126. framework.Failf("Node %s failed to enter NotReady state", pod.Spec.NodeName)
  127. }
  128. }
  129. if kOp == KRestart {
  130. // Wait for a minute to check if kubelet Pid is getting changed
  131. isPidChanged := false
  132. for start := time.Now(); time.Since(start) < 1*time.Minute; time.Sleep(2 * time.Second) {
  133. kubeletPidAfterRestart := getKubeletMainPid(nodeIP, sudoPresent, systemctlPresent)
  134. if kubeletPid != kubeletPidAfterRestart {
  135. isPidChanged = true
  136. break
  137. }
  138. }
  139. gomega.Expect(isPidChanged).To(gomega.BeTrue(), "Kubelet PID remained unchanged after restarting Kubelet")
  140. e2elog.Logf("Noticed that kubelet PID is changed. Waiting for 30 Seconds for Kubelet to come back")
  141. time.Sleep(30 * time.Second)
  142. }
  143. if kOp == KStart || kOp == KRestart {
  144. // For kubelet start and restart operations, Wait until Node becomes Ready
  145. if ok := framework.WaitForNodeToBeReady(c, pod.Spec.NodeName, NodeStateTimeout); !ok {
  146. framework.Failf("Node %s failed to enter Ready state", pod.Spec.NodeName)
  147. }
  148. }
  149. }
  150. // getKubeletMainPid return the Main PID of the Kubelet Process
  151. func getKubeletMainPid(nodeIP string, sudoPresent bool, systemctlPresent bool) string {
  152. command := ""
  153. if systemctlPresent {
  154. command = "systemctl status kubelet | grep 'Main PID'"
  155. } else {
  156. command = "service kubelet status | grep 'Main PID'"
  157. }
  158. if sudoPresent {
  159. command = fmt.Sprintf("sudo %s", command)
  160. }
  161. e2elog.Logf("Attempting `%s`", command)
  162. sshResult, err := e2essh.SSH(command, nodeIP, framework.TestContext.Provider)
  163. framework.ExpectNoError(err, fmt.Sprintf("SSH to Node %q errored.", nodeIP))
  164. e2essh.LogResult(sshResult)
  165. gomega.Expect(sshResult.Code).To(gomega.BeZero(), "Failed to get kubelet PID")
  166. gomega.Expect(sshResult.Stdout).NotTo(gomega.BeEmpty(), "Kubelet Main PID should not be Empty")
  167. return sshResult.Stdout
  168. }
  169. // TestKubeletRestartsAndRestoresMount tests that a volume mounted to a pod remains mounted after a kubelet restarts
  170. func TestKubeletRestartsAndRestoresMount(c clientset.Interface, f *framework.Framework, clientPod *v1.Pod) {
  171. ginkgo.By("Writing to the volume.")
  172. file := "/mnt/_SUCCESS"
  173. out, err := PodExec(clientPod, fmt.Sprintf("touch %s", file))
  174. e2elog.Logf(out)
  175. framework.ExpectNoError(err)
  176. ginkgo.By("Restarting kubelet")
  177. KubeletCommand(KRestart, c, clientPod)
  178. ginkgo.By("Testing that written file is accessible.")
  179. out, err = PodExec(clientPod, fmt.Sprintf("cat %s", file))
  180. e2elog.Logf(out)
  181. framework.ExpectNoError(err)
  182. e2elog.Logf("Volume mount detected on pod %s and written file %s is readable post-restart.", clientPod.Name, file)
  183. }
  184. // TestVolumeUnmountsFromDeletedPod tests that a volume unmounts if the client pod was deleted while the kubelet was down.
  185. // forceDelete is true indicating whether the pod is forcefully deleted.
  186. func TestVolumeUnmountsFromDeletedPodWithForceOption(c clientset.Interface, f *framework.Framework, clientPod *v1.Pod, forceDelete bool, checkSubpath bool) {
  187. nodeIP, err := framework.GetHostExternalAddress(c, clientPod)
  188. framework.ExpectNoError(err)
  189. nodeIP = nodeIP + ":22"
  190. ginkgo.By("Expecting the volume mount to be found.")
  191. result, err := e2essh.SSH(fmt.Sprintf("mount | grep %s | grep -v volume-subpaths", clientPod.UID), nodeIP, framework.TestContext.Provider)
  192. e2essh.LogResult(result)
  193. framework.ExpectNoError(err, "Encountered SSH error.")
  194. gomega.Expect(result.Code).To(gomega.BeZero(), fmt.Sprintf("Expected grep exit code of 0, got %d", result.Code))
  195. if checkSubpath {
  196. ginkgo.By("Expecting the volume subpath mount to be found.")
  197. result, err := e2essh.SSH(fmt.Sprintf("cat /proc/self/mountinfo | grep %s | grep volume-subpaths", clientPod.UID), nodeIP, framework.TestContext.Provider)
  198. e2essh.LogResult(result)
  199. framework.ExpectNoError(err, "Encountered SSH error.")
  200. gomega.Expect(result.Code).To(gomega.BeZero(), fmt.Sprintf("Expected grep exit code of 0, got %d", result.Code))
  201. }
  202. // This command is to make sure kubelet is started after test finishes no matter it fails or not.
  203. defer func() {
  204. KubeletCommand(KStart, c, clientPod)
  205. }()
  206. ginkgo.By("Stopping the kubelet.")
  207. KubeletCommand(KStop, c, clientPod)
  208. ginkgo.By(fmt.Sprintf("Deleting Pod %q", clientPod.Name))
  209. if forceDelete {
  210. err = c.CoreV1().Pods(clientPod.Namespace).Delete(clientPod.Name, metav1.NewDeleteOptions(0))
  211. } else {
  212. err = c.CoreV1().Pods(clientPod.Namespace).Delete(clientPod.Name, &metav1.DeleteOptions{})
  213. }
  214. framework.ExpectNoError(err)
  215. ginkgo.By("Starting the kubelet and waiting for pod to delete.")
  216. KubeletCommand(KStart, c, clientPod)
  217. err = f.WaitForPodNotFound(clientPod.Name, framework.PodDeleteTimeout)
  218. if err != nil {
  219. framework.ExpectNoError(err, "Expected pod to be not found.")
  220. }
  221. if forceDelete {
  222. // With forceDelete, since pods are immediately deleted from API server, there is no way to be sure when volumes are torn down
  223. // so wait some time to finish
  224. time.Sleep(30 * time.Second)
  225. }
  226. ginkgo.By("Expecting the volume mount not to be found.")
  227. result, err = e2essh.SSH(fmt.Sprintf("mount | grep %s | grep -v volume-subpaths", clientPod.UID), nodeIP, framework.TestContext.Provider)
  228. e2essh.LogResult(result)
  229. framework.ExpectNoError(err, "Encountered SSH error.")
  230. gomega.Expect(result.Stdout).To(gomega.BeEmpty(), "Expected grep stdout to be empty (i.e. no mount found).")
  231. e2elog.Logf("Volume unmounted on node %s", clientPod.Spec.NodeName)
  232. if checkSubpath {
  233. ginkgo.By("Expecting the volume subpath mount not to be found.")
  234. result, err = e2essh.SSH(fmt.Sprintf("cat /proc/self/mountinfo | grep %s | grep volume-subpaths", clientPod.UID), nodeIP, framework.TestContext.Provider)
  235. e2essh.LogResult(result)
  236. framework.ExpectNoError(err, "Encountered SSH error.")
  237. gomega.Expect(result.Stdout).To(gomega.BeEmpty(), "Expected grep stdout to be empty (i.e. no subpath mount found).")
  238. e2elog.Logf("Subpath volume unmounted on node %s", clientPod.Spec.NodeName)
  239. }
  240. }
  241. // TestVolumeUnmountsFromDeletedPod tests that a volume unmounts if the client pod was deleted while the kubelet was down.
  242. func TestVolumeUnmountsFromDeletedPod(c clientset.Interface, f *framework.Framework, clientPod *v1.Pod) {
  243. TestVolumeUnmountsFromDeletedPodWithForceOption(c, f, clientPod, false, false)
  244. }
  245. // TestVolumeUnmountsFromFoceDeletedPod tests that a volume unmounts if the client pod was forcefully deleted while the kubelet was down.
  246. func TestVolumeUnmountsFromForceDeletedPod(c clientset.Interface, f *framework.Framework, clientPod *v1.Pod) {
  247. TestVolumeUnmountsFromDeletedPodWithForceOption(c, f, clientPod, true, false)
  248. }
  249. // RunInPodWithVolume runs a command in a pod with given claim mounted to /mnt directory.
  250. func RunInPodWithVolume(c clientset.Interface, ns, claimName, command string) {
  251. pod := &v1.Pod{
  252. TypeMeta: metav1.TypeMeta{
  253. Kind: "Pod",
  254. APIVersion: "v1",
  255. },
  256. ObjectMeta: metav1.ObjectMeta{
  257. GenerateName: "pvc-volume-tester-",
  258. },
  259. Spec: v1.PodSpec{
  260. Containers: []v1.Container{
  261. {
  262. Name: "volume-tester",
  263. Image: imageutils.GetE2EImage(imageutils.BusyBox),
  264. Command: []string{"/bin/sh"},
  265. Args: []string{"-c", command},
  266. VolumeMounts: []v1.VolumeMount{
  267. {
  268. Name: "my-volume",
  269. MountPath: "/mnt/test",
  270. },
  271. },
  272. },
  273. },
  274. RestartPolicy: v1.RestartPolicyNever,
  275. Volumes: []v1.Volume{
  276. {
  277. Name: "my-volume",
  278. VolumeSource: v1.VolumeSource{
  279. PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
  280. ClaimName: claimName,
  281. ReadOnly: false,
  282. },
  283. },
  284. },
  285. },
  286. },
  287. }
  288. pod, err := c.CoreV1().Pods(ns).Create(pod)
  289. framework.ExpectNoError(err, "Failed to create pod: %v", err)
  290. defer func() {
  291. framework.DeletePodOrFail(c, ns, pod.Name)
  292. }()
  293. framework.ExpectNoError(framework.WaitForPodSuccessInNamespaceSlow(c, pod.Name, pod.Namespace))
  294. }
  295. func StartExternalProvisioner(c clientset.Interface, ns string, externalPluginName string) *v1.Pod {
  296. podClient := c.CoreV1().Pods(ns)
  297. provisionerPod := &v1.Pod{
  298. TypeMeta: metav1.TypeMeta{
  299. Kind: "Pod",
  300. APIVersion: "v1",
  301. },
  302. ObjectMeta: metav1.ObjectMeta{
  303. GenerateName: "external-provisioner-",
  304. },
  305. Spec: v1.PodSpec{
  306. Containers: []v1.Container{
  307. {
  308. Name: "nfs-provisioner",
  309. Image: "quay.io/kubernetes_incubator/nfs-provisioner:v2.2.0-k8s1.12",
  310. SecurityContext: &v1.SecurityContext{
  311. Capabilities: &v1.Capabilities{
  312. Add: []v1.Capability{"DAC_READ_SEARCH"},
  313. },
  314. },
  315. Args: []string{
  316. "-provisioner=" + externalPluginName,
  317. "-grace-period=0",
  318. },
  319. Ports: []v1.ContainerPort{
  320. {Name: "nfs", ContainerPort: 2049},
  321. {Name: "mountd", ContainerPort: 20048},
  322. {Name: "rpcbind", ContainerPort: 111},
  323. {Name: "rpcbind-udp", ContainerPort: 111, Protocol: v1.ProtocolUDP},
  324. },
  325. Env: []v1.EnvVar{
  326. {
  327. Name: "POD_IP",
  328. ValueFrom: &v1.EnvVarSource{
  329. FieldRef: &v1.ObjectFieldSelector{
  330. FieldPath: "status.podIP",
  331. },
  332. },
  333. },
  334. },
  335. ImagePullPolicy: v1.PullIfNotPresent,
  336. VolumeMounts: []v1.VolumeMount{
  337. {
  338. Name: "export-volume",
  339. MountPath: "/export",
  340. },
  341. },
  342. },
  343. },
  344. Volumes: []v1.Volume{
  345. {
  346. Name: "export-volume",
  347. VolumeSource: v1.VolumeSource{
  348. EmptyDir: &v1.EmptyDirVolumeSource{},
  349. },
  350. },
  351. },
  352. },
  353. }
  354. provisionerPod, err := podClient.Create(provisionerPod)
  355. framework.ExpectNoError(err, "Failed to create %s pod: %v", provisionerPod.Name, err)
  356. framework.ExpectNoError(framework.WaitForPodRunningInNamespace(c, provisionerPod))
  357. ginkgo.By("locating the provisioner pod")
  358. pod, err := podClient.Get(provisionerPod.Name, metav1.GetOptions{})
  359. framework.ExpectNoError(err, "Cannot locate the provisioner pod %v: %v", provisionerPod.Name, err)
  360. return pod
  361. }
  362. func PrivilegedTestPSPClusterRoleBinding(client clientset.Interface,
  363. namespace string,
  364. teardown bool,
  365. saNames []string) {
  366. bindingString := "Binding"
  367. if teardown {
  368. bindingString = "Unbinding"
  369. }
  370. roleBindingClient := client.RbacV1().RoleBindings(namespace)
  371. for _, saName := range saNames {
  372. ginkgo.By(fmt.Sprintf("%v priviledged Pod Security Policy to the service account %s", bindingString, saName))
  373. binding := &rbacv1.RoleBinding{
  374. ObjectMeta: metav1.ObjectMeta{
  375. Name: "psp-" + saName,
  376. Namespace: namespace,
  377. },
  378. Subjects: []rbacv1.Subject{
  379. {
  380. Kind: rbacv1.ServiceAccountKind,
  381. Name: saName,
  382. Namespace: namespace,
  383. },
  384. },
  385. RoleRef: rbacv1.RoleRef{
  386. Kind: "ClusterRole",
  387. Name: podSecurityPolicyPrivilegedClusterRoleName,
  388. APIGroup: "rbac.authorization.k8s.io",
  389. },
  390. }
  391. roleBindingClient.Delete(binding.GetName(), &metav1.DeleteOptions{})
  392. err := wait.Poll(2*time.Second, 2*time.Minute, func() (bool, error) {
  393. _, err := roleBindingClient.Get(binding.GetName(), metav1.GetOptions{})
  394. return apierrs.IsNotFound(err), nil
  395. })
  396. framework.ExpectNoError(err, "Timed out waiting for deletion: %v", err)
  397. if teardown {
  398. continue
  399. }
  400. _, err = roleBindingClient.Create(binding)
  401. framework.ExpectNoError(err, "Failed to create %s role binding: %v", binding.GetName(), err)
  402. }
  403. }
  404. func CheckVolumeModeOfPath(pod *v1.Pod, volMode v1.PersistentVolumeMode, path string) {
  405. if volMode == v1.PersistentVolumeBlock {
  406. // Check if block exists
  407. VerifyExecInPodSucceed(pod, fmt.Sprintf("test -b %s", path))
  408. // Double check that it's not directory
  409. VerifyExecInPodFail(pod, fmt.Sprintf("test -d %s", path), 1)
  410. } else {
  411. // Check if directory exists
  412. VerifyExecInPodSucceed(pod, fmt.Sprintf("test -d %s", path))
  413. // Double check that it's not block
  414. VerifyExecInPodFail(pod, fmt.Sprintf("test -b %s", path), 1)
  415. }
  416. }
  417. func CheckReadWriteToPath(pod *v1.Pod, volMode v1.PersistentVolumeMode, path string) {
  418. if volMode == v1.PersistentVolumeBlock {
  419. // random -> file1
  420. VerifyExecInPodSucceed(pod, "dd if=/dev/urandom of=/tmp/file1 bs=64 count=1")
  421. // file1 -> dev (write to dev)
  422. VerifyExecInPodSucceed(pod, fmt.Sprintf("dd if=/tmp/file1 of=%s bs=64 count=1", path))
  423. // dev -> file2 (read from dev)
  424. VerifyExecInPodSucceed(pod, fmt.Sprintf("dd if=%s of=/tmp/file2 bs=64 count=1", path))
  425. // file1 == file2 (check contents)
  426. VerifyExecInPodSucceed(pod, "diff /tmp/file1 /tmp/file2")
  427. // Clean up temp files
  428. VerifyExecInPodSucceed(pod, "rm -f /tmp/file1 /tmp/file2")
  429. // Check that writing file to block volume fails
  430. VerifyExecInPodFail(pod, fmt.Sprintf("echo 'Hello world.' > %s/file1.txt", path), 1)
  431. } else {
  432. // text -> file1 (write to file)
  433. VerifyExecInPodSucceed(pod, fmt.Sprintf("echo 'Hello world.' > %s/file1.txt", path))
  434. // grep file1 (read from file and check contents)
  435. VerifyExecInPodSucceed(pod, fmt.Sprintf("grep 'Hello world.' %s/file1.txt", path))
  436. // Check that writing to directory as block volume fails
  437. VerifyExecInPodFail(pod, fmt.Sprintf("dd if=/dev/urandom of=%s bs=64 count=1", path), 1)
  438. }
  439. }
  440. func genBinDataFromSeed(len int, seed int64) []byte {
  441. binData := make([]byte, len)
  442. rand.Seed(seed)
  443. len, err := rand.Read(binData)
  444. if err != nil {
  445. fmt.Printf("Error: %v\n", err)
  446. }
  447. return binData
  448. }
  449. func CheckReadFromPath(pod *v1.Pod, volMode v1.PersistentVolumeMode, path string, len int, seed int64) {
  450. var pathForVolMode string
  451. if volMode == v1.PersistentVolumeBlock {
  452. pathForVolMode = path
  453. } else {
  454. pathForVolMode = filepath.Join(path, "file1.txt")
  455. }
  456. sum := sha256.Sum256(genBinDataFromSeed(len, seed))
  457. VerifyExecInPodSucceed(pod, fmt.Sprintf("dd if=%s bs=%d count=1 | sha256sum", pathForVolMode, len))
  458. VerifyExecInPodSucceed(pod, fmt.Sprintf("dd if=%s bs=%d count=1 | sha256sum | grep -Fq %x", pathForVolMode, len, sum))
  459. }
  460. func CheckWriteToPath(pod *v1.Pod, volMode v1.PersistentVolumeMode, path string, len int, seed int64) {
  461. var pathForVolMode string
  462. if volMode == v1.PersistentVolumeBlock {
  463. pathForVolMode = path
  464. } else {
  465. pathForVolMode = filepath.Join(path, "file1.txt")
  466. }
  467. encoded := base64.StdEncoding.EncodeToString(genBinDataFromSeed(len, seed))
  468. VerifyExecInPodSucceed(pod, fmt.Sprintf("echo %s | base64 -d | sha256sum", encoded))
  469. VerifyExecInPodSucceed(pod, fmt.Sprintf("echo %s | base64 -d | dd of=%s bs=%d count=1", encoded, pathForVolMode, len))
  470. }