...

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 2020 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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package iochaos
    15  
    16  import (
    17  	"errors"
    18  	"fmt"
    19  	"io/ioutil"
    20  	"net/http"
    21  	"strings"
    22  	"time"
    23  )
    24  
    25  // get pod io delay
    26  func getPodIODelay(c http.Client, port uint16) (time.Duration, error) {
    27  	resp, err := c.Get(fmt.Sprintf("http://localhost:%d/io", port))
    28  	if err != nil {
    29  		return 0, err
    30  	}
    31  
    32  	out, err := ioutil.ReadAll(resp.Body)
    33  	defer resp.Body.Close()
    34  	if err != nil {
    35  		return 0, err
    36  	}
    37  
    38  	result := string(out)
    39  	if strings.Contains(result, "failed to write file") {
    40  		return 0, errors.New(result)
    41  	}
    42  	dur, err := time.ParseDuration(result)
    43  	if err != nil {
    44  		return 0, err
    45  	}
    46  
    47  	return dur, nil
    48  }
    49  
    50  func getPodIoMistake(c http.Client, port uint16) (bool, error) {
    51  	resp, err := c.Get(fmt.Sprintf("http://localhost:%d/mistake", port))
    52  	if err != nil {
    53  		return false, err
    54  	}
    55  
    56  	out, err := ioutil.ReadAll(resp.Body)
    57  	defer resp.Body.Close()
    58  	if err != nil {
    59  		return false, err
    60  	}
    61  
    62  	result := string(out)
    63  	fmt.Println("e2e server io mistake test response: ", resp, result)
    64  	if strings.Contains(result, "true") {
    65  		return true, nil
    66  	}
    67  	if strings.Contains(result, "false") {
    68  		return false, nil
    69  	}
    70  	if strings.Contains(result, "err") {
    71  		return false, errors.New(result)
    72  	}
    73  	return false, errors.New("unexpected reply from e2e server")
    74  }
    75