(t *testing.T)
| 86 | } |
| 87 | |
| 88 | func TestReinitializeAgent(t *testing.T) { |
| 89 | t.Parallel() |
| 90 | |
| 91 | // Ensure that workspace agents can reinitialize against claimed prebuilds in non-default organizations: |
| 92 | for _, useDefaultOrg := range []bool{true, false} { |
| 93 | t.Run(fmt.Sprintf("useDefaultOrg=%t", useDefaultOrg), func(t *testing.T) { |
| 94 | t.Parallel() |
| 95 | |
| 96 | // Create the temp file in os.TempDir() rather than t.TempDir(). |
| 97 | // On Windows, t.TempDir() includes the test name which |
| 98 | // contains "=" (e.g. useDefaultOrg=true). The "=" in the |
| 99 | // path breaks both cmd.exe and powershell scripts, causing |
| 100 | // the startup script to exit 1 and the agent to never |
| 101 | // reach the ready lifecycle state. |
| 102 | tempAgentLog := testutil.CreateTemp(t, os.TempDir(), "testReinitializeAgent") |
| 103 | |
| 104 | // Dump environment variables to a temp file so we can verify |
| 105 | // CODER_AGENT_TOKEN appears twice (once per init). On Windows |
| 106 | // the agent runs scripts via powershell.exe /c, so we must |
| 107 | // use PowerShell-native commands. |
| 108 | var startupScript string |
| 109 | if runtime.GOOS == "windows" { |
| 110 | startupScript = fmt.Sprintf( |
| 111 | `[System.Environment]::GetEnvironmentVariables().GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" } | Add-Content -Path '%s'; '---' | Add-Content -Path '%s'`, |
| 112 | tempAgentLog.Name(), tempAgentLog.Name(), |
| 113 | ) |
| 114 | } else { |
| 115 | startupScript = fmt.Sprintf("printenv >> %s; echo '---\n' >> %s", tempAgentLog.Name(), tempAgentLog.Name()) |
| 116 | } |
| 117 | |
| 118 | db, ps := dbtestutil.NewDB(t) |
| 119 | // GIVEN a live enterprise API with the prebuilds feature enabled |
| 120 | client, user := coderdenttest.New(t, &coderdenttest.Options{ |
| 121 | Options: &coderdtest.Options{ |
| 122 | Database: db, |
| 123 | Pubsub: ps, |
| 124 | DeploymentValues: coderdtest.DeploymentValues(t, func(dv *codersdk.DeploymentValues) { |
| 125 | dv.Prebuilds.ReconciliationInterval = serpent.Duration(time.Second) |
| 126 | }), |
| 127 | }, |
| 128 | LicenseOptions: &coderdenttest.LicenseOptions{ |
| 129 | Features: license.Features{ |
| 130 | codersdk.FeatureWorkspacePrebuilds: 1, |
| 131 | codersdk.FeatureExternalProvisionerDaemons: 1, |
| 132 | }, |
| 133 | }, |
| 134 | }) |
| 135 | |
| 136 | orgID := user.OrganizationID |
| 137 | if !useDefaultOrg { |
| 138 | secondOrg := dbgen.Organization(t, db, database.Organization{}) |
| 139 | orgID = secondOrg.ID |
| 140 | } |
| 141 | provisionerCloser := coderdenttest.NewExternalProvisionerDaemon(t, client, orgID, map[string]string{ |
| 142 | provisionersdk.TagScope: provisionersdk.ScopeOrganization, |
| 143 | }) |
| 144 | defer provisionerCloser.Close() |
| 145 |
nothing calls this directly
no test coverage detected