...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/action/multiplexer_test.go

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

     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 action
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"github.com/pkg/errors"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    25  )
    26  
    27  type mockApplyError struct {
    28  }
    29  
    30  func (it *mockApplyError) Error() string {
    31  	return "mock apply error"
    32  }
    33  
    34  type mockRecoverError struct {
    35  }
    36  
    37  func (it mockRecoverError) Error() string {
    38  	return "mock recover error"
    39  }
    40  
    41  type chaosImplMustFailed struct {
    42  }
    43  
    44  func (it *chaosImplMustFailed) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    45  	return v1alpha1.NotInjected, &mockApplyError{}
    46  
    47  }
    48  
    49  func (it *chaosImplMustFailed) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    50  	return v1alpha1.Injected, mockRecoverError{}
    51  }
    52  
    53  func TestMultiplexer_passthroughsError(t *testing.T) {
    54  	type adHoc struct {
    55  		Backend *chaosImplMustFailed `action:"must-failed"`
    56  	}
    57  	multiplexer := NewMultiplexer(&adHoc{
    58  		Backend: &chaosImplMustFailed{},
    59  	})
    60  
    61  	// Just use PodChaos as example, you could use any struct that contains Spec.Action for it.
    62  	chaos := v1alpha1.PodChaos{
    63  		Spec: v1alpha1.PodChaosSpec{
    64  			Action: "must-failed",
    65  		},
    66  	}
    67  	_, err := multiplexer.Apply(context.Background(), 0, []*v1alpha1.Record{}, &chaos)
    68  	applyError := &mockApplyError{}
    69  	if !errors.As(err, &applyError) {
    70  		t.Fatal("returned error is not mockApplyError")
    71  	}
    72  
    73  	_, err = multiplexer.Recover(context.Background(), 0, []*v1alpha1.Record{}, &chaos)
    74  	recoverError := mockRecoverError{}
    75  	if !errors.As(err, &recoverError) {
    76  		t.Fatal("returned error is not recoverError")
    77  	}
    78  }
    79  
    80  func TestMultiplexer_unhandledAction(t *testing.T) {
    81  	type adHoc struct {
    82  		Backend *chaosImplMustFailed `action:"must-failed"`
    83  	}
    84  	multiplexer := NewMultiplexer(&adHoc{
    85  		// No fields here
    86  	})
    87  	chaos := v1alpha1.PodChaos{
    88  		Spec: v1alpha1.PodChaosSpec{
    89  			Action: "not-exist",
    90  		},
    91  	}
    92  	_, err := multiplexer.Apply(context.Background(), 0, []*v1alpha1.Record{}, &chaos)
    93  	unknownAction := ErrorUnknownAction{}
    94  	if !errors.As(err, &unknownAction) {
    95  		t.Fatal("should not return error")
    96  	}
    97  
    98  }
    99