k8s.io/kubernetes@v1.29.3/test/e2e/storage/drivers/proxy/io.go (about)

     1  /*
     2  Copyright 2020 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 proxy
    18  
    19  import (
    20  	"encoding/base64"
    21  	"fmt"
    22  	"io"
    23  
    24  	"k8s.io/klog/v2"
    25  	"k8s.io/kubernetes/test/e2e/framework"
    26  	e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
    27  	"k8s.io/kubernetes/test/e2e/storage/drivers/csi-test/mock/service"
    28  )
    29  
    30  type PodDirIO struct {
    31  	F             *framework.Framework
    32  	Namespace     string
    33  	PodName       string
    34  	ContainerName string
    35  	Logger        *klog.Logger
    36  }
    37  
    38  var _ service.DirIO = PodDirIO{}
    39  
    40  func (p PodDirIO) DirExists(path string) (bool, error) {
    41  	stdout, stderr, err := p.execute([]string{
    42  		"sh",
    43  		"-c",
    44  		fmt.Sprintf("if ! [ -e '%s' ]; then echo notexist; elif [ -d '%s' ]; then echo dir; else echo nodir; fi", path, path),
    45  	}, nil)
    46  	if err != nil {
    47  		return false, fmt.Errorf("error executing dir test commands: stderr=%q, %v", stderr, err)
    48  	}
    49  	switch stdout {
    50  	case "notexist":
    51  		return false, nil
    52  	case "nodir":
    53  		return false, fmt.Errorf("%s: not a directory", path)
    54  	case "dir":
    55  		return true, nil
    56  	default:
    57  		return false, fmt.Errorf("unexpected output from dir test commands: %q", stdout)
    58  	}
    59  }
    60  
    61  func (p PodDirIO) Mkdir(path string) error {
    62  	_, stderr, err := p.execute([]string{"mkdir", path}, nil)
    63  	if err != nil {
    64  		return fmt.Errorf("mkdir %q: stderr=%q, %v", path, stderr, err)
    65  	}
    66  	return nil
    67  }
    68  
    69  func (p PodDirIO) CreateFile(path string, content io.Reader) error {
    70  	// Piping the content into dd via stdin turned out to be unreliable.
    71  	// Sometimes dd would stop after writing zero bytes, without an error
    72  	// from ExecWithOptions (reported as
    73  	// https://github.com/kubernetes/kubernetes/issues/112834).
    74  	//
    75  	// Therefore the content is now encoded inside the command itself.
    76  	data, err := io.ReadAll(content)
    77  	if err != nil {
    78  		return fmt.Errorf("read content: %w", err)
    79  	}
    80  	encoded := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
    81  	base64.StdEncoding.Encode(encoded, data)
    82  	_, stderr, err := p.execute([]string{"sh", "-c", fmt.Sprintf(`base64 -d >'%s' <<EOF
    83  %s
    84  EOF`, path, string(encoded))}, nil)
    85  	if err != nil {
    86  		return fmt.Errorf("decoding into %q: stderr=%q, %v", path, stderr, err)
    87  	}
    88  	return nil
    89  }
    90  
    91  func (p PodDirIO) Rename(oldPath, newPath string) error {
    92  	_, stderr, err := p.execute([]string{"mv", oldPath, newPath}, nil)
    93  	if err != nil {
    94  		return fmt.Errorf("rename %q -> %q: stderr=%q, %v", oldPath, newPath, stderr, err)
    95  	}
    96  	return nil
    97  }
    98  
    99  func (p PodDirIO) RemoveAll(path string) error {
   100  	_, stderr, err := p.execute([]string{"rm", "-rf", path}, nil)
   101  	if err != nil {
   102  		return fmt.Errorf("rm -rf %q: stderr=%q, %v", path, stderr, err)
   103  	}
   104  	return nil
   105  }
   106  
   107  func (p PodDirIO) execute(command []string, stdin io.Reader) (string, string, error) {
   108  	stdout, stderr, err := e2epod.ExecWithOptions(p.F, e2epod.ExecOptions{
   109  		Command:       command,
   110  		Namespace:     p.Namespace,
   111  		PodName:       p.PodName,
   112  		ContainerName: p.ContainerName,
   113  		Stdin:         stdin,
   114  		CaptureStdout: true,
   115  		CaptureStderr: true,
   116  		Quiet:         true,
   117  	})
   118  	if p.Logger != nil {
   119  		p.Logger.Info("Command completed", "command", command, "stdout", stdout, "stderr", stderr, "err", err)
   120  
   121  	}
   122  	return stdout, stderr, err
   123  
   124  }