-
Notifications
You must be signed in to change notification settings - Fork 244
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
Add more tests for Podman + write Component abstraction for integration tests #6427
Merged
openshift-merge-robot
merged 4 commits into
redhat-developer:main
from
feloy:tests-6416/more-podman-tests
Dec 20, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,39 @@ | ||
package helper | ||
|
||
import ( | ||
"fmt" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
// ClusterComponent is an abstraction for a Devfile Component deployed on a cluster (either Kubernetes or OpenShift) | ||
type ClusterComponent struct { | ||
name string | ||
app string | ||
namespace string | ||
cli CliRunner | ||
} | ||
|
||
func NewClusterComponent(name string, app string, namespace string, cli CliRunner) *ClusterComponent { | ||
return &ClusterComponent{ | ||
name: name, | ||
app: app, | ||
namespace: namespace, | ||
cli: cli, | ||
} | ||
} | ||
|
||
func (o *ClusterComponent) ExpectIsNotDeployed() { | ||
deploymentName := fmt.Sprintf("%s-%s", o.name, o.app) | ||
stdout := o.cli.Run("get", "deployment", "-n", o.namespace).Out.Contents() | ||
Expect(string(stdout)).To(Not(ContainSubstring(deploymentName))) | ||
} | ||
|
||
func (o *ClusterComponent) Exec(container string, args ...string) string { | ||
podName := o.cli.GetRunningPodNameByComponent(o.name, o.namespace) | ||
return o.cli.Exec(podName, o.namespace, append([]string{"--"}, args...)...) | ||
} | ||
|
||
func (o *ClusterComponent) GetEnvVars() map[string]string { | ||
return o.cli.GetEnvsDevFileDeployment(o.name, o.app, o.namespace) | ||
} |
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,23 @@ | ||
package helper | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
) | ||
|
||
// Component is an abstraction for a Devfile Component deployed on a specific platform | ||
type Component interface { | ||
// ExpectIsNotDeployed checks that the component is not deployed | ||
ExpectIsNotDeployed() | ||
// Exec executes the command in specific container of the component | ||
Exec(container string, args ...string) string | ||
// GetEnvVars returns the environment variables defined for the component | ||
GetEnvVars() map[string]string | ||
} | ||
|
||
func NewComponent(name string, app string, namespace string, cli CliRunner) Component { | ||
if NeedsCluster(CurrentSpecReport().Labels()) { | ||
return NewClusterComponent(name, app, namespace, cli) | ||
} else { | ||
return NewPodmanComponent(name, app) | ||
} | ||
} |
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,75 @@ | ||
package helper | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
|
||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
jsonserializer "k8s.io/apimachinery/pkg/runtime/serializer/json" | ||
"k8s.io/kubectl/pkg/scheme" | ||
) | ||
|
||
// PodmanComponent is an abstraction for a Devfile Component deployed on podman | ||
type PodmanComponent struct { | ||
name string | ||
app string | ||
} | ||
|
||
func NewPodmanComponent(name string, app string) *PodmanComponent { | ||
return &PodmanComponent{ | ||
name: name, | ||
app: app, | ||
} | ||
} | ||
|
||
func (o *PodmanComponent) ExpectIsNotDeployed() { | ||
podName := fmt.Sprintf("%s-%s", o.name, o.app) | ||
cmd := exec.Command("podman", "pod", "list", "--format", "{{.Name}}", "--noheading") | ||
stdout, err := cmd.Output() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(string(stdout)).ToNot(ContainSubstring(podName)) | ||
} | ||
|
||
func (o *PodmanComponent) Exec(container string, args ...string) string { | ||
containerName := fmt.Sprintf("%s-%s-%s", o.name, o.app, container) | ||
cmdargs := []string{"exec", "--interactive"} | ||
cmdargs = append(cmdargs, "--tty") | ||
cmdargs = append(cmdargs, containerName) | ||
cmdargs = append(cmdargs, args...) | ||
|
||
command := exec.Command("podman", cmdargs...) | ||
fmt.Printf("exec %v\n", cmdargs) | ||
out, err := command.Output() | ||
Expect(err).ToNot(HaveOccurred()) | ||
return string(out) | ||
} | ||
|
||
func (o *PodmanComponent) GetEnvVars() map[string]string { | ||
podName := fmt.Sprintf("%s-%s", o.name, o.app) | ||
podDef := getPodDef(podName) | ||
res := map[string]string{} | ||
for _, env := range podDef.Spec.Containers[0].Env { | ||
res[env.Name] = env.Value | ||
} | ||
return res | ||
} | ||
|
||
func getPodDef(podname string) *corev1.Pod { | ||
serializer := jsonserializer.NewSerializerWithOptions( | ||
jsonserializer.SimpleMetaFactory{}, | ||
scheme.Scheme, | ||
scheme.Scheme, | ||
jsonserializer.SerializerOptions{ | ||
Yaml: true, | ||
}, | ||
) | ||
|
||
cmd := exec.Command("podman", "kube", "generate", podname) | ||
resultBytes, err := cmd.Output() | ||
Expect(err).ToNot(HaveOccurred()) | ||
var pod corev1.Pod | ||
_, _, err = serializer.Decode(resultBytes, nil, &pod) | ||
Expect(err).ToNot(HaveOccurred()) | ||
return &pod | ||
} |
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.
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.
Would it make sense to make the path to
podman
configurable (ideally using thePODMAN_CMD
env var just like with the internalPodmanCli
client)?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.
I'm not sure. This is used during integration tests only, on a stable environment, I cannot see a reason for the moment to have to configure it
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.
I agree from the environment perspective.
I thought it would make sense for an
odo
dev to make sure the same podman binary is used when running the actualodo
commands in the tests and when performing the assertions here..But okay, that's something that can be optimized later on.
/lgtm