Skip to content
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

Fix e2e tests on k8s environment #155

Merged
merged 1 commit into from
Jul 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions test/util/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"

sriovv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
testclient "github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/client"
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/namespaces"
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/nodes"
"github.com/k8snetworkplumbingwg/sriov-network-operator/test/util/pod"
)
Expand Down Expand Up @@ -50,6 +50,11 @@ func DiscoverSriov(clients *testclient.ClientSet, operatorNamespace string) (*En
return nil, fmt.Errorf("Failed to find matching node states %v", err)
}

err = sriovv1.InitNicIdMap(kubernetes.NewForConfigOrDie(clients.Config), operatorNamespace)
if err != nil {
return nil, fmt.Errorf("Failed to InitNicIdMap %v", err)
}

for _, state := range ss {
isStable, err := stateStable(state)
if err != nil {
Expand All @@ -70,7 +75,7 @@ func DiscoverSriov(clients *testclient.ClientSet, operatorNamespace string) (*En
}

for _, node := range res.Nodes {
isSecureBootEnabled, err := GetNodeSecureBootState(clients, node)
isSecureBootEnabled, err := GetNodeSecureBootState(clients, node, operatorNamespace)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -263,22 +268,23 @@ func SetDisableNodeDrainState(clients *testclient.ClientSet, operatorNamespace s
return nil
}

func GetNodeSecureBootState(clients *testclient.ClientSet, nodeName string) (bool, error) {
func GetNodeSecureBootState(clients *testclient.ClientSet, nodeName, namespace string) (bool, error) {
podDefinition := pod.GetDefinition()
podDefinition = pod.RedefineWithNodeSelector(podDefinition, nodeName)
podDefinition = pod.RedefineAsPrivileged(podDefinition)
podDefinition.Namespace = namespace

volume := corev1.Volume{Name: "host", VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: "/"}}}
mount := corev1.VolumeMount{Name: "host", MountPath: "/host"}
podDefinition = pod.RedefineWithMount(podDefinition, volume, mount)
created, err := clients.Pods(namespaces.Test).Create(context.Background(), podDefinition, metav1.CreateOptions{})
created, err := clients.Pods(namespace).Create(context.Background(), podDefinition, metav1.CreateOptions{})
if err != nil {
return false, err
}

var runningPod *corev1.Pod
err = wait.PollImmediate(time.Second, 3*time.Minute, func() (bool, error) {
runningPod, err = clients.Pods(namespaces.Test).Get(context.Background(), created.Name, metav1.GetOptions{})
runningPod, err = clients.Pods(namespace).Get(context.Background(), created.Name, metav1.GetOptions{})
if err != nil {
return false, err
}
Expand All @@ -293,14 +299,14 @@ func GetNodeSecureBootState(clients *testclient.ClientSet, nodeName string) (boo
return false, err
}

stdout, stderr, err := pod.ExecCommand(clients, runningPod, "cat", "/host/sys/kernel/security/lockdown")
stdout, _, err := pod.ExecCommand(clients, runningPod, "cat", "/host/sys/kernel/security/lockdown")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove the stderr check. I still can failed for other reason

Copy link
Collaborator Author

@ykulazhenkov ykulazhenkov Jul 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is useless. Stderr will be always empty.

TTY option set to true that mean that stdout will be used for stderr also.

Check Goclient src

Also, If command completes with non zero exit code, then err will be not nil, so check for error should be enough to catch all unexpected failures.


if strings.Contains(stdout, "No such file or directory") {
return false, nil
}
if err != nil {
return false, err
}

if stderr != "" {
return false, fmt.Errorf("command return non 0 code: %s", stderr)
}

return strings.Contains(stdout, "[integrity]") || strings.Contains(stdout, "[confidentiality]"), nil
}