|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/client-go/kubernetes" |
| 12 | + "k8s.io/client-go/pkg/api/v1" |
| 13 | + "k8s.io/client-go/tools/clientcmd" |
| 14 | +) |
| 15 | + |
| 16 | +// global client for use by all tests |
| 17 | +var client kubernetes.Interface |
| 18 | + |
| 19 | +// non-configurable for now |
| 20 | +const namespace = "bootkube-e2e-testing" |
| 21 | + |
| 22 | +// TestMain handles setup before all tests |
| 23 | +func TestMain(m *testing.M) { |
| 24 | + var ( |
| 25 | + kubeconfig = flag.String("kubeconfig", "../hack/quickstart/cluster/auth/kubeconfig", "absolute path to the kubeconfig file") |
| 26 | + ) |
| 27 | + flag.Parse() |
| 28 | + |
| 29 | + config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) |
| 30 | + if err != nil { |
| 31 | + fmt.Println(err) |
| 32 | + os.Exit(1) |
| 33 | + } |
| 34 | + // create the clientset |
| 35 | + client, err = kubernetes.NewForConfig(config) |
| 36 | + if err != nil { |
| 37 | + fmt.Println(err) |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + |
| 41 | + if err := ready(client); err != nil { |
| 42 | + fmt.Println(err) |
| 43 | + os.Exit(1) |
| 44 | + } |
| 45 | + |
| 46 | + // createNamespace |
| 47 | + if _, err := createNamespace(client, namespace); err != nil { |
| 48 | + fmt.Println(err) |
| 49 | + os.Exit(1) |
| 50 | + } |
| 51 | + |
| 52 | + // run tests |
| 53 | + exitCode := m.Run() |
| 54 | + |
| 55 | + if err := deleteNamespace(client, namespace); err != nil { |
| 56 | + fmt.Println(err) |
| 57 | + os.Exit(1) |
| 58 | + } |
| 59 | + |
| 60 | + os.Exit(exitCode) |
| 61 | +} |
| 62 | + |
| 63 | +func createNamespace(c kubernetes.Interface, name string) (*v1.Namespace, error) { |
| 64 | + namespace, err := c.CoreV1().Namespaces().Create(&v1.Namespace{ |
| 65 | + ObjectMeta: metav1.ObjectMeta{ |
| 66 | + Name: name, |
| 67 | + }, |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("failed to create namespace with name %v %v", name, namespace) |
| 71 | + } |
| 72 | + return namespace, nil |
| 73 | +} |
| 74 | + |
| 75 | +func deleteNamespace(c kubernetes.Interface, name string) error { |
| 76 | + return c.CoreV1().Namespaces().Delete(name, nil) |
| 77 | +} |
| 78 | + |
| 79 | +// Ready blocks until the cluster is considered available. The current |
| 80 | +// implementation checks that 1 schedulable node is ready. |
| 81 | +func ready(c kubernetes.Interface) error { |
| 82 | + f := func() error { |
| 83 | + list, err := c.CoreV1().Nodes().List(metav1.ListOptions{}) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + if len(list.Items) < 1 { |
| 89 | + return fmt.Errorf("cluster is not ready, waiting for 1 or more worker nodes: %v", len(list.Items)) |
| 90 | + } |
| 91 | + |
| 92 | + // check for 1 or more ready nodes by ignoring nodes marked |
| 93 | + // unschedulable or containing taints |
| 94 | + var oneReady bool |
| 95 | + for _, node := range list.Items { |
| 96 | + if node.Spec.Unschedulable { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + if len(node.Spec.Taints) != 0 { |
| 101 | + continue |
| 102 | + } |
| 103 | + |
| 104 | + for _, condition := range node.Status.Conditions { |
| 105 | + if condition.Type == v1.NodeReady { |
| 106 | + if condition.Status == v1.ConditionTrue { |
| 107 | + oneReady = true |
| 108 | + } |
| 109 | + break |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + if !oneReady { |
| 114 | + return fmt.Errorf("waiting for one worker node to be ready") |
| 115 | + } |
| 116 | + |
| 117 | + return nil |
| 118 | + } |
| 119 | + |
| 120 | + if err := retry(50, 10*time.Second, f); err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func retry(attempts int, delay time.Duration, f func() error) error { |
| 127 | + var err error |
| 128 | + |
| 129 | + for i := 0; i < attempts; i++ { |
| 130 | + err = f() |
| 131 | + if err == nil { |
| 132 | + break |
| 133 | + } |
| 134 | + |
| 135 | + if i < attempts-1 { |
| 136 | + time.Sleep(delay) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return err |
| 141 | +} |
0 commit comments