-
Notifications
You must be signed in to change notification settings - Fork 212
NO-JIRA: Introduce oc-cli for e2e tests #1267
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package api | ||
|
|
||
| type ReleaseExtractOptions struct { | ||
| To string | ||
| } | ||
|
|
||
| type VersionOptions struct { | ||
| Client bool | ||
| } | ||
|
|
||
| type OC interface { | ||
| AdmReleaseExtract(o ReleaseExtractOptions) error | ||
| Version(o VersionOptions) (string, error) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/go-logr/logr" | ||
|
|
||
| "github.com/openshift/cluster-version-operator/test/oc/api" | ||
| ) | ||
|
|
||
| type client struct { | ||
| logger logr.Logger | ||
| executor executor | ||
| } | ||
|
|
||
| type executor interface { | ||
| Run(args ...string) ([]byte, error) | ||
| } | ||
|
|
||
| type ocExecutor struct { | ||
| // logger is used to log oc commands | ||
| logger logr.Logger | ||
| // oc is the path to the oc binary | ||
| oc string | ||
| // execute executes a command | ||
| execute func(dir, command string, args ...string) ([]byte, error) | ||
| } | ||
|
|
||
| func (e *ocExecutor) Run(args ...string) ([]byte, error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not implement
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is my way/habit to layer things up. Let us see if everyone is OK with this way, building up
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my experience, interfaces should only be introduced when there are many different instances, but for the CVO testing, there is only one OC client (we will not use client-go), so the interfaces are not necessary. Leave it for @DavidHurta @jhou1 @jiajliu @shahsahil264 to confirm as you will use it in the future. |
||
| logger := e.logger.WithValues("cmd", e.oc, "args", strings.Join(args, " ")) | ||
| b, err := e.execute("", e.oc, args...) | ||
| if err != nil { | ||
| logger.Error(err, "Running command failed", "output", string(b)) | ||
| } else { | ||
| logger.Info("Running command succeeded.") | ||
| } | ||
| return b, err | ||
| } | ||
|
|
||
| func newOCExecutor(oc string, timeout time.Duration, logger logr.Logger) (executor, error) { | ||
| return &ocExecutor{ | ||
| logger: logger, | ||
| oc: oc, | ||
| execute: func(dir, command string, args ...string) ([]byte, error) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
| defer cancel() | ||
| c := exec.CommandContext(ctx, command, args...) | ||
| c.Dir = dir | ||
| o, err := c.CombinedOutput() | ||
| if errors.Is(ctx.Err(), context.DeadlineExceeded) { | ||
| return o, fmt.Errorf("execution timed out after %s: %w", timeout.String(), ctx.Err()) | ||
| } | ||
| return o, err | ||
| }, | ||
| }, nil | ||
| } | ||
|
|
||
| // NewOCCli return a client for oc-cli. | ||
| func NewOCCli(logger logr.Logger) (api.OC, error) { | ||
| oc, err := exec.LookPath("oc") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| timeout := 30 * time.Second | ||
| timeoutStr := os.Getenv("OC_CLI_TIMEOUT") | ||
hongkailiu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if timeoutStr != "" { | ||
| timeout, err = time.ParseDuration(timeoutStr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| executor, err := newOCExecutor(oc, timeout, logger) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| ret := client{ | ||
| logger: logger, | ||
| executor: executor, | ||
| } | ||
| return &ret, nil | ||
| } | ||
|
|
||
| func (c *client) AdmReleaseExtract(o api.ReleaseExtractOptions) error { | ||
hongkailiu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| args := []string{"adm", "release", "extract", fmt.Sprintf("--to=%s", o.To)} | ||
| _, err := c.executor.Run(args...) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *client) Version(o api.VersionOptions) (string, error) { | ||
| args := []string{"version", fmt.Sprintf("--client=%t", o.Client)} | ||
| output, err := c.executor.Run(args...) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return string(output), nil | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package oc | ||
|
|
||
| import ( | ||
| "github.com/go-logr/logr" | ||
|
|
||
| "github.com/openshift/cluster-version-operator/test/oc/api" | ||
| "github.com/openshift/cluster-version-operator/test/oc/cli" | ||
| ) | ||
|
|
||
| // NewOC returns OC that provides utility functions used by the e2e tests | ||
| func NewOC(logger logr.Logger) (api.OC, error) { | ||
| return cli.NewOCCli(logger) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.