<commit_before>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package time

import (
	"once"
)

// A Ticker holds a synchronous channel that delivers `ticks' of a clock
// at intervals.
type Ticker struct {
	C        <-chan int64 // The channel on which the ticks are delivered.
	c        chan<- int64 // The same channel, but the end we use.
	ns       int64
	shutdown bool
	nextTick int64
	next     *Ticker
}

// Stop turns off a ticker.  After Stop, no more ticks will be sent.
func (t *Ticker) Stop() { t.shutdown = true }

// Tick is a convenience wrapper for NewTicker providing access to the ticking
// channel only.  Useful for clients that have no need to shut down the ticker.
func Tick(ns int64) <-chan int64 {
	if ns <= 0 {
		return nil
	}
	return NewTicker(ns).C
}

type alarmer struct {
	wakeUp   chan bool // wakeup signals sent/received here
	wakeMeAt chan int64
	wakeTime int64
}

// Set alarm to go off at time ns, if not already set earlier.
func (a *alarmer) set(ns int64) {
	// If there's no wakeLoop or the next tick we expect is too late, start a new wakeLoop
	if a.wakeMeAt == nil || a.wakeTime > ns {
		// Stop previous wakeLoop.
		if a.wakeMeAt != nil {
			a.wakeMeAt <- -1
		}
		a.wakeMeAt = make(chan int64, 10)
		go wakeLoop(a.wakeMeAt, a.wakeUp)
		a.wakeMeAt <- ns
	}
}

// Channel to notify tickerLoop of new Tickers being created.
var newTicker chan *Ticker

func startTickerLoop() {
	newTicker = make(chan *Ticker)
	go tickerLoop()
}

// wakeLoop delivers ticks at scheduled times, sleeping until the right moment.
// If another, earlier Ticker is created while it sleeps, tickerLoop() will start a new
// wakeLoop but they will share the wakeUp channel and signal that this one
// is done by giving it a negative time request.
func wakeLoop(wakeMeAt chan int64, wakeUp chan bool) {
	for {
		wakeAt := <-wakeMeAt
		if wakeAt < 0 { // tickerLoop has started another wakeLoop
			return
		}
		now := Nanoseconds()
		if wakeAt > now {
			Sleep(wakeAt - now)
			now = Nanoseconds()
		}
		wakeUp <- true
	}
}

// A single tickerLoop serves all ticks to Tickers.  It waits for two events:
// either the creation of a new Ticker or a tick from the alarm,
// signalling a time to wake up one or more Tickers.
func tickerLoop() {
	// Represents the next alarm to be delivered.
	var alarm alarmer
	// All wakeLoops deliver wakeups to this channel.
	alarm.wakeUp = make(chan bool, 10)
	var now, prevTime, wakeTime int64
	var tickers *Ticker
	for {
		select {
		case t := <-newTicker:
			// Add Ticker to list
			t.next = tickers
			tickers = t
			// Arrange for a new alarm if this one precedes the existing one.
			alarm.set(t.nextTick)
		case <-alarm.wakeUp:
			now = Nanoseconds()
			// Ignore an old time due to a dying wakeLoop
			if now < prevTime {
				continue
			}
			wakeTime = now + 1e15 // very long in the future
			var prev *Ticker = nil
			// Scan list of tickers, delivering updates to those
			// that need it and determining the next wake time.
			// TODO(r): list should be sorted in time order.
			for t := tickers; t != nil; t = t.next {
				if t.shutdown {
					// Ticker is done; remove it from list.
					if prev == nil {
						tickers = t.next
					} else {
						prev.next = t.next
					}
					continue
				}
				if tickers.nextTick <= now {
					if len(t.c) == 0 {
						// Only send if there's room.  We must not block.
						// The channel is allocated with a one-element
						// buffer, which is sufficient: if he hasn't picked
						// up the last tick, no point in sending more.
						t.c <- now
					}
					t.nextTick += t.ns
					if t.nextTick <= now {
						// Still behind; advance in one big step.
						t.nextTick += (now - t.nextTick + t.ns) / t.ns * t.ns
					}
					if t.nextTick > now && t.nextTick < wakeTime {
						wakeTime = t.nextTick
					}
				}
				prev = t
			}
			if tickers != nil {
				// Please send wakeup at earliest required time.
				// If there are no tickers, don't bother.
				alarm.wakeMeAt <- wakeTime
			}
		}
		prevTime = now
	}
}

// Ticker returns a new Ticker containing a channel that will
// send the time, in nanoseconds, every ns nanoseconds.  It adjusts the
// intervals to make up for pauses in delivery of the ticks.
func NewTicker(ns int64) *Ticker {
	if ns <= 0 {
		return nil
	}
	c := make(chan int64, 1) //  See comment on send in tickerLoop
	t := &Ticker{c, c, ns, false, Nanoseconds() + ns, nil}
	once.Do(startTickerLoop)
	// must be run in background so global Tickers can be created
	go func() { newTicker <- t }()
	return t
}
<commit_msg>fix bug in tick<commit_after>// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package time

import (
	"once"
)

// A Ticker holds a synchronous channel that delivers `ticks' of a clock
// at intervals.
type Ticker struct {
	C        <-chan int64 // The channel on which the ticks are delivered.
	c        chan<- int64 // The same channel, but the end we use.
	ns       int64
	shutdown bool
	nextTick int64
	next     *Ticker
}

// Stop turns off a ticker.  After Stop, no more ticks will be sent.
func (t *Ticker) Stop() { t.shutdown = true }

// Tick is a convenience wrapper for NewTicker providing access to the ticking
// channel only.  Useful for clients that have no need to shut down the ticker.
func Tick(ns int64) <-chan int64 {
	if ns <= 0 {
		return nil
	}
	return NewTicker(ns).C
}

type alarmer struct {
	wakeUp   chan bool // wakeup signals sent/received here
	wakeMeAt chan int64
	wakeTime int64
}

// Set alarm to go off at time ns, if not already set earlier.
func (a *alarmer) set(ns int64) {
	// If there's no wakeLoop or the next tick we expect is too late, start a new wakeLoop
	if a.wakeMeAt == nil || a.wakeTime > ns {
		// Stop previous wakeLoop.
		if a.wakeMeAt != nil {
			a.wakeMeAt <- -1
		}
		a.wakeMeAt = make(chan int64, 10)
		go wakeLoop(a.wakeMeAt, a.wakeUp)
		a.wakeTime = ns
		a.wakeMeAt <- ns
	}
}

// Channel to notify tickerLoop of new Tickers being created.
var newTicker chan *Ticker

func startTickerLoop() {
	newTicker = make(chan *Ticker)
	go tickerLoop()
}

// wakeLoop delivers ticks at scheduled times, sleeping until the right moment.
// If another, earlier Ticker is created while it sleeps, tickerLoop() will start a new
// wakeLoop but they will share the wakeUp channel and signal that this one
// is done by giving it a negative time request.
func wakeLoop(wakeMeAt chan int64, wakeUp chan bool) {
	for {
		wakeAt := <-wakeMeAt
		if wakeAt < 0 { // tickerLoop has started another wakeLoop
			return
		}
		now := Nanoseconds()
		if wakeAt > now {
			Sleep(wakeAt - now)
			now = Nanoseconds()
		}
		wakeUp <- true
	}
}

// A single tickerLoop serves all ticks to Tickers.  It waits for two events:
// either the creation of a new Ticker or a tick from the alarm,
// signalling a time to wake up one or more Tickers.
func tickerLoop() {
	// Represents the next alarm to be delivered.
	var alarm alarmer
	// All wakeLoops deliver wakeups to this channel.
	alarm.wakeUp = make(chan bool, 10)
	var now, prevTime, wakeTime int64
	var tickers *Ticker
	for {
		select {
		case t := <-newTicker:
			// Add Ticker to list
			t.next = tickers
			tickers = t
			// Arrange for a new alarm if this one precedes the existing one.
			alarm.set(t.nextTick)
		case <-alarm.wakeUp:
			now = Nanoseconds()
			// Ignore an old time due to a dying wakeLoop
			if now < prevTime {
				continue
			}
			wakeTime = now + 1e15 // very long in the future
			var prev *Ticker = nil
			// Scan list of tickers, delivering updates to those
			// that need it and determining the next wake time.
			// TODO(r): list should be sorted in time order.
			for t := tickers; t != nil; t = t.next {
				if t.shutdown {
					// Ticker is done; remove it from list.
					if prev == nil {
						tickers = t.next
					} else {
						prev.next = t.next
					}
					continue
				}
				if t.nextTick <= now {
					if len(t.c) == 0 {
						// Only send if there's room.  We must not block.
						// The channel is allocated with a one-element
						// buffer, which is sufficient: if he hasn't picked
						// up the last tick, no point in sending more.
						t.c <- now
					}
					t.nextTick += t.ns
					if t.nextTick <= now {
						// Still behind; advance in one big step.
						t.nextTick += (now - t.nextTick + t.ns) / t.ns * t.ns
					}
				}
				if t.nextTick < wakeTime {
					wakeTime = t.nextTick
				}
				prev = t
			}
			if tickers != nil {
				// Please send wakeup at earliest required time.
				// If there are no tickers, don't bother.
				alarm.wakeMeAt <- wakeTime
			}
		}
		prevTime = now
	}
}

// Ticker returns a new Ticker containing a channel that will
// send the time, in nanoseconds, every ns nanoseconds.  It adjusts the
// intervals to make up for pauses in delivery of the ticks.
func NewTicker(ns int64) *Ticker {
	if ns <= 0 {
		return nil
	}
	c := make(chan int64, 1) //  See comment on send in tickerLoop
	t := &Ticker{c, c, ns, false, Nanoseconds() + ns, nil}
	once.Do(startTickerLoop)
	// must be run in background so global Tickers can be created
	go func() { newTicker <- t }()
	return t
}
<|endoftext|>


<commit_before>package main

import (
	// Stdlib
	"fmt"
	"os"
	"regexp"
	"strings"

	// Internal
	"github.com/salsaflow/salsaflow/errs"
	"github.com/salsaflow/salsaflow/git"

	// Vendor
	"github.com/fatih/color"
)

func main() {
	if err := hook(); err != nil {
		errs.Fatal(err)
	}
}

func hook() error {
	// There are always 3 arguments passed to this hook.
	prevRef, newRef, flag := os.Args[1], os.Args[2], os.Args[3]

	// Return in case prevRef is the zero hash since that means
	// that this hook is being run right after 'git clone'.
	if prevRef == git.ZeroHash {
		return nil
	}

	// Return in case flag is '0'. That signals retrieving a file from the index.
	if flag == "0" {
		return nil
	}

	// Return unless the new HEAD is a core branch.
	isCore, err := isCoreBranch(newRef)
	if err != nil {
		return err
	}
	if !isCore {
		return nil
	}

	// Get the relevant commits.
	// These are the commits specified by newRef..prevRef, e.g. trunk..story/foobar.
	commits, err := git.ShowCommitRange(fmt.Sprintf("%v..%v", newRef, prevRef))
	if err != nil {
		return err
	}

	// Collect the commits with missing Story-Id tag.
	missing := make([]*git.Commit, 0, len(commits))
	for _, commit := range commits {
		if commit.StoryIdTag == "" {
			missing = append(missing, commit)
		}
	}
	if len(missing) == 0 {
		return nil
	}

	// Print the warning.
	printWarning(missing)
	return nil
}

func isCoreBranch(ref string) (bool, error) {
	// Get the ref names using 'git log'.
	// This prints the ref names in the following format:
	//
	//   (ref1, ref2, ..., refN)
	//
	outputBuffer, err := git.Log("-1", "--pretty=format:%d", ref)
	if err != nil {
		return false, err
	}
	output := outputBuffer.String()

	// Parse the output.
	match := regexp.MustCompile("^[ ]*[(]([^)]+)[)][ ]*$").FindStringSubmatch(output)
	if len(match) != 2 {
		return false, fmt.Errorf("failed to parse git log: %v", output)
	}
	refNames := strings.Split(match[1], ", ")

	// Iterate over the ref names and return the result.
	for _, ref := range refNames {
		isCore, err := git.IsCoreBranch(ref)
		if err != nil {
			return false, err
		}
		if isCore {
			return true, nil
		}
	}
	return false, nil
}

func printWarning(commits []*git.Commit) {
	// Let's be colorful!
	redBold := color.New(color.FgRed).Add(color.Bold)
	redBold.Println("\nWarning: There are some commits missing the Story-Id tag.")

	red := color.New(color.FgRed)
	red.Println("Make sure this is really what you want before proceeding further.\n")

	yellow := color.New(color.FgYellow).SprintFunc()
	for _, commit := range commits {
		fmt.Printf("  %v %v\n", yellow(commit.SHA), commit.MessageTitle)
	}
	fmt.Println()
}
<commit_msg>hooks: post-checkout: Register special flag<commit_after>package main

