Example #1
0
    def test_dashboard_app_update_redirection(self, redis):
        """
        Test that after swarming an app that belongs to the user's default
        dashboard, he/she gets redirected to /dashboard/.

        See self.setUp() for more info.
        """
        url = reverse('edit_swarm', kwargs={'swarm_id': self.swarm2.id})
        payload = {
            'app_id':
            self.swarm2.app.id,
            'os_image_id':
            getattr(self.swarm2.release.build.os_image, 'id', ''),
            'squad_id':
            self.swarm2.squad.id,
            'tag':
            randchars(),
            'config_name':
            self.swarm2.config_name,
            'config_yaml':
            yamlize(self.swarm2.config_yaml),
            'env_yaml':
            yamlize(self.swarm2.env_yaml),
            'volumes':
            yamlize(self.swarm2.volumes),
            'run_as':
            self.swarm2.run_as or 'nobody',
            'mem_limit':
            self.swarm2.mem_limit,
            'memsw_limit':
            self.swarm2.memsw_limit,
            'proc_name':
            self.swarm2.proc_name,
            'size':
            self.swarm2.size,
            'pool':
            self.swarm2.pool or '',
            'balancer':
            '',
            'config_ingredients':
            [ing.pk for ing in self.swarm2.config_ingredients.all()]
        }
        resp = self.client.post(url, data=payload)
        assert resp._headers['location'][1] == 'http://testserver/dashboard/'
Example #2
0
 def test_invalid_tags(self):
     url = reverse('edit_swarm', kwargs={'swarm_id': self.swarm.id})
     invalid_tags = [
         'gittag/v1', 'my:tag', '^tag', '~other:tag', 'tag?'
         'other*tag'
         '\\tag', 'a tag'
     ]
     payload = {
         'app_id':
         self.swarm.app.id,
         'os_image_id':
         getattr(self.swarm.release.build.os_image, 'id', ''),
         'squad_id':
         self.swarm.squad.id,
         'config_name':
         self.swarm.config_name,
         'config_yaml':
         yamlize(self.swarm.config_yaml),
         'env_yaml':
         yamlize(self.swarm.env_yaml),
         'volumes':
         yamlize(self.swarm.volumes),
         'run_as':
         self.swarm.run_as or 'nobody',
         'mem_limit':
         self.swarm.mem_limit,
         'memsw_limit':
         self.swarm.memsw_limit,
         'proc_name':
         self.swarm.proc_name,
         'size':
         self.swarm.size,
         'pool':
         self.swarm.pool or '',
         'balancer':
         '',
         'config_ingredients':
         [ing.pk for ing in self.swarm.config_ingredients.all()]
     }
     for tag in invalid_tags:
         payload['tag'] = tag
         resp = self.client.post(url, data=payload)
         assert 'Invalid tag name' in resp.content.decode('utf-8')
Example #3
0
    def test_simple_update(self, redis):

        url = reverse('edit_swarm', kwargs={'swarm_id': self.swarm.id})
        payload = {
            'app_id':
            self.swarm.app.id,
            'os_image_id':
            getattr(self.swarm.release.build.os_image, 'id', ''),
            'squad_id':
            self.swarm.squad.id,
            'tag':
            randchars(),
            'config_name':
            self.swarm.config_name,
            'config_yaml':
            yamlize(self.swarm.config_yaml),
            'env_yaml':
            yamlize(self.swarm.env_yaml),
            'volumes':
            yamlize(self.swarm.volumes),
            'run_as':
            self.swarm.run_as or 'nobody',
            'mem_limit':
            self.swarm.mem_limit,
            'memsw_limit':
            self.swarm.memsw_limit,
            'proc_name':
            self.swarm.proc_name,
            'size':
            self.swarm.size,
            'pool':
            self.swarm.pool or '',
            'balancer':
            '',
            'config_ingredients':
            [ing.pk for ing in self.swarm.config_ingredients.all()]
        }
        previous_release_id = self.swarm.release_id
        self.client.post(url, data=payload)
        saved = models.Swarm.objects.get(id=self.swarm.id)
        new_release_id = saved.release_id
        assert previous_release_id != new_release_id
