|
| 1 | +// Copyright (c) 2017 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package api |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "testing" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | + "github.com/stretchr/testify/require" |
| 29 | + |
| 30 | + "github.com/uber/cadence/common" |
| 31 | + "github.com/uber/cadence/common/types" |
| 32 | +) |
| 33 | + |
| 34 | +func TestFailoverDomainValidation_WithoutReason_ExpectError(t *testing.T) { |
| 35 | + // This test verifies that when a failover is attempted without providing a reason, |
| 36 | + // the validation fails and returns an appropriate error. |
| 37 | + |
| 38 | + v, _ := setupMocksForRequestValidator(t) |
| 39 | + |
| 40 | + // Test case 1: Failover request WITHOUT a reason |
| 41 | + failoverRequestWithoutReason := &types.FailoverDomainRequest{ |
| 42 | + DomainName: "test-domain", |
| 43 | + DomainActiveClusterName: common.StringPtr("cluster2"), |
| 44 | + // Reason is intentionally missing |
| 45 | + } |
| 46 | + |
| 47 | + err := v.ValidateFailoverDomainRequest(context.Background(), failoverRequestWithoutReason) |
| 48 | + |
| 49 | + // Verify we get the expected error |
| 50 | + assert.Error(t, err) |
| 51 | + assert.Contains(t, err.Error(), "Reason must be provided for domain failover") |
| 52 | + |
| 53 | + // Test case 2: Failover request with empty reason |
| 54 | + failoverRequestEmptyReason := &types.FailoverDomainRequest{ |
| 55 | + DomainName: "test-domain", |
| 56 | + DomainActiveClusterName: common.StringPtr("cluster2"), |
| 57 | + Reason: "", // Empty reason |
| 58 | + } |
| 59 | + |
| 60 | + err = v.ValidateFailoverDomainRequest(context.Background(), failoverRequestEmptyReason) |
| 61 | + |
| 62 | + // Verify we get the expected error |
| 63 | + assert.Error(t, err) |
| 64 | + assert.Contains(t, err.Error(), "Reason must be provided for domain failover") |
| 65 | +} |
| 66 | + |
| 67 | +func TestFailoverDomainValidation_WithReason_ExpectSuccess(t *testing.T) { |
| 68 | + // This test verifies that when a failover is attempted with a valid reason, |
| 69 | + // the validation succeeds. |
| 70 | + |
| 71 | + v, _ := setupMocksForRequestValidator(t) |
| 72 | + |
| 73 | + // Create a failover request WITH a reason |
| 74 | + failoverRequestWithReason := &types.FailoverDomainRequest{ |
| 75 | + DomainName: "test-domain", |
| 76 | + DomainActiveClusterName: common.StringPtr("cluster2"), |
| 77 | + Reason: "Planned maintenance on cluster1 - upgrading to v1.2.3", |
| 78 | + } |
| 79 | + |
| 80 | + err := v.ValidateFailoverDomainRequest(context.Background(), failoverRequestWithReason) |
| 81 | + |
| 82 | + // Verify validation passes |
| 83 | + require.NoError(t, err) |
| 84 | +} |
| 85 | + |
| 86 | +func TestFailoverDomainReason_StoredInDomainData(t *testing.T) { |
| 87 | + // This test verifies that the reason provided during failover |
| 88 | + // is properly converted to domain data for storage. |
| 89 | + |
| 90 | + failoverRequest := &types.FailoverDomainRequest{ |
| 91 | + DomainName: "test-domain", |
| 92 | + DomainActiveClusterName: common.StringPtr("cluster2"), |
| 93 | + Reason: "Emergency failover due to cluster1 outage", |
| 94 | + } |
| 95 | + |
| 96 | + // Call ToUpdateDomainRequest and verify reason is in data |
| 97 | + updateReq := failoverRequest.ToUpdateDomainRequest() |
| 98 | + |
| 99 | + assert.NotNil(t, updateReq) |
| 100 | + assert.Equal(t, "test-domain", updateReq.Name) |
| 101 | + assert.Equal(t, common.StringPtr("cluster2"), updateReq.ActiveClusterName) |
| 102 | + assert.NotNil(t, updateReq.Data) |
| 103 | + assert.Equal(t, "Emergency failover due to cluster1 outage", updateReq.Data["FailoverReason"]) |
| 104 | +} |
0 commit comments