vitess.io/vitess@v0.16.2/go/vt/vtadmin/testutil/workflows.go (about)

     1  /*
     2  Copyright 2021 The Vitess 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 testutil
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  
    25  	vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin"
    26  )
    27  
    28  // AssertClusterWorkflowsEqual is a test helper for asserting that two
    29  // ClusterWorkflows objects are equal.
    30  func AssertClusterWorkflowsEqual(t *testing.T, expected *vtadminpb.ClusterWorkflows, actual *vtadminpb.ClusterWorkflows, msgAndArgs ...any) {
    31  	t.Helper()
    32  
    33  	if expected == nil && actual == nil {
    34  		return
    35  	}
    36  
    37  	require.NotNil(t, expected, msgAndArgs...)
    38  	require.NotNil(t, actual, msgAndArgs...)
    39  
    40  	if expected.Warnings != nil && actual.Warnings != nil {
    41  		assert.Equal(t, len(expected.Warnings), len(actual.Warnings), msgAndArgs...)
    42  	}
    43  
    44  	assert.ElementsMatch(t, expected.Workflows, actual.Workflows, msgAndArgs...)
    45  }
    46  
    47  // AssertGetWorkflowsResponsesEqual is a test helper for asserting that two
    48  // GetWorkflowsResponse objects are equal.
    49  func AssertGetWorkflowsResponsesEqual(t *testing.T, expected *vtadminpb.GetWorkflowsResponse, actual *vtadminpb.GetWorkflowsResponse, msgAndArgs ...any) {
    50  	t.Helper()
    51  
    52  	if expected == nil && actual == nil {
    53  		return
    54  	}
    55  
    56  	require.NotNil(t, expected, msgAndArgs...)
    57  	require.NotNil(t, actual, msgAndArgs...)
    58  
    59  	keysLeft := make([]string, 0, len(expected.WorkflowsByCluster))
    60  	keysRight := make([]string, 0, len(actual.WorkflowsByCluster))
    61  
    62  	for k := range expected.WorkflowsByCluster {
    63  		keysLeft = append(keysLeft, k)
    64  	}
    65  
    66  	for k := range actual.WorkflowsByCluster {
    67  		keysRight = append(keysRight, k)
    68  	}
    69  
    70  	require.ElementsMatch(t, keysLeft, keysRight, msgAndArgs...)
    71  
    72  	for _, k := range keysLeft {
    73  		AssertClusterWorkflowsEqual(t, expected.WorkflowsByCluster[k], actual.WorkflowsByCluster[k], msgAndArgs...)
    74  	}
    75  }