Example #4
0
    def test_env_yaml_marshaling(self):

        url = reverse('edit_swarm', kwargs={'swarm_id': self.swarm.id})
        payload = {
            'app_id':
            self.swarm.app.id,
            'os_image_id':
            getattr(self.swarm.release.build.os_image, 'id', ''),
            'squad_id':
            self.swarm.squad.id,
            'tag':
            randchars(),
            'config_name':
            self.swarm.config_name,
            'config_yaml':
            yamlize(self.swarm.config_yaml),
            'env_yaml':
            'TOO_BIG_NUMBER: 1234123412341234',
            'volumes':
            yamlize(self.swarm.volumes),
            'run_as':
            self.swarm.run_as or 'nobody',
            'mem_limit':
            self.swarm.mem_limit,
            'memsw_limit':
            self.swarm.memsw_limit,
            'proc_name':
            self.swarm.proc_name,
            'size':
            self.swarm.size,
            'pool':
            self.swarm.pool or '',
            'balancer':
            '',
            'config_ingredients':
            [ing.pk for ing in self.swarm.config_ingredients.all()]
        }
        resp = self.client.post(url, data=payload)
        assert "Cannot be marshalled to XMLRPC" in resp.content
Example #5
0
def edit_swarm(request, swarm_id=None):
    if swarm_id:
        # Need to populate form from swarm
        swarm = get_object_or_404(models.Swarm, id=swarm_id)
        initial = {
            'app_id': swarm.app.id,
            'squad_id': swarm.squad.id,
            'tag': swarm.release.build.tag,
            'config_name': swarm.config_name,
            'config_yaml': yamlize(swarm.config_yaml),
            'env_yaml': yamlize(swarm.env_yaml),
            'volumes': yamlize(swarm.volumes),
            'run_as': swarm.run_as or 'nobody',
            'mem_limit': swarm.mem_limit,
            'memsw_limit': swarm.memsw_limit,
            'proc_name': swarm.proc_name,
            'size': swarm.size,
            'pool': swarm.pool or '',
            'balancer': swarm.balancer,
            'config_ingredients': [
                ing.pk for ing in swarm.config_ingredients.all()]
        }
        version_diffs, _last_edited = _get_version_diffs_for_obj(
            swarm, VERSION_DIFFS_LIMIT)
        compiled_config = yamlize(swarm.get_config())
        compiled_env = yamlize(swarm.get_env())

    else:
        initial = None
        swarm = models.Swarm()
        version_diffs = []
        compiled_config = yamlize({})
        compiled_env = yamlize({})

    form = forms.SwarmForm(request.POST or None, initial=initial)
    error_msg = None
    if form.is_valid():
        data = form.cleaned_data

        # Check if we already have a swarm with these parameters
        # Note: exclude itself, in case we are editing an existing swarm
        n = models.Swarm.objects.filter(
            app=data['app_id'],
            proc_name=data['proc_name'],
            config_name=data['config_name'],
            squad=data['squad_id'],
        ).exclude(id=swarm_id).count()

        if n > 0:
            error_msg = (
                'Swarm already exists for this app, proc, config and squad!'
            )

        else:
            swarm.app = models.App.objects.get(id=data['app_id'])
            swarm.squad = models.Squad.objects.get(id=data['squad_id'])
            swarm.config_name = data['config_name']
            swarm.config_yaml = data['config_yaml']
            swarm.env_yaml = data['env_yaml']
            swarm.volumes = data['volumes']
            swarm.run_as = data['run_as']
            swarm.mem_limit = data['mem_limit']
            swarm.memsw_limit = data['memsw_limit']
            swarm.proc_name = data['proc_name']
            swarm.size = data['size']
            swarm.pool = data['pool'] or None
            swarm.balancer = data['balancer'] or None
            swarm.release = swarm.get_current_release(data['tag'])
            swarm.save()
            swarm.config_ingredients.clear()
            for ingredient in data['config_ingredients']:
                swarm.config_ingredients.add(ingredient)

            # Set the version metadata as recommended in the low-level API docs
            # https://django-reversion.readthedocs.org/en/latest/api.html?#version-meta-data
            revisions.set_user(request.user)
            revisions.set_comment("Created from web form.")

            do_swarm(swarm, request.user)

            # If app is part of the user's default dashboard, redirect there.
            if app_in_default_dashboard(swarm.app, request.user):
                return redirect('default_dash')

            return redirect('dash')

    return render(request, 'swarm.html', {
        'swarm': swarm,
        'form': form,
        'error_msg': error_msg,
        'btn_text': 'Swarm',
        'version_diffs': version_diffs,
        'version_diffs_limit': VERSION_DIFFS_LIMIT,
        'compiled_config': compiled_config,
        'compiled_env': compiled_env
    })
Example #6
0
 def dehydrate_config_yaml(self, bundle):
     # Explicit dehydrate to avoid this field is stringified with str()
     return yamlize(bundle.obj.config_yaml)