Repository: packit/packit
Issue: 'FedPKG' object has no attribute 'init_ticket'

https://github.com/abrt/gnome-abrt/issues/280

https://github.com/packit-service/packit/blob/fc81b48dcd451120721295ca671c242f81126db9/packit/distgit.py#L95

I’m guessing that 5ceaa4caa568fb9673b04640dce30cf90ef2e487 missed that line?

Changed files:
tests/integration/test_get_api.py, packit/distgit.py, tests/integration/test_update.py, packit/api.py, tests/integration/test_create_update.py, tests/integration/test_build.py

diff --git a/packit/api.py b/packit/api.py
index 83429826..92699d10 100644
--- a/packit/api.py
+++ b/packit/api.py
@@ -75,6 +75,7 @@ class PackitAPI:
         self._up = None
         self._dg = None
         self._copr_helper: Optional[CoprHelper] = None
+        self._kerberos_initialized = False
 
     @property
     def up(self):
@@ -89,6 +90,7 @@ class PackitAPI:
     @property
     def dg(self):
         if self._dg is None:
+            self.init_kerberos_ticket()
             self._dg = DistGit(
                 config=self.config,
                 package_config=self.package_config,
@@ -748,6 +750,17 @@ class PackitAPI:
     def init_kerberos_ticket(self) -> None:
         """
         Initialize the kerberos ticket if we have fas_user and keytab_path configured.
+
+        The `kinit` command is run only once when called multiple times.
+        """
+        if self._kerberos_initialized:
+            return
+        self._run_kinit()
+        self._kerberos_initialized = True
+
+    def _run_kinit(self) -> None:
+        """
+        Run `kinit` if we have fas_user and keytab_path configured.
         """
         if (
             not self.config.fas_user
diff --git a/packit/distgit.py b/packit/distgit.py
index 57af2a64..c8c1f761 100644
--- a/packit/distgit.py
+++ b/packit/distgit.py
@@ -1,7 +1,6 @@
 # MIT License
 #
 # Copyright (c) 2018-2019 Red Hat, Inc.
-
 # Permission is hereby granted, free of charge, to any person obtaining a copy
 # of this software and associated documentation files (the "Software"), to deal
 # in the Software without restriction, including without limitation the rights
@@ -19,16 +18,16 @@
 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 # SOFTWARE.
+
+import cccolutils
+import git
 import logging
 import os
+import requests
 import tempfile
 from pathlib import Path
 from typing import Optional, Sequence, List
 
-import cccolutils
-import git
-import requests
-
 from ogr.abstract import PullRequest
 from packit.base_git import PackitRepositoryBase
 from packit.config import (
@@ -92,7 +91,6 @@ class DistGit(PackitRepositoryBase):
             else:
                 tmpdir = tempfile.mkdtemp(prefix="packit-dist-git")
                 f = FedPKG(fas_username=self.fas_user, directory=tmpdir)
-                f.init_ticket(self.config.keytab_path)
                 f.clone(
                     self.package_config.downstream_package_name,
                     tmpdir,
diff --git a/tests/integration/test_build.py b/tests/integration/test_build.py
index da52997e..087e3b4e 100644
--- a/tests/integration/test_build.py
+++ b/tests/integration/test_build.py
@@ -31,12 +31,14 @@ def test_basic_build(
     cwd_upstream_or_distgit, api_instance, mock_remote_functionality_upstream
 ):
     u, d, api = api_instance
+    flexmock(api).should_receive("init_kerberos_ticket").at_least().once()
     flexmock(utils).should_receive("run_command_remote").with_args(
         cmd=["fedpkg", "build", "--scratch", "--nowait", "--target", "asdqwe"],
         cwd=api.dg.local_project.working_dir,
         error_message="Submission of build to koji failed.",
         fail=True,
     ).once()
+
     api.build("master", scratch=True, nowait=True, koji_target="asdqwe")
 
 
diff --git a/tests/integration/test_create_update.py b/tests/integration/test_create_update.py
index 908754bb..2fb9fde8 100644
--- a/tests/integration/test_create_update.py
+++ b/tests/integration/test_create_update.py
@@ -290,6 +290,7 @@ def test_basic_bodhi_update(
     from bodhi.client.bindings import BodhiClient
 
     u, d, api = api_instance
+    flexmock(api).should_receive("init_kerberos_ticket").at_least().once()
 
     flexmock(
         BodhiClient,
diff --git a/tests/integration/test_get_api.py b/tests/integration/test_get_api.py
index c1b071ff..5f00364d 100644
--- a/tests/integration/test_get_api.py
+++ b/tests/integration/test_get_api.py
@@ -3,6 +3,7 @@ from pathlib import Path
 import pytest
 from flexmock import flexmock
 
+from packit.api import PackitAPI
 from packit.cli import utils
 from packit.cli.utils import get_packit_api
 from packit.local_project import LocalProject
@@ -54,11 +55,29 @@ def test_url_is_upstream():
 @pytest.mark.parametrize(
     "remotes,package_config,is_upstream",
     [
-        ([], flexmock(upstream_project_url=None, dist_git_base_url=None), True),
-        ([], flexmock(upstream_project_url="some-url", dist_git_base_url=None), True),
+        (
+            [],
+            flexmock(
+                upstream_project_url=None, dist_git_base_url=None, synced_files=None
+            ),
+            True,
+        ),
+        (
+            [],
+            flexmock(
+                upstream_project_url="some-url",
+                dist_git_base_url=None,
+                synced_files=None,
+            ),
+            True,
+        ),
         (
             [("origin", "https://github.com/packit-service/ogr.git")],
-            flexmock(upstream_project_url="some-url", dist_git_base_url=None),
+            flexmock(
+                upstream_project_url="some-url",
+                dist_git_base_url=None,
+                synced_files=None,
+            ),
             True,
         ),
         (
@@ -66,6 +85,7 @@ def test_url_is_upstream():
             flexmock(
                 upstream_project_url="https://github.com/packit-service/ogr",
                 dist_git_base_url=None,
+                synced_files=None,
             ),
             True,
         ),
@@ -74,6 +94,7 @@ def test_url_is_upstream():
             flexmock(
                 upstream_project_url="https://github.com/packit-service/ogr",
                 dist_git_base_url=None,
+                synced_files=None,
             ),
             True,
         ),
@@ -82,6 +103,7 @@ def test_url_is_upstream():
             flexmock(
                 upstream_project_url="https://github.com/packit-service/ogr",
                 dist_git_base_url="https://src.fedoraproject.org",
+                synced_files=None,
             ),
             False,
         ),
@@ -90,6 +112,7 @@ def test_url_is_upstream():
             flexmock(
                 upstream_project_url="https://github.com/packit-service/ogr",
                 dist_git_base_url="src.fedoraproject.org",
+                synced_files=None,
             ),
             False,
         ),
@@ -98,6 +121,7 @@ def test_url_is_upstream():
             flexmock(
                 upstream_project_url=None,
                 dist_git_base_url="https://src.fedoraproject.org",
+                synced_files=None,
             ),
             False,
         ),
@@ -106,6 +130,7 @@ def test_url_is_upstream():
             flexmock(
                 upstream_project_url=None,
                 dist_git_base_url="https://src.fedoraproject.org",
+                synced_files=None,
             ),
             False,
         ),
@@ -114,6 +139,7 @@ def test_url_is_upstream():
             flexmock(
                 upstream_project_url="https://github.com/packit-service/ogr",
                 dist_git_base_url="https://src.fedoraproject.org",
+                synced_files=None,
             ),
             True,
         ),
@@ -125,6 +151,7 @@ def test_url_is_upstream():
             flexmock(
                 upstream_project_url="https://github.com/packit-service/ogr",
                 dist_git_base_url="https://src.fedoraproject.org",
+                synced_files=None,
             ),
             True,
         ),
@@ -147,4 +174,6 @@ def test_get_api(tmpdir, remotes, package_config, is_upstream):
     if is_upstream:
         assert api.upstream_local_project
     else:
+        flexmock(PackitAPI).should_receive("_run_kinit").once()
         assert api.downstream_local_project
+        assert api.dg
diff --git a/tests/integration/test_update.py b/tests/integration/test_update.py
index 48dccb4e..359da678 100644
--- a/tests/integration/test_update.py
+++ b/tests/integration/test_update.py
@@ -61,6 +61,7 @@ def test_basic_local_update(
     """ basic propose-update test: mock remote API, use local upstream and dist-git """
     u, d, api = api_instance
     mock_spec_download_remote_s(d)
+    flexmock(api).should_receive("init_kerberos_ticket").at_least().once()
 
     api.sync_release("master", "0.1.0")