Esempio n. 1
0
 def test_get_variables_override_app_env_test(self):
     """
     Check that application/version/env specific variables are overriden by application/env/test specific
     Order is (bottom take precedence):
     - global (no app, no env, no version, no test)
     - specific to on of the parameter (matching app or matching version or matching env or matching test in this order)
     - specific to tuple (application / environment)
     - specific to tuple (application / version / environment)
     - specific to tuple (application / environment / test)
     - specific to tuple (application / version / environment / test)
     """
     version = Version.objects.get(pk=3)
     env = TestEnvironment.objects.get(pk=3)
     test = TestCase.objects.get(pk=1)
     Variable(name='var0', value='value0', application=version.application, version=version, environment=env).save()
     var1 = Variable(name='var0', value='value1', application=version.application, environment=env)
     var1.save()
     var1.test.add(test)
          
     response = self.client.get(reverse('variableApi'), data={'version': version.id, 'environment': 3, 'test': 1})
     self.assertEqual(response.status_code, 200, 'status code should be 200: ' + str(response.content))
     all_variables = self._convert_to_dict(response.data)
          
     # check overriding of variables
     self.assertEqual(all_variables['var0']['value'], 'value1')
Esempio n. 2
0
 def test_get_variables_override_multiple_generic_environment(self):
     """
     Check that application/version specific variables are overriden by environment specific. Here, we have multiple parents for environment
     Order is (bottom take precedence):
     - global (no app, no env, no version, no test)
     - specific to on of the parameter (matching app or matching version or matching env or matching test in this order)
     - specific to tuple (application / environment)
     - specific to tuple (application / version / environment)
     - specific to tuple (application / environment / test)
     - specific to tuple (application / version / environment / test)
     """
     version = Version.objects.get(pk=3)
     env = TestEnvironment.objects.get(pk=4)
     gen_env1 = TestEnvironment.objects.get(pk=3)
     gen_env2 = TestEnvironment.objects.get(pk=1)
     Variable(name='var0', value='value0', environment=gen_env2).save()
     Variable(name='var0', value='value1', environment=gen_env1).save()
     Variable(name='var0', value='value2', environment=env).save()
          
     response = self.client.get(reverse('variableApi'), data={'version': version.id, 'environment': 4, 'test': 1})
     self.assertEqual(response.status_code, 200, 'status code should be 200: ' + str(response.content))
     all_variables = self._convert_to_dict(response.data)
          
     # check overriding of variables
     self.assertEqual(all_variables['var0']['value'], 'value2')
Esempio n. 3
0
 def test_reservable_state_correction_with_test(self):
     """
     Check that when a variable is added with the same characteristics of another, reservable state is set to the newly created variable
     Check with test as ManyToMany relationship must be treated seperately
     """
     test = TestCase.objects.get(pk=1)
     version = Version.objects.get(pk=3)
     var0 = Variable(name='var0', value='value0', application=version.application, reservable=True)
     var0.save()
     var0.test.add(test)
          
     response = self.client.post(reverse('variableApi'), data={'name': 'var0', 'value': 'value1', 'application': version.application.id, 'reservable': False, 'test': [1]})
     self.assertEqual(response.status_code, 201, 'status code should be 201: ' + str(response.content))
        
     for v in Variable.objects.filter(name='var0'):
         self.assertFalse(v.reservable)
Esempio n. 4
0
 def test_reservable_state_correction(self):
     """
     Check that when a variable is added with the same characteristics of another, reservable state is set to the newly created variable
     """
     version = Version.objects.get(pk=3)
     Variable(name='var0', value='value0', application=version.application, reservable=True).save()
          
     response = self.client.post(reverse('variableApi'), data={'name': 'var0', 'value': 'value1', 'application': version.application.id, 'reservable': False})
     self.assertEqual(response.status_code, 201, 'status code should be 201: ' + str(response.content))
        
     for v in Variable.objects.filter(name='var0'):
         self.assertFalse(v.reservable)
