-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create run using config from repo (#466)
Neither OTF (nor TFC) currently permits, in a single API call, creating a run using configuration from a VCS repo (assuming the run's workspace is connected to a VCS repo). Instead the user/client has to manually invoke several operations: 1. retrieve tarball of vcs repo contents 2. create config version and upload tarball to config version 3. create run, specifying aforementioned config version Step 1 can be particularly onerous for the client, because connectivity and credentials are needed to access the repo, as well as necessary tooling, (curl, git, or equivalent libs in a programmatic context). This PR introduces a "magic string", `__pull_vcs__`, that if specified in place of the configuration version ID when creating the run, instructs OTF to perform those operations on behalf of the client. Using a magic string rather than introducing another API endpoint means the existing APIs and SDKs can be used, including `go-tfe`. Note: I've also taken this opportunity to refactor out the "run starter", which has been folded into the "run factory".
- Loading branch information
Showing
16 changed files
with
348 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package integration | ||
|
||
import ( | ||
"testing" | ||
|
||
tfe "github.com/hashicorp/go-tfe" | ||
"github.com/leg100/otf/internal" | ||
"github.com/leg100/otf/internal/cloud" | ||
"github.com/leg100/otf/internal/github" | ||
"github.com/leg100/otf/internal/run" | ||
"github.com/leg100/otf/internal/testutils" | ||
"github.com/leg100/otf/internal/workspace" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestIntegration_RunAPI tests those parts of the run API that are not covered | ||
// by the `go-tfe` integration tests, i.e. behaviours that are specific to | ||
// OTF. | ||
func TestIntegration_RunAPI(t *testing.T) { | ||
t.Parallel() | ||
|
||
// setup daemon along with fake github repo | ||
repo := cloud.NewTestRepo() | ||
daemon := setup(t, nil, | ||
github.WithRepo(repo), | ||
github.WithArchive(testutils.ReadFile(t, "../testdata/github.tar.gz")), | ||
) | ||
org := daemon.createOrganization(t, ctx) | ||
sub := daemon.createSubscriber(t, ctx) | ||
_, token := daemon.createToken(t, ctx, nil) | ||
|
||
tfeClient, err := tfe.NewClient(&tfe.Config{ | ||
Address: "https://" + daemon.Hostname(), | ||
Token: string(token), | ||
RetryServerErrors: true, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// test the "magic string" behaviour specific to OTF: if | ||
// run.PullVCSMagicString is specified for the config version ID then the | ||
// config is pulled from the workspace's connected repo. | ||
t.Run("create run using config from repo", func(t *testing.T) { | ||
vcsProvider := daemon.createVCSProvider(t, ctx, org) | ||
ws, err := daemon.CreateWorkspace(ctx, workspace.CreateOptions{ | ||
Name: internal.String("connected-workspace"), | ||
Organization: internal.String(org.Name), | ||
ConnectOptions: &workspace.ConnectOptions{ | ||
RepoPath: repo, | ||
VCSProviderID: vcsProvider.ID, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
created, err := tfeClient.Runs.Create(ctx, tfe.RunCreateOptions{ | ||
ConfigurationVersion: &tfe.ConfigurationVersion{ | ||
ID: run.PullVCSMagicString, | ||
}, | ||
Workspace: &tfe.Workspace{ | ||
ID: ws.ID, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// wait for run to reach planned status | ||
for event := range sub { | ||
if r, ok := event.Payload.(*run.Run); ok { | ||
switch r.Status { | ||
case internal.RunErrored: | ||
t.Fatal("run unexpectedly errored") | ||
case internal.RunPlanned: | ||
// run should have planned two resources (defined in the config from the | ||
// github repo) | ||
planned, err := daemon.GetRun(ctx, created.ID) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, 2, planned.Plan.Additions) | ||
return // success | ||
} | ||
} | ||
} | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.