...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/action/multiplexer_examples_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  	"fmt"
    21  
    22  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    23  )
    24  
    25  type chaosImplForAction1 struct {
    26  }
    27  
    28  func (it *chaosImplForAction1) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    29  	fmt.Println("action1-apply")
    30  	return v1alpha1.Injected, nil
    31  }
    32  
    33  func (it *chaosImplForAction1) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    34  	fmt.Println("action1-recover")
    35  	return v1alpha1.NotInjected, nil
    36  }
    37  
    38  type chaosImplForAction2 struct {
    39  }
    40  
    41  func (it *chaosImplForAction2) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    42  	fmt.Println("action2-apply")
    43  	return v1alpha1.Injected, nil
    44  }
    45  
    46  func (it *chaosImplForAction2) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    47  	fmt.Println("action2-recover")
    48  	return v1alpha1.NotInjected, nil
    49  }
    50  
    51  func ExampleMultiplexer() {
    52  	type adHoc struct {
    53  		AnyName1        *chaosImplForAction1 `action:"struct-tag"`
    54  		WhateverTheName *chaosImplForAction2 `action:"is-important"`
    55  	}
    56  	multiplexer := NewMultiplexer(&adHoc{
    57  		AnyName1:        &chaosImplForAction1{},
    58  		WhateverTheName: &chaosImplForAction2{},
    59  	})
    60  
    61  	// Just use PodChaos as example, you could use any struct that contains Spec.Action for it.
    62  	chaosA := v1alpha1.PodChaos{
    63  		Spec: v1alpha1.PodChaosSpec{
    64  			Action: "struct-tag",
    65  		},
    66  	}
    67  	chaosB := v1alpha1.PodChaos{
    68  		Spec: v1alpha1.PodChaosSpec{
    69  			Action: "is-important",
    70  		},
    71  	}
    72  
    73  	if _, err := multiplexer.Apply(context.Background(), 0, nil, &chaosA); err != nil {
    74  		panic(err)
    75  	}
    76  	if _, err := multiplexer.Recover(context.Background(), 0, nil, &chaosA); err != nil {
    77  		panic(err)
    78  	}
    79  	if _, err := multiplexer.Apply(context.Background(), 0, nil, &chaosB); err != nil {
    80  		panic(err)
    81  	}
    82  	if _, err := multiplexer.Recover(context.Background(), 0, nil, &chaosB); err != nil {
    83  		panic(err)
    84  	}
    85  
    86  	// Output: action1-apply
    87  	// action1-recover
    88  	// action2-apply
    89  	// action2-recover
    90  }
    91