import (
	// Stdlib
	"fmt"
	"os"
	"regexp"
	"strings"

	// Internal
	"github.com/salsaflow/salsaflow/errs"
	"github.com/salsaflow/salsaflow/git"
	"github.com/salsaflow/salsaflow/hooks"

	// Vendor
	"github.com/fatih/color"
)

func main() {
	// Register the magical -salsaflow.version flag.
	hooks.IdentifyYourself()

	// Run the hook logic itself.
	if err := hook(); err != nil {
		errs.Fatal(err)
	}
}

func hook() error {
	// There are always 3 arguments passed to this hook.
	prevRef, newRef, flag := os.Args[1], os.Args[2], os.Args[3]

	// Return in case prevRef is the zero hash since that means
	// that this hook is being run right after 'git clone'.
	if prevRef == git.ZeroHash {
		return nil
	}

	// Return in case flag is '0'. That signals retrieving a file from the index.
	if flag == "0" {
		return nil
	}

	// Return unless the new HEAD is a core branch.
	isCore, err := isCoreBranch(newRef)
	if err != nil {
		return err
	}
	if !isCore {
		return nil
	}

	// Get the relevant commits.
	// These are the commits specified by newRef..prevRef, e.g. trunk..story/foobar.
	commits, err := git.ShowCommitRange(fmt.Sprintf("%v..%v", newRef, prevRef))
	if err != nil {
		return err
	}

	// Collect the commits with missing Story-Id tag.
	missing := make([]*git.Commit, 0, len(commits))
	for _, commit := range commits {
		if commit.StoryIdTag == "" {
			missing = append(missing, commit)
		}
	}
	if len(missing) == 0 {
		return nil
	}

	// Print the warning.
	printWarning(missing)
	return nil
}

func isCoreBranch(ref string) (bool, error) {
	// Get the ref names using 'git log'.
	// This prints the ref names in the following format:
	//
	//   (ref1, ref2, ..., refN)
	//
	outputBuffer, err := git.Log("-1", "--pretty=format:%d", ref)
	if err != nil {
		return false, err
	}
	output := outputBuffer.String()

	// Parse the output.
	match := regexp.MustCompile("^[ ]*[(]([^)]+)[)][ ]*$").FindStringSubmatch(output)
	if len(match) != 2 {
		return false, fmt.Errorf("failed to parse git log: %v", output)
	}
	refNames := strings.Split(match[1], ", ")

	// Iterate over the ref names and return the result.
	for _, ref := range refNames {
		isCore, err := git.IsCoreBranch(ref)
		if err != nil {
			return false, err
		}
		if isCore {
			return true, nil
		}
	}
	return false, nil
}

func printWarning(commits []*git.Commit) {
	// Let's be colorful!
	redBold := color.New(color.FgRed).Add(color.Bold)
	redBold.Println("\nWarning: There are some commits missing the Story-Id tag.")

	red := color.New(color.FgRed)
	red.Println("Make sure this is really what you want before proceeding further.\n")

	yellow := color.New(color.FgYellow).SprintFunc()
	for _, commit := range commits {
		fmt.Printf("  %v %v\n", yellow(commit.SHA), commit.MessageTitle)
	}
	fmt.Println()
}
<|endoftext|>


<commit_before>package libvirt

import (
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/mitchellh/multistep"
	"github.com/mitchellh/packer/packer"
	"gopkg.in/alexzorin/libvirt-go.v2"
)

const KeyLeftShift uint32 = 0xFFE1

type bootCommandTemplateData struct {
	HTTPIP   string
	HTTPPort uint
	Name     string
}

// This step "types" the boot command into the VM over VNC.
//
// Uses:
//   config *config
//   http_port int
//   ui     packer.Ui
//
// Produces:
//   <nothing>
type stepTypeBootCommand struct{}

