1#!/usr/bin/env python3 2# Copyright 2024, The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import unittest 17from unittest import mock 18from atest import rollout_control 19 20 21class RolloutControlledFeatureUnittests(unittest.TestCase): 22 23 def test_is_enabled_username_hash_is_greater_than_rollout_percentage_returns_false( 24 self, 25 ): 26 sut = rollout_control.RolloutControlledFeature( 27 name='test_feature', 28 rollout_percentage=66, 29 env_control_flag='TEST_FEATURE', 30 ) 31 32 self.assertFalse(sut.is_enabled('username')) 33 34 def test_is_enabled_username_hash_is_equal_to_rollout_percentage_returns_false( 35 self, 36 ): 37 sut = rollout_control.RolloutControlledFeature( 38 name='test_feature', 39 rollout_percentage=67, 40 env_control_flag='TEST_FEATURE', 41 ) 42 43 self.assertFalse(sut.is_enabled('username')) 44 45 def test_is_enabled_username_hash_is_less_or_equal_than_rollout_percentage_returns_true( 46 self, 47 ): 48 sut = rollout_control.RolloutControlledFeature( 49 name='test_feature', 50 rollout_percentage=68, 51 env_control_flag='TEST_FEATURE', 52 ) 53 54 self.assertTrue(sut.is_enabled('username')) 55 56 def test_is_enabled_username_undetermined_returns_false(self): 57 sut = rollout_control.RolloutControlledFeature( 58 name='test_feature', 59 rollout_percentage=99, 60 env_control_flag='TEST_FEATURE', 61 ) 62 63 self.assertFalse(sut.is_enabled('')) 64 65 def test_is_enabled_flag_set_to_true_returns_true(self): 66 sut = rollout_control.RolloutControlledFeature( 67 name='test_feature', 68 rollout_percentage=0, 69 env_control_flag='TEST_FEATURE', 70 ) 71 72 with mock.patch.dict('os.environ', {'TEST_FEATURE': 'true'}): 73 self.assertTrue(sut.is_enabled()) 74 75 def test_is_enabled_flag_set_to_1_returns_true(self): 76 sut = rollout_control.RolloutControlledFeature( 77 name='test_feature', 78 rollout_percentage=0, 79 env_control_flag='TEST_FEATURE', 80 ) 81 82 with mock.patch.dict('os.environ', {'TEST_FEATURE': '1'}): 83 self.assertTrue(sut.is_enabled()) 84 85 def test_is_enabled_flag_set_to_false_returns_false(self): 86 sut = rollout_control.RolloutControlledFeature( 87 name='test_feature', 88 rollout_percentage=100, 89 env_control_flag='TEST_FEATURE', 90 ) 91 92 with mock.patch.dict('os.environ', {'TEST_FEATURE': 'false'}): 93 self.assertFalse(sut.is_enabled()) 94 95 def test_is_enabled_is_owner_returns_true(self): 96 sut = rollout_control.RolloutControlledFeature( 97 name='test_feature', 98 rollout_percentage=0, 99 env_control_flag='TEST_FEATURE', 100 owners=['owner_name'], 101 ) 102 103 self.assertFalse(sut.is_enabled('name')) 104 self.assertTrue(sut.is_enabled('owner_name')) 105