Esempio n. 5
0
 def test_return_all_variables_if_no_older_than_provided(self):
     """
     Check that we get all variables if 'olderThan' param is not provided
     """
          
     version = Version.objects.get(pk=2)
     env = TestEnvironment.objects.get(pk=3)
     Variable(name='oldVar', value='oldValue1', application=version.application, version=version, environment=env, 
              creationDate=timezone.now() - datetime.timedelta(2), 
              timeToLive=5).save()
     Variable(name='oldVar2', value='oldValue2', application=version.application, version=version, environment=env, 
              creationDate=timezone.now() - datetime.timedelta(1), 
              timeToLive=5).save()
 
     response = self.client.get(reverse('variableApi'), data={'version': 2, 'environment': 3, 'test': 1})
          
     self.assertEqual(response.status_code, 200, 'status code should be 200: ' + str(response.content))
          
     all_variables = self._convert_to_dict(response.data)
          
     self.assertIn('oldVar', all_variables, "oldVar should be get as it's older than requested")
     self.assertIn('oldVar2', all_variables, "oldVar2 should not be get as it's younger than requested")
Esempio n. 6
0
 def test_get_all_variables_with_release_date(self):
     """
     Check that release dates are correctly managed
     variable is returned if
     - releaseDate is None
     - releaseDate is in the past (then it should be set to None)
     """
     version = Version.objects.get(pk=3)
     Variable(name='var0', value='value0', application=version.application, version=version).save()
     Variable(name='var1', value='value1', application=version.application, version=version, releaseDate=timezone.now() + datetime.timedelta(seconds=60)).save()
     Variable(name='var1', value='value2', application=version.application, version=version, releaseDate=timezone.now() - datetime.timedelta(seconds=60)).save()
          
     response = self.client.get(reverse('variableApi'), data={'version': 3, 'environment': 3, 'test': 1})
     self.assertEqual(response.status_code, 200, 'status code should be 200: ' + str(response.content))
     self.assertEqual(1, len([v for v in response.data if v['name'] == 'var1']), "Only one value should be get")
          
     all_variables = self._convert_to_dict(response.data)
          
     # check we only get variable where release date is before now and variables without release date
     self.assertTrue('var1' in all_variables)                 # release date in the past, should be removed
     self.assertEqual("value2", all_variables['var1']['value'])
     self.assertIsNone(all_variables['var1']['releaseDate'])  # release date should be reset 
     self.assertTrue('var0' in all_variables)                 # no release date
Esempio n. 7
0
 def test_update_reservable_state_correction_with_different_tests(self):
     """
     Check that when a variable with the same characteristics of another (except test list) is not changed
     """
     test = TestCase.objects.get(pk=1)
     version = Version.objects.get(pk=3)
     var0 = Variable(name='var0', value='value0', application=version.application, reservable=True)
     var0.save()
     var0.test.add(test)
     var1 = Variable(name='var0', value='value0', application=version.application, reservable=True)
     var1.save()
         
     response = self.client.patch(reverse('variableApiPut', args=[var1.id]), {'reservable': False, 'test': [1]})
     self.assertEqual(response.status_code, 200, 'status code should be 200: ' + str(response.content))
       
     # var0 and var1 are similar, except for test list, so when 'reservable' is updated on var1, var0 is not updated
     self.assertTrue(Variable.objects.get(pk=var0.id).reservable)
     self.assertFalse(Variable.objects.get(pk=var1.id).reservable)
Esempio n. 8
0
 def test_do_not_destroy_not_so_old_variables(self):
     """
     Check that if a variable did not reach its max number of days, it's not removed
     """
         
     version = Version.objects.get(pk=2)
     env = TestEnvironment.objects.get(pk=3)
     Variable(name='oldVar', value='oldValue', application=version.application, version=version, environment=env, 
              creationDate=timezone.now() - datetime.timedelta(0, 23 * 60 * 60), 
              timeToLive=1).save()
         
     response = self.client.get(reverse('variableApi'), data={'version': 2, 'environment': 3, 'test': 1})
         
     self.assertEqual(response.status_code, 200, 'status code should be 200: ' + str(response.content))
         
     all_variables = self._convert_to_dict(response.data)
        
     self.assertIn('oldVar', all_variables, "oldVar should not be removed, as it's not old enough")
