Uninett / Argus

Argus is an alert aggregator for monitoring systems
GNU General Public License v3.0
19 stars 14 forks source link

Spurious test-failure when using SourceSystemFactory #791

Closed hmpf closed 2 months ago

hmpf commented 6 months ago

OneToOneField and factories are tricky. The test tests.dev.test_check_token_expiry will sometimes fail when it randomly generates a user that already exists.

Traceback:

          ERROR [0.365s]: test_close_expiry_incidents_without_expiring_tokens_closes_unconnected_incidents (tests.dev.test_check_token_expiry.CheckTokenExpiryTests)
          ----------------------------------------------------------------------
          Traceback (most recent call last):
            File "/VENV/django/db/models/query.py", line 916, in get_or_create
              return self.get(**kwargs), False
            File "/VENV/django/db/models/query.py", line 637, in get
              raise self.model.DoesNotExist(
          argus.incident.models.SourceSystem.DoesNotExist: SourceSystem matching query does not exist.

          During handling of the above exception, another exception occurred:

          Traceback (most recent call last):
            File "/VENV/django/db/backends/utils.py", line 89, in _execute
              return self.cursor.execute(sql, params)
          psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "argus_incident_sourcesystem_user_id_key"
          DETAIL:  Key (user_id)=(75) already exists.

          The above exception was the direct cause of the following exception:

          Traceback (most recent call last):
            File "/../tests/dev/test_check_token_expiry.py", line 25, in setUp
              self.source_system2 = SourceSystemFactory()
            File "/VENV/factory/base.py", line 40, in __call__
              return cls.create(**kwargs)
            File "/VENV/factory/base.py", line 528, in create
              return cls._generate(enums.CREATE_STRATEGY, kwargs)
            File "/VENV/factory/django.py", line 121, in _generate
              return super()._generate(strategy, params)
            File "/VENV/factory/base.py", line 465, in _generate
              return step.build()
            File "/VENV/factory/builder.py", line 274, in build
              instance = self.factory_meta.instantiate(
            File "/VENV/factory/base.py", line 317, in instantiate
              return self.factory._create(model, *args, **kwargs)
            File "/VENV/factory/django.py", line 171, in _create
              return cls._get_or_create(model_class, *args, **kwargs)
            File "/VENV/factory/django.py", line 163, in _get_or_create
              raise e
            File "/VENV/factory/django.py", line 144, in _get_or_create
              instance, _created = manager.get_or_create(*args, **key_fields)
            File "/VENV/django/db/models/manager.py", line 87, in manager_method
              return getattr(self.get_queryset(), name)(*args, **kwargs)
            File "/VENV/django/db/models/query.py", line 923, in get_or_create
              return self.create(**params), True
            File "/VENV/django/db/models/query.py", line 658, in create
              obj.save(force_insert=True, using=self.db)
            File "/VENV/django/db/models/base.py", line 814, in save
              self.save_base(
            File "/VENV/django/db/models/base.py", line 877, in save_base
              updated = self._save_table(
            File "/VENV/django/db/models/base.py", line 1020, in _save_table
              results = self._do_insert(
            File "/VENV/django/db/models/base.py", line 1061, in _do_insert
              return manager._insert(
            File "/VENV/django/db/models/manager.py", line 87, in manager_method
              return getattr(self.get_queryset(), name)(*args, **kwargs)
            File "/VENV/django/db/models/query.py", line 1805, in _insert
              return query.get_compiler(using=using).execute_sql(returning_fields)
            File "/VENV/django/db/models/sql/compiler.py", line 1822, in execute_sql
              cursor.execute(sql, params)
            File "/VENV/django/db/backends/utils.py", line 67, in execute
              return self._execute_with_wrappers(
            File "/VENV/django/db/backends/utils.py", line 80, in _execute_with_wrappers
              return executor(sql, params, many, context)
            File "/VENV/django/db/backends/utils.py", line 89, in _execute
              return self.cursor.execute(sql, params)
            File "/VENV/django/db/utils.py", line 91, in __exit__
              raise dj_exc_value.with_traceback(traceback) from exc_value
            File "/VENV/django/db/backends/utils.py", line 89, in _execute
      return self.cursor.execute(sql, params)
  django.db.utils.IntegrityError: duplicate key value violates unique constraint "argus_incident_sourcesystem_user_id_key"
  DETAIL:  Key (user_id)=(75) already exists.

Fix: always explicitly create a user when using a SourceSystemFactory.

johannaengland commented 3 months ago

Part of it has been fixed by #851.

johannaengland commented 2 months ago

Closed by #856.