github.com/tilt-dev/tilt@v0.36.0/integration/event_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"os/exec"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/pkg/errors"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func getNodeName(f *k8sFixture) string {
    18  	cmd := exec.Command("kubectl", "get", "nodes", "-o", "jsonpath={.items[*].metadata.name}")
    19  	out, err := cmd.CombinedOutput()
    20  	if err != nil {
    21  		f.t.Fatal(errors.Wrap(err, "get nodes"))
    22  	}
    23  
    24  	nodeName := strings.TrimSpace(string(out))
    25  	assert.NotEqual(f.t, "", nodeName)
    26  	return nodeName
    27  }
    28  
    29  func markNodeUnschedulable(f *k8sFixture, name string) {
    30  	f.runOrFail(
    31  		exec.Command("kubectl", "taint", "nodes", name, "key=value:NoSchedule", "--overwrite"),
    32  		"markNodeUnschedulable")
    33  }
    34  
    35  func markNodeSchedulable(f *k8sFixture, name string) {
    36  	// There is no idempotent way to remove a taint.
    37  	// If the taint doesn't exist, removing the taint will fail. This is dumb.
    38  	// But you can use --overwrite to add a taint idempotently, then remove it :eyeroll:
    39  	markNodeUnschedulable(f, name)
    40  	f.runOrFail(
    41  		exec.Command("kubectl", "taint", "nodes", name, "key:NoSchedule-"),
    42  		"markNodeSchedulable")
    43  }
    44  
    45  func TestEvent(t *testing.T) {
    46  	f := newK8sFixture(t, "event")
    47  
    48  	node := getNodeName(f)
    49  	markNodeUnschedulable(f, node)
    50  	defer markNodeSchedulable(f, node)
    51  
    52  	f.TiltUp()
    53  
    54  	ctx, cancel := context.WithTimeout(f.ctx, time.Minute)
    55  	defer cancel()
    56  	f.WaitUntil(ctx, "unschedulable pod event", func() (string, error) {
    57  		logs := strings.Split(f.logs.String(), "\n")
    58  		for _, log := range logs {
    59  			if strings.Contains(log, "[event") &&
    60  				(strings.Contains(log, "the pod didn't tolerate") || strings.Contains(log, "had untolerated taint")) {
    61  				return "unschedulable event", nil
    62  			}
    63  		}
    64  
    65  		return "", nil
    66  	}, "unschedulable event")
    67  
    68  	markNodeSchedulable(f, node)
    69  
    70  	// Make sure that the pod schedules successfully
    71  	ctx, cancel = context.WithTimeout(f.ctx, time.Minute)
    72  	defer cancel()
    73  	f.CurlUntil(ctx, "http://localhost:31234", "Hello world")
    74  }