Esempio n. 9
0
 def test_update_reservable_state_correction_with_test(self):
     """
     Check that when a variable is changed with the same characteristics of another, reservable state is set to the updated variable
     Check with test as ManyToMany relationship must be treated seperately
     """
     test = TestCase.objects.get(pk=1)
     version = Version.objects.get(pk=3)
     var0 = Variable(name='var0', value='value0', application=version.application, reservable=True)
     var0.save()
     var0.test.add(test)
     var1 = Variable(name='var0', value='value0', application=version.application, reservable=True)
     var1.save()
     var1.test.add(test)
         
     response = self.client.patch(reverse('variableApiPut', args=[var1.id]), {'reservable': False, 'test': [1]})
     self.assertEqual(response.status_code, 200, 'status code should be 200: ' + str(response.content))
       
     # var0 and var1 are similar, so when 'reservable' is updated on var1, var0 is also updated
     self.assertFalse(Variable.objects.get(pk=var0.id).reservable)
     self.assertFalse(Variable.objects.get(pk=var1.id).reservable)
Esempio n. 10
0
    def _get_linked_application_variables(self, all_variables, application, environment_tree):
        """
        Get all variables of the applications linked to the requested application
        """
        
        
        linked_application_variables = Variable.objects.none()
        if application.linkedApplication:
            for linked_application in application.linkedApplication.all():
                linked_application_variables = updateVariables(linked_application_variables, all_variables.filter(application=linked_application, version=None, environment=None, test=None, reservable=False))
            
                for env in environment_tree:
                    linked_application_variables = updateVariables(linked_application_variables, all_variables.filter(application=linked_application, version=None, environment=env, test=None, reservable=False))
 
        updated_linked_application_variables = []
        for var in linked_application_variables:
            updated_linked_application_variables.append(Variable(name=var.nameWithApp, value=var.value, application=var.application, version=var.version, environment=var.environment))
 
        return updated_linked_application_variables
Esempio n. 11
0
def copyVariables(request):
    """
    Copy a set of variables and change some of the parameters
    """

    variableIds = [int(id) for id in request.POST['ids'].split(',')]

    try:
        tests = TestCase.objects.filter(id__in=request.POST.getlist('test'))
    except:
        tests = []

    try:
        environment = TestEnvironment.objects.get(
            id=int(request.POST['environment']))
    except:
        environment = None

    try:
        version = Version.objects.get(id=int(request.POST['version']))
    except:
        version = None

    try:
        application = Application.objects.get(
            id=int(request.POST['application']))
    except:
        application = None

    try:
        reservable = request.POST['reservable'] == 'on'
    except:
        reservable = False

    # init message
    from variableServer.admin import VariableAdmin
    varAdmin = VariableAdmin(Variable, admin.site)

    if FLAG_RESTRICT_APP and application and not request.user.has_perm(
            'commonsServer.can_view_application_' + application.name):
        varAdmin.message_user(
            request,
            "You don't have right to copy variable to application %s" %
            (application.name, ),
            level=messages.ERROR)
        return HttpResponseRedirect(request.POST['nexturl'])

    # copy content of each variable in a new variable
    for variableId in variableIds:
        try:
            variable = Variable.objects.get(id=variableId)

            newVariable = Variable(name=variable.name,
                                   value=variable.value,
                                   application=application,
                                   environment=environment,
                                   reservable=reservable,
                                   version=version,
                                   releaseDate=None,
                                   internal=variable.internal,
                                   description=variable.description)
            newVariable.save()
            for test in tests:
                newVariable.test.add(test)
            varAdmin.message_user(request,
                                  "Variable %s has been copied" %
                                  (variable.name, ),
                                  level=messages.INFO)

        except Exception as e:
            varAdmin.message_user(
                request,
                "Variable with id %d has not been copied: %s" %
                (variableId, str(e)),
                level=messages.ERROR)

    return HttpResponseRedirect(request.POST['nexturl'])