Skip to content

test/e2e: add MC smoke test #387

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

Merged
merged 1 commit into from
Feb 7, 2019
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
112 changes: 109 additions & 3 deletions test/e2e/mcd_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package e2e_test

import (
"testing"
"fmt"
"strings"
"testing"
"time"

ignv2_2types "github.com/coreos/ignition/config/v2_2/types"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/apimachinery/pkg/util/wait"

"github.com/openshift/machine-config-operator/cmd/common"
mcv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
)

// Test case for https://github.com/openshift/machine-config-operator/issues/358
func TestMCDToken(t *testing.T) {
cb, err := common.NewClientBuilder("")
if err != nil{
if err != nil {
t.Errorf("%#v", err)
}
k := cb.KubeClientOrDie("sanity-test")
k := cb.KubeClientOrDie("mcd-token-test")

listOptions := metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{"k8s-app": "machine-config-daemon"}).String(),
Expand All @@ -40,3 +46,103 @@ func TestMCDToken(t *testing.T) {
}
}
}

func mcLabelForWorkers() map[string]string {
mcLabels := make(map[string]string)
mcLabels["machineconfiguration.openshift.io/role"] = "worker"
return mcLabels
}

func createMCFile(path, content string, mode int) ignv2_2types.File {
return ignv2_2types.File{
FileEmbedded1: ignv2_2types.FileEmbedded1{
Contents: ignv2_2types.FileContents{
Source: content,
},
Mode: &mode,
},
Node: ignv2_2types.Node{
Filesystem: "root",
Path: path,
},
}
}

func TestMCDeployed(t *testing.T) {
cb, err := common.NewClientBuilder("")
if err != nil {
t.Errorf("%#v", err)
}
mcClient := cb.MachineConfigClientOrDie("mc-file-add")
k := cb.KubeClientOrDie("mc-file-add")

// create a dummy MC
mcName := fmt.Sprintf("00-0add-a-file-%s", uuid.NewUUID())
mcadd := &mcv1.MachineConfig{}
mcadd.ObjectMeta = metav1.ObjectMeta{
Name: mcName,
Labels: mcLabelForWorkers(),
}
mcadd.Spec = mcv1.MachineConfigSpec{
Config: ignv2_2types.Config{
Ignition: ignv2_2types.Ignition{
Version: "2.2.0",
},
Storage: ignv2_2types.Storage{
Files: []ignv2_2types.File{
createMCFile("/etc/mytestconf", "data:,test", 420),
},
},
},
}

// create the dummy MC now
_, err = mcClient.MachineconfigurationV1().MachineConfigs().Create(mcadd)
if err != nil {
t.Errorf("failed to create machine config %v", err)
}

// grab the latest worker- MC
var newMCName string
err = wait.Poll(2*time.Second, 5*time.Minute, func() (bool, error) {
mcp, err := mcClient.MachineconfigurationV1().MachineConfigPools().Get("worker", metav1.GetOptions{})
if err != nil {
return false, err
}
for _, mc := range mcp.Status.Configuration.Source {
if mc.Name == mcName {
newMCName = mcp.Status.Configuration.Name
return true, nil
}
}
return false, nil
})

listOptions := metav1.ListOptions{
LabelSelector: labels.SelectorFromSet(labels.Set{"k8s-app": "machine-config-daemon"}).String(),
}

err = wait.Poll(3*time.Second, 5*time.Minute, func() (bool, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Minor: hmm, so this is going over every log of every pod? Could we make this smarter by only going over pods on worker nodes?

Copy link
Member Author

Choose a reason for hiding this comment

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

I could do that yeah

Copy link
Member

Choose a reason for hiding this comment

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

It's also only the MCD pods right?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah, should be #387 (diff)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, true. So every log of every MCD pod, rather than just worker MCD pods. Guess if it's not hard to select for only worker nodes, it'd be a nice addition, otherwise meh!

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm checking that, but MCDs aren't referencing worker or master explicitly so it's gonna be a great dance to get worker nodes and then match on names, what do you think?

mcdList, err := k.CoreV1().Pods("openshift-machine-config-operator").List(listOptions)
if err != nil {
return false, err
}

for _, pod := range mcdList.Items {
res, err := k.CoreV1().Pods(pod.Namespace).GetLogs(pod.Name, &v1.PodLogOptions{}).DoRaw()
if err != nil {
// do not error out, we may be rebooting, that's why we list at every iteration
return false, nil
}
for _, line := range strings.Split(string(res), "\n") {
if strings.Contains(line, "completed update for config "+newMCName) {
return true, nil
}
}
}
return false, nil
})
if err != nil {
t.Errorf("machine config didn't result in file being on any worker: %v", err)
}
}