github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/scheduler/strategy/failover_test.go (about)

     1  /*
     2  Copyright 2024 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package strategy_test
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	prowv1 "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    25  	"sigs.k8s.io/prow/pkg/config"
    26  	"sigs.k8s.io/prow/pkg/scheduler/strategy"
    27  )
    28  
    29  func TestFailover(t *testing.T) {
    30  	for _, tc := range []struct {
    31  		name         string
    32  		mappings     map[string]string
    33  		pj           *prowv1.ProwJob
    34  		wantDecision strategy.Result
    35  	}{
    36  		{
    37  			name:         "Replace broken cluster",
    38  			mappings:     map[string]string{"broken": "running"},
    39  			pj:           &prowv1.ProwJob{Spec: prowv1.ProwJobSpec{Cluster: "broken"}},
    40  			wantDecision: strategy.Result{Cluster: "running"},
    41  		},
    42  		{
    43  			name:         "Do not replace",
    44  			mappings:     map[string]string{"broken": "running"},
    45  			pj:           &prowv1.ProwJob{Spec: prowv1.ProwJobSpec{Cluster: "a-cluster"}},
    46  			wantDecision: strategy.Result{Cluster: "a-cluster"},
    47  		},
    48  		{
    49  			name:         "No mappings, do not replace",
    50  			pj:           &prowv1.ProwJob{Spec: prowv1.ProwJobSpec{Cluster: "a-cluster"}},
    51  			wantDecision: strategy.Result{Cluster: "a-cluster"},
    52  		},
    53  	} {
    54  		tc := tc
    55  		t.Run(tc.name, func(t *testing.T) {
    56  			t.Parallel()
    57  
    58  			failover := strategy.NewFailover(config.FailoverScheduling{ClusterMappings: tc.mappings})
    59  
    60  			d, err := failover.Schedule(context.TODO(), tc.pj)
    61  			if err != nil {
    62  				t.Fatalf("Unexpected error: %s", err)
    63  			}
    64  
    65  			if diff := cmp.Diff(tc.wantDecision, d); diff != "" {
    66  				t.Errorf("Unexpected decisions: %s", diff)
    67  			}
    68  		})
    69  	}
    70  }