Move the stats fd cleanup code into stats_test() and drop the superfluous vm_stats_test() and vcpu_stats_test() helpers in order to decouple creation of the stats file from consuming/testing the file (deduping code is a bonus). This will make it easier to test various edge cases related to stats, e.g. that userspace can dup() a stats fd, that userspace can have multiple stats files for a singleVM/vCPU, etc.
Unified · Split
| @@ -167,23 +167,7 @@ static void stats_test(int stats_fd) · 166 unmodified lines | ||
| 167 | 167 | free(stats_data); |
| 168 | 168 | free(stats_desc); |
| 169 | 169 | free(id); |
| 170 | −} | |
| 171 | − | |
| 172 | − | |
| 173 | −static void vm_stats_test(struct kvm_vm *vm) | |
| 174 | −{ | |
| 175 | − int stats_fd = vm_get_stats_fd(vm); | |
| 176 | − | |
| 177 | − stats_test(stats_fd); | |
| 178 | − close(stats_fd); | |
| 179 | − TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed"); | |
| 180 | −} | |
| 181 | − | |
| 182 | −static void vcpu_stats_test(struct kvm_vcpu *vcpu) | |
| 183 | −{ | |
| 184 | − int stats_fd = vcpu_get_stats_fd(vcpu); | |
| 185 | 170 | |
| 186 | − stats_test(stats_fd); | |
| 187 | 171 | close(stats_fd); |
| 188 | 172 | TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed"); |
| 189 | 173 | } |
| @@ -241,9 +225,11 @@ int main(int argc, char *argv[]) · 51 unmodified lines | ||
| 241 | 225 | |
| 242 | 226 | /* Check stats read for every VM and VCPU */ |
| 243 | 227 | for (i = 0; i < max_vm; ++i) { |
| 244 | − vm_stats_test(vms[i]); | |
| 228 | + stats_test(vm_get_stats_fd(vms[i])); | |
| 229 | + | |
| 245 | 230 | for (j = 0; j < max_vcpu; ++j) |
| 246 | − vcpu_stats_test(vcpus[i * max_vcpu + j]); | |
| 231 | + stats_test(vcpu_get_stats_fd(vcpus[i * max_vcpu + j])); | |
| 232 | + | |
| 247 | 233 | ksft_test_result_pass("vm%i\n", i); |
| 248 | 234 | } |
| 249 | 235 | |