diff options
author | Remi NGUYEN VAN <reminv@google.com> | 2022-02-25 17:29:19 +0900 |
---|---|---|
committer | Remi NGUYEN VAN <reminv@google.com> | 2022-03-09 12:23:49 +0900 |
commit | cb9e79d97699392e9f28f6f51b49fea4f7aabc55 (patch) | |
tree | 488ffead0d926eeeddbb98862610de7158c8f45e /common | |
parent | 17da516f6542efe7ff107f83d01427aa9a85dbf9 (diff) | |
download | net-cb9e79d97699392e9f28f6f51b49fea4f7aabc55.tar.gz |
Add DisableConfigSyncTargetPreparer
The TargetPreparer disables DeviceConfig sync before a test module, and
attempts to reset it to its original state afterwards.
It only supports S+ as the associated commands are not available on R-.
The format of commands has also changed in some T branches , so support
legacy format if the newer format is not supported by the device.
Bug: 210377950
Test: atest CtsNetTestCases using the preparer, verify setting set
during the test.
Change-Id: Iaabaa1050b024e1378ab1cf87a394cc8ebab227b
Diffstat (limited to 'common')
-rw-r--r-- | common/testutils/host/com/android/testutils/DisableConfigSyncTargetPreparer.kt | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/common/testutils/host/com/android/testutils/DisableConfigSyncTargetPreparer.kt b/common/testutils/host/com/android/testutils/DisableConfigSyncTargetPreparer.kt new file mode 100644 index 00000000..63f05a6c --- /dev/null +++ b/common/testutils/host/com/android/testutils/DisableConfigSyncTargetPreparer.kt @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.testutils + +import com.android.tradefed.invoker.TestInformation +import com.android.tradefed.targetprep.BaseTargetPreparer + +/** + * A target preparer that disables DeviceConfig sync while running a test. + * + * Without this preparer, tests that rely on stable values of DeviceConfig flags, for example to + * test behavior when setting the flag and resetting it afterwards, may flake as the flags may + * be synced with remote servers during the test. + */ +class DisableConfigSyncTargetPreparer : BaseTargetPreparer() { + private var syncDisabledOriginalValue = "none" + + override fun setUp(testInfo: TestInformation) { + if (isDisabled) return + syncDisabledOriginalValue = readSyncDisabledOriginalValue(testInfo) + + // The setter is the same in current and legacy S versions + testInfo.exec("cmd device_config set_sync_disabled_for_tests until_reboot") + } + + override fun tearDown(testInfo: TestInformation, e: Throwable?) { + if (isTearDownDisabled) return + // May fail harmlessly if called before S + testInfo.exec("cmd device_config set_sync_disabled_for_tests $syncDisabledOriginalValue") + } + + private fun readSyncDisabledOriginalValue(testInfo: TestInformation): String { + return when (val reply = testInfo.exec("cmd device_config get_sync_disabled_for_tests")) { + "until_reboot", "persistent", "none" -> reply + // Reply does not match known modes, try legacy commands used on S and some T builds + else -> when (testInfo.exec("cmd device_config is_sync_disabled_for_tests")) { + // The legacy command just said "true" for "until_reboot" or "persistent". There is + // no way to know which one was used, so just reset to "until_reboot" to be + // conservative. + "true" -> "until_reboot" + else -> "none" + } + } + } +} + +private fun TestInformation.exec(cmd: String) = this.device.executeShellCommand(cmd)
\ No newline at end of file |