Skip to content

Commit

Permalink
Introduce machinehealthcheck tests
Browse files Browse the repository at this point in the history
Tests:
- [Feature:MachineHealthCheck] MachineHealthCheck controller
  with node-unhealth-conditions configmap should delete unhealthy machine
- [Feature:MachineHealthCheck] MachineHealthCheck controller
  should delete unhealthy machine

Copy tests from the openshift/machine-api-operator#204
  • Loading branch information
Artyom Lukianov committed Mar 13, 2019
1 parent 0785ce8 commit d1ea871
Show file tree
Hide file tree
Showing 5 changed files with 372 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ vet: ## Apply go vet to all go files

.PHONY: test-e2e
test-e2e: ## Run openshift specific e2e test
go test -timeout 60m \
go test -timeout 90m \
-v github.com/openshift/cluster-api-actuator-pkg/pkg/e2e \
-kubeconfig $${KUBECONFIG:-~/.kube/config} \
-machine-api-namespace $${NAMESPACE:-openshift-machine-api} \
Expand Down
6 changes: 6 additions & 0 deletions pkg/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
osconfigv1 "github.com/openshift/api/config/v1"
mapiv1beta1 "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
caov1alpha1 "github.com/openshift/cluster-autoscaler-operator/pkg/apis"
healthcheckingv1alpha1 "github.com/openshift/machine-api-operator/pkg/apis/healthchecking/v1alpha1"
"k8s.io/client-go/kubernetes/scheme"

_ "github.com/openshift/cluster-api-actuator-pkg/pkg/e2e/actuators"
_ "github.com/openshift/cluster-api-actuator-pkg/pkg/e2e/autoscaler"
_ "github.com/openshift/cluster-api-actuator-pkg/pkg/e2e/infra"
_ "github.com/openshift/cluster-api-actuator-pkg/pkg/e2e/machinehealthcheck"
_ "github.com/openshift/cluster-api-actuator-pkg/pkg/e2e/operators"
)

Expand All @@ -30,6 +32,10 @@ func init() {
if err := osconfigv1.AddToScheme(scheme.Scheme); err != nil {
glog.Fatal(err)
}

if err := healthcheckingv1alpha1.AddToScheme(scheme.Scheme); err != nil {
glog.Fatal(err)
}
}

func TestE2E(t *testing.T) {
Expand Down
16 changes: 13 additions & 3 deletions pkg/e2e/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/golang/glog"

"github.com/openshift/cluster-api/pkg/client/clientset_generated/clientset"
healthcheckingclient "github.com/openshift/machine-api-operator/pkg/generated/clientset/versioned"
appsv1beta2 "k8s.io/api/apps/v1beta2"
corev1 "k8s.io/api/core/v1"
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
Expand Down Expand Up @@ -95,9 +96,11 @@ type SSHConfig struct {

// Framework supports common operations used by tests
type Framework struct {
KubeClient *kubernetes.Clientset
CAPIClient *clientset.Clientset
APIExtensionClient *apiextensionsclientset.Clientset
KubeClient *kubernetes.Clientset
CAPIClient *clientset.Clientset
APIExtensionClient *apiextensionsclientset.Clientset
HealthCheckingClient *healthcheckingclient.Clientset

// APIRegistrationClient *apiregistrationclientset.Clientset
Kubeconfig string
RestConfig *rest.Config
Expand Down Expand Up @@ -209,6 +212,13 @@ func (f *Framework) buildClientsets() error {
}
}

if f.HealthCheckingClient == nil {
f.HealthCheckingClient, err = healthcheckingclient.NewForConfig(f.RestConfig)
if err != nil {
return err
}
}

return nil
}

Expand Down
122 changes: 122 additions & 0 deletions pkg/e2e/framework/machinehealthcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package framework

import (
"context"

"github.com/ghodss/yaml"
healthcheckingv1alpha1 "github.com/openshift/machine-api-operator/pkg/apis/healthchecking/v1alpha1"
"github.com/openshift/machine-api-operator/pkg/util/conditions"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
)

const (
// KubeletKillerPodName contains the name of the pod that stops kubelet process
KubeletKillerPodName = "kubelet-killer"
// NodeWorkerLabel contains label that every worker node has
NodeWorkerLabel = "node-role.kubernetes.io/worker"
// MachineHealthCheckName contains the name of the machinehealthcheck used for tests
MachineHealthCheckName = "workers-check"
)

// CreateUnhealthyConditionsConfigMap creates node-unhealthy-conditions configmap with relevant conditions
func CreateUnhealthyConditionsConfigMap(unhealthyConditions *conditions.UnhealthyConditions) error {
client, err := LoadClient()
if err != nil {
return err
}

cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: TestContext.MachineApiNamespace,
Name: healthcheckingv1alpha1.ConfigMapNodeUnhealthyConditions,
},
}

conditionsData, err := yaml.Marshal(unhealthyConditions)
if err != nil {
return err
}

cm.Data = map[string]string{"conditions": string(conditionsData)}
return client.Create(context.TODO(), cm)
}

// DeleteUnhealthyConditionsConfigMap deletes node-unhealthy-conditions configmap
func DeleteUnhealthyConditionsConfigMap() error {
client, err := LoadClient()
if err != nil {
return err
}

key := types.NamespacedName{
Name: healthcheckingv1alpha1.ConfigMapNodeUnhealthyConditions,
Namespace: TestContext.MachineApiNamespace,
}
cm := &corev1.ConfigMap{}
err = client.Get(context.TODO(), key, cm)
if err != nil {
return err
}

return client.Delete(context.TODO(), cm)
}

// CreateMachineHealthCheck will create MachineHealthCheck CR with the relevant selector
func CreateMachineHealthCheck(labels map[string]string) error {
client, err := LoadClient()
if err != nil {
return err
}

mhc := &healthcheckingv1alpha1.MachineHealthCheck{
ObjectMeta: metav1.ObjectMeta{
Name: MachineHealthCheckName,
Namespace: TestContext.MachineApiNamespace,
},
Spec: healthcheckingv1alpha1.MachineHealthCheckSpec{
Selector: metav1.LabelSelector{
MatchLabels: labels,
},
},
}
return client.Create(context.TODO(), mhc)
}

// StopKubelet creates pod in the node PID namespace that stops kubelet process
func StopKubelet(nodeName string) error {
client, err := LoadClient()
if err != nil {
return err
}

_true := true
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: KubeletKillerPodName + rand.String(5),
Namespace: TestContext.MachineApiNamespace,
Labels: map[string]string{
KubeletKillerPodName: "",
},
},
Spec: corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyNever,
Containers: []corev1.Container{
{
Name: KubeletKillerPodName,
Image: "busybox",
Command: []string{"pkill", "-STOP", "hyperkube"},
SecurityContext: &corev1.SecurityContext{
Privileged: &_true,
},
},
},
NodeName: nodeName,
HostPID: true,
},
}
return client.Create(context.TODO(), pod)
}
Loading

0 comments on commit d1ea871

Please sign in to comment.