...

Source file src/github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/iochaos/misc.go

Documentation: github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/iochaos

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     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  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package iochaos
    17  
    18  import (
    19  	"fmt"
    20  	"io"
    21  	"net/http"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/pkg/errors"
    26  )
    27  
    28  // get pod io delay
    29  func getPodIODelay(c http.Client, port uint16) (time.Duration, error) {
    30  	resp, err := c.Get(fmt.Sprintf("http://localhost:%d/io", port))
    31  	if err != nil {
    32  		return 0, err
    33  	}
    34  
    35  	out, err := io.ReadAll(resp.Body)
    36  	defer resp.Body.Close()
    37  	if err != nil {
    38  		return 0, err
    39  	}
    40  
    41  	result := string(out)
    42  	if strings.Contains(result, "failed to write file") {
    43  		return 0, errors.New(result)
    44  	}
    45  	dur, err := time.ParseDuration(result)
    46  	if err != nil {
    47  		return 0, err
    48  	}
    49  
    50  	return dur, nil
    51  }
    52  
    53  func getPodIoMistake(c http.Client, port uint16) (bool, error) {
    54  	resp, err := c.Get(fmt.Sprintf("http://localhost:%d/mistake", port))
    55  	if err != nil {
    56  		return false, err
    57  	}
    58  
    59  	out, err := io.ReadAll(resp.Body)
    60  	defer resp.Body.Close()
    61  	if err != nil {
    62  		return false, err
    63  	}
    64  
    65  	result := string(out)
    66  	fmt.Println("e2e server io mistake test response: ", resp, result)
    67  	if strings.Contains(result, "true") {
    68  		return true, nil
    69  	}
    70  	if strings.Contains(result, "false") {
    71  		return false, nil
    72  	}
    73  	if strings.Contains(result, "err") {
    74  		return false, errors.New(result)
    75  	}
    76  	return false, errors.New("unexpected reply from e2e server")
    77  }
    78