read_write.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  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 clients
  14. import (
  15. "fmt"
  16. "github.com/rook/rook/tests/framework/utils"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. // ReadWriteOperation is a wrapper for k8s rook file operations
  20. type ReadWriteOperation struct {
  21. k8sh *utils.K8sHelper
  22. }
  23. // CreateReadWriteOperation Constructor to create ReadWriteOperation - client to perform rook file system operations on k8s
  24. func CreateReadWriteOperation(k8sh *utils.K8sHelper) *ReadWriteOperation {
  25. return &ReadWriteOperation{k8sh: k8sh}
  26. }
  27. // CreateWriteClient Function to create a nfs client in rook
  28. func (f *ReadWriteOperation) CreateWriteClient(volName string) ([]string, error) {
  29. logger.Infof("creating the filesystem via replication controller")
  30. writerSpec := getDeployment(volName)
  31. if err := f.k8sh.ResourceOperation("apply", writerSpec); err != nil {
  32. return nil, err
  33. }
  34. assert.True(f.k8sh.T(), f.k8sh.CheckPodCountAndState("read-write-test", "default", 2, "Running"),
  35. "Make sure there are two read-write-test pods present in Running state")
  36. podList, err := f.k8sh.GetPodNamesForApp("read-write-test", "default")
  37. if err != nil {
  38. return nil, err
  39. }
  40. return podList, nil
  41. }
  42. // Delete Function to delete a nfs consuming pod in rook
  43. func (f *ReadWriteOperation) Delete() error {
  44. return f.k8sh.DeleteResource("deployment", "read-write-test")
  45. }
  46. // Read Function to read from nfs mount point created by rook ,i.e. Read data from a pod that has an nfs export mounted
  47. func (f *ReadWriteOperation) Read(name string) (string, error) {
  48. rd := "/mnt/data"
  49. args := []string{"exec", name}
  50. args = append(args, "--", "cat", rd)
  51. result, err := f.k8sh.Kubectl(args...)
  52. if err != nil {
  53. return "", fmt.Errorf("unable to write data to pod -- : %s", err)
  54. }
  55. return result, nil
  56. }
  57. func getDeployment(volName string) string {
  58. return `apiVersion: apps/v1
  59. kind: Deployment
  60. metadata:
  61. name: read-write-test
  62. spec:
  63. replicas: 2
  64. selector:
  65. matchLabels:
  66. app: read-write-test
  67. template:
  68. metadata:
  69. labels:
  70. app: read-write-test
  71. spec:
  72. containers:
  73. - image: alpine
  74. command:
  75. - sh
  76. - -c
  77. - 'while true; do hostname > /mnt/data; sleep 3; done'
  78. name: alpine
  79. volumeMounts:
  80. - name: test-vol
  81. mountPath: "/mnt"
  82. volumes:
  83. - name: test-vol
  84. persistentVolumeClaim:
  85. claimName: ` + volName + `
  86. `
  87. }