| Ben Craig | 262cfb4 | 2015-07-08 20:37:15 -0500 | [diff] [blame^] | 1 | #!/usr/bin/perl -w | 
|  | 2 |  | 
|  | 3 | # Licensed to the Apache Software Foundation (ASF) under one | 
|  | 4 | # or more contributor license agreements. See the NOTICE file | 
|  | 5 | # distributed with this work for additional information | 
|  | 6 | # regarding copyright ownership. The ASF licenses this file | 
|  | 7 | # to you under the Apache License, Version 2.0 (the | 
|  | 8 | # "License"); you may not use this file except in compliance | 
|  | 9 | # with the License. You may obtain a copy of the License at | 
|  | 10 | # | 
|  | 11 | #   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | # | 
|  | 13 | # Unless required by applicable law or agreed to in writing, | 
|  | 14 | # software distributed under the License is distributed on an | 
|  | 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
|  | 16 | # KIND, either express or implied. See the License for the | 
|  | 17 | # specific language governing permissions and limitations | 
|  | 18 | # under the License. | 
|  | 19 |  | 
|  | 20 |  | 
|  | 21 | #break1 - Thrift method removed from service base | 
|  | 22 | #break2 - Struct field changed in test_struct1(i16 to i32) | 
|  | 23 | #break3 - Struct field changed in test_struct1(enum1 to enum2) | 
|  | 24 | #break4 - Field type changed in test_struct1(bool to string) | 
|  | 25 | #break5- member field type changed in test_struct1(bool to list<bool>) | 
|  | 26 | #break6-  Field type changed in test_struct2 (list<double> to list<i16>) | 
|  | 27 | #break7 - requiredness removed in struct6 | 
|  | 28 | #break8 - requiredness addedd in struct5 | 
|  | 29 | #break9 - Struct field removed from struct1 | 
|  | 30 | #break10 - Struct field removed from struct2 id = 1 | 
|  | 31 | #break11 - Struct field removed from struct3 last id | 
|  | 32 | #break12 -  derived1_function1 return type changed from enum1 to enum2 | 
|  | 33 | #break13 - derived1_function6 return type changed from struct1 to struct2 | 
|  | 34 | #break14 -  derived1_function4 return type changed from string to double | 
|  | 35 | #break15 - derived2_function1 return type changed from list<i32> to list<i16> | 
|  | 36 | #break16 - derived2_function5 return type changed from map<test_enum1,test_enum2> to map<test_enum3, test_enum2> | 
|  | 37 | #break17 - derived2_function6 return type changed from map<struct1,struct2> to map<struct1, struct3> | 
|  | 38 | #break18- oneway removed from base_oneway | 
|  | 39 | #break19 - oneway added to base_function1 | 
|  | 40 | #break20 - first enum value removed from enum1 | 
|  | 41 | #break21- last enum value removed from enum2 | 
|  | 42 | #break22 - in-between enum value removed from enum1 | 
|  | 43 | #break23 - required struct field added to struct4 | 
|  | 44 | #break24 - removed inheritance of derived1. | 
|  | 45 | #break25 - changed inheritance of derived2. | 
|  | 46 | #break26 - Field type changed in base_function1 argument id=3 | 
|  | 47 | #break27 - argument changed base_function2 list<enum1> to list<enum3> id =8 | 
|  | 48 | #break28- derived1_function5 arguement type changed map<i64, double> to list<i64> | 
|  | 49 | #break29 - base_function2 arguemnt type changed list<string> to string | 
|  | 50 | #break30- derived1_function6 argument changed struct1 to map<struct1,struct1> | 
|  | 51 | #break31 - Exception removed to base_function2 | 
|  | 52 | #break32- Exception1 field type changed for id =1 | 
|  | 53 | #break33 - derived1_function1 exception type changed. | 
|  | 54 | #break34 - Field added to struct with Field ID being in between two existing field IDs | 
|  | 55 |  | 
|  | 56 | #warning.thrift | 
|  | 57 | #Changing defaults | 
|  | 58 | #Id=1 struct5 | 
|  | 59 | #id=2 struct5 | 
|  | 60 | #id=4 struct2(list<double>) | 
|  | 61 | #id=3 struct2(list<i64>  default values removed) | 
|  | 62 | #id 4 struct1 change in double value | 
|  | 63 | #id 5 struct1 (default string value removed) | 
|  | 64 | #id=1 struct3 (change in map values) | 
|  | 65 | #id2 struct3 (change in map keys) | 
|  | 66 |  | 
|  | 67 | #change in inheritance for derived1 and derived2 | 
|  | 68 |  | 
|  | 69 | #change in struct field names | 
|  | 70 | #id9 struct1 | 
|  | 71 | #id2 struct2 | 
|  | 72 |  | 
|  | 73 | use strict; | 
|  | 74 | use warnings; | 
|  | 75 | use Getopt::Std; | 
|  | 76 |  | 
|  | 77 | # globals | 
|  | 78 | my $gArguments = "";                # arguments that will be passed to AuditTool | 
|  | 79 | my $gAuditToolPath = ""; | 
|  | 80 | my $gPreviousThriftPath;            # previous thrift path | 
|  | 81 | my $gCurrentThriftPath;             # current thrift path | 
|  | 82 | my $gThriftFileFolder; | 
|  | 83 | my $gBreakingFilesCount =34; | 
|  | 84 |  | 
|  | 85 | my $gVerbose = 0; | 
|  | 86 | #functions | 
|  | 87 | sub auditBreakingChanges; | 
|  | 88 | sub auditNonBreakingChanges; | 
|  | 89 |  | 
|  | 90 | main(); | 
|  | 91 |  | 
|  | 92 | sub main | 
|  | 93 | { | 
|  | 94 | parseOptions(); | 
|  | 95 | auditBreakingChanges(); | 
|  | 96 | auditNonBreakingChanges(); | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | sub parseOptions | 
|  | 100 | { | 
|  | 101 | my %options = (); | 
|  | 102 | if ( getopts ('vf:o:t:',\%options) ) | 
|  | 103 | { | 
|  | 104 | # current (new) thrift folder | 
|  | 105 | if ($options{'f'}) | 
|  | 106 | { | 
|  | 107 | $gThriftFileFolder = $options{'f'}; | 
|  | 108 | $gPreviousThriftPath = $gThriftFileFolder."/test.thrift"; | 
|  | 109 | } | 
|  | 110 | else | 
|  | 111 | { | 
|  | 112 | die "Missing Folder containing thrift files\n"; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | if($options{'t'}) | 
|  | 116 | { | 
|  | 117 | $gAuditToolPath = $options{'t'}; | 
|  | 118 | } | 
|  | 119 | else | 
|  | 120 | { | 
|  | 121 | die "Audit Tool Path required \n"; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | if ($options{'v'}) | 
|  | 125 | { | 
|  | 126 | $gVerbose = 1; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | } | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | sub auditBreakingChanges | 
|  | 133 | { | 
|  | 134 | my $breakingFileBaseName = $gThriftFileFolder."/break"; | 
|  | 135 | my $newThriftFile; | 
|  | 136 | for(my $i=1; $i <= $gBreakingFilesCount; $i++) | 
|  | 137 | { | 
|  | 138 | $newThriftFile = $breakingFileBaseName."$i.thrift"; | 
|  | 139 | my $arguments =  $gPreviousThriftPath." ".$newThriftFile; | 
|  | 140 | my ($exitCode, $output) = callThriftAuditTool($arguments); | 
|  | 141 | print $output if $gVerbose eq 1; | 
|  | 142 |  | 
|  | 143 | if($exitCode == 1) | 
|  | 144 | { | 
|  | 145 | # thrift_audit returns 1 when it is not able to find files or other non-audit failures | 
|  | 146 | print "exiting with exit code =1 i = ".$i."\n"; | 
|  | 147 | print $output; | 
|  | 148 | exit $exitCode; | 
|  | 149 | } | 
|  | 150 | if($exitCode != 2) | 
|  | 151 | { | 
|  | 152 | # thrift-audit return 2 for audit failures. So for Breaking changes we should get 2 as return value. | 
|  | 153 | print $output; | 
|  | 154 | die "\nTEST FAILURE: Breaking Change not detected for thrift file $newThriftFile, code=$exitCode \n"; | 
|  | 155 | } | 
|  | 156 | if(index($output,getMessageSubString("break$i")) == -1) | 
|  | 157 | { | 
|  | 158 | #Audit tool detected failure, but not the expected one. The change in breaking thrift file does not match getMessageSubString() | 
|  | 159 | print $output; | 
|  | 160 | die "\nTest FAILURE: Audit tool detected failure, but not the expected one!\n"; | 
|  | 161 | } | 
|  | 162 | else | 
|  | 163 | { | 
|  | 164 | #Thrift audit tool has detected audit failure and has returned exited to status code 2 | 
|  | 165 | print "Test Pass: Audit Failure detected for thrift file break$i.thrift \n"; | 
|  | 166 | } | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | sub auditNonBreakingChanges | 
|  | 172 | { | 
|  | 173 | my $breakingFileBaseName = $gThriftFileFolder."/warning"; | 
|  | 174 | my $newThriftFile; | 
|  | 175 | $newThriftFile = $breakingFileBaseName.".thrift"; | 
|  | 176 | my $arguments =  $gPreviousThriftPath." ".$newThriftFile; | 
|  | 177 | my ($exitCode, $output) = callThriftAuditTool($arguments); | 
|  | 178 | print $output if $gVerbose eq 1; | 
|  | 179 |  | 
|  | 180 | if($exitCode == 1) | 
|  | 181 | { | 
|  | 182 | # thrift_audit returns 1 when it is not able to find files or other non-audit failures | 
|  | 183 | print "exiting with exit code = 1  for file warning.thrift\n"; | 
|  | 184 | exit $exitCode; | 
|  | 185 | } | 
|  | 186 | elsif($exitCode != 0) | 
|  | 187 | { | 
|  | 188 | # thrift-audit return 0 if there are no audit failures. | 
|  | 189 | die "\nTEST FAILURE: Non Breaking changes returned failure for thrift file $newThriftFile \n"; | 
|  | 190 | } | 
|  | 191 | else | 
|  | 192 | { | 
|  | 193 | #Thrift audit tool has exited with status 0. | 
|  | 194 | print "Test Pass: Audit tool exits with success for warnings \n"; | 
|  | 195 | } | 
|  | 196 |  | 
|  | 197 |  | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | # ----------------------------------------------------------------------------------------------------- | 
|  | 201 | # call thriftAuditTool script | 
|  | 202 | sub callThriftAuditTool ( $ ) | 
|  | 203 | { | 
|  | 204 | my $args = shift; | 
|  | 205 |  | 
|  | 206 | my $command = "$gAuditToolPath --audit $args"; | 
|  | 207 | my $output = `$command 2>&1`; | 
|  | 208 | my $exitCode = $? >> 8; | 
|  | 209 |  | 
|  | 210 | return ($exitCode,$output); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | sub getMessageSubString( $ ) | 
|  | 214 | { | 
|  | 215 | my $fileName = shift; | 
|  | 216 | my %lookupTable = ( | 
|  | 217 | "break1"  => "base_function3", | 
|  | 218 | "break2"  => "test_struct1", | 
|  | 219 | "break3"  => "test_struct1", | 
|  | 220 | "break4"  => "test_struct1", | 
|  | 221 | "break5"  => "test_struct1", | 
|  | 222 | "break6"  => "test_struct2", | 
|  | 223 | "break7"  => "test_struct6", | 
|  | 224 | "break8"  => "test_struct5", | 
|  | 225 | "break9"  => "test_struct1", | 
|  | 226 | "break10" => "test_struct2", | 
|  | 227 | "break11" => "test_struct3", | 
|  | 228 | "break12" => "derived1_function1", | 
|  | 229 | "break13" => "derived1_function6", | 
|  | 230 | "break14" => "derived1_function4", | 
|  | 231 | "break15" => "derived2_function1", | 
|  | 232 | "break16" => "derived2_function5", | 
|  | 233 | "break17" => "derived2_function6", | 
|  | 234 | "break18" => "base_oneway", | 
|  | 235 | "break19" => "base_function1", | 
|  | 236 | "break20" => "test_enum1", | 
|  | 237 | "break21" => "test_enum2", | 
|  | 238 | "break22" => "test_enum1", | 
|  | 239 | "break23" => "test_struct4", | 
|  | 240 | "break24" => "derived1", | 
|  | 241 | "break25" => "derived2", | 
|  | 242 | "break26" => "base_function1", | 
|  | 243 | "break27" => "base_function2_args", | 
|  | 244 | "break28" => "derived1_function5_args", | 
|  | 245 | "break29" => "base_function2_args", | 
|  | 246 | "break30" => "derived1_function6", | 
|  | 247 | "break31" => "base_function2_exception", | 
|  | 248 | "break32" => "test_exception1", | 
|  | 249 | "break33" => "derived1_function1_exception", | 
|  | 250 | "break34" => "test_struct3", | 
|  | 251 | ); | 
|  | 252 | if (not exists $lookupTable{ $fileName }) | 
|  | 253 | { | 
|  | 254 | print "in the null case\n"; | 
|  | 255 | return "NULL"; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | my $retval = $lookupTable{ $fileName }; | 
|  | 259 | print "$fileName => $retval\n"; | 
|  | 260 | return $lookupTable{ $fileName }; | 
|  | 261 | } |