|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/Azure/azure-storage-azcopy/v10/common" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestValidateProtocolCompatibility(t *testing.T) { |
| 12 | + a := assert.New(t) |
| 13 | + ctx := context.Background() |
| 14 | + |
| 15 | + // Test cases where validation should NOT be called (no File locations involved) |
| 16 | + testCases := []struct { |
| 17 | + name string |
| 18 | + fromTo common.FromTo |
| 19 | + shouldValidate bool |
| 20 | + description string |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "S3ToBlob", |
| 24 | + fromTo: common.EFromTo.S3Blob(), |
| 25 | + shouldValidate: false, |
| 26 | + description: "S3 to Blob should not validate (neither side is File)", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "GCPToBlob", |
| 30 | + fromTo: common.EFromTo.GCPBlob(), |
| 31 | + shouldValidate: false, |
| 32 | + description: "GCP to Blob should not validate (neither side is File)", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "LocalToBlob", |
| 36 | + fromTo: common.EFromTo.LocalBlob(), |
| 37 | + shouldValidate: false, |
| 38 | + description: "Local to Blob should not validate (neither side is File)", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "BlobToLocal", |
| 42 | + fromTo: common.EFromTo.BlobLocal(), |
| 43 | + shouldValidate: false, |
| 44 | + description: "Blob to Local should not validate (neither side is File)", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "BlobToBlob", |
| 48 | + fromTo: common.EFromTo.BlobBlob(), |
| 49 | + shouldValidate: false, |
| 50 | + description: "Blob to Blob should not validate (neither side is File)", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "LocalToBlobFS", |
| 54 | + fromTo: common.EFromTo.LocalBlobFS(), |
| 55 | + shouldValidate: false, |
| 56 | + description: "Local to BlobFS should not validate (neither side is File)", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "LocalToFile", |
| 60 | + fromTo: common.EFromTo.LocalFile(), |
| 61 | + shouldValidate: true, |
| 62 | + description: "Local to File should validate (destination is File)", |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "FileToLocal", |
| 66 | + fromTo: common.EFromTo.FileLocal(), |
| 67 | + shouldValidate: true, |
| 68 | + description: "File to Local should validate (source is File)", |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "LocalToFileNFS", |
| 72 | + fromTo: common.EFromTo.LocalFileNFS(), |
| 73 | + shouldValidate: true, |
| 74 | + description: "Local to FileNFS should validate (destination is FileNFS)", |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "FileNFSToLocal", |
| 78 | + fromTo: common.EFromTo.FileNFSLocal(), |
| 79 | + shouldValidate: true, |
| 80 | + description: "FileNFS to Local should validate (source is FileNFS)", |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "FileToFile", |
| 84 | + fromTo: common.EFromTo.FileFile(), |
| 85 | + shouldValidate: true, |
| 86 | + description: "File to File should validate (both sides are File)", |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "FileNFSToFileNFS", |
| 90 | + fromTo: common.EFromTo.FileNFSFileNFS(), |
| 91 | + shouldValidate: true, |
| 92 | + description: "FileNFS to FileNFS should validate (both sides are FileNFS)", |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "FileToBlob", |
| 96 | + fromTo: common.EFromTo.FileBlob(), |
| 97 | + shouldValidate: true, |
| 98 | + description: "File to Blob should validate (source is File)", |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "BlobToFile", |
| 102 | + fromTo: common.EFromTo.BlobFile(), |
| 103 | + shouldValidate: true, |
| 104 | + description: "Blob to File should validate (destination is File)", |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + for _, tc := range testCases { |
| 109 | + t.Run(tc.name, func(t *testing.T) { |
| 110 | + // Create dummy resource strings |
| 111 | + src := common.ResourceString{Value: "https://source.example.com/path"} |
| 112 | + dst := common.ResourceString{Value: "https://dest.example.com/path"} |
| 113 | + |
| 114 | + // For non-File transfers, we can pass nil service clients since validation should be skipped |
| 115 | + // For File transfers, we would need proper service clients, but we're testing the conditional logic |
| 116 | + var srcClient, dstClient *common.ServiceClient |
| 117 | + |
| 118 | + if !tc.shouldValidate { |
| 119 | + // Test that validation is skipped when no File locations are involved |
| 120 | + // This should not panic even with nil service clients |
| 121 | + err := validateProtocolCompatibility(ctx, tc.fromTo, src, dst, srcClient, dstClient) |
| 122 | + a.NoError(err, "validateProtocolCompatibility should not fail for %s: %s", tc.name, tc.description) |
| 123 | + } else { |
| 124 | + // For File transfers, we expect the function to attempt validation |
| 125 | + // Since we're passing nil service clients, we expect it to fail gracefully |
| 126 | + // This tests that the conditional logic correctly identifies File transfers |
| 127 | + err := validateProtocolCompatibility(ctx, tc.fromTo, src, dst, srcClient, dstClient) |
| 128 | + // We expect an error here because we're passing nil service clients for File transfers |
| 129 | + // The important thing is that it doesn't panic and attempts validation |
| 130 | + if tc.fromTo.From().IsFile() || tc.fromTo.To().IsFile() { |
| 131 | + a.Error(err, "validateProtocolCompatibility should attempt validation for %s and fail with nil clients: %s", tc.name, tc.description) |
| 132 | + } |
| 133 | + } |
| 134 | + }) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestValidateProtocolCompatibility_ConditionalLogic(t *testing.T) { |
| 139 | + a := assert.New(t) |
| 140 | + ctx := context.Background() |
| 141 | + |
| 142 | + // Test the specific conditional logic |
| 143 | + src := common.ResourceString{Value: "https://source.example.com/path"} |
| 144 | + dst := common.ResourceString{Value: "https://dest.example.com/path"} |
| 145 | + |
| 146 | + // Test that S3->Blob doesn't call validation (should not panic with nil clients) |
| 147 | + err := validateProtocolCompatibility(ctx, common.EFromTo.S3Blob(), src, dst, nil, nil) |
| 148 | + a.NoError(err, "S3->Blob should skip validation and not panic with nil service clients") |
| 149 | + |
| 150 | + // Test that GCP->Blob doesn't call validation (should not panic with nil clients) |
| 151 | + err = validateProtocolCompatibility(ctx, common.EFromTo.GCPBlob(), src, dst, nil, nil) |
| 152 | + a.NoError(err, "GCP->Blob should skip validation and not panic with nil service clients") |
| 153 | + |
| 154 | + // Test that Local->Blob doesn't call validation (should not panic with nil clients) |
| 155 | + err = validateProtocolCompatibility(ctx, common.EFromTo.LocalBlob(), src, dst, nil, nil) |
| 156 | + a.NoError(err, "Local->Blob should skip validation and not panic with nil service clients") |
| 157 | +} |
0 commit comments