-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add basic smoketest functionality #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
siherrmann
wants to merge
12
commits into
main
Choose a base branch
from
smoketest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0f6b0ef
feat: add basic smoketest functionality
siherrmann f349e88
chore(docs): Auto-update docs and licenses
siherrmann 055c657
update: seperate steps from command with new interface
siherrmann 3f3ba55
Merge branch 'main' into smoketest
siherrmann 564542b
chore(docs): Auto-update docs and licenses
siherrmann 723a07c
chore(docs): Auto-update docs and licenses
siherrmann dca295c
fix: remove unused function
siherrmann b96e331
Merge branch 'smoketest' of https://github.com/codesphere-cloud/oms i…
siherrmann 73f72a2
Merge branch 'main' into smoketest
siherrmann 4496924
chore(docs): Auto-update docs and licenses
siherrmann 5d46ee2
chore(docs): Auto-update docs and licenses
siherrmann 2fea3c5
Merge branch 'main' into smoketest
siherrmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,27 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/codesphere-cloud/cs-go/pkg/io" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // SmoketestCmd represents the smoketest command | ||
| type SmoketestCmd struct { | ||
| cmd *cobra.Command | ||
| } | ||
|
|
||
| func AddSmoketestCmd(rootCmd *cobra.Command, opts *GlobalOptions) { | ||
| smoketest := SmoketestCmd{ | ||
| cmd: &cobra.Command{ | ||
| Use: "smoketest", | ||
| Short: "Run smoke tests for Codesphere components", | ||
| Long: io.Long(`Run automated smoke tests for Codesphere installations to verify functionality.`), | ||
| }, | ||
| } | ||
| rootCmd.AddCommand(smoketest.cmd) | ||
|
|
||
| AddSmoketestCodesphereCmd(smoketest.cmd, opts) | ||
| } |
This file contains hidden or 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,169 @@ | ||
| // Copyright (c) Codesphere Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
| "slices" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/codesphere-cloud/cs-go/pkg/io" | ||
| "github.com/codesphere-cloud/oms/internal/codesphere" | ||
| "github.com/codesphere-cloud/oms/internal/codesphere/teststeps" | ||
| "github.com/codesphere-cloud/oms/internal/util" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultTimeout = 10 * time.Minute | ||
| defaultProfile = "ci.yml" | ||
| ) | ||
|
|
||
| var availableSteps = []teststeps.SmokeTestStep{ | ||
| &teststeps.CreateWorkspaceStep{}, | ||
| &teststeps.SetEnvVarStep{}, | ||
| &teststeps.CreateFilesStep{}, | ||
| &teststeps.SyncLandscapeStep{}, | ||
| &teststeps.StartPipelineStep{}, | ||
| &teststeps.DeleteWorkspaceStep{}, | ||
| } | ||
|
|
||
| type SmoketestCodesphereCmd struct { | ||
| cmd *cobra.Command | ||
| Opts *teststeps.SmoketestCodesphereOpts | ||
| } | ||
|
|
||
| func (c *SmoketestCodesphereCmd) RunE(_ *cobra.Command, args []string) error { | ||
| // Initialize client if not set (for testing) | ||
| if c.Opts.Client == nil { | ||
| client, err := codesphere.NewClient(c.Opts.BaseURL, c.Opts.Token) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create Codesphere client: %w", err) | ||
| } | ||
| c.Opts.Client = client | ||
| } | ||
|
|
||
| return c.RunSmoketest() | ||
| } | ||
|
|
||
| func AddSmoketestCodesphereCmd(parent *cobra.Command, opts *GlobalOptions) { | ||
| var stepNames []string | ||
| for _, s := range availableSteps { | ||
| stepNames = append(stepNames, s.Name()) | ||
| } | ||
|
|
||
| c := SmoketestCodesphereCmd{ | ||
| cmd: &cobra.Command{ | ||
| Use: "codesphere", | ||
| Short: "Run smoke tests for a Codesphere installation", | ||
| Long: io.Long(`Run automated smoke tests for a Codesphere installation by creating a workspace, | ||
| setting environment variables, executing commands, syncing landscape, and running a pipeline stage. | ||
| The workspace is automatically deleted after the test completes.`), | ||
| Example: formatExamplesWithBinary("smoketest codesphere", []io.Example{ | ||
| { | ||
| Cmd: "--baseurl https://codesphere.example.com/api --token YOUR_TOKEN --team-id TEAM_ID --plan-id PLAN_ID", | ||
| Desc: "Run smoke tests against a Codesphere installation", | ||
| }, | ||
| { | ||
| Cmd: "--baseurl https://codesphere.example.com/api --token YOUR_TOKEN --team-id TEAM_ID --plan-id PLAN_ID --quiet", | ||
| Desc: "Run smoke tests in quiet mode (no progress logging)", | ||
| }, | ||
| { | ||
| Cmd: "--baseurl https://codesphere.example.com/api --token YOUR_TOKEN --team-id TEAM_ID --plan-id PLAN_ID --timeout 15m", | ||
| Desc: "Run smoke tests with custom timeout", | ||
| }, | ||
| { | ||
| Cmd: "--baseurl https://codesphere.example.com/api --token YOUR_TOKEN --team-id TEAM_ID --plan-id PLAN_ID --steps createWorkspace,syncLandscape", | ||
| Desc: "Run only specific steps of the smoke test (workspace won't be deleted)", | ||
| }, | ||
| { | ||
| Cmd: "--baseurl https://codesphere.example.com/api --token YOUR_TOKEN --team-id TEAM_ID --plan-id PLAN_ID --steps createWorkspace,syncLandscape,deleteWorkspace", | ||
| Desc: "Run specific steps and delete the workspace afterwards", | ||
| }, | ||
| }, "oms-cli"), | ||
| }, | ||
| Opts: &teststeps.SmoketestCodesphereOpts{}, | ||
| } | ||
| c.cmd.Flags().StringVar(&c.Opts.BaseURL, "baseurl", "", "Base URL of the Codesphere API") | ||
| c.cmd.Flags().StringVar(&c.Opts.Token, "token", "", "API token for authentication") | ||
| c.cmd.Flags().StringVar(&c.Opts.TeamID, "team-id", "", "Team ID for workspace creation") | ||
| c.cmd.Flags().StringVar(&c.Opts.PlanID, "plan-id", "", "Plan ID for workspace creation") | ||
| c.cmd.Flags().BoolVarP(&c.Opts.Quiet, "quiet", "q", false, "Suppress progress logging") | ||
| c.cmd.Flags().DurationVar(&c.Opts.Timeout, "timeout", defaultTimeout, "Timeout for the entire smoke test") | ||
| c.cmd.Flags().StringVar(&c.Opts.Profile, "profile", defaultProfile, "CI profile to use for landscape and pipeline") | ||
| c.cmd.Flags().StringSliceVar(&c.Opts.Steps, "steps", []string{}, fmt.Sprintf("Comma-separated list of steps to run (%s). If empty, all steps including deleteWorkspace are run. If specified without deleteWorkspace, the workspace will be kept for manual inspection.", strings.Join(stepNames, ","))) | ||
|
|
||
| util.MarkFlagRequired(c.cmd, "baseurl") | ||
| util.MarkFlagRequired(c.cmd, "token") | ||
| util.MarkFlagRequired(c.cmd, "team-id") | ||
| util.MarkFlagRequired(c.cmd, "plan-id") | ||
|
|
||
| c.cmd.RunE = c.RunE | ||
|
|
||
| parent.AddCommand(c.cmd) | ||
| } | ||
|
|
||
| func (c *SmoketestCodesphereCmd) RunSmoketest() (err error) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), c.Opts.Timeout) | ||
| defer cancel() | ||
|
|
||
| availableStepsMap := make(map[string]teststeps.SmokeTestStep) | ||
| for _, s := range availableSteps { | ||
| availableStepsMap[s.Name()] = s | ||
| } | ||
|
|
||
| stepsToRun := make([]teststeps.SmokeTestStep, len(availableSteps)) | ||
| copy(stepsToRun, availableSteps) | ||
|
|
||
| if len(c.Opts.Steps) > 0 { | ||
| stepsToRun = slices.DeleteFunc(stepsToRun, func(s teststeps.SmokeTestStep) bool { | ||
| return !slices.Contains(c.Opts.Steps, s.Name()) | ||
| }) | ||
| } | ||
|
|
||
| var workspaceID int | ||
| deleteStep := &teststeps.DeleteWorkspaceStep{} | ||
| defer func() { | ||
| if err != nil { | ||
| log.Printf("Smoketest failed: %s", err.Error()) | ||
| } | ||
|
|
||
| shouldDelete := false | ||
| for _, s := range stepsToRun { | ||
| if s.Name() == deleteStep.Name() { | ||
| shouldDelete = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if workspaceID != 0 && shouldDelete { | ||
| deleteErr := deleteStep.Run(context.Background(), c.Opts, &workspaceID) | ||
| if deleteErr != nil { | ||
| if err == nil { | ||
| err = deleteErr | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if err == nil { | ||
| log.Println("Smoketest completed successfully!") | ||
| } | ||
| }() | ||
|
|
||
| // Execute steps | ||
| for _, step := range stepsToRun { | ||
| // Skip deleteWorkspace in the main loop as it's handled in defer | ||
| if step.Name() == deleteStep.Name() { | ||
| continue | ||
| } | ||
| if err = step.Run(ctx, c.Opts, &workspaceID); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when would this be not the case?