func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction {
	config := state.Get("config").(*Config)
	//	httpPort := state.Get("http_port").(uint)
	//	hostIp := state.Get("host_ip").(string)
	ui := state.Get("ui").(packer.Ui)

	var lvd libvirt.VirDomain
	lv, err := libvirt.NewVirConnection(config.LibvirtUrl)
	if err != nil {
		err := fmt.Errorf("Error connecting to libvirt: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}
	defer lv.CloseConnection()
	if lvd, err = lv.LookupDomainByName(config.VMName); err != nil {
		err := fmt.Errorf("Error lookup domain: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}
	defer lvd.Free()

	//	tplData := &bootCommandTemplateData{
	//		hostIp,
	//		httpPort,
	//		config.VMName,
	//	}

	ui.Say("Typing the boot command...")
	for _, command := range config.BootCommand {
		//		command, err := config.tpl.Process(command, tplData)
		//		if err != nil {
		//			err := fmt.Errorf("Error preparing boot command: %s", err)
		//			state.Put("error", err)
		//			ui.Error(err.Error())
		//			return multistep.ActionHalt
		//		}

		// Check for interrupts between typing things so we can cancel
		// since this isn't the fastest thing.
		if _, ok := state.GetOk(multistep.StateCancelled); ok {
			return multistep.ActionHalt
		}

		sendBootString(lvd, command)
	}

	return multistep.ActionContinue
}

func (*stepTypeBootCommand) Cleanup(multistep.StateBag) {}

func sendBootString(d libvirt.VirDomain, original string) {
	//	shiftedChars := "~!@#$%^&*()_+{}|:\"<>?"
	var keys []uint
	var key uint
	var ok bool
	var err error

	for len(original) > 0 {
		//		var keyCode uint
		//		keyShift := false

		if strings.HasPrefix(original, "<wait>") {
			log.Printf("Special code '<wait>' found, sleeping one second")
			time.Sleep(1 * time.Second)
			original = original[len("<wait>"):]
			continue
		}

		if strings.HasPrefix(original, "<wait5>") {
			log.Printf("Special code '<wait5>' found, sleeping 5 seconds")
			time.Sleep(5 * time.Second)
			original = original[len("<wait5>"):]
			continue
		}

		if strings.HasPrefix(original, "<wait10>") {
			log.Printf("Special code '<wait10>' found, sleeping 10 seconds")
			time.Sleep(10 * time.Second)
			original = original[len("<wait10>"):]
			continue
		}

		if strings.HasPrefix(original, "<esc>") {
			keys = append(keys, ecodes["<esc>"])
			original = original[len("<esc>"):]
			continue
		}
		if strings.HasPrefix(original, "<enter>") {
			keys = append(keys, ecodes["<enter>"])
			original = original[len("<enter>"):]
			continue
		}

		for _, char := range original {
			if key, ok = ecodes[string(char)]; ok {
				keys = append(keys, key)
				//			keyShift = unicode.IsUpper(r) || strings.ContainsRune(shiftedChars, r)
			}
		}

		//VIR_KEYCODE_SET_LINUX, VIR_KEYCODE_SET_USB, VIR_KEYCODE_SET_RFB, VIR_KEYCODE_SET_WIN32, VIR_KEYCODE_SET_XT_KBD
		for _, key := range keys {
			log.Printf("Sending code %d", key)
			if err = d.SendKey(libvirt.VIR_KEYCODE_SET_RFB, 1000, []uint{key}, 0); err != nil {
				log.Printf("Sending code %d failed: %s", key, err.Error())
			}
		}
	}
}
<commit_msg>fix<commit_after>package libvirt

import (
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/mitchellh/multistep"
	"github.com/mitchellh/packer/packer"
	"gopkg.in/alexzorin/libvirt-go.v2"
)

const KeyLeftShift uint32 = 0xFFE1

type bootCommandTemplateData struct {
	HTTPIP   string
	HTTPPort uint
	Name     string
}

// This step "types" the boot command into the VM over VNC.
//
// Uses:
//   config *config
//   http_port int
//   ui     packer.Ui
//
// Produces:
//   <nothing>
type stepTypeBootCommand struct{}

func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction {
	config := state.Get("config").(*Config)
	//	httpPort := state.Get("http_port").(uint)
	//	hostIp := state.Get("host_ip").(string)
	ui := state.Get("ui").(packer.Ui)

	var lvd libvirt.VirDomain
	lv, err := libvirt.NewVirConnection(config.LibvirtUrl)
	if err != nil {
		err := fmt.Errorf("Error connecting to libvirt: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}
	defer lv.CloseConnection()
	if lvd, err = lv.LookupDomainByName(config.VMName); err != nil {
		err := fmt.Errorf("Error lookup domain: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}
	defer lvd.Free()

	//	tplData := &bootCommandTemplateData{
	//		hostIp,
	//		httpPort,
	//		config.VMName,
	//	}

	ui.Say("Typing the boot command...")
	for _, command := range config.BootCommand {
		//		command, err := config.tpl.Process(command, tplData)
		//		if err != nil {
		//			err := fmt.Errorf("Error preparing boot command: %s", err)
		//			state.Put("error", err)
		//			ui.Error(err.Error())
		//			return multistep.ActionHalt
		//		}

		// Check for interrupts between typing things so we can cancel
		// since this isn't the fastest thing.
		if _, ok := state.GetOk(multistep.StateCancelled); ok {
			return multistep.ActionHalt
		}

		sendBootString(lvd, command)
	}

	return multistep.ActionContinue
}

func (*stepTypeBootCommand) Cleanup(multistep.StateBag) {}

func sendBootString(d libvirt.VirDomain, original string) {
	//	shiftedChars := "~!@#$%^&*()_+{}|:\"<>?"
	var keys []uint
	var key uint
	var ok bool
	var err error

	for len(original) > 0 {
		//		var keyCode uint
		//		keyShift := false

		if strings.HasPrefix(original, "<wait>") {
			log.Printf("Special code '<wait>' found, sleeping one second")
			time.Sleep(1 * time.Second)
			original = original[len("<wait>"):]
			continue
		}

		if strings.HasPrefix(original, "<wait5>") {
			log.Printf("Special code '<wait5>' found, sleeping 5 seconds")
			time.Sleep(5 * time.Second)
			original = original[len("<wait5>"):]
			continue
		}

		if strings.HasPrefix(original, "<wait10>") {
			log.Printf("Special code '<wait10>' found, sleeping 10 seconds")
			time.Sleep(10 * time.Second)
			original = original[len("<wait10>"):]
			continue
		}

		if strings.HasPrefix(original, "<esc>") {
			keys = append(keys, ecodes["<esc>"])
			original = original[len("<esc>"):]
		}
		if strings.HasPrefix(original, "<enter>") {
			keys = append(keys, ecodes["<enter>"])
			original = original[len("<enter>"):]
		}

		char := original[0]
		if key, ok = ecodes[string(char)]; ok {
			keys = append(keys, key)
			//			keyShift = unicode.IsUpper(r) || strings.ContainsRune(shiftedChars, r)
		}
	}
	//VIR_KEYCODE_SET_LINUX, VIR_KEYCODE_SET_USB, VIR_KEYCODE_SET_RFB, VIR_KEYCODE_SET_WIN32, VIR_KEYCODE_SET_XT_KBD
	for _, key := range keys {
		log.Printf("Sending code %d", key)
		if err = d.SendKey(libvirt.VIR_KEYCODE_SET_RFB, 1000, []uint{key}, 0); err != nil {
			log.Printf("Sending code %d failed: %s", key, err.Error())
		}
	}

}
<|endoftext|>


<commit_before>package libvirt

import (
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/mitchellh/multistep"
	"github.com/mitchellh/packer/packer"
	"gopkg.in/alexzorin/libvirt-go.v2"
)

const KeyLeftShift uint32 = 0xFFE1

type bootCommandTemplateData struct {
	HTTPIP   string
	HTTPPort uint
	Name     string
}

// This step "types" the boot command into the VM over VNC.
//
// Uses:
//   config *config
//   http_port int
//   ui     packer.Ui
//
// Produces:
//   <nothing>
type stepTypeBootCommand struct{}

func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction {
	config := state.Get("config").(*Config)
	//	httpPort := state.Get("http_port").(uint)
	//	hostIp := state.Get("host_ip").(string)
	ui := state.Get("ui").(packer.Ui)

	var lvd libvirt.VirDomain
	lv, err := libvirt.NewVirConnection(config.LibvirtUrl)
	if err != nil {
		err := fmt.Errorf("Error connecting to libvirt: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}
	defer lv.CloseConnection()
	if lvd, err = lv.LookupDomainByName(config.VMName); err != nil {
		err := fmt.Errorf("Error lookup domain: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}
	defer lvd.Free()

	//	tplData := &bootCommandTemplateData{
	//		hostIp,
	//		httpPort,
	//		config.VMName,
	//	}

	ui.Say("Typing the boot command...")
	for _, command := range config.BootCommand {
		//		command, err := config.tpl.Process(command, tplData)
		//		if err != nil {
		//			err := fmt.Errorf("Error preparing boot command: %s", err)
		//			state.Put("error", err)
		//			ui.Error(err.Error())
		//			return multistep.ActionHalt
		//		}

		// Check for interrupts between typing things so we can cancel
		// since this isn't the fastest thing.
		if _, ok := state.GetOk(multistep.StateCancelled); ok {
			return multistep.ActionHalt
		}

		sendBootString(lvd, command)
	}

	return multistep.ActionContinue
}

func (*stepTypeBootCommand) Cleanup(multistep.StateBag) {}

func sendBootString(d libvirt.VirDomain, original string) {
	//	shiftedChars := "~!@#$%^&*()_+{}|:\"<>?"
	var keys []uint
	var key uint
	var ok bool
	var err error

	for len(original) > 0 {
		//		var keyCode uint
		//		keyShift := false

		if strings.HasPrefix(original, "<wait>") {
			log.Printf("Special code '<wait>' found, sleeping one second")
			time.Sleep(1 * time.Second)
			original = original[len("<wait>"):]
			continue
		}

		if strings.HasPrefix(original, "<wait5>") {
			log.Printf("Special code '<wait5>' found, sleeping 5 seconds")
			time.Sleep(5 * time.Second)
			original = original[len("<wait5>"):]
			continue
		}

		if strings.HasPrefix(original, "<wait10>") {
			log.Printf("Special code '<wait10>' found, sleeping 10 seconds")
			time.Sleep(10 * time.Second)
			original = original[len("<wait10>"):]
			continue
		}

		if strings.HasPrefix(original, "<esc>") {
			keys = append(keys, ecodes["<esc>"])
			original = original[len("<esc>"):]
		}
		if strings.HasPrefix(original, "<enter>") {
			keys = append(keys, ecodes["<enter>"])
			original = original[len("<enter>"):]
		}

		char := original[0]
		log.Printf("try to find code for char %s", string(char))
		if key, ok = ecodes[string(char)]; ok {
			log.Printf("find code for char %s %d", string(char), key)
			keys = append(keys, key)
			//			keyShift = unicode.IsUpper(r) || strings.ContainsRune(shiftedChars, r)
		}
		original = original[1:]
	}
	//VIR_KEYCODE_SET_LINUX, VIR_KEYCODE_SET_USB, VIR_KEYCODE_SET_RFB, VIR_KEYCODE_SET_WIN32, VIR_KEYCODE_SET_XT_KBD
	for _, key := range keys {
		log.Printf("send code %d", key)
		if err = d.SendKey(libvirt.VIR_KEYCODE_SET_RFB, 1000, []uint{key}, 0); err != nil {
			log.Printf("Sending code %d failed: %s", key, err.Error())
		}
	}

}
<commit_msg>fix<commit_after>package libvirt

import (
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/mitchellh/multistep"
	"github.com/mitchellh/packer/packer"
	"gopkg.in/alexzorin/libvirt-go.v2"
)

const KeyLeftShift uint32 = 0xFFE1

type bootCommandTemplateData struct {
	HTTPIP   string
	HTTPPort uint
	Name     string
}

// This step "types" the boot command into the VM over VNC.
//
// Uses:
//   config *config
//   http_port int
//   ui     packer.Ui
//
// Produces:
//   <nothing>
type stepTypeBootCommand struct{}

func (s *stepTypeBootCommand) Run(state multistep.StateBag) multistep.StepAction {
	config := state.Get("config").(*Config)
	//	httpPort := state.Get("http_port").(uint)
	//	hostIp := state.Get("host_ip").(string)
	ui := state.Get("ui").(packer.Ui)

	var lvd libvirt.VirDomain
	lv, err := libvirt.NewVirConnection(config.LibvirtUrl)
	if err != nil {
		err := fmt.Errorf("Error connecting to libvirt: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}
	defer lv.CloseConnection()
	if lvd, err = lv.LookupDomainByName(config.VMName); err != nil {
		err := fmt.Errorf("Error lookup domain: %s", err)
		state.Put("error", err)
		ui.Error(err.Error())
		return multistep.ActionHalt
	}
	defer lvd.Free()

	//	tplData := &bootCommandTemplateData{
	//		hostIp,
	//		httpPort,
	//		config.VMName,
	//	}

	ui.Say("Typing the boot command...")
	for _, command := range config.BootCommand {
		//		command, err := config.tpl.Process(command, tplData)
		//		if err != nil {
		//			err := fmt.Errorf("Error preparing boot command: %s", err)
		//			state.Put("error", err)
		//			ui.Error(err.Error())
		//			return multistep.ActionHalt
		//		}

		// Check for interrupts between typing things so we can cancel
		// since this isn't the fastest thing.
		if _, ok := state.GetOk(multistep.StateCancelled); ok {
			return multistep.ActionHalt
		}

		sendBootString(lvd, command)
	}

	return multistep.ActionContinue
}

func (*stepTypeBootCommand) Cleanup(multistep.StateBag) {}

func sendBootString(d libvirt.VirDomain, original string) {
	//	shiftedChars := "~!@#$%^&*()_+{}|:\"<>?"
	var err error
	var ok bool
	var key uint

	for len(original) > 0 {
		//		var keyCode uint
		//		keyShift := false

		if strings.HasPrefix(original, "<wait>") {
			log.Printf("Special code '<wait>' found, sleeping one second")
			time.Sleep(1 * time.Second)
			original = original[len("<wait>"):]
			continue
		}

		if strings.HasPrefix(original, "<wait5>") {
			log.Printf("Special code '<wait5>' found, sleeping 5 seconds")
			time.Sleep(5 * time.Second)
			original = original[len("<wait5>"):]
			continue
		}

		if strings.HasPrefix(original, "<wait10>") {
			log.Printf("Special code '<wait10>' found, sleeping 10 seconds")
			time.Sleep(10 * time.Second)
			original = original[len("<wait10>"):]
			continue
		}

		if strings.HasPrefix(original, "<esc>") {
			d.SendKey(libvirt.VIR_KEYCODE_SET_RFB, 1000, []uint{ecodes["<esc>"]}, 0)
			original = original[len("<esc>"):]
		}
		if strings.HasPrefix(original, "<enter>") {
			d.SendKey(libvirt.VIR_KEYCODE_SET_RFB, 1000, []uint{ecodes["<enter>"]}, 0)
			original = original[len("<enter>"):]
		}

		char := original[0]
		log.Printf("try to find code for char %s", string(char))
		if key, ok = ecodes[string(char)]; ok {
			log.Printf("find code for char %s %d", string(char), key)
			//			keyShift = unicode.IsUpper(r) || strings.ContainsRune(shiftedChars, r)
		} else {
			log.Printf("can't find code for char %s", string(char))
			continue
		}
		original = original[1:]
		//VIR_KEYCODE_SET_LINUX, VIR_KEYCODE_SET_USB, VIR_KEYCODE_SET_RFB, VIR_KEYCODE_SET_WIN32, VIR_KEYCODE_SET_XT_KBD
		log.Printf("send code %d", key)
		if err = d.SendKey(libvirt.VIR_KEYCODE_SET_RFB, 1000, []uint{key}, 0); err != nil {
			log.Printf("Sending code %d failed: %s", key, err.Error())
		}
	}

}
<|endoftext|>


<commit_before>package static

import (
	"bytes"
	"github.com/viant/toolbox/storage"
	"log"
)

func init() {
	var memStorage = storage.NewMemoryService();
	{
		err := memStorage.Upload("mem://github.com/viant/endly/Version", bytes.NewReader([]byte{48,46,49,49,46,50,10}))
		if err != nil {
			log.Printf("failed to upload: mem://github.com/viant/endly/Version %v", err)
		}
	}
}
<commit_msg>version update for release<commit_after>package static

import (
	"bytes"
	"github.com/viant/toolbox/storage"
	"log"
)

func init() {
	var memStorage = storage.NewMemoryService();
	{
		err := memStorage.Upload("mem://github.com/viant/endly/Version", bytes.NewReader([]byte{48,46,49,50,46,49,10}))
		if err != nil {
			log.Printf("failed to upload: mem://github.com/viant/endly/Version %v", err)
		}
	}
}
<|endoftext|>


<commit_before>/*
Copyright 2016 The Kubernetes Authors.

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 routes

import (
	restful "github.com/emicklei/go-restful"
	"github.com/go-openapi/spec"
	"k8s.io/klog/v2"

	"k8s.io/apiserver/pkg/server/mux"
	"k8s.io/kube-openapi/pkg/builder"
	"k8s.io/kube-openapi/pkg/common"
	"k8s.io/kube-openapi/pkg/handler"
)

// OpenAPI installs spec endpoints for each web service.
type OpenAPI struct {
	Config *common.Config
}

// Install adds the SwaggerUI webservice to the given mux.
func (oa OpenAPI) Install(c *restful.Container, mux *mux.PathRecorderMux) (*handler.OpenAPIService, *spec.Swagger) {
	spec, err := builder.BuildOpenAPISpec(c.RegisteredWebServices(), oa.Config)
	if err != nil {
		klog.Fatalf("Failed to build open api spec for root: %v", err)
	}

	openAPIVersionedService, err := handler.NewOpenAPIService(spec)
	if err != nil {
		klog.Fatalf("Failed to create OpenAPIService: %v", err)
	}

	err = openAPIVersionedService.RegisterOpenAPIVersionedService("/openapi/v2", mux)
	if err != nil {
		klog.Fatalf("Failed to register versioned open api spec for root: %v", err)
	}

	return openAPIVersionedService, spec
}
<commit_msg>UPSTREAM: <carry>: filter out CustomResourceQuota paths from OpenAPI<commit_after>/*
Copyright 2016 The Kubernetes Authors.

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 routes

import (
	"strings"

	restful "github.com/emicklei/go-restful"
	"github.com/go-openapi/spec"
	"k8s.io/klog/v2"

	"k8s.io/apiserver/pkg/server/mux"
	"k8s.io/kube-openapi/pkg/builder"
	"k8s.io/kube-openapi/pkg/common"
	"k8s.io/kube-openapi/pkg/handler"
)

// OpenAPI installs spec endpoints for each web service.
type OpenAPI struct {
	Config *common.Config
}

// Install adds the SwaggerUI webservice to the given mux.
func (oa OpenAPI) Install(c *restful.Container, mux *mux.PathRecorderMux) (*handler.OpenAPIService, *spec.Swagger) {
	// we shadow ClustResourceQuotas, RoleBindingRestrictions, and SecurityContextContstraints
	// with a CRD. This loop removes all CRQ,RBR, SCC paths
	// from the OpenAPI spec such that they don't conflict with the CRD
	// apiextensions-apiserver spec during merging.
	oa.Config.IgnorePrefixes = append(oa.Config.IgnorePrefixes,
		"/apis/quota.openshift.io/v1/clusterresourcequotas",
		"/apis/security.openshift.io/v1/securitycontextconstraints",
		"/apis/authorization.openshift.io/v1/rolebindingrestrictions",
		"/apis/authorization.openshift.io/v1/namespaces/{namespace}/rolebindingrestrictions",
		"/apis/authorization.openshift.io/v1/watch/namespaces/{namespace}/rolebindingrestrictions",
		"/apis/authorization.openshift.io/v1/watch/rolebindingrestrictions")

	spec, err := builder.BuildOpenAPISpec(c.RegisteredWebServices(), oa.Config)
	if err != nil {
		klog.Fatalf("Failed to build open api spec for root: %v", err)
	}

	// we shadow ClustResourceQuotas, RoleBindingRestrictions, and SecurityContextContstraints
	// with a CRD. This loop removes all CRQ,RBR, SCC paths
	// from the OpenAPI spec such that they don't conflict with the CRD
	// apiextensions-apiserver spec during merging.
	for pth := range spec.Paths.Paths {
		if strings.HasPrefix(pth, "/apis/quota.openshift.io/v1/clusterresourcequotas") ||
			strings.Contains(pth, "rolebindingrestrictions") ||
			strings.HasPrefix(pth, "/apis/security.openshift.io/v1/securitycontextconstraints") {
			delete(spec.Paths.Paths, pth)
		}
	}

	openAPIVersionedService, err := handler.NewOpenAPIService(spec)
	if err != nil {
		klog.Fatalf("Failed to create OpenAPIService: %v", err)
	}

	err = openAPIVersionedService.RegisterOpenAPIVersionedService("/openapi/v2", mux)
	if err != nil {
		klog.Fatalf("Failed to register versioned open api spec for root: %v", err)
	}

	return openAPIVersionedService, spec
}
<|endoftext|>


<commit_before>// ReadPassword for OSes which are supported by golang.org/x/crypto/ssh/terminal
// See https://github.com/golang/go/issues/14441 - plan9
//     https://github.com/golang/go/issues/13085 - solaris

// +build !solaris,!plan9

package config

import (
	"fmt"
	"log"
	"os"

	"golang.org/x/crypto/ssh/terminal"
)

// ReadPassword reads a password without echoing it to the terminal.
func ReadPassword() string {
	line, err := terminal.ReadPassword(int(os.Stdin.Fd()))
	_, _ = fmt.Fprintln(os.Stderr)
	if err != nil {
		log.Fatalf("Failed to read password: %v", err)
	}
	return string(line)
}
<commit_msg>config: fix error reading password from piped input - fixes #1308<commit_after>// ReadPassword for OSes which are supported by golang.org/x/crypto/ssh/terminal
// See https://github.com/golang/go/issues/14441 - plan9
//     https://github.com/golang/go/issues/13085 - solaris

// +build !solaris,!plan9

package config

import (
	"fmt"
	"log"
	"os"

	"golang.org/x/crypto/ssh/terminal"
)

// ReadPassword reads a password without echoing it to the terminal.
func ReadPassword() string {
	stdin := int(os.Stdin.Fd())
	if !terminal.IsTerminal(stdin) {
		return ReadLine()
	}
	line, err := terminal.ReadPassword(stdin)
	_, _ = fmt.Fprintln(os.Stderr)
	if err != nil {
		log.Fatalf("Failed to read password: %v", err)
	}
	return string(line)
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	"./buildkite"
	"github.com/apex/go-apex"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/cloudwatch"
)

var (
	orgSlug string
	apiKey  string
)

// Generates:
// Buildkite > RunningBuildsCount
// Buildkite > RunningJobsCount
// Buildkite > ScheduledBuildsCount
// Buildkite > ScheduledJobsCount
// Buildkite > (Queue) > RunningBuildsCount
// Buildkite > (Queue) > RunningJobsCount
// Buildkite > (Queue) > ScheduledBuildsCount
// Buildkite > (Queue) > ScheduledJobsCount
// Buildkite > (Pipeline) > RunningBuildsCount
// Buildkite > (Pipeline) > RunningJobsCount
// Buildkite > (Pipeline) > ScheduledBuildsCount
// Buildkite > (Pipeline) > ScheduledJobsCount

func main() {
	orgSlug = os.Getenv("BUILDKITE_ORG_SLUG")
	apiKey = os.Getenv("BUILDKITE_API_ACCESS_TOKEN")

	apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) {
		svc := cloudwatch.New(session.New())

		log.Printf("Querying buildkite for builds for org %s for past 5 mins", orgSlug)
		builds, err := recentBuildkiteBuilds()
		if err != nil {
			return nil, err
		}

		var res Result = Result{
			Queues:    map[string]Counts{},
			Pipelines: map[string]Counts{},
		}

		log.Printf("Aggregating results from %d builds", len(builds))
		for _, build := range builds {
			res.Counts = res.Counts.addBuild(build)
			res.Pipelines[build.Pipeline.Name] = res.Pipelines[build.Pipeline.Name].addBuild(build)

			var buildQueues = map[string]int{}
			for _, job := range build.Jobs {
				res.Counts = res.Counts.addJob(job)
				res.Pipelines[build.Pipeline.Name] = res.Pipelines[build.Pipeline.Name].addJob(job)
				res.Queues[job.Queue()] = res.Queues[job.Queue()].addJob(job)
				buildQueues[job.Queue()]++
			}

			for queue := range buildQueues {
				res.Queues[queue] = res.Queues[queue].addBuild(build)
			}
		}

		log.Printf("Extracting cloudwatch metrics from results")
		metrics := res.extractMetricData()

		for _, chunk := range chunkMetricData(10, metrics) {
			log.Printf("Submitting chunk of %d metrics to Cloudwatch", len(chunk))
			if err = putMetricData(svc, chunk); err != nil {
				return nil, err
			}
		}

		return res, nil
	})
}

type Counts struct {
	RunningBuilds, RunningJobs, ScheduledBuilds, ScheduledJobs int
}

func (c Counts) addBuild(build buildkite.Build) Counts {
	switch build.State {
	case "running":
		c.RunningBuilds++
	case "scheduled":
		c.ScheduledBuilds++
	}
	return c
}

func (c Counts) addJob(job buildkite.Job) Counts {
	switch job.State {
	case "running":
		c.RunningJobs++
	case "scheduled":
		c.ScheduledJobs++
	}
	return c
}

func (c Counts) asMetrics(dimensions []*cloudwatch.Dimension) []*cloudwatch.MetricDatum {
	return []*cloudwatch.MetricDatum{
		&cloudwatch.MetricDatum{
			MetricName: aws.String("RunningBuildsCount"),
			Dimensions: dimensions,
			Value:      aws.Float64(float64(c.RunningBuilds)),
			Unit:       aws.String("Count"),
		},
		&cloudwatch.MetricDatum{
			MetricName: aws.String("ScheduledBuildsCount"),
			Dimensions: dimensions,
			Value:      aws.Float64(float64(c.ScheduledBuilds)),
			Unit:       aws.String("Count"),
		},
		&cloudwatch.MetricDatum{
			MetricName: aws.String("RunningJobsCount"),
			Dimensions: dimensions,
			Value:      aws.Float64(float64(c.RunningJobs)),
			Unit:       aws.String("Count"),
		},
		&cloudwatch.MetricDatum{
			MetricName: aws.String("ScheduledJobsCount"),
			Dimensions: dimensions,
			Value:      aws.Float64(float64(c.ScheduledJobs)),
			Unit:       aws.String("Count"),
		},
	}
}

type Result struct {
	Counts
	Queues, Pipelines map[string]Counts
}

func (r Result) extractMetricData() []*cloudwatch.MetricDatum {
	data := []*cloudwatch.MetricDatum{}
	data = append(data, r.Counts.asMetrics(nil)...)

	for name, _ := range r.Queues {
		data = append(data, r.Counts.asMetrics([]*cloudwatch.Dimension{
			{Name: aws.String("Queue"), Value: aws.String(name)},
		})...)
	}

	// write pipeline metrics, include project dimension for backwards compat
	for name, _ := range r.Pipelines {
		data = append(data, r.Counts.asMetrics([]*cloudwatch.Dimension{
			{Name: aws.String("Project"), Value: aws.String(name)},
			{Name: aws.String("Pipeline"), Value: aws.String(name)},
		})...)
	}

	return data
}

func recentBuildkiteBuilds() ([]buildkite.Build, error) {
	url := fmt.Sprintf(
		"https://api.buildkite.com/v2/organizations/%s/builds?created_from=%s&page=%d",
		orgSlug,
		time.Now().UTC().Add(time.Minute*-5).Format("2006-01-02T15:04:05Z"),
		1,
	)

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}

	req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiKey))

	//Issue the request and get the bearer token from the JSON you get back
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("Failed to request %s", url)
	}

	// TODO: Pagination, but ain't nobody got time for that.
	// log.Printf("%#v", resp.Header.Get("Link"))

	var builds []buildkite.Build
	if err = json.NewDecoder(resp.Body).Decode(&builds); err != nil {
		return nil, err
	}

	return builds, nil
}

func chunkMetricData(size int, data []*cloudwatch.MetricDatum) [][]*cloudwatch.MetricDatum {
	var chunks = [][]*cloudwatch.MetricDatum{}
	for i := 0; i < len(data); i += size {
		end := i + size
		if end > len(data) {
			end = len(data)
		}
		chunks = append(chunks, data[i:end])
	}
	return chunks
}

func putMetricData(svc *cloudwatch.CloudWatch, data []*cloudwatch.MetricDatum) error {
	_, err := svc.PutMetricData(&cloudwatch.PutMetricDataInput{
		MetricData: data,
		Namespace:  aws.String("Buildkite"),
	})
	if err != nil {
		return err
	}

	return nil
}
<commit_msg>Use full import paths for subpackage<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/apex/go-apex"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/cloudwatch"
	"github.com/buildkite/buildkite-cloudwatch-metrics-publisher/functions/collect-metrics/buildkite"
)

var (
	orgSlug string
	apiKey  string
)

// Generates:
// Buildkite > RunningBuildsCount
// Buildkite > RunningJobsCount
// Buildkite > ScheduledBuildsCount
// Buildkite > ScheduledJobsCount
// Buildkite > (Queue) > RunningBuildsCount
// Buildkite > (Queue) > RunningJobsCount
// Buildkite > (Queue) > ScheduledBuildsCount
// Buildkite > (Queue) > ScheduledJobsCount
// Buildkite > (Pipeline) > RunningBuildsCount
// Buildkite > (Pipeline) > RunningJobsCount
// Buildkite > (Pipeline) > ScheduledBuildsCount
// Buildkite > (Pipeline) > ScheduledJobsCount

func main() {
	orgSlug = os.Getenv("BUILDKITE_ORG_SLUG")
	apiKey = os.Getenv("BUILDKITE_API_ACCESS_TOKEN")

	apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) {
		svc := cloudwatch.New(session.New())

		log.Printf("Querying buildkite for builds for org %s for past 5 mins", orgSlug)
		builds, err := recentBuildkiteBuilds()
		if err != nil {
			return nil, err
		}

		var res Result = Result{
			Queues:    map[string]Counts{},
			Pipelines: map[string]Counts{},
		}

		log.Printf("Aggregating results from %d builds", len(builds))
		for _, build := range builds {
			res.Counts = res.Counts.addBuild(build)
			res.Pipelines[build.Pipeline.Name] = res.Pipelines[build.Pipeline.Name].addBuild(build)

			var buildQueues = map[string]int{}
			for _, job := range build.Jobs {
				res.Counts = res.Counts.addJob(job)
				res.Pipelines[build.Pipeline.Name] = res.Pipelines[build.Pipeline.Name].addJob(job)
				res.Queues[job.Queue()] = res.Queues[job.Queue()].addJob(job)
				buildQueues[job.Queue()]++
			}

			for queue := range buildQueues {
				res.Queues[queue] = res.Queues[queue].addBuild(build)
			}
		}

		log.Printf("Extracting cloudwatch metrics from results")
		metrics := res.extractMetricData()

		for _, chunk := range chunkMetricData(10, metrics) {
			log.Printf("Submitting chunk of %d metrics to Cloudwatch", len(chunk))
			if err = putMetricData(svc, chunk); err != nil {
				return nil, err
			}
		}

		return res, nil
	})
}

type Counts struct {
	RunningBuilds, RunningJobs, ScheduledBuilds, ScheduledJobs int
}

func (c Counts) addBuild(build buildkite.Build) Counts {
	switch build.State {
	case "running":
		c.RunningBuilds++
	case "scheduled":
		c.ScheduledBuilds++
	}
	return c
}

func (c Counts) addJob(job buildkite.Job) Counts {
	switch job.State {
	case "running":
		c.RunningJobs++
	case "scheduled":
		c.ScheduledJobs++
	}
	return c
}

func (c Counts) asMetrics(dimensions []*cloudwatch.Dimension) []*cloudwatch.MetricDatum {
	return []*cloudwatch.MetricDatum{
		&cloudwatch.MetricDatum{
			MetricName: aws.String("RunningBuildsCount"),
			Dimensions: dimensions,
			Value:      aws.Float64(float64(c.RunningBuilds)),
			Unit:       aws.String("Count"),
		},
		&cloudwatch.MetricDatum{
			MetricName: aws.String("ScheduledBuildsCount"),
			Dimensions: dimensions,
			Value:      aws.Float64(float64(c.ScheduledBuilds)),
			Unit:       aws.String("Count"),
		},
		&cloudwatch.MetricDatum{
			MetricName: aws.String("RunningJobsCount"),
			Dimensions: dimensions,
			Value:      aws.Float64(float64(c.RunningJobs)),
			Unit:       aws.String("Count"),
		},
		&cloudwatch.MetricDatum{
			MetricName: aws.String("ScheduledJobsCount"),
			Dimensions: dimensions,
			Value:      aws.Float64(float64(c.ScheduledJobs)),
			Unit:       aws.String("Count"),
		},
	}
}

type Result struct {
	Counts
	Queues, Pipelines map[string]Counts
}

func (r Result) extractMetricData() []*cloudwatch.MetricDatum {
	data := []*cloudwatch.MetricDatum{}
	data = append(data, r.Counts.asMetrics(nil)...)

	for name, _ := range r.Queues {
		data = append(data, r.Counts.asMetrics([]*cloudwatch.Dimension{
			{Name: aws.String("Queue"), Value: aws.String(name)},
		})...)
	}

	// write pipeline metrics, include project dimension for backwards compat
	for name, _ := range r.Pipelines {
		data = append(data, r.Counts.asMetrics([]*cloudwatch.Dimension{
			{Name: aws.String("Project"), Value: aws.String(name)},
			{Name: aws.String("Pipeline"), Value: aws.String(name)},
		})...)
	}

	return data
}

func recentBuildkiteBuilds() ([]buildkite.Build, error) {
	url := fmt.Sprintf(
		"https://api.buildkite.com/v2/organizations/%s/builds?created_from=%s&page=%d",
		orgSlug,
		time.Now().UTC().Add(time.Minute*-5).Format("2006-01-02T15:04:05Z"),
		1,
	)

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}

	req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", apiKey))

	//Issue the request and get the bearer token from the JSON you get back
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("Failed to request %s", url)
	}

	// TODO: Pagination, but ain't nobody got time for that.
	// log.Printf("%#v", resp.Header.Get("Link"))

	var builds []buildkite.Build
	if err = json.NewDecoder(resp.Body).Decode(&builds); err != nil {
		return nil, err
	}

	return builds, nil
}

func chunkMetricData(size int, data []*cloudwatch.MetricDatum) [][]*cloudwatch.MetricDatum {
	var chunks = [][]*cloudwatch.MetricDatum{}
	for i := 0; i < len(data); i += size {
		end := i + size
		if end > len(data) {
			end = len(data)
		}
		chunks = append(chunks, data[i:end])
	}
	return chunks
}

func putMetricData(svc *cloudwatch.CloudWatch, data []*cloudwatch.MetricDatum) error {
	_, err := svc.PutMetricData(&cloudwatch.PutMetricDataInput{
		MetricData: data,
		Namespace:  aws.String("Buildkite"),
	})
	if err != nil {
		return err
	}

	return nil
}
<|endoftext|>


<commit_before>// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 gcscaching_test

import (
	"testing"
	"time"

	"github.com/jacobsa/gcloud/gcs"
	"github.com/jacobsa/gcloud/gcs/gcscaching"
	. "github.com/jacobsa/ogletest"
)

func TestStatCache(t *testing.T) { RunTests(t) }

////////////////////////////////////////////////////////////////////////
// Invariant-checking cache
////////////////////////////////////////////////////////////////////////

type invariantsCache struct {
	wrapped gcscaching.StatCache
}

func (c *invariantsCache) Insert(
	o *gcs.Object,
	expiration time.Time) {
	c.wrapped.CheckInvariants()
	defer c.wrapped.CheckInvariants()

	c.wrapped.Insert(o, expiration)
	return
}

func (c *invariantsCache) Erase(name string) {
	c.wrapped.CheckInvariants()
	defer c.wrapped.CheckInvariants()

	c.wrapped.Erase(name)
	return
}

func (c *invariantsCache) LookUp(name string, now time.Time) (o *gcs.Object) {
	c.wrapped.CheckInvariants()
	defer c.wrapped.CheckInvariants()

	o = c.wrapped.LookUp(name, now)
	return
}

////////////////////////////////////////////////////////////////////////
// Boilerplate
////////////////////////////////////////////////////////////////////////

const capacity = 3

var someTime = time.Date(2015, 4, 5, 2, 15, 0, 0, time.Local)
var expiration = someTime.Add(time.Second)

type StatCacheTest struct {
	cache invariantsCache
}

func init() { RegisterTestSuite(&StatCacheTest{}) }

func (t *StatCacheTest) SetUp(ti *TestInfo) {
	t.cache.wrapped = gcscaching.NewStatCache(capacity)
}

////////////////////////////////////////////////////////////////////////
// Test functions
////////////////////////////////////////////////////////////////////////

func (t *StatCacheTest) LookUpInEmptyCache() {
	ExpectEq(nil, t.cache.LookUp("", someTime))
	ExpectEq(nil, t.cache.LookUp("taco", someTime))
}

func (t *StatCacheTest) LookUpUnknownKey() {
	o0 := &gcs.Object{Name: "burrito"}
	o1 := &gcs.Object{Name: "taco"}

	t.cache.Insert(o0, someTime.Add(time.Second))
	t.cache.Insert(o1, someTime.Add(time.Second))

	ExpectEq(nil, t.cache.LookUp("", someTime))
	ExpectEq(nil, t.cache.LookUp("enchilada", someTime))
}

func (t *StatCacheTest) KeysPresentButEverythingIsExpired() {
	o0 := &gcs.Object{Name: "burrito"}
	o1 := &gcs.Object{Name: "taco"}

	t.cache.Insert(o0, someTime.Add(-time.Second))
	t.cache.Insert(o1, someTime.Add(-time.Second))

	ExpectEq(nil, t.cache.LookUp("burrito", someTime))
	ExpectEq(nil, t.cache.LookUp("taco", someTime))
}

func (t *StatCacheTest) FillUpToCapacity() {
	AssertEq(3, capacity)

	o0 := &gcs.Object{Name: "burrito"}
	o1 := &gcs.Object{Name: "taco"}
	o2 := &gcs.Object{Name: "enchilada"}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)
	t.cache.Insert(o2, expiration)

	// Before expiration
	justBefore := expiration.Add(-time.Nanosecond)
	ExpectEq(o0, t.cache.LookUp("burrito", justBefore))
	ExpectEq(o1, t.cache.LookUp("taco", justBefore))
	ExpectEq(o2, t.cache.LookUp("enchilada", justBefore))

	// At expiration
	ExpectEq(nil, t.cache.LookUp("burrito", expiration))
	ExpectEq(nil, t.cache.LookUp("taco", expiration))
	ExpectEq(nil, t.cache.LookUp("enchilada", expiration))

	// After expiration
	justAfter := expiration.Add(time.Nanosecond)
	ExpectEq(nil, t.cache.LookUp("burrito", justAfter))
	ExpectEq(nil, t.cache.LookUp("taco", justAfter))
	ExpectEq(nil, t.cache.LookUp("enchilada", justAfter))
}

func (t *StatCacheTest) ExpiresLeastRecentlyUsed() {
	AssertEq(3, capacity)

	o0 := &gcs.Object{Name: "burrito"}
	o1 := &gcs.Object{Name: "taco"}
	o2 := &gcs.Object{Name: "enchilada"}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)                    // Least recent
	t.cache.Insert(o2, expiration)                    // Second most recent
	AssertEq(o0, t.cache.LookUp("burrito", someTime)) // Most recent

	// Insert another.
	o3 := &gcs.Object{Name: "queso"}
	t.cache.Insert(o3, expiration)

	// See what's left.
	ExpectEq(nil, t.cache.LookUp("taco", someTime))
	ExpectEq(o0, t.cache.LookUp("burrito", someTime))
	ExpectEq(o2, t.cache.LookUp("enchilada", someTime))
	ExpectEq(o3, t.cache.LookUp("queso", someTime))
}

func (t *StatCacheTest) Overwrite_NewerGeneration() {
	o0 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}
	o1 := &gcs.Object{Name: "taco", Generation: 19, MetaGeneration: 1}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)

	ExpectEq(o1, t.cache.LookUp("taco", someTime))

	// The overwritten entry shouldn't count toward capacity.
	AssertEq(3, capacity)

	t.cache.Insert(&gcs.Object{Name: "burrito"}, expiration)
	t.cache.Insert(&gcs.Object{Name: "enchilada"}, expiration)

	ExpectNe(nil, t.cache.LookUp("taco", someTime))
	ExpectNe(nil, t.cache.LookUp("burrito", someTime))
	ExpectNe(nil, t.cache.LookUp("enchilada", someTime))
}

func (t *StatCacheTest) Overwrite_SameGeneration_NewerMetadataGen() {
	o0 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}
	o1 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 7}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)

	ExpectEq(o1, t.cache.LookUp("taco", someTime))

	// The overwritten entry shouldn't count toward capacity.
	AssertEq(3, capacity)

	t.cache.Insert(&gcs.Object{Name: "burrito"}, expiration)
	t.cache.Insert(&gcs.Object{Name: "enchilada"}, expiration)

	ExpectNe(nil, t.cache.LookUp("taco", someTime))
	ExpectNe(nil, t.cache.LookUp("burrito", someTime))
	ExpectNe(nil, t.cache.LookUp("enchilada", someTime))
}

func (t *StatCacheTest) Overwrite_SameGeneration_SameMetadataGen() {
	o0 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}
	o1 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)

	ExpectEq(o0, t.cache.LookUp("taco", someTime))
}

func (t *StatCacheTest) Overwrite_SameGeneration_OlderMetadataGen() {
	o0 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}
	o1 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 3}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)

	ExpectEq(o0, t.cache.LookUp("taco", someTime))
}

func (t *StatCacheTest) Overwrite_OlderGeneration() {
	o0 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}
	o1 := &gcs.Object{Name: "taco", Generation: 13, MetaGeneration: 7}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)

	ExpectEq(o0, t.cache.LookUp("taco", someTime))
}
<commit_msg>Fixed a test bug.<commit_after>// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 gcscaching_test

import (
	"testing"
	"time"

	"github.com/jacobsa/gcloud/gcs"
	"github.com/jacobsa/gcloud/gcs/gcscaching"
	. "github.com/jacobsa/ogletest"
)

func TestStatCache(t *testing.T) { RunTests(t) }

////////////////////////////////////////////////////////////////////////
// Invariant-checking cache
////////////////////////////////////////////////////////////////////////

type invariantsCache struct {
	wrapped gcscaching.StatCache
}

func (c *invariantsCache) Insert(
	o *gcs.Object,
	expiration time.Time) {
	c.wrapped.CheckInvariants()
	defer c.wrapped.CheckInvariants()

	c.wrapped.Insert(o, expiration)
	return
}

func (c *invariantsCache) Erase(name string) {
	c.wrapped.CheckInvariants()
	defer c.wrapped.CheckInvariants()

	c.wrapped.Erase(name)
	return
}

func (c *invariantsCache) LookUp(name string, now time.Time) (o *gcs.Object) {
	c.wrapped.CheckInvariants()
	defer c.wrapped.CheckInvariants()

	o = c.wrapped.LookUp(name, now)
	return
}

////////////////////////////////////////////////////////////////////////
// Boilerplate
////////////////////////////////////////////////////////////////////////

const capacity = 3

var someTime = time.Date(2015, 4, 5, 2, 15, 0, 0, time.Local)
var expiration = someTime.Add(time.Second)

type StatCacheTest struct {
	cache invariantsCache
}

func init() { RegisterTestSuite(&StatCacheTest{}) }

func (t *StatCacheTest) SetUp(ti *TestInfo) {
	t.cache.wrapped = gcscaching.NewStatCache(capacity)
}

////////////////////////////////////////////////////////////////////////
// Test functions
////////////////////////////////////////////////////////////////////////

func (t *StatCacheTest) LookUpInEmptyCache() {
	ExpectEq(nil, t.cache.LookUp("", someTime))
	ExpectEq(nil, t.cache.LookUp("taco", someTime))
}

func (t *StatCacheTest) LookUpUnknownKey() {
	o0 := &gcs.Object{Name: "burrito"}
	o1 := &gcs.Object{Name: "taco"}

	t.cache.Insert(o0, someTime.Add(time.Second))
	t.cache.Insert(o1, someTime.Add(time.Second))

	ExpectEq(nil, t.cache.LookUp("", someTime))
	ExpectEq(nil, t.cache.LookUp("enchilada", someTime))
}

func (t *StatCacheTest) KeysPresentButEverythingIsExpired() {
	o0 := &gcs.Object{Name: "burrito"}
	o1 := &gcs.Object{Name: "taco"}

	t.cache.Insert(o0, someTime.Add(-time.Second))
	t.cache.Insert(o1, someTime.Add(-time.Second))

	ExpectEq(nil, t.cache.LookUp("burrito", someTime))
	ExpectEq(nil, t.cache.LookUp("taco", someTime))
}

func (t *StatCacheTest) FillUpToCapacity() {
	AssertEq(3, capacity)

	o0 := &gcs.Object{Name: "burrito"}
	o1 := &gcs.Object{Name: "taco"}
	o2 := &gcs.Object{Name: "enchilada"}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)
	t.cache.Insert(o2, expiration)

	// Before expiration
	justBefore := expiration.Add(-time.Nanosecond)
	ExpectEq(o0, t.cache.LookUp("burrito", justBefore))
	ExpectEq(o1, t.cache.LookUp("taco", justBefore))
	ExpectEq(o2, t.cache.LookUp("enchilada", justBefore))

	// At expiration
	ExpectEq(o0, t.cache.LookUp("burrito", expiration))
	ExpectEq(o1, t.cache.LookUp("taco", expiration))
	ExpectEq(o2, t.cache.LookUp("enchilada", expiration))

	// After expiration
	justAfter := expiration.Add(time.Nanosecond)
	ExpectEq(nil, t.cache.LookUp("burrito", justAfter))
	ExpectEq(nil, t.cache.LookUp("taco", justAfter))
	ExpectEq(nil, t.cache.LookUp("enchilada", justAfter))
}

func (t *StatCacheTest) ExpiresLeastRecentlyUsed() {
	AssertEq(3, capacity)

	o0 := &gcs.Object{Name: "burrito"}
	o1 := &gcs.Object{Name: "taco"}
	o2 := &gcs.Object{Name: "enchilada"}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)                    // Least recent
	t.cache.Insert(o2, expiration)                    // Second most recent
	AssertEq(o0, t.cache.LookUp("burrito", someTime)) // Most recent

	// Insert another.
	o3 := &gcs.Object{Name: "queso"}
	t.cache.Insert(o3, expiration)

	// See what's left.
	ExpectEq(nil, t.cache.LookUp("taco", someTime))
	ExpectEq(o0, t.cache.LookUp("burrito", someTime))
	ExpectEq(o2, t.cache.LookUp("enchilada", someTime))
	ExpectEq(o3, t.cache.LookUp("queso", someTime))
}

func (t *StatCacheTest) Overwrite_NewerGeneration() {
	o0 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}
	o1 := &gcs.Object{Name: "taco", Generation: 19, MetaGeneration: 1}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)

	ExpectEq(o1, t.cache.LookUp("taco", someTime))

	// The overwritten entry shouldn't count toward capacity.
	AssertEq(3, capacity)

	t.cache.Insert(&gcs.Object{Name: "burrito"}, expiration)
	t.cache.Insert(&gcs.Object{Name: "enchilada"}, expiration)

	ExpectNe(nil, t.cache.LookUp("taco", someTime))
	ExpectNe(nil, t.cache.LookUp("burrito", someTime))
	ExpectNe(nil, t.cache.LookUp("enchilada", someTime))
}

func (t *StatCacheTest) Overwrite_SameGeneration_NewerMetadataGen() {
	o0 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}
	o1 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 7}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)

	ExpectEq(o1, t.cache.LookUp("taco", someTime))

	// The overwritten entry shouldn't count toward capacity.
	AssertEq(3, capacity)

	t.cache.Insert(&gcs.Object{Name: "burrito"}, expiration)
	t.cache.Insert(&gcs.Object{Name: "enchilada"}, expiration)

	ExpectNe(nil, t.cache.LookUp("taco", someTime))
	ExpectNe(nil, t.cache.LookUp("burrito", someTime))
	ExpectNe(nil, t.cache.LookUp("enchilada", someTime))
}

func (t *StatCacheTest) Overwrite_SameGeneration_SameMetadataGen() {
	o0 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}
	o1 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)

	ExpectEq(o0, t.cache.LookUp("taco", someTime))
}

func (t *StatCacheTest) Overwrite_SameGeneration_OlderMetadataGen() {
	o0 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}
	o1 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 3}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)

	ExpectEq(o0, t.cache.LookUp("taco", someTime))
}

func (t *StatCacheTest) Overwrite_OlderGeneration() {
	o0 := &gcs.Object{Name: "taco", Generation: 17, MetaGeneration: 5}
	o1 := &gcs.Object{Name: "taco", Generation: 13, MetaGeneration: 7}

	t.cache.Insert(o0, expiration)
	t.cache.Insert(o1, expiration)

	ExpectEq(o0, t.cache.LookUp("taco", someTime))
}
<|endoftext|>


<commit_before>package google

import (
	"fmt"
	"testing"

	"github.com/hashicorp/terraform/helper/acctest"
	"github.com/hashicorp/terraform/helper/resource"
)

func TestAccRedisInstance_basic(t *testing.T) {
	t.Parallel()

	name := acctest.RandomWithPrefix("tf-test")

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckComputeAddressDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccRedisInstance_basic(name),
			},
			resource.TestStep{
				ResourceName:      "google_redis_instance.test",
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccRedisInstance_full(t *testing.T) {
	t.Parallel()

	name := acctest.RandomWithPrefix("tf-test")
	network := acctest.RandomWithPrefix("tf-test")

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckComputeAddressDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccRedisInstance_full(name, network),
			},
			resource.TestStep{
				ResourceName:      "google_redis_instance.test",
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func testAccRedisInstance_basic(name string) string {
	return fmt.Sprintf(`
resource "google_redis_instance" "test" {
	name           = "%s"
	memory_size_gb = 1
}`, name)
}

func testAccRedisInstance_full(name, network string) string {
	return fmt.Sprintf(`
resource "google_compute_network" "test" {
	name = "%s"
}

resource "google_redis_instance" "test" {
	name           = "%s"
	tier           = "STANDARD_HA"
	memory_size_gb = 1

	region                  = "us-central1"
	location_id             = "us-central1-a"
	alternative_location_id = "us-central1-f"

	redis_version     = "REDIS_3_2"
	display_name      = "Terraform Test Instance"
	reserved_ip_range = "192.168.0.0/29"

	labels {
		my_key    = "my_val"
		other_key = "other_val"
	}
}`, name, network)
}
<commit_msg>redis tests are based in MM (#248)<commit_after>package google

import (
	"fmt"
	"testing"

	"github.com/hashicorp/terraform/helper/acctest"
	"github.com/hashicorp/terraform/helper/resource"
)

func TestAccRedisInstance_basic(t *testing.T) {
	t.Parallel()

	name := acctest.RandomWithPrefix("tf-test")

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckComputeAddressDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccRedisInstance_basic(name),
			},
			resource.TestStep{
				ResourceName:      "google_redis_instance.test",
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccRedisInstance_update(t *testing.T) {
	t.Parallel()

	name := acctest.RandomWithPrefix("tf-test")

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckComputeAddressDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccRedisInstance_update(name),
			},
			resource.TestStep{
				ResourceName:      "google_redis_instance.test",
				ImportState:       true,
				ImportStateVerify: true,
			},
			resource.TestStep{
				Config: testAccRedisInstance_update2(name),
			},
			resource.TestStep{
				ResourceName:      "google_redis_instance.test",
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccRedisInstance_full(t *testing.T) {
	t.Parallel()

	name := acctest.RandomWithPrefix("tf-test")
	network := acctest.RandomWithPrefix("tf-test")

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckComputeAddressDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccRedisInstance_full(name, network),
			},
			resource.TestStep{
				ResourceName:      "google_redis_instance.test",
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func testAccRedisInstance_basic(name string) string {
	return fmt.Sprintf(`
resource "google_redis_instance" "test" {
	name           = "%s"
	memory_size_gb = 1
}`, name)
}

func testAccRedisInstance_update(name string) string {
	return fmt.Sprintf(`
resource "google_redis_instance" "test" {
	name           = "%s"
	display_name   = "pre-update"
	memory_size_gb = 1

	labels {
		my_key    = "my_val"
		other_key = "other_val"
	}
}`, name)
}

func testAccRedisInstance_update2(name string) string {
	return fmt.Sprintf(`
resource "google_redis_instance" "test" {
	name           = "%s"
	display_name   = "post-update"
	memory_size_gb = 1

	labels {
		my_key    = "my_val"
		other_key = "new_val"
	}
}`, name)
}

func testAccRedisInstance_full(name, network string) string {
	return fmt.Sprintf(`
resource "google_compute_network" "test" {
	name = "%s"
}

resource "google_redis_instance" "test" {
	name           = "%s"
	tier           = "STANDARD_HA"
	memory_size_gb = 1

	region                  = "us-central1"
	location_id             = "us-central1-a"
	alternative_location_id = "us-central1-f"

	redis_version     = "REDIS_3_2"
	display_name      = "Terraform Test Instance"
	reserved_ip_range = "192.168.0.0/29"

	labels {
		my_key    = "my_val"
		other_key = "other_val"
	}
}`, name, network)
}
<|endoftext|>


<commit_before>package testutil

import (
	"fmt"
	"io/ioutil"

	"github.com/GoogleContainerTools/kpt-functions-catalog/thirdparty/kyaml/fnsdk"
	"sigs.k8s.io/kustomize/kyaml/kio"
)

// ResourceListFromFile reads a yaml file and converts it to a fnsdk.ResourceList.
func ResourceListFromFile(path string) (*fnsdk.ResourceList, error) {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}

	return fnsdk.ParseResourceList(content)
}

// ResourceListFromDirectory reads yaml files from dir and functionConfig file,
// and then assemble them as a fnsdk.ResourceList.
func ResourceListFromDirectory(dir string, fnConfigFile string) (*fnsdk.ResourceList, error) {
	reader := &kio.LocalPackageReader{
		PackagePath:        dir,
		IncludeSubpackages: true,
	}
	items, err := reader.Read()
	if err != nil {
		return nil, fmt.Errorf("unable to read resources from %v: %w", dir, err)
	}

	rl := &fnsdk.ResourceList{
		Items: fnsdk.NewFromRNodes(items),
	}

	if fnConfigFile != "" {
		content, err := ioutil.ReadFile(fnConfigFile)
		if err != nil {
			return nil, err
		}
		fnConfig, err := fnsdk.ParseKubeObject(content)
		if err != nil {
			return nil, fmt.Errorf("unable to parse the functionConfig object: %w", err)
		}
		rl.FunctionConfig = fnConfig
	}
	return rl, nil
}

// ResourceListToDirectory write ResourceList.items to yaml files according to
// the path annotation (https://github.com/kubernetes-sigs/kustomize/blob/master/cmd/config/docs/api-conventions/functions-spec.md#internalconfigkubernetesiopath).
func ResourceListToDirectory(rl *fnsdk.ResourceList, dir string) error {
	writer := &kio.LocalPackageWriter{
		PackagePath: dir,
	}
	err := writer.Write(fnsdk.ToRNodes(rl.Items))
	if err != nil {
		return fmt.Errorf("unable to write resources to %v: %w", dir, err)
	}
	return nil
}
<commit_msg>Don't add path and index annotation in golden test util (#725)<commit_after>package testutil

import (
	"fmt"
	"io/ioutil"

	"github.com/GoogleContainerTools/kpt-functions-catalog/thirdparty/kyaml/fnsdk"
	"sigs.k8s.io/kustomize/kyaml/kio"
)

// ResourceListFromFile reads a yaml file and converts it to a fnsdk.ResourceList.
func ResourceListFromFile(path string) (*fnsdk.ResourceList, error) {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}

	return fnsdk.ParseResourceList(content)
}

// ResourceListFromDirectory reads yaml files from dir and functionConfig file,
// and then assemble them as a fnsdk.ResourceList.
func ResourceListFromDirectory(dir string, fnConfigFile string) (*fnsdk.ResourceList, error) {
	reader := &kio.LocalPackageReader{
		PackagePath:        dir,
		IncludeSubpackages: true,
		// TODO(mengqiy): We should figure out how to let the user control this behavior.
		OmitReaderAnnotations: true,
	}
	items, err := reader.Read()
	if err != nil {
		return nil, fmt.Errorf("unable to read resources from %v: %w", dir, err)
	}

	rl := &fnsdk.ResourceList{
		Items: fnsdk.NewFromRNodes(items),
	}

	if fnConfigFile != "" {
		content, err := ioutil.ReadFile(fnConfigFile)
		if err != nil {
			return nil, err
		}
		fnConfig, err := fnsdk.ParseKubeObject(content)
		if err != nil {
			return nil, fmt.Errorf("unable to parse the functionConfig object: %w", err)
		}
		rl.FunctionConfig = fnConfig
	}
	return rl, nil
}

// ResourceListToDirectory write ResourceList.items to yaml files according to
// the path annotation (https://github.com/kubernetes-sigs/kustomize/blob/master/cmd/config/docs/api-conventions/functions-spec.md#internalconfigkubernetesiopath).
func ResourceListToDirectory(rl *fnsdk.ResourceList, dir string) error {
	writer := &kio.LocalPackageWriter{
		PackagePath: dir,
	}
	err := writer.Write(fnsdk.ToRNodes(rl.Items))
	if err != nil {
		return fmt.Errorf("unable to write resources to %v: %w", dir, err)
	}
	return nil
}
<|endoftext|>


<commit_before>package estimator

import (
	"github.com/youtube/vitess/go/ewma"
	"sync"
	"testing"
)

func TestEstimator(t *testing.T) {
	wg := &sync.WaitGroup{}
	e := NewEstimator(1000, 0.8)
	input := map[string][]float64{
		"select aa from t_test where col1=:bv1":              []float64{200000, 210000, 201000, 197000},
		"select bb from t_test where col1=:bv1 or col2=:bv2": []float64{900000, 1100000, 950000, 970000, 990000},
		"select * from t_test_small":                         []float64{10000, 11000, 9000},
	}
	output := map[string]float64{
		"select aa from t_test where col1=:bv1":              200840,
		"select bb from t_test where col1=:bv1 or col2=:bv2": 956080,
		"select * from t_test_small":                         9960,
	}
	// Record history
	for k, v := range input {
		wg.Add(1)
		go func(key string, values []float64) {
			defer wg.Done()
			for _, data := range values {
				e.AddHistory(key, data)
			}
		}(k, v)
	}
	wg.Wait()
	// Validata calculation
	for k, v := range output {
		if ev := e.Estimate(k); ev != v {
			t.Errorf("Expect the estimated value of key %v to be %v, but got %v", k, v, ev)
		}
	}
	// Test invalid arguments to NewEstimator
	e = NewEstimator(0, 0.8)
	if ca := e.records.Capacity(); ca != DefaultCapacity {
		t.Errorf("Expect Estimator to have default capacity(%v), but got %v", DefaultCapacity, ca)
	}
	e = NewEstimator(10, -0.1)
	if e.weightingFactor != ewma.DefaultWeightingFactor {
		t.Errorf("Expect Estimator to have default weighting factor(%v), but got %v", ewma.DefaultWeightingFactor, e.weightingFactor)
	}
}
<commit_msg>go format changes<commit_after>package estimator

import (
	"sync"
	"testing"

	"github.com/youtube/vitess/go/ewma"
)

func TestEstimator(t *testing.T) {
	wg := &sync.WaitGroup{}
	e := NewEstimator(1000, 0.8)
	input := map[string][]float64{
		"select aa from t_test where col1=:bv1":              []float64{200000, 210000, 201000, 197000},
		"select bb from t_test where col1=:bv1 or col2=:bv2": []float64{900000, 1100000, 950000, 970000, 990000},
		"select * from t_test_small":                         []float64{10000, 11000, 9000},
	}
	output := map[string]float64{
		"select aa from t_test where col1=:bv1":              200840,
		"select bb from t_test where col1=:bv1 or col2=:bv2": 956080,
		"select * from t_test_small":                         9960,
	}
	// Record history
	for k, v := range input {
		wg.Add(1)
		go func(key string, values []float64) {
			defer wg.Done()
			for _, data := range values {
				e.AddHistory(key, data)
			}
		}(k, v)
	}
	wg.Wait()
	// Validata calculation
	for k, v := range output {
		if ev := e.Estimate(k); ev != v {
			t.Errorf("Expect the estimated value of key %v to be %v, but got %v", k, v, ev)
		}
	}
	// Test invalid arguments to NewEstimator
	e = NewEstimator(0, 0.8)
	if ca := e.records.Capacity(); ca != DefaultCapacity {
		t.Errorf("Expect Estimator to have default capacity(%v), but got %v", DefaultCapacity, ca)
	}
	e = NewEstimator(10, -0.1)
	if e.weightingFactor != ewma.DefaultWeightingFactor {
		t.Errorf("Expect Estimator to have default weighting factor(%v), but got %v", ewma.DefaultWeightingFactor, e.weightingFactor)
	}
}
<|endoftext|>


<commit_before>// Package events defines the structures used for events dispatched from the
// wrangler package.
package events

import (
	"github.com/youtube/vitess/go/event"
	"github.com/youtube/vitess/go/vt/topo"
)

// Reparent is an event that describes a single step in the reparent process.
type Reparent struct {
	ShardInfo topo.ShardInfo

	OldMaster, NewMaster topo.Tablet

	Status string
}

// UpdateStatus sets a new status and then dispatches the event.
func (r *Reparent) UpdateStatus(status string) {
	r.Status = status

	// make a copy since we're calling Dispatch asynchronously
	ev := *r
	go event.Dispatch(&ev)
}
<commit_msg>Use synchronous Dispatch to avoid dropped events.<commit_after>// Package events defines the structures used for events dispatched from the
// wrangler package.
package events

import (
	"github.com/youtube/vitess/go/event"
	"github.com/youtube/vitess/go/vt/topo"
)

// Reparent is an event that describes a single step in the reparent process.
type Reparent struct {
	ShardInfo topo.ShardInfo

	OldMaster, NewMaster topo.Tablet

	Status string
}

// UpdateStatus sets a new status and then dispatches the event.
func (r *Reparent) UpdateStatus(status string) {
	r.Status = status

	// Dispatch must be synchronous here to avoid dropping events that are
	// queued up just before main() returns.
	event.Dispatch(r)
}
<|endoftext|>


<commit_before>// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !msan

// Dummy MSan support API, used when not built with -msan.

package runtime

import (
	"unsafe"
)

const msanenabled = false

// Because msanenabled is false, none of these functions should be called.

func msanread(addr unsafe.Pointer, sz uintptr)   { throw("msan") }
func msanwrite(addr unsafe.Pointer, sz uintptr)  { throw("msan") }
func msanmalloc(addr unsafe.Pointer, sz uintptr) { throw("msan") }
func msanfree(addr unsafe.Pointer, sz uintptr)   { throw("msan") }
<commit_msg>runtime: define dummy msanmove<commit_after>// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !msan

// Dummy MSan support API, used when not built with -msan.

package runtime

import (
	"unsafe"
)

const msanenabled = false

// Because msanenabled is false, none of these functions should be called.

func msanread(addr unsafe.Pointer, sz uintptr)     { throw("msan") }
func msanwrite(addr unsafe.Pointer, sz uintptr)    { throw("msan") }
func msanmalloc(addr unsafe.Pointer, sz uintptr)   { throw("msan") }
func msanfree(addr unsafe.Pointer, sz uintptr)     { throw("msan") }
func msanmove(dst, src unsafe.Pointer, sz uintptr) { throw("msan") }
<|endoftext|>


<commit_before>/*
Copyright 2018 The Kubernetes Authors.

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 framework

import (
	"fmt"
	"regexp"
	"sync"

	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/client-go/dynamic"
	clientset "k8s.io/client-go/kubernetes"
	"k8s.io/perf-tests/clusterloader2/pkg/framework/client"
	"k8s.io/perf-tests/clusterloader2/pkg/framework/config"

	// ensure auth plugins are loaded
	_ "k8s.io/client-go/plugin/pkg/client/auth"
)

const (
	// AutomanagedNamespaceName is a basename for automanaged namespaces.
	AutomanagedNamespaceName = "namespace"
)

// Framework allows for interacting with Kubernetes cluster via
// official Kubernetes client.
type Framework struct {
	automanagedNamespaceCount int
	clientSet                 clientset.Interface
	dynamicClient             dynamic.Interface
}

// NewFramework creates new framework based on given kubeconfig.
func NewFramework(kubeconfigPath string) (*Framework, error) {
	conf, err := config.PrepareConfig(kubeconfigPath)
	if err != nil {
		return nil, fmt.Errorf("config prepare failed: %v", err)
	}
	clientSet, err := clientset.NewForConfig(conf)
	if err != nil {
		return nil, fmt.Errorf("creating clientset failed: %v", err)
	}
	dynamicClient, err := dynamic.NewForConfig(conf)
	if err != nil {
		return nil, fmt.Errorf("creating dynamic config failed: %v", err)
	}
	return &Framework{
		automanagedNamespaceCount: 0,
		clientSet:                 clientSet,
		dynamicClient:             dynamicClient,
	}, nil
}

// GetClientSet returns clientSet client.
func (f *Framework) GetClientSet() clientset.Interface {
	return f.clientSet
}

// CreateAutomanagedNamespaces creates automanged namespaces.
func (f *Framework) CreateAutomanagedNamespaces(namespaceCount int) error {
	if f.automanagedNamespaceCount != 0 {
		return fmt.Errorf("automanaged namespaces already created")
	}
	for i := 1; i <= namespaceCount; i++ {
		name := fmt.Sprintf("%v-%d", AutomanagedNamespaceName, i)
		if err := client.CreateNamespace(f.clientSet, name); err != nil {
			return err
		}
		f.automanagedNamespaceCount++
	}
	return nil
}

// ListAutomanagedNamespaces returns all existing automanged namespace names.
func (f *Framework) ListAutomanagedNamespaces() ([]string, error) {
	var automanagedNamespacesList []string
	namespacesList, err := client.ListNamespaces(f.clientSet)
	if err != nil {
		return automanagedNamespacesList, err
	}
	for _, namespace := range namespacesList {
		matched, err := isAutomanagedNamespace(namespace.Name)
		if err != nil {
			return automanagedNamespacesList, err
		}
		if matched {
			automanagedNamespacesList = append(automanagedNamespacesList, namespace.Name)
		}
	}
	return automanagedNamespacesList, nil
}

// DeleteAutomanagedNamespaces deletes all automanged namespaces.
func (f *Framework) DeleteAutomanagedNamespaces() []error {
	var wg wait.Group
	var lock sync.Mutex
	var errList []error
	for i := 1; i <= f.automanagedNamespaceCount; i++ {
		name := fmt.Sprintf("%v-%d", AutomanagedNamespaceName, i)
		wg.Start(func() {
			if err := client.DeleteNamespace(f.clientSet, name); err != nil {
				lock.Lock()
				defer lock.Unlock()
				errList = append(errList, err)
				return
			}
			if err := client.WaitForDeleteNamespace(f.clientSet, name); err != nil {
				lock.Lock()
				defer lock.Unlock()
				errList = append(errList, err)
			}
		})
	}
	wg.Wait()
	return errList
}

// CreateObject creates object base on given object description.
func (f *Framework) CreateObject(namespace string, name string, obj *unstructured.Unstructured) error {
	return client.CreateObject(f.dynamicClient, namespace, name, obj)
}

// PatchObject updates object (using patch) with given name using given object description.
func (f *Framework) PatchObject(namespace string, name string, obj *unstructured.Unstructured) error {
	return client.PatchObject(f.dynamicClient, namespace, name, obj)
}

// DeleteObject deletes object with given name and group-version-kind.
func (f *Framework) DeleteObject(gvk schema.GroupVersionKind, namespace string, name string) error {
	return client.DeleteObject(f.dynamicClient, gvk, namespace, name)
}

// GetObject retrieves object with given name and group-version-kind.
func (f *Framework) GetObject(gvk schema.GroupVersionKind, namespace string, name string) (*unstructured.Unstructured, error) {
	return client.GetObject(f.dynamicClient, gvk, namespace, name)
}

func isAutomanagedNamespace(name string) (bool, error) {
	return regexp.MatchString(AutomanagedNamespaceName+"-[1-9][0-9]*", name)
}
<commit_msg>fixing namespace cleanup<commit_after>/*
Copyright 2018 The Kubernetes Authors.

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 framework

import (
	"fmt"
	"regexp"
	"sync"

	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/client-go/dynamic"
	clientset "k8s.io/client-go/kubernetes"
	"k8s.io/perf-tests/clusterloader2/pkg/framework/client"
	"k8s.io/perf-tests/clusterloader2/pkg/framework/config"

	// ensure auth plugins are loaded
	_ "k8s.io/client-go/plugin/pkg/client/auth"
)

const (
	// AutomanagedNamespaceName is a basename for automanaged namespaces.
	AutomanagedNamespaceName = "namespace"
)

// Framework allows for interacting with Kubernetes cluster via
// official Kubernetes client.
type Framework struct {
	automanagedNamespaceCount int
	clientSet                 clientset.Interface
	dynamicClient             dynamic.Interface
}

// NewFramework creates new framework based on given kubeconfig.
func NewFramework(kubeconfigPath string) (*Framework, error) {
	conf, err := config.PrepareConfig(kubeconfigPath)
	if err != nil {
		return nil, fmt.Errorf("config prepare failed: %v", err)
	}
	clientSet, err := clientset.NewForConfig(conf)
	if err != nil {
		return nil, fmt.Errorf("creating clientset failed: %v", err)
	}
	dynamicClient, err := dynamic.NewForConfig(conf)
	if err != nil {
		return nil, fmt.Errorf("creating dynamic config failed: %v", err)
	}
	return &Framework{
		automanagedNamespaceCount: 0,
		clientSet:                 clientSet,
		dynamicClient:             dynamicClient,
	}, nil
}

// GetClientSet returns clientSet client.
func (f *Framework) GetClientSet() clientset.Interface {
	return f.clientSet
}

// CreateAutomanagedNamespaces creates automanged namespaces.
func (f *Framework) CreateAutomanagedNamespaces(namespaceCount int) error {
	if f.automanagedNamespaceCount != 0 {
		return fmt.Errorf("automanaged namespaces already created")
	}
	for i := 1; i <= namespaceCount; i++ {
		name := fmt.Sprintf("%v-%d", AutomanagedNamespaceName, i)
		if err := client.CreateNamespace(f.clientSet, name); err != nil {
			return err
		}
		f.automanagedNamespaceCount++
	}
	return nil
}

// ListAutomanagedNamespaces returns all existing automanged namespace names.
func (f *Framework) ListAutomanagedNamespaces() ([]string, error) {
	var automanagedNamespacesList []string
	namespacesList, err := client.ListNamespaces(f.clientSet)
	if err != nil {
		return automanagedNamespacesList, err
	}
	for _, namespace := range namespacesList {
		matched, err := isAutomanagedNamespace(namespace.Name)
		if err != nil {
			return automanagedNamespacesList, err
		}
		if matched {
			automanagedNamespacesList = append(automanagedNamespacesList, namespace.Name)
		}
	}
	return automanagedNamespacesList, nil
}

// DeleteAutomanagedNamespaces deletes all automanged namespaces.
func (f *Framework) DeleteAutomanagedNamespaces() []error {
	var wg wait.Group
	var lock sync.Mutex
	var errList []error
	for i := 1; i <= f.automanagedNamespaceCount; i++ {
		name := fmt.Sprintf("%v-%d", AutomanagedNamespaceName, i)
		wg.Start(func() {
			if err := client.DeleteNamespace(f.clientSet, name); err != nil {
				lock.Lock()
				defer lock.Unlock()
				errList = append(errList, err)
				return
			}
			if err := client.WaitForDeleteNamespace(f.clientSet, name); err != nil {
				lock.Lock()
				defer lock.Unlock()
				errList = append(errList, err)
			}
		})
	}
	wg.Wait()
	f.automanagedNamespaceCount = 0
	return errList
}

// CreateObject creates object base on given object description.
func (f *Framework) CreateObject(namespace string, name string, obj *unstructured.Unstructured) error {
	return client.CreateObject(f.dynamicClient, namespace, name, obj)
}

// PatchObject updates object (using patch) with given name using given object description.
func (f *Framework) PatchObject(namespace string, name string, obj *unstructured.Unstructured) error {
	return client.PatchObject(f.dynamicClient, namespace, name, obj)
}

// DeleteObject deletes object with given name and group-version-kind.
func (f *Framework) DeleteObject(gvk schema.GroupVersionKind, namespace string, name string) error {
	return client.DeleteObject(f.dynamicClient, gvk, namespace, name)
}

// GetObject retrieves object with given name and group-version-kind.
func (f *Framework) GetObject(gvk schema.GroupVersionKind, namespace string, name string) (*unstructured.Unstructured, error) {
	return client.GetObject(f.dynamicClient, gvk, namespace, name)
}

func isAutomanagedNamespace(name string) (bool, error) {
	return regexp.MatchString(AutomanagedNamespaceName+"-[1-9][0-9]*", name)
}
<|endoftext|>


<commit_before>/*
Copyright 2016 The Kubernetes Authors.

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 remote

import (
	"fmt"
	"path/filepath"
	"strings"

	"k8s.io/klog/v2"
)

// utils.go contains functions used across test suites.

const (
	cniVersion       = "v0.8.7"
	cniArch          = "amd64"
	cniDirectory     = "cni/bin" // The CNI tarball places binaries under directory under "cni/bin".
	cniConfDirectory = "cni/net.d"
	cniURL           = "https://storage.googleapis.com/k8s-artifacts-cni/release/" + cniVersion + "/" + "cni-plugins-linux-" + cniArch + "-" + cniVersion + ".tgz"
)

const cniConfig = `{
  "name": "mynet",
  "type": "bridge",
  "bridge": "mynet0",
  "isDefaultGateway": true,
  "forceAddress": false,
  "ipMasq": true,
  "hairpinMode": true,
  "ipam": {
    "type": "host-local",
    "subnet": "10.10.0.0/16"
  }
}
`

// Install the cni plugin and add basic bridge configuration to the
// configuration directory.
func setupCNI(host, workspace string) error {
	klog.V(2).Infof("Install CNI on %q", host)
	cniPath := filepath.Join(workspace, cniDirectory)
	cmd := getSSHCommand(" ; ",
		fmt.Sprintf("mkdir -p %s", cniPath),
		fmt.Sprintf("curl -s -L %s | tar -xz -C %s", cniURL, cniPath),
	)
	if output, err := SSH(host, "sh", "-c", cmd); err != nil {
		return fmt.Errorf("failed to install cni plugin on %q: %v output: %q", host, err, output)
	}

	// The added CNI network config is not needed for kubenet. It is only
	// used when testing the CNI network plugin, but is added in both cases
	// for consistency and simplicity.
	klog.V(2).Infof("Adding CNI configuration on %q", host)
	cniConfigPath := filepath.Join(workspace, cniConfDirectory)
	cmd = getSSHCommand(" ; ",
		fmt.Sprintf("mkdir -p %s", cniConfigPath),
		fmt.Sprintf("echo %s > %s", quote(cniConfig), filepath.Join(cniConfigPath, "mynet.conf")),
	)
	if output, err := SSH(host, "sh", "-c", cmd); err != nil {
		return fmt.Errorf("failed to write cni configuration on %q: %v output: %q", host, err, output)
	}
	return nil
}

// configureFirewall configures iptable firewall rules.
func configureFirewall(host string) error {
	klog.V(2).Infof("Configure iptables firewall rules on %q", host)
	// TODO: consider calling bootstrap script to configure host based on OS
	output, err := SSH(host, "iptables", "-L", "INPUT")
	if err != nil {
		return fmt.Errorf("failed to get iptables INPUT on %q: %v output: %q", host, err, output)
	}
	if strings.Contains(output, "Chain INPUT (policy DROP)") {
		cmd := getSSHCommand("&&",
			"(iptables -C INPUT -w -p TCP -j ACCEPT || iptables -A INPUT -w -p TCP -j ACCEPT)",
			"(iptables -C INPUT -w -p UDP -j ACCEPT || iptables -A INPUT -w -p UDP -j ACCEPT)",
			"(iptables -C INPUT -w -p ICMP -j ACCEPT || iptables -A INPUT -w -p ICMP -j ACCEPT)")
		output, err := SSH(host, "sh", "-c", cmd)
		if err != nil {
			return fmt.Errorf("failed to configured firewall on %q: %v output: %v", host, err, output)
		}
	}
	output, err = SSH(host, "iptables", "-L", "FORWARD")
	if err != nil {
		return fmt.Errorf("failed to get iptables FORWARD on %q: %v output: %q", host, err, output)
	}
	if strings.Contains(output, "Chain FORWARD (policy DROP)") {
		cmd := getSSHCommand("&&",
			"(iptables -C FORWARD -w -p TCP -j ACCEPT || iptables -A FORWARD -w -p TCP -j ACCEPT)",
			"(iptables -C FORWARD -w -p UDP -j ACCEPT || iptables -A FORWARD -w -p UDP -j ACCEPT)",
			"(iptables -C FORWARD -w -p ICMP -j ACCEPT || iptables -A FORWARD -w -p ICMP -j ACCEPT)")
		output, err = SSH(host, "sh", "-c", cmd)
		if err != nil {
			return fmt.Errorf("failed to configured firewall on %q: %v output: %v", host, err, output)
		}
	}
	return nil
}

// cleanupNodeProcesses kills all running node processes may conflict with the test.
func cleanupNodeProcesses(host string) {
	klog.V(2).Infof("Killing any existing node processes on %q", host)
	cmd := getSSHCommand(" ; ",
		"pkill kubelet",
		"pkill kube-apiserver",
		"pkill etcd",
		"pkill e2e_node.test",
	)
	// No need to log an error if pkill fails since pkill will fail if the commands are not running.
	// If we are unable to stop existing running k8s processes, we should see messages in the kubelet/apiserver/etcd
	// logs about failing to bind the required ports.
	SSH(host, "sh", "-c", cmd)
}

// Quotes a shell literal so it can be nested within another shell scope.
func quote(s string) string {
	return fmt.Sprintf("'\"'\"'%s'\"'\"'", s)
}
<commit_msg>Verify iptable rules are applied for tcp, udp and icmp<commit_after>/*
Copyright 2016 The Kubernetes Authors.

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 remote

import (
	"fmt"
	"path/filepath"

	"k8s.io/klog/v2"
)

// utils.go contains functions used across test suites.

const (
	cniVersion       = "v0.8.7"
	cniArch          = "amd64"
	cniDirectory     = "cni/bin" // The CNI tarball places binaries under directory under "cni/bin".
	cniConfDirectory = "cni/net.d"
	cniURL           = "https://storage.googleapis.com/k8s-artifacts-cni/release/" + cniVersion + "/" + "cni-plugins-linux-" + cniArch + "-" + cniVersion + ".tgz"
)

const cniConfig = `{
  "name": "mynet",
  "type": "bridge",
  "bridge": "mynet0",
  "isDefaultGateway": true,
  "forceAddress": false,
  "ipMasq": true,
  "hairpinMode": true,
  "ipam": {
    "type": "host-local",
    "subnet": "10.10.0.0/16"
  }
}
`

// Install the cni plugin and add basic bridge configuration to the
// configuration directory.
func setupCNI(host, workspace string) error {
	klog.V(2).Infof("Install CNI on %q", host)
	cniPath := filepath.Join(workspace, cniDirectory)
	cmd := getSSHCommand(" ; ",
		fmt.Sprintf("mkdir -p %s", cniPath),
		fmt.Sprintf("curl -s -L %s | tar -xz -C %s", cniURL, cniPath),
	)
	if output, err := SSH(host, "sh", "-c", cmd); err != nil {
		return fmt.Errorf("failed to install cni plugin on %q: %v output: %q", host, err, output)
	}

	// The added CNI network config is not needed for kubenet. It is only
	// used when testing the CNI network plugin, but is added in both cases
	// for consistency and simplicity.
	klog.V(2).Infof("Adding CNI configuration on %q", host)
	cniConfigPath := filepath.Join(workspace, cniConfDirectory)
	cmd = getSSHCommand(" ; ",
		fmt.Sprintf("mkdir -p %s", cniConfigPath),
		fmt.Sprintf("echo %s > %s", quote(cniConfig), filepath.Join(cniConfigPath, "mynet.conf")),
	)
	if output, err := SSH(host, "sh", "-c", cmd); err != nil {
		return fmt.Errorf("failed to write cni configuration on %q: %v output: %q", host, err, output)
	}
	return nil
}

// configureFirewall configures iptable firewall rules.
func configureFirewall(host string) error {
	klog.V(2).Infof("Configure iptables HEYHO firewall rules on %q", host)

	// Since the goal is to enable connectivity without taking into account current rule,
	// we can just prepend the accept rules directly without any check
	cmd := getSSHCommand("&&",
		"iptables -I INPUT 1 -w -p tcp -j ACCEPT",
		"iptables -I INPUT 1 -w -p udp -j ACCEPT",
		"iptables -I INPUT 1 -w -p icmp -j ACCEPT",
		"iptables -I FORWARD 1 -w -p tcp -j ACCEPT",
		"iptables -I FORWARD 1 -w -p udp -j ACCEPT",
		"iptables -I FORWARD 1 -w -p icmp -j ACCEPT",
	)
	output, err := SSH(host, "sh", "-c", cmd)
	if err != nil {
		return fmt.Errorf("failed to configured firewall on %q: %v output: %v", host, err, output)
	}
	return nil
}

// cleanupNodeProcesses kills all running node processes may conflict with the test.
func cleanupNodeProcesses(host string) {
	klog.V(2).Infof("Killing any existing node processes on %q", host)
	cmd := getSSHCommand(" ; ",
		"pkill kubelet",
		"pkill kube-apiserver",
		"pkill etcd",
		"pkill e2e_node.test",
	)
	// No need to log an error if pkill fails since pkill will fail if the commands are not running.
	// If we are unable to stop existing running k8s processes, we should see messages in the kubelet/apiserver/etcd
	// logs about failing to bind the required ports.
	SSH(host, "sh", "-c", cmd)
}

// Quotes a shell literal so it can be nested within another shell scope.
func quote(s string) string {
	return fmt.Sprintf("'\"'\"'%s'\"'\"'", s)
}
<|endoftext|>