MustWaitForProvisionersAvailable waits for provisioners to be available for a specific workspace.
(t *testing.T, db database.Store, workspace codersdk.Workspace, ts time.Time)
| 1876 | |
| 1877 | // MustWaitForProvisionersAvailable waits for provisioners to be available for a specific workspace. |
| 1878 | func MustWaitForProvisionersAvailable(t *testing.T, db database.Store, workspace codersdk.Workspace, ts time.Time) uuid.UUID { |
| 1879 | t.Helper() |
| 1880 | ctx := ctxWithProvisionerPermissions(testutil.Context(t, testutil.WaitLong)) |
| 1881 | id := uuid.UUID{} |
| 1882 | // Get the workspace from the database |
| 1883 | testutil.Eventually(ctx, t, func(ctx context.Context) (done bool) { |
| 1884 | ws, err := db.GetWorkspaceByID(ctx, workspace.ID) |
| 1885 | if err != nil { |
| 1886 | return false |
| 1887 | } |
| 1888 | |
| 1889 | // Get the latest build |
| 1890 | latestBuild, err := db.GetWorkspaceBuildByID(ctx, workspace.LatestBuild.ID) |
| 1891 | if err != nil { |
| 1892 | return false |
| 1893 | } |
| 1894 | |
| 1895 | // Get the template version job |
| 1896 | templateVersionJob, err := db.GetProvisionerJobByID(ctx, latestBuild.JobID) |
| 1897 | if err != nil { |
| 1898 | return false |
| 1899 | } |
| 1900 | |
| 1901 | // Check if provisioners are available using the same logic as hasAvailableProvisioners |
| 1902 | provisionerDaemons, err := db.GetProvisionerDaemonsByOrganization(ctx, database.GetProvisionerDaemonsByOrganizationParams{ |
| 1903 | OrganizationID: ws.OrganizationID, |
| 1904 | WantTags: templateVersionJob.Tags, |
| 1905 | }) |
| 1906 | if err != nil { |
| 1907 | return false |
| 1908 | } |
| 1909 | |
| 1910 | // Check if any provisioners are active (not stale) |
| 1911 | for _, pd := range provisionerDaemons { |
| 1912 | if pd.LastSeenAt.Valid { |
| 1913 | age := ts.Sub(pd.LastSeenAt.Time) |
| 1914 | if age <= provisionerdserver.StaleInterval { |
| 1915 | id = pd.ID |
| 1916 | return true // Found an active provisioner |
| 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | return false // No active provisioners found |
| 1921 | }, testutil.IntervalFast, "no active provisioners available for workspace, expected at least one (non-stale)") |
| 1922 | |
| 1923 | return id |
| 1924 | } |