<commit_before>// Copyright 2017 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bootkube

import (
	"fmt"
	"strings"
	"time"

	"github.com/coreos/mantle/kola/cluster"
	"github.com/coreos/mantle/pluton"
	"github.com/coreos/mantle/pluton/spawn"
	"github.com/coreos/mantle/util"
)

func etcdScale(tc cluster.TestCluster) error {
	// create cluster with self-hosted etcd
	c, err := spawn.MakeBootkubeCluster(tc, 1, true)
	if err != nil {
		return err
	}

	// add two master nodes to cluster
	if err := c.AddMasters(2); err != nil {
		return err
	}

	// scale up etcd operator
	if err := resizeSelfHostedEtcd(c, 3); err != nil {
		return err
	}

	// todo check that each pod runs on a different master node
	if err := checkEtcdPodDistribution(c, 3); err != nil {
		return err
	}

	// scale back to 1
	if err := resizeSelfHostedEtcd(c, 1); err != nil {
		return err
	}

	// run an nginx deployment and ping it
	if err := nginxCheck(c); err != nil {
		return fmt.Errorf("nginxCheck: %v", err)
	}
	return nil
}

// resizes self-hosted etcd and checks that the desired number of pods are in a running state
func resizeSelfHostedEtcd(c *pluton.Cluster, size int) error {
	const (
		tprGroup   = "etcd.coreos.com"
		apiVersion = "v1beta1"
		tprKind    = "clusters"
	)
	var tprEndpoint = fmt.Sprintf("http://127.0.0.1:8080/apis/%s/%s/namespaces/kube-system/%s/kube-etcd",
		tprGroup, apiVersion, tprKind)

	scaleCmds := []string{
		fmt.Sprintf("curl -H 'Content-Type: application/json' -X GET %v > body.json", tprEndpoint),
		// delete resourceVersion field before curling back
		fmt.Sprintf("jq 'recurse(.metadata) |= del(.resourceVersion)' < body.json | jq .spec.size=%v > newbody.json", size),
		fmt.Sprintf("curl -H 'Content-Type: application/json' -X PUT --data @newbody.json %v", tprEndpoint),
	}
	for _, cmd := range scaleCmds {
		sout, serr, err := c.SSH(cmd)
		if err != nil {
			return fmt.Errorf("error in scale up command: %v:\nSTDERR: %s\nSTDOUT: %s", cmd, serr, sout)
		}
	}

	// check that all pods are running
	podsReady := func() error {
		out, err := c.Kubectl(`get po -l etcd_cluster=kube-etcd -o jsonpath='{.items[*].status.phase}' --namespace=kube-system`)
		if err != nil {
			return err
		}
		phases := strings.Split(out, " ")
		if len(phases) != size {
			return fmt.Errorf("expected %d etcd pods got %d: %v", size, len(phases), phases)
		}
		for _, phase := range phases {
			if phase != "Running" {
				return fmt.Errorf("one or more etcd pods not in a 'Running' phase")
			}
		}
		return nil
	}

	if err := util.Retry(10, 12*time.Second, podsReady); err != nil {
		return err
	}
	return nil<commit_msg>pluton/tests/bootkube: bump timeout in scale test<commit_after>// Copyright 2017 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bootkube

import (
	"fmt"
	"strings"
	"time"

	"github.com/coreos/mantle/kola/cluster"
	"github.com/coreos/mantle/pluton"
	"github.com/coreos/mantle/pluton/spawn"
	"github.com/coreos/mantle/util"
)

func etcdScale(tc cluster.TestCluster) error {
	// create cluster with self-hosted etcd
	c, err := spawn.MakeBootkubeCluster(tc, 1, true)
	if err != nil {
		return err
	}

	// add two master nodes to cluster
	if err := c.AddMasters(2); err != nil {
		return err
	}

	// scale up etcd operator
	if err := resizeSelfHostedEtcd(c, 3); err != nil {
		return err
	}

	// todo check that each pod runs on a different master node
	if err := checkEtcdPodDistribution(c, 3); err != nil {
		return err
	}

	// scale back to 1
	if err := resizeSelfHostedEtcd(c, 1); err != nil {
		return err
	}

	// run an nginx deployment and ping it
	if err := nginxCheck(c); err != nil {
		return fmt.Errorf("nginxCheck: %v", err)
	}
	return nil
}

// resizes self-hosted etcd and checks that the desired number of pods are in a running state
func resizeSelfHostedEtcd(c *pluton.Cluster, size int) error {
	const (
		tprGroup   = "etcd.coreos.com"
		apiVersion = "v1beta1"
		tprKind    = "clusters"
	)
	var tprEndpoint = fmt.Sprintf("http://127.0.0.1:8080/apis/%s/%s/namespaces/kube-system/%s/kube-etcd",
		tprGroup, apiVersion, tprKind)

	scaleCmds := []string{
		fmt.Sprintf("curl -H 'Content-Type: application/json' -X GET %v > body.json", tprEndpoint),
		// delete resourceVersion field before curling back
		fmt.Sprintf("jq 'recurse(.metadata) |= del(.resourceVersion)' < body.json | jq .spec.size=%v > newbody.json", size),
		fmt.Sprintf("curl -H 'Content-Type: application/json' -X PUT --data @newbody.json %v", tprEndpoint),
	}
	for _, cmd := range scaleCmds {
		sout, serr, err := c.SSH(cmd)
		if err != nil {
			return fmt.Errorf("error in scale up command: %v:\nSTDERR: %s\nSTDOUT: %s", cmd, serr, sout)
		}
	}

	// check that all pods are running
	podsReady := func() error {
		out, err := c.Kubectl(`get po -l etcd_cluster=kube-etcd -o jsonpath='{.items[*].status.phase}' --namespace=kube-system`)
		if err != nil {
			return err
		}
		phases := strings.Split(out, " ")
		if len(phases) != size {
			return fmt.Errorf("expected %d etcd pods got %d: %v", size, len(phases), phases)
		}
		for _, phase := range phases {
			if phase != "Running" {
				return fmt.Errorf("one or more etcd pods not in a 'Running' phase")
			}
		}
		return nil
	}

	if err := util.Retry(15, 10*time.Second, podsReady); err != nil {
		return err
	}
	return nil
{'fraction_non_alphanumeric': 0.13557449494949494, 'fraction_numerical': 0.00946969696969697, 'mean_word_length': 3.3945908460471568, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4894348', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2182, 'n_words': 702}