...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/statuscheck/common_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/statuscheck

     1  // Copyright 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 statuscheck
    17  
    18  import (
    19  	"time"
    20  
    21  	"github.com/go-logr/logr"
    22  	"github.com/pkg/errors"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    25  )
    26  
    27  // fakeHTTPExecutor
    28  type fakeHTTPExecutor struct {
    29  	logger logr.Logger
    30  
    31  	httpStatusCheck v1alpha1.HTTPStatusCheck
    32  	timeoutSeconds  int
    33  }
    34  
    35  func (e *fakeHTTPExecutor) Do() (bool, string, error) {
    36  	return e.handle()
    37  }
    38  
    39  func (e *fakeHTTPExecutor) Type() string {
    40  	return "Fake-HTTP"
    41  }
    42  
    43  func newFakeExecutor(logger logr.Logger, statusCheck v1alpha1.StatusCheck) (Executor, error) {
    44  	var executor Executor
    45  	switch statusCheck.Spec.Type {
    46  	case v1alpha1.TypeHTTP:
    47  		if statusCheck.Spec.EmbedStatusCheck == nil || statusCheck.Spec.HTTPStatusCheck == nil {
    48  			// this should not happen, if the webhook works as expected
    49  			return nil, errors.New("illegal status check, http should not be empty")
    50  		}
    51  		executor = &fakeHTTPExecutor{
    52  			logger:          logger.WithName("fake-http-executor"),
    53  			httpStatusCheck: *statusCheck.Spec.HTTPStatusCheck,
    54  			timeoutSeconds:  statusCheck.Spec.TimeoutSeconds,
    55  		}
    56  	default:
    57  		return nil, errors.New("unsupported type")
    58  	}
    59  	return executor, nil
    60  }
    61  
    62  func (e *fakeHTTPExecutor) handle() (bool, string, error) {
    63  	switch e.httpStatusCheck.RequestBody {
    64  	case "failure":
    65  		return false, "failure", nil
    66  	case "timeout":
    67  		time.Sleep(time.Duration(e.timeoutSeconds) * time.Second)
    68  		return false, "timeout", nil
    69  	default:
    70  		return true, "", nil
    71  	}
    72  }
    73