github.com/kyma-project/kyma-environment-broker@v0.0.1/common/orchestration/strategies/parallel_test.go (about)

     1  package strategies
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/kyma-project/kyma-environment-broker/common/orchestration"
     9  
    10  	"github.com/sirupsen/logrus"
    11  	"github.com/stretchr/testify/assert"
    12  	"k8s.io/apimachinery/pkg/util/rand"
    13  )
    14  
    15  type testExecutor struct {
    16  	mux      sync.Mutex
    17  	opCalled map[string]bool
    18  }
    19  
    20  func (t *testExecutor) Execute(opID string) (time.Duration, error) {
    21  	t.mux.Lock()
    22  	called := t.opCalled[opID]
    23  	t.opCalled[opID] = true
    24  	t.mux.Unlock()
    25  
    26  	if !called {
    27  		time.Sleep(1 * time.Second)
    28  		return 1 * time.Second, nil
    29  	} else {
    30  		return 0, nil
    31  	}
    32  }
    33  
    34  func (t *testExecutor) Reschedule(operationID string, maintenanceWindowBegin, maintenanceWindowEnd time.Time) error {
    35  	return nil
    36  }
    37  
    38  func TestNewParallelOrchestrationStrategy_Immediate(t *testing.T) {
    39  	// given
    40  	executor := &testExecutor{opCalled: map[string]bool{}}
    41  	s := NewParallelOrchestrationStrategy(executor, logrus.New(), 0)
    42  
    43  	ops := make([]orchestration.RuntimeOperation, 3)
    44  	for i := range ops {
    45  		ops[i] = orchestration.RuntimeOperation{
    46  			ID: rand.String(5),
    47  		}
    48  	}
    49  
    50  	// when
    51  	id, err := s.Execute(ops, orchestration.StrategySpec{Schedule: time.Now().Format(time.RFC3339), Parallel: orchestration.ParallelStrategySpec{Workers: 2}})
    52  
    53  	// then
    54  	assert.NoError(t, err)
    55  	s.Wait(id)
    56  }
    57  
    58  func TestNewParallelOrchestrationStrategy_MaintenanceWindow(t *testing.T) {
    59  	// given
    60  	executor := &testExecutor{opCalled: map[string]bool{}}
    61  	s := NewParallelOrchestrationStrategy(executor, logrus.New(), 0)
    62  
    63  	start := time.Now().Add(3 * time.Second)
    64  
    65  	ops := make([]orchestration.RuntimeOperation, 3)
    66  	for i := range ops {
    67  		ops[i] = orchestration.RuntimeOperation{
    68  			ID: rand.String(5),
    69  			Runtime: orchestration.Runtime{
    70  				MaintenanceWindowBegin: start,
    71  				MaintenanceWindowEnd:   start.Add(10 * time.Minute),
    72  			},
    73  		}
    74  	}
    75  
    76  	// when
    77  	id, err := s.Execute(ops, orchestration.StrategySpec{Schedule: "imideate", MaintenanceWindow: true, Parallel: orchestration.ParallelStrategySpec{Workers: 2}})
    78  
    79  	// then
    80  	assert.NoError(t, err)
    81  	s.Wait(id)
    82  }
    83  
    84  func TestNewParallelOrchestrationStrategy_Reschedule(t *testing.T) {
    85  	// given
    86  	executor := &testExecutor{opCalled: map[string]bool{}}
    87  	s := NewParallelOrchestrationStrategy(executor, logrus.New(), 5*time.Second)
    88  
    89  	start := time.Now().Add(-5 * time.Second)
    90  
    91  	ops := make([]orchestration.RuntimeOperation, 3)
    92  	for i := range ops {
    93  		ops[i] = orchestration.RuntimeOperation{
    94  			ID: rand.String(5),
    95  			Runtime: orchestration.Runtime{
    96  				MaintenanceWindowBegin: start,
    97  				MaintenanceWindowEnd:   start.Add(time.Second),
    98  			},
    99  		}
   100  	}
   101  
   102  	// when
   103  	id, err := s.Execute(ops, orchestration.StrategySpec{Schedule: "now", MaintenanceWindow: true, Parallel: orchestration.ParallelStrategySpec{Workers: 2}})
   104  
   105  	// then
   106  	assert.NoError(t, err)
   107  	s.Wait(id)
   108  }