<commit_before>/*
The MIT License (MIT)

Copyright (c) 2016 ZiRo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package main

import (
	"crypto/rand"
	"crypto/sha256"
	"encoding/base64"
	"flag"
	"fmt"
	"github.com/ZiRo-/cuckgo/cuckoo"
	"sync"
)

const MAXPATHLEN = 4096
const RANDOFFS = 64
const MAXLEN = 1024

type CuckooSolve struct {
	graph    *cuckoo.Cuckoo
	easiness int
	cuckoo   []int
	sols     [][]int
	nsols    int
	nthreads int
}

func NewCuckooSolve(hdr []byte, en, ms, nt int) *CuckooSolve {
	self := &CuckooSolve{
		graph:    cuckoo.NewCuckoo(hdr),
		easiness: en,
		sols:     make([][]int, 2*ms), //this isn't completley safe for high easiness
		cuckoo:   make([]int, 1+int(cuckoo.SIZE)),
		nsols:    0,
		nthreads: 1,
	}
	for i := range self.sols {
		self.sols[i] = make([]int, cuckoo.PROOFSIZE)
	}
	return self
}

func (self *CuckooSolve) path(u int, us []int, done chan int) int {
	nu := 0
	for nu = 0; u != 0; u = self.cuckoo[u] {
		nu++
		if nu >= MAXPATHLEN {
			for nu != 0 && us[nu-1] != u {
				nu--
			}
			if nu < 0 {
				//fmt.Println("maximum path length exceeded")
			} else {
				//fmt.Println("illegal", (MAXPATHLEN - nu), "-cycle")
			}
			close(done)
			return -1
		}
		us[nu] = u
	}
	return nu
}

func (self *CuckooSolve) solution(us []int, nu int, vs []int, nv int) {
	cycle := make(map[int]*cuckoo.Edge)
	n := 0
	edg := &cuckoo.Edge{uint64(us[0]), uint64(vs[0]) - cuckoo.HALFSIZE}
	cycle[edg.HashCode()] = edg
	for nu != 0 { // u's in even position; v's in odd
		nu--
		edg := &cuckoo.Edge{uint64(us[(nu+1)&^1]), uint64(us[nu|1]) - cuckoo.HALFSIZE}
		_, has := cycle[edg.HashCode()]
		if !has {
			cycle[edg.HashCode()] = edg
		}
	}
	for nv != 0 { // u's in odd position; v's in even
		nv--
		edg := &cuckoo.Edge{uint64(vs[nv|1]), uint64(vs[(nv+1)&^1]) - cuckoo.HALFSIZE}
		_, has := cycle[edg.HashCode()]
		if !has {
			cycle[edg.HashCode()] = edg
		}
	}
	n = 0
	for nonce := 0; nonce < self.easiness; nonce++ {
		e := self.graph.Sipedge(uint64(nonce))
		has, key := contains(cycle, e)
		if has {
			self.sols[self.nsols][n] = nonce
			n++
			delete(cycle, key)
		}
	}
	if uint64(n) == cuckoo.PROOFSIZE {
		self.nsols++
	} else {
		//fmt.Println("Only recovered ", n, " nonces")
	}
}

func contains(m map[int]*cuckoo.Edge, e *cuckoo.Edge) (bool, int) {
	h := e.HashCode()
	for k, v := range m {
		if k == h && v.U == e.U && v.V == e.V { //fuck Java for making me waste time just to figure out that that's how Java does contains
			return true, k
		}
	}
	return false, 0
}

func worker(id int, solve *CuckooSolve, done chan int) {
	cuck := solve.cuckoo
	us := make([]int, MAXPATHLEN)
	vs := make([]int, MAXPATHLEN)
	for nonce := id; nonce < solve.easiness; nonce += solve.nthreads {
		us[0] = (int)(solve.graph.Sipnode(uint64(nonce), 0))
		u := cuck[us[0]]
		vs[0] = (int)(cuckoo.HALFSIZE + solve.graph.Sipnode(uint64(nonce), 1))
		v := cuck[vs[0]]
		if u == vs[0] || v == us[0] {
			continue // ignore duplicate edges
		}
		nu := solve.path(u, us, done)
		nv := solve.path(v, vs, done)

		if nu == -1 || nv == -1 {
			return
		}

		if us[nu] == vs[nv] {
			min := 0
			if nu < nv {
				min = nu
			} else {
				min = nv
			}
			nu -= min
			nv -= min
			for us[nu] != vs[nv] {
				nu++
				nv++
			}
			length := nu + nv + 1
			//fmt.Println(" " , length , "-cycle found at " , id , ":" , (int)(nonce*100/solve.easiness) , "%")
			if uint64(length) == cuckoo.PROOFSIZE && solve.nsols < len(solve.sols) {
				solve.solution(us, nu, vs, nv)
			}
			continue
		}
		if nu < nv {
			for nu != 0 {
				nu--
				cuck[us[nu+1]] = us[nu]
			}
			cuck[us[0]] = vs[0]
		} else {
			for nv != 0 {
				nv--
				cuck[vs[nv+1]] = vs[nv]
			}
			cuck[vs[0]] = us[0]
		}
	}
	close(done)
}

var nthreads int
var maxsols int
var easipct float64

//var header string

func init() {
	flag.IntVar(&nthreads, "t", 4, "number of miner threads")
	flag.IntVar(&maxsols, "m", 8, "maximum number of solutions")
	flag.Float64Var(&easipct, "e", 50.0, "easiness in percentage")
	//flag.StringVar(&header, "h", "cuck", "Header")
}

func main() {
	flag.Parse()
	//fmt.Println("Looking for", cuckoo.PROOFSIZE, "-cycle on cuckoo", cuckoo.SIZESHIFT, "with", easipct, "% edges and", nthreads, "threads")

	b := make([]byte, RANDOFFS)
	_, err := rand.Read(b)
	if err != nil {
		panic(err)
	}
	easy := int(easipct * float64(cuckoo.SIZE) / 100.0)
	solve := NewCuckooSolve(b, easy, maxsols, 1)

	for k := 0; k < MAXLEN-RANDOFFS; k++ {
		b = append(b, 0)
		for i := 0; i < 256; i += nthreads {

			cs := make([]chan int, nthreads)
			solvers := make([]*CuckooSolve, nthreads)
			for n := 0; n < nthreads; n++ {
				cs[n] = make(chan int)
			}
			out := merge(cs...)

			for n := 0; n < nthreads; n++ {
				if i+n < 256 {
					b[RANDOFFS+k] = byte(i + n)
					solvers[n] = NewCuckooSolve(b, easy, maxsols, 1)
					go worker(i, solvers[n], cs[n])
				} else {
					close(cs[n])
				}
			}

			<-out

			for n, s := range solvers {
				if s.nsols > 0 {
					b[RANDOFFS+k] = byte(i + n)
					solve = s
					goto done
				}
			}
		}
	}

done:
	/* for s := 0; s < solve.nsols; s++ {
		//fmt.Print("Solution")
		for i := 0; uint64(i) < cuckoo.PROOFSIZE; i++ {
			fmt.Printf(" %x", solve.sols[s][i])
		}
		fmt.Println()
	}*/
	if len(solve.sols) > 0 {
		c := formatProof(solve, b)
		//fmt.Println(c)
		json, _ := cuckoo.EncodeCuckooJSON(c)
		//fmt.Println(json)
		str := base64.StdEncoding.EncodeToString(json)
		fmt.Println(str)
	} else {
		fmt.Println("No Solution found.")
	}
}

func formatProof(solve *CuckooSolve, b []byte) cuckoo.CuckooJSON {
	sha := sha256.Sum256(b)
	easy := uint64(solve.easiness)
	cycle := make([]uint64, len(solve.sols[0]))
	m := make(map[string]uint64)
	m["easiness"] = easy

	for i, n := range solve.sols[0] {
		cycle[i] = uint64(n)
	}

	return cuckoo.CuckooJSON{m, sha[:], cycle}
}

func merge(cs ...chan int) chan int {
	var wg sync.WaitGroup
	out := make(chan int)

	// Start an output goroutine for each input channel in cs.  output
	// copies values from c to out until c is closed, then calls wg.Done.
	output := func(c chan int) {
		for n := range c {
			out <- n
		}
		wg.Done()
	}
	wg.Add(len(cs))
	for _, c := range cs {
		go output(c)
	}

	// Start a goroutine to close out once all the output goroutines are
	// done.  This must start after the wg.Add call.
	go func() {
		wg.Wait()
		close(out)
	}()
	return out
}
<commit_msg>user number of cpus for default number of threads<commit_after>/*
The MIT License (MIT)

Copyright (c) 2016 ZiRo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package main

import (
	"crypto/rand"
	"crypto/sha256"
	"encoding/base64"
	"flag"
	"fmt"
	"github.com/ZiRo-/cuckgo/cuckoo"
	"runtime"
	"sync"
)

const MAXPATHLEN = 4096
const RANDOFFS = 64
const MAXLEN = 1024

type CuckooSolve struct {
	graph    *cuckoo.Cuckoo
	easiness int
	cuckoo   []int
	sols     [][]int
	nsols    int
	nthreads int
}

func NewCuckooSolve(hdr []byte, en, ms, nt int) *CuckooSolve {
	self := &CuckooSolve{
		graph:    cuckoo.NewCuckoo(hdr),
		easiness: en,
		sols:     make([][]int, 2*ms), //this isn't completley safe for high easiness
		cuckoo:   make([]int, 1+int(cuckoo.SIZE)),
		nsols:    0,
		nthreads: 1,
	}
	for i := range self.sols {
		self.sols[i] = make([]int, cuckoo.PROOFSIZE)
	}
	return self
}

func (self *CuckooSolve) path(u int, us []int, done chan int) int {
	nu := 0
	for nu = 0; u != 0; u = self.cuckoo[u] {
		nu++
		if nu >= MAXPATHLEN {
			for nu != 0 && us[nu-1] != u {
				nu--
			}
			if nu < 0 {
				//fmt.Println("maximum path length exceeded")
			} else {
				//fmt.Println("illegal", (MAXPATHLEN - nu), "-cycle")
			}
			close(done)
			return -1
		}
		us[nu] = u
	}
	return nu
}

func (self *CuckooSolve) solution(us []int, nu int, vs []int, nv int) {
	cycle := make(map[int]*cuckoo.Edge)
	n := 0
	edg := &cuckoo.Edge{uint64(us[0]), uint64(vs[0]) - cuckoo.HALFSIZE}
	cycle[edg.HashCode()] = edg
	for nu != 0 { // u's in even position; v's in odd
		nu--
		edg := &cuckoo.Edge{uint64(us[(nu+1)&^1]), uint64(us[nu|1]) - cuckoo.HALFSIZE}
		_, has := cycle[edg.HashCode()]
		if !has {
			cycle[edg.HashCode()] = edg
		}
	}
	for nv != 0 { // u's in odd position; v's in even
		nv--
		edg := &cuckoo.Edge{uint64(vs[nv|1]), uint64(vs[(nv+1)&^1]) - cuckoo.HALFSIZE}
		_, has := cycle[edg.HashCode()]
		if !has {
			cycle[edg.HashCode()] = edg
		}
	}
	n = 0
	for nonce := 0; nonce < self.easiness; nonce++ {
		e := self.graph.Sipedge(uint64(nonce))
		has, key := contains(cycle, e)
		if has {
			self.sols[self.nsols][n] = nonce
			n++
			delete(cycle, key)
		}
	}
	if uint64(n) == cuckoo.PROOFSIZE {
		self.nsols++
	} else {
		//fmt.Println("Only recovered ", n, " nonces")
	}
}

func contains(m map[int]*cuckoo.Edge, e *cuckoo.Edge) (bool, int) {
	h := e.HashCode()
	for k, v := range m {
		if k == h && v.U == e.U && v.V == e.V { //fuck Java for making me waste time just to figure out that that's how Java does contains
			return true, k
		}
	}
	return false, 0
}

func worker(id int, solve *CuckooSolve, done chan int) {
	cuck := solve.cuckoo
	us := make([]int, MAXPATHLEN)
	vs := make([]int, MAXPATHLEN)
	for nonce := id; nonce < solve.easiness; nonce += solve.nthreads {
		us[0] = (int)(solve.graph.Sipnode(uint64(nonce), 0))
		u := cuck[us[0]]
		vs[0] = (int)(cuckoo.HALFSIZE + solve.graph.Sipnode(uint64(nonce), 1))
		v := cuck[vs[0]]
		if u == vs[0] || v == us[0] {
			continue // ignore duplicate edges
		}
		nu := solve.path(u, us, done)
		nv := solve.path(v, vs, done)

		if nu == -1 || nv == -1 {
			return
		}

		if us[nu] == vs[nv] {
			min := 0
			if nu < nv {
				min = nu
			} else {
				min = nv
			}
			nu -= min
			nv -= min
			for us[nu] != vs[nv] {
				nu++
				nv++
			}
			length := nu + nv + 1
			//fmt.Println(" " , length , "-cycle found at " , id , ":" , (int)(nonce*100/solve.easiness) , "%")
			if uint64(length) == cuckoo.PROOFSIZE && solve.nsols < len(solve.sols) {
				solve.solution(us, nu, vs, nv)
			}
			continue
		}
		if nu < nv {
			for nu != 0 {
				nu--
				cuck[us[nu+1]] = us[nu]
			}
			cuck[us[0]] = vs[0]
		} else {
			for nv != 0 {
				nv--
				cuck[vs[nv+1]] = vs[nv]
			}
			cuck[vs[0]] = us[0]
		}
	}
	close(done)
}

var nthreads int
var maxsols int
var easipct float64

//var header string

func init() {
	flag.IntVar(&nthreads, "t", 0, "number of miner threads")
	flag.IntVar(&maxsols, "m", 8, "maximum number of solutions")
	flag.Float64Var(&easipct, "e", 50.0, "easiness in percentage")
	//flag.StringVar(&header, "h", "cuck", "Header")
}

func main() {
	flag.Parse()
	if (nthreads < 1) {
		nthreads = runtime.NumCPU()
	}
	//fmt.Println("Looking for", cuckoo.PROOFSIZE, "-cycle on cuckoo", cuckoo.SIZESHIFT, "with", easipct, "% edges and", nthreads, "threads")

	b := make([]byte, RANDOFFS)
	_, err := rand.Read(b)
	if err != nil {
		panic(err)
	}
	easy := int(easipct * float64(cuckoo.SIZE) / 100.0)
	solve := NewCuckooSolve(b, easy, maxsols, 1)

	for k := 0; k < MAXLEN-RANDOFFS; k++ {
		b = append(b, 0)
		for i := 0; i < 256; i += nthreads {

			cs := make([]chan int, nthreads)
			solvers := make([]*CuckooSolve, nthreads)
			for n := 0; n < nthreads; n++ {
				cs[n] = make(chan int)
			}
			out := merge(cs...)

			for n := 0; n < nthreads; n++ {
				if i+n < 256 {
					b[RANDOFFS+k] = byte(i + n)
					solvers[n] = NewCuckooSolve(b, easy, maxsols, 1)
					go worker(i, solvers[n], cs[n])
				} else {
					close(cs[n])
				}
			}

			<-out

			for n, s := range solvers {
				if s.nsols > 0 {
					b[RANDOFFS+k] = byte(i + n)
					solve = s
					goto done
				}
			}
		}
	}

done:
	/* for s := 0; s < solve.nsols; s++ {
		//fmt.Print("Solution")
		for i := 0; uint64(i) < cuckoo.PROOFSIZE; i++ {
			fmt.Printf(" %x", solve.sols[s][i])
		}
		fmt.Println()
	}*/
	if len(solve.sols) > 0 {
		c := formatProof(solve, b)
		//fmt.Println(c)
		json, _ := cuckoo.EncodeCuckooJSON(c)
		//fmt.Println(json)
		str := base64.StdEncoding.EncodeToString(json)
		fmt.Println(str)
	} else {
		fmt.Println("No Solution found.")
	}
}

func formatProof(solve *CuckooSolve, b []byte) cuckoo.CuckooJSON {
	sha := sha256.Sum256(b)
	easy := uint64(solve.easiness)
	cycle := make([]uint64, len(solve.sols[0]))
	m := make(map[string]uint64)
	m["easiness"] = easy

	for i, n := range solve.sols[0] {
		cycle[i] = uint64(n)
	}

	return cuckoo.CuckooJSON{m, sha[:], cycle}
}

func merge(cs ...chan int) chan int {
	var wg sync.WaitGroup
	out := make(chan int)

	// Start an output goroutine for each input channel in cs.  output
	// copies values from c to out until c is closed, then calls wg.Done.
	output := func(c chan int) {
		for n := range c {
			out <- n
		}
		wg.Done()
	}
	wg.Add(len(cs))
	for _, c := range cs {
		go output(c)
	}

	// Start a goroutine to close out once all the output goroutines are
	// done.  This must start after the wg.Add call.
	go func() {
		wg.Wait()
		close(out)
	}()
	return out
}
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"regexp"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/mediaconvert"

	"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
	"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
	"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccAWSMediaConvertQueue_basic(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_Basic(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					testAccMatchResourceAttrRegionalARN(resourceName, "arn", "mediaconvert", regexp.MustCompile(`queues/.+`)),
					resource.TestCheckResourceAttr(resourceName, "name", rName),
					resource.TestCheckResourceAttr(resourceName, "pricing_plan", mediaconvert.PricingPlanOnDemand),
					resource.TestCheckResourceAttr(resourceName, "status", mediaconvert.QueueStatusActive),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAWSMediaConvertReservedQueue_basic(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_ReservedQueue(rName, mediaconvert.CommitmentOneYear, mediaconvert.RenewalTypeAutoRenew, 1),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "pricing_plan", mediaconvert.PricingPlanReserved),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.#", "1"),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.commitment", "1"),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.commitment", mediaconvert.CommitmentOneYear),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.renewal_type", mediaconvert.RenewalTypeAutoRenew),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.reserved_slots", "1"),
				),
			},
			{
				Config: testAccMediaConvertQueueConfig_ReservedQueue(rName, mediaconvert.CommitmentOneYear, mediaconvert.RenewalTypeExpire, 2),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "pricing_plan", mediaconvert.PricingPlanReserved),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.#", "1"),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.commitment", "1"),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.commitment", mediaconvert.CommitmentOneYear),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.renewal_type", mediaconvert.RenewalTypeExpire),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.reserved_slots", "1"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAWSMediaConvertQueue_withStatus(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_withStatus(rName, mediaconvert.QueueStatusPaused),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "status", mediaconvert.QueueStatusPaused),
				),
			},
			{
				Config: testAccMediaConvertQueueConfig_withStatus(rName, mediaconvert.QueueStatusActive),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "status", mediaconvert.QueueStatusActive),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAWSMediaConvertQueue_withTags(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_withTags(rName, "foo", "bar", "fizz", "buzz"),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
					resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar"),
					resource.TestCheckResourceAttr(resourceName, "tags.fizz", "buzz"),
				),
			},
			{
				Config: testAccMediaConvertQueueConfig_withTags(rName, "foo", "bar2", "fizz2", "buzz2"),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
					resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar2"),
					resource.TestCheckResourceAttr(resourceName, "tags.fizz2", "buzz2"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
			{
				Config: testAccMediaConvertQueueConfig_Basic(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
				),
			},
		},
	})
}

func TestAccAWSMediaConvertQueue_disappears(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_Basic(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					testAccCheckAwsMediaConvertQueueDisappears(&queue),
				),
				ExpectNonEmptyPlan: true,
			},
		},
	})
}

func TestAccAWSMediaConvertQueue_withDescription(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")
	description1 := acctest.RandomWithPrefix("Description: ")
	description2 := acctest.RandomWithPrefix("Description: ")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_withDescription(rName, description1),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "description", description1),
				),
			},
			{
				Config: testAccMediaConvertQueueConfig_withDescription(rName, description2),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "description", description2),
				),
			},
		},
	})
}

func testAccCheckAwsMediaConvertQueueDestroy(s *terraform.State) error {
	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_media_convert_queue" {
			continue
		}
		originalConn := testAccProvider.Meta().(*AWSClient).mediaconvertconn
		endpointURL, err := getAwsMediaConvertEndpoint(originalConn)
		if err != nil {
			return fmt.Errorf("Error getting Media Convert Endpoint: %s", err)
		}

		sess, err := session.NewSession(&originalConn.Config)
		if err != nil {
			return fmt.Errorf("Error creating AWS session: %s", err)
		}
		conn := mediaconvert.New(sess.Copy(&aws.Config{Endpoint: aws.String(endpointURL)}))

		_, err = conn.GetQueue(&mediaconvert.GetQueueInput{
			Name: aws.String(rs.Primary.ID),
		})
		if err != nil {
			if isAWSErr(err, mediaconvert.ErrCodeNotFoundException, "") {
				continue
			}
			return err
		}
	}

	return nil
}

func testAccCheckAwsMediaConvertQueueDisappears(queue *mediaconvert.Queue) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		originalConn := testAccProvider.Meta().(*AWSClient).mediaconvertconn
		endpointURL, err := getAwsMediaConvertEndpoint(originalConn)
		if err != nil {
			return fmt.Errorf("Error getting Media Convert Endpoint: %s", err)
		}

		sess, err := session.NewSession(&originalConn.Config)
		if err != nil {
			return fmt.Errorf("Error creating AWS session: %s", err)
		}
		conn := mediaconvert.New(sess.Copy(&aws.Config{Endpoint: aws.String(endpointURL)}))

		_, err = conn.DeleteQueue(&mediaconvert.DeleteQueueInput{
			Name: queue.Name,
		})
		if err != nil {
			return fmt.Errorf("Deleting Media Convert Queue: %s", err)
		}
		return nil
	}
}

func testAccCheckAwsMediaConvertQueueExists(n string, queue *mediaconvert.Queue) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No Queue id is set")
		}

		originalConn := testAccProvider.Meta().(*AWSClient).mediaconvertconn
		endpointURL, err := getAwsMediaConvertEndpoint(originalConn)
		if err != nil {
			return fmt.Errorf("Error getting Media Convert Endpoint: %s", err)
		}

		sess, err := session.NewSession(&originalConn.Config)
		if err != nil {
			return fmt.Errorf("Error creating AWS session: %s", err)
		}
		conn := mediaconvert.New(sess.Copy(&aws.Config{Endpoint: aws.String(endpointURL)}))

		resp, err := conn.GetQueue(&mediaconvert.GetQueueInput{
			Name: aws.String(rs.Primary.ID),
		})
		if err != nil {
			return fmt.Errorf("Error getting queue: %s", err)
		}

		*queue = *resp.Queue
		return nil
	}
}

func testAccMediaConvertQueueConfig_Basic(rName string) string {
	return fmt.Sprintf(`
resource "aws_media_convert_queue" "test" {
  name = %[1]q
}

`, rName)
}

func testAccMediaConvertQueueConfig_withStatus(rName, status string) string {
	return fmt.Sprintf(`
resource "aws_media_convert_queue" "test" {
  name   = %[1]q
  status = %[2]q
}

`, rName, status)
}

func testAccMediaConvertQueueConfig_withDescription(rName, description string) string {
	return fmt.Sprintf(`
resource "aws_media_convert_queue" "test" {
  name        = %[1]q
  description = %[2]q
}

`, rName, description)
}

func testAccMediaConvertQueueConfig_withTags(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
	return fmt.Sprintf(`
resource "aws_media_convert_queue" "test" {
  name   = %[1]q

  tags = {
	  %[2]s = %[3]q
	  %[4]s = %[5]q
  }
}

`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

func testAccMediaConvertQueueConfig_ReservedQueue(rName, commitment, renewalType string, reservedSlots int) string {
	return fmt.Sprintf(`
resource "aws_media_convert_queue" "test" {
  name         = %[1]q
  pricing_plan = %[2]q

  reservation_plan_settings = {
	commitment     = %[3]q
	renewal_type   = %[4]q
	reserved_slots = %[5]d
  }
}

`, rName, mediaconvert.PricingPlanReserved, commitment, renewalType, reservedSlots)
}
<commit_msg>fix test method name<commit_after>package aws

import (
	"fmt"
	"regexp"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/mediaconvert"

	"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
	"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
	"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccAWSMediaConvertQueue_basic(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_Basic(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					testAccMatchResourceAttrRegionalARN(resourceName, "arn", "mediaconvert", regexp.MustCompile(`queues/.+`)),
					resource.TestCheckResourceAttr(resourceName, "name", rName),
					resource.TestCheckResourceAttr(resourceName, "pricing_plan", mediaconvert.PricingPlanOnDemand),
					resource.TestCheckResourceAttr(resourceName, "status", mediaconvert.QueueStatusActive),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAWSMediaConvertQueue_ReservationPlanSettings(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_ReservedQueue(rName, mediaconvert.CommitmentOneYear, mediaconvert.RenewalTypeAutoRenew, 1),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "pricing_plan", mediaconvert.PricingPlanReserved),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.#", "1"),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.commitment", "1"),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.commitment", mediaconvert.CommitmentOneYear),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.renewal_type", mediaconvert.RenewalTypeAutoRenew),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.reserved_slots", "1"),
				),
			},
			{
				Config: testAccMediaConvertQueueConfig_ReservedQueue(rName, mediaconvert.CommitmentOneYear, mediaconvert.RenewalTypeExpire, 2),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "pricing_plan", mediaconvert.PricingPlanReserved),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.#", "1"),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.commitment", "1"),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.commitment", mediaconvert.CommitmentOneYear),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.renewal_type", mediaconvert.RenewalTypeExpire),
					resource.TestCheckResourceAttr(resourceName, "reservation_plan_settings.0.reserved_slots", "1"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAWSMediaConvertQueue_withStatus(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_withStatus(rName, mediaconvert.QueueStatusPaused),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "status", mediaconvert.QueueStatusPaused),
				),
			},
			{
				Config: testAccMediaConvertQueueConfig_withStatus(rName, mediaconvert.QueueStatusActive),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "status", mediaconvert.QueueStatusActive),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccAWSMediaConvertQueue_withTags(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_withTags(rName, "foo", "bar", "fizz", "buzz"),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
					resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar"),
					resource.TestCheckResourceAttr(resourceName, "tags.fizz", "buzz"),
				),
			},
			{
				Config: testAccMediaConvertQueueConfig_withTags(rName, "foo", "bar2", "fizz2", "buzz2"),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
					resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar2"),
					resource.TestCheckResourceAttr(resourceName, "tags.fizz2", "buzz2"),
				),
			},
			{
				ResourceName:      resourceName,
				ImportState:       true,
				ImportStateVerify: true,
			},
			{
				Config: testAccMediaConvertQueueConfig_Basic(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
				),
			},
		},
	})
}

func TestAccAWSMediaConvertQueue_disappears(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_Basic(rName),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					testAccCheckAwsMediaConvertQueueDisappears(&queue),
				),
				ExpectNonEmptyPlan: true,
			},
		},
	})
}

func TestAccAWSMediaConvertQueue_withDescription(t *testing.T) {
	var queue mediaconvert.Queue
	resourceName := "aws_media_convert_queue.test"
	rName := acctest.RandomWithPrefix("tf-acc-test")
	description1 := acctest.RandomWithPrefix("Description: ")
	description2 := acctest.RandomWithPrefix("Description: ")

	resource.ParallelTest(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAwsMediaConvertQueueDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccMediaConvertQueueConfig_withDescription(rName, description1),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "description", description1),
				),
			},
			{
				Config: testAccMediaConvertQueueConfig_withDescription(rName, description2),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAwsMediaConvertQueueExists(resourceName, &queue),
					resource.TestCheckResourceAttr(resourceName, "description", description2),
				),
			},
		},
	})
}

func testAccCheckAwsMediaConvertQueueDestroy(s *terraform.State) error {
	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_media_convert_queue" {
			continue
		}
		originalConn := testAccProvider.Meta().(*AWSClient).mediaconvertconn
		endpointURL, err := getAwsMediaConvertEndpoint(originalConn)
		if err != nil {
			return fmt.Errorf("Error getting Media Convert Endpoint: %s", err)
		}

		sess, err := session.NewSession(&originalConn.Config)
		if err != nil {
			return fmt.Errorf("Error creating AWS session: %s", err)
		}
		conn := mediaconvert.New(sess.Copy(&aws.Config{Endpoint: aws.String(endpointURL)}))

		_, err = conn.GetQueue(&mediaconvert.GetQueueInput{
			Name: aws.String(rs.Primary.ID),
		})
		if err != nil {
			if isAWSErr(err, mediaconvert.ErrCodeNotFoundException, "") {
				continue
			}
			return err
		}
	}

	return nil
}

func testAccCheckAwsMediaConvertQueueDisappears(queue *mediaconvert.Queue) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		originalConn := testAccProvider.Meta().(*AWSClient).mediaconvertconn
		endpointURL, err := getAwsMediaConvertEndpoint(originalConn)
		if err != nil {
			return fmt.Errorf("Error getting Media Convert Endpoint: %s", err)
		}

		sess, err := session.NewSession(&originalConn.Config)
		if err != nil {
			return fmt.Errorf("Error creating AWS session: %s", err)
		}
		conn := mediaconvert.New(sess.Copy(&aws.Config{Endpoint: aws.String(endpointURL)}))

		_, err = conn.DeleteQueue(&mediaconvert.DeleteQueueInput{
			Name: queue.Name,
		})
		if err != nil {
			return fmt.Errorf("Deleting Media Convert Queue: %s", err)
		}
		return nil
	}
}

func testAccCheckAwsMediaConvertQueueExists(n string, queue *mediaconvert.Queue) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No Queue id is set")
		}

		originalConn := testAccProvider.Meta().(*AWSClient).mediaconvertconn
		endpointURL, err := getAwsMediaConvertEndpoint(originalConn)
		if err != nil {
			return fmt.Errorf("Error getting Media Convert Endpoint: %s", err)
		}

		sess, err := session.NewSession(&originalConn.Config)
		if err != nil {
			return fmt.Errorf("Error creating AWS session: %s", err)
		}
		conn := mediaconvert.New(sess.Copy(&aws.Config{Endpoint: aws.String(endpointURL)}))

		resp, err := conn.GetQueue(&mediaconvert.GetQueueInput{
			Name: aws.String(rs.Primary.ID),
		})
		if err != nil {
			return fmt.Errorf("Error getting queue: %s", err)
		}

		*queue = *resp.Queue
		return nil
	}
}

func testAccMediaConvertQueueConfig_Basic(rName string) string {
	return fmt.Sprintf(`
resource "aws_media_convert_queue" "test" {
  name = %[1]q
}

`, rName)
}

func testAccMediaConvertQueueConfig_withStatus(rName, status string) string {
	return fmt.Sprintf(`
resource "aws_media_convert_queue" "test" {
  name   = %[1]q
  status = %[2]q
}

`, rName, status)
}

func testAccMediaConvertQueueConfig_withDescription(rName, description string) string {
	return fmt.Sprintf(`
resource "aws_media_convert_queue" "test" {
  name        = %[1]q
  description = %[2]q
}

`, rName, description)
}

func testAccMediaConvertQueueConfig_withTags(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
	return fmt.Sprintf(`
resource "aws_media_convert_queue" "test" {
  name   = %[1]q

  tags = {
	  %[2]s = %[3]q
	  %[4]s = %[5]q
  }
}

`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}

func testAccMediaConvertQueueConfig_ReservedQueue(rName, commitment, renewalType string, reservedSlots int) string {
	return fmt.Sprintf(`
resource "aws_media_convert_queue" "test" {
  name         = %[1]q
  pricing_plan = %[2]q

  reservation_plan_settings = {
	commitment     = %[3]q
	renewal_type   = %[4]q
	reserved_slots = %[5]d
  }
}

`, rName, mediaconvert.PricingPlanReserved, commitment, renewalType, reservedSlots)
}
<|endoftext|>


<commit_before>package lmm

import (
	"encoding/json"
	"github.com/Symantec/scotty/store"
	"github.com/Symantec/tricorder/go/tricorder/messages"
	"github.com/Symantec/tricorder/go/tricorder/types"
	"github.com/Symantec/tricorder/go/tricorder/units"
	"github.com/optiopay/kafka"
	"github.com/optiopay/kafka/proto"
	"strconv"
	"time"
)

const (
	kVersion   = "@version"
	kTimestamp = "@timestamp"
	kValue     = "value"
	kName      = "name"
	kHost      = "host"
	kTenantId  = "tenant_id"
	kApiKey    = "apikey"
)

var (
	supportedTypes = map[types.Type]bool{
		types.Bool:       true,
		types.Int:        true,
		types.Uint:       true,
		types.Float:      true,
		types.GoTime:     true,
		types.GoDuration: true,
	}
)

func isTypeSupported(t types.Type) bool {
	return supportedTypes[t]
}

func newWriter(topic string, tenantId, apiKey string, addresses []string) (
	result *Writer, err error) {
	var writer Writer
	writer.topic = topic
	writer.tenantId = tenantId
	writer.apiKey = apiKey
	writer.broker, err = kafka.Dial(addresses, kafka.NewBrokerConf("test"))
	if err != nil {
		return
	}
	var count int32
	count, err = writer.broker.PartitionCount(topic)
	if err != nil {
		return
	}
	conf := kafka.NewProducerConf()
	conf.RequiredAcks = proto.RequiredAcksLocal
	producer := writer.broker.Producer(conf)
	writer.producer = kafka.NewRoundRobinProducer(producer, count)
	result = &writer
	return
}

func (w *Writer) write(records []*store.Record) (err error) {
	serializer := newRecordSerializer(w.tenantId, w.apiKey)
	msgs := make([]*proto.Message, len(records))
	for i := range records {
		var payload []byte
		payload, err = serializer.Serialize(recordType{records[i]})
		if err != nil {
			return
		}
		msgs[i] = &proto.Message{Value: payload}
	}
	_, err = w.producer.Distribute(w.topic, msgs...)
	return
}

type iRecordType interface {
	Kind() types.Type
	Unit() units.Unit
	Timestamp() float64
	Value() interface{}
	Path() string
	HostName() string
}

type recordType struct {
	*store.Record
}

func (r recordType) Kind() types.Type {
	return r.Info().Kind()
}

func (r recordType) Unit() units.Unit {
	return r.Info().Unit()
}

func (r recordType) Path() string {
	return r.Info().Path()
}

func (r recordType) HostName() string {
	return r.MachineId().HostName()
}

// recordSerializerType serializes a record to bytes for LMM.
// Warning, instances of this type are not thread safe.
type recordSerializerType struct {
	record       map[string]string
	formatString string
}

func newRecordSerializer(tenantId, apiKey string) *recordSerializerType {
	return &recordSerializerType{
		record: map[string]string{
			kVersion:  "1",
			kTenantId: tenantId,
			kApiKey:   apiKey},
		formatString: "2006-01-02T15:04:05.000Z"}
}

func (s *recordSerializerType) Serialize(r iRecordType) ([]byte, error) {
	kind := r.Kind()

	if !isTypeSupported(kind) {
		panic("Cannot record given kind.")
	}
	s.record[kTimestamp] = messages.FloatToTime(
		r.Timestamp()).Format(s.formatString)
	switch kind {
	case types.Bool:
		if r.Value().(bool) {
			s.record[kValue] = "1"
		} else {
			s.record[kValue] = "0"
		}
	case types.Int:
		s.record[kValue] = strconv.FormatInt(r.Value().(int64), 10)
	case types.Uint:
		s.record[kValue] = strconv.FormatUint(r.Value().(uint64), 10)
	case types.Float:
		s.record[kValue] = strconv.FormatFloat(
			r.Value().(float64), 'f', -1, 64)
	case types.GoTime:
		s.record[kValue] = strconv.FormatFloat(
			messages.TimeToFloat(r.Value().(time.Time)),
			'f', -1, 64)
	case types.GoDuration:
		s.record[kValue] = strconv.FormatFloat(
			messages.DurationToFloat(
				r.Value().(time.Duration))*units.FromSeconds(r.Unit()),
			'f', -1, 64)
	default:
		panic("Unsupported type")

	}
	s.record[kName] = r.Path()
	s.record[kHost] = r.HostName()
	return json.Marshal(s.record)
}
<commit_msg>Fix LMM package to use new store API<commit_after>package lmm

import (
	"encoding/json"
	"github.com/Symantec/scotty/store"
	"github.com/Symantec/tricorder/go/tricorder/messages"
	"github.com/Symantec/tricorder/go/tricorder/types"
	"github.com/Symantec/tricorder/go/tricorder/units"
	"github.com/optiopay/kafka"
	"github.com/optiopay/kafka/proto"
	"strconv"
	"time"
)

const (
	kVersion   = "@version"
	kTimestamp = "@timestamp"
	kValue     = "value"
	kName      = "name"
	kHost      = "host"
	kTenantId  = "tenant_id"
	kApiKey    = "apikey"
)

var (
	supportedTypes = map[types.Type]bool{
		types.Bool:       true,
		types.Int:        true,
		types.Uint:       true,
		types.Float:      true,
		types.GoTime:     true,
		types.GoDuration: true,
	}
)

func isTypeSupported(t types.Type) bool {
	return supportedTypes[t]
}

func newWriter(topic string, tenantId, apiKey string, addresses []string) (
	result *Writer, err error) {
	var writer Writer
	writer.topic = topic
	writer.tenantId = tenantId
	writer.apiKey = apiKey
	writer.broker, err = kafka.Dial(addresses, kafka.NewBrokerConf("test"))
	if err != nil {
		return
	}
	var count int32
	count, err = writer.broker.PartitionCount(topic)
	if err != nil {
		return
	}
	conf := kafka.NewProducerConf()
	conf.RequiredAcks = proto.RequiredAcksLocal
	producer := writer.broker.Producer(conf)
	writer.producer = kafka.NewRoundRobinProducer(producer, count)
	result = &writer
	return
}

func (w *Writer) write(records []*store.Record) (err error) {
	serializer := newRecordSerializer(w.tenantId, w.apiKey)
	msgs := make([]*proto.Message, len(records))
	for i := range records {
		var payload []byte
		payload, err = serializer.Serialize(recordType{records[i]})
		if err != nil {
			return
		}
		msgs[i] = &proto.Message{Value: payload}
	}
	_, err = w.producer.Distribute(w.topic, msgs...)
	return
}

type iRecordType interface {
	Kind() types.Type
	Unit() units.Unit
	Timestamp() float64
	Value() interface{}
	Path() string
	HostName() string
}

type recordType struct {
	*store.Record
}

func (r recordType) Kind() types.Type {
	return r.Info.Kind()
}

func (r recordType) Unit() units.Unit {
	return r.Info.Unit()
}

func (r recordType) Path() string {
	return r.Info.Path()
}

func (r recordType) HostName() string {
	return r.ApplicationId.HostName()
}

func (r recordType) Timestamp() float64 {
	return r.TimeStamp
}

func (r recordType) Value() interface{} {
	return r.Value
}

// recordSerializerType serializes a record to bytes for LMM.
// Warning, instances of this type are not thread safe.
type recordSerializerType struct {
	record       map[string]string
	formatString string
}

func newRecordSerializer(tenantId, apiKey string) *recordSerializerType {
	return &recordSerializerType{
		record: map[string]string{
			kVersion:  "1",
			kTenantId: tenantId,
			kApiKey:   apiKey},
		formatString: "2006-01-02T15:04:05.000Z"}
}

func (s *recordSerializerType) Serialize(r iRecordType) ([]byte, error) {
	kind := r.Kind()

	if !isTypeSupported(kind) {
		panic("Cannot record given kind.")
	}
	s.record[kTimestamp] = messages.FloatToTime(
		r.Timestamp()).Format(s.formatString)
	switch kind {
	case types.Bool:
		if r.Value().(bool) {
			s.record[kValue] = "1"
		} else {
			s.record[kValue] = "0"
		}
	case types.Int:
		s.record[kValue] = strconv.FormatInt(r.Value().(int64), 10)
	case types.Uint:
		s.record[kValue] = strconv.FormatUint(r.Value().(uint64), 10)
	case types.Float:
		s.record[kValue] = strconv.FormatFloat(
			r.Value().(float64), 'f', -1, 64)
	case types.GoTime:
		s.record[kValue] = strconv.FormatFloat(
			messages.TimeToFloat(r.Value().(time.Time)),
			'f', -1, 64)
	case types.GoDuration:
		s.record[kValue] = strconv.FormatFloat(
			messages.DurationToFloat(
				r.Value().(time.Duration))*units.FromSeconds(r.Unit()),
			'f', -1, 64)
	default:
		panic("Unsupported type")

	}
	s.record[kName] = r.Path()
	s.record[kHost] = r.HostName()
	return json.Marshal(s.record)
}
<|endoftext|>


<commit_before>/*
Copyright 2014 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 watch_test

import (
	"reflect"
	"sync"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/util/wait"
	. "k8s.io/apimachinery/pkg/watch"
)

type myType struct {
	ID    string
	Value string
}

func (obj *myType) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (obj *myType) DeepCopyObject() runtime.Object {
	if obj == nil {
		return nil
	}
	clone := *obj
	return &clone
}

func TestBroadcaster(t *testing.T) {
	table := []Event{
		{Type: Added, Object: &myType{"foo", "hello world 1"}},
		{Type: Added, Object: &myType{"bar", "hello world 2"}},
		{Type: Modified, Object: &myType{"foo", "goodbye world 3"}},
		{Type: Deleted, Object: &myType{"bar", "hello world 4"}},
	}

	// The broadcaster we're testing
	m := NewBroadcaster(0, WaitIfChannelFull)

	// Add a bunch of watchers
	const testWatchers = 2
	wg := sync.WaitGroup{}
	wg.Add(testWatchers)
	for i := 0; i < testWatchers; i++ {
		w, err := m.Watch()
		if err != nil {
			t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
		}
		// Verify that each watcher gets the events in the correct order
		go func(watcher int, w Interface) {
			tableLine := 0
			for {
				event, ok := <-w.ResultChan()
				if !ok {
					break
				}
				if e, a := table[tableLine], event; !reflect.DeepEqual(e, a) {
					t.Errorf("Watcher %v, line %v: Expected (%v, %#v), got (%v, %#v)",
						watcher, tableLine, e.Type, e.Object, a.Type, a.Object)
				} else {
					t.Logf("Got (%v, %#v)", event.Type, event.Object)
				}
				tableLine++
			}
			wg.Done()
		}(i, w)
	}

	for i, item := range table {
		t.Logf("Sending %v", i)
		m.Action(item.Type, item.Object)
	}

	m.Shutdown()

	wg.Wait()
}

func TestBroadcasterWatcherClose(t *testing.T) {
	m := NewBroadcaster(0, WaitIfChannelFull)
	w, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w2, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w.Stop()
	m.Shutdown()
	if _, open := <-w.ResultChan(); open {
		t.Errorf("Stop didn't work?")
	}
	if _, open := <-w2.ResultChan(); open {
		t.Errorf("Shutdown didn't work?")
	}
	// Extra stops don't hurt things
	w.Stop()
	w2.Stop()
}

func TestBroadcasterWatcherStopDeadlock(t *testing.T) {
	done := make(chan bool)
	m := NewBroadcaster(0, WaitIfChannelFull)
	w, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w2, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	go func(w0, w1 Interface) {
		// We know Broadcaster is in the distribute loop once one watcher receives
		// an event. Stop the other watcher while distribute is trying to
		// send to it.
		select {
		case <-w0.ResultChan():
			w1.Stop()
		case <-w1.ResultChan():
			w0.Stop()
		}
		close(done)
	}(w, w2)
	m.Action(Added, &myType{})
	select {
	case <-time.After(wait.ForeverTestTimeout):
		t.Error("timeout: deadlocked")
	case <-done:
	}
	m.Shutdown()
}

func TestBroadcasterDropIfChannelFull(t *testing.T) {
	m := NewBroadcaster(1, DropIfChannelFull)

	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	event2 := Event{Type: Added, Object: &myType{"bar", "hello world 2"}}

	// Add a couple watchers
	watches := make([]Interface, 2)
	var err error
	for i := range watches {
		watches[i], err = m.Watch()
		if err != nil {
			t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
		}
	}

	// Send a couple events before closing the broadcast channel.
	t.Log("Sending event 1")
	m.Action(event1.Type, event1.Object)
	t.Log("Sending event 2")
	m.Action(event2.Type, event2.Object)
	m.Shutdown()

	// Pull events from the queue.
	wg := sync.WaitGroup{}
	wg.Add(len(watches))
	for i := range watches {
		// Verify that each watcher only gets the first event because its watch
		// queue of length one was full from the first one.
		go func(watcher int, w Interface) {
			defer wg.Done()
			e1, ok := <-w.ResultChan()
			if !ok {
				t.Errorf("Watcher %v failed to retrieve first event.", watcher)
			}
			if e, a := event1, e1; !reflect.DeepEqual(e, a) {
				t.Errorf("Watcher %v: Expected (%v, %#v), got (%v, %#v)",
					watcher, e.Type, e.Object, a.Type, a.Object)
			}
			t.Logf("Got (%v, %#v)", e1.Type, e1.Object)
			e2, ok := <-w.ResultChan()
			if ok {
				t.Errorf("Watcher %v received second event (%v, %#v) even though it shouldn't have.",
					watcher, e2.Type, e2.Object)
			}
		}(i, watches[i])
	}
	wg.Wait()
}

func BenchmarkBroadCaster(b *testing.B) {
	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	m := NewBroadcaster(0, WaitIfChannelFull)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			m.Action(event1.Type, event1.Object)
		}
	})
	b.StopTimer()
}

func TestBroadcasterWatchAfterShutdown(t *testing.T) {
	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	event2 := Event{Type: Added, Object: &myType{"bar", "hello world 2"}}

	m := NewBroadcaster(0, WaitIfChannelFull)
	m.Shutdown()

	_, err := m.Watch()
	assert.EqualError(t, err, "broadcaster already stopped", "Watch should report error id broadcaster is shutdown")

	_, err = m.WatchWithPrefix([]Event{event1, event2})
	assert.EqualError(t, err, "broadcaster already stopped", "WatchWithPrefix should report error id broadcaster is shutdown")
}

func TestBroadcasterSendEventAfterShutdown(t *testing.T) {
	m := NewBroadcaster(1, DropIfChannelFull)

	event := Event{Type: Added, Object: &myType{"foo", "hello world"}}

	// Add a couple watchers
	watches := make([]Interface, 2)
	for i := range watches {
		watches[i], _ = m.Watch()
	}
	m.Shutdown()

	// Send a couple events after closing the broadcast channel.
	t.Log("Sending event")

	err := m.Action(event.Type, event.Object)
	assert.EqualError(t, err, "broadcaster already stopped", "ActionOrDrop should report error id broadcaster is shutdown")

	sendOnClosed, err := m.ActionOrDrop(event.Type, event.Object)
	assert.Equal(t, sendOnClosed, false, "ActionOrDrop should return false if broadcaster is already shutdown")
	assert.EqualError(t, err, "broadcaster already stopped", "ActionOrDrop should report error id broadcaster is shutdown")
}
<commit_msg>Write Unit test to imitate Panic<commit_after>/*
Copyright 2014 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 watch

import (
	"reflect"
	"sync"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/util/wait"
)

type myType struct {
	ID    string
	Value string
}

func (obj *myType) GetObjectKind() schema.ObjectKind { return schema.EmptyObjectKind }
func (obj *myType) DeepCopyObject() runtime.Object {
	if obj == nil {
		return nil
	}
	clone := *obj
	return &clone
}

func TestBroadcaster(t *testing.T) {
	table := []Event{
		{Type: Added, Object: &myType{"foo", "hello world 1"}},
		{Type: Added, Object: &myType{"bar", "hello world 2"}},
		{Type: Modified, Object: &myType{"foo", "goodbye world 3"}},
		{Type: Deleted, Object: &myType{"bar", "hello world 4"}},
	}

	// The broadcaster we're testing
	m := NewBroadcaster(0, WaitIfChannelFull)

	// Add a bunch of watchers
	const testWatchers = 2
	wg := sync.WaitGroup{}
	wg.Add(testWatchers)
	for i := 0; i < testWatchers; i++ {
		w, err := m.Watch()
		if err != nil {
			t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
		}
		// Verify that each watcher gets the events in the correct order
		go func(watcher int, w Interface) {
			tableLine := 0
			for {
				event, ok := <-w.ResultChan()
				if !ok {
					break
				}
				if e, a := table[tableLine], event; !reflect.DeepEqual(e, a) {
					t.Errorf("Watcher %v, line %v: Expected (%v, %#v), got (%v, %#v)",
						watcher, tableLine, e.Type, e.Object, a.Type, a.Object)
				} else {
					t.Logf("Got (%v, %#v)", event.Type, event.Object)
				}
				tableLine++
			}
			wg.Done()
		}(i, w)
	}

	for i, item := range table {
		t.Logf("Sending %v", i)
		m.Action(item.Type, item.Object)
	}

	m.Shutdown()

	wg.Wait()
}

func TestBroadcasterWatcherClose(t *testing.T) {
	m := NewBroadcaster(0, WaitIfChannelFull)
	w, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w2, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w.Stop()
	m.Shutdown()
	if _, open := <-w.ResultChan(); open {
		t.Errorf("Stop didn't work?")
	}
	if _, open := <-w2.ResultChan(); open {
		t.Errorf("Shutdown didn't work?")
	}
	// Extra stops don't hurt things
	w.Stop()
	w2.Stop()
}

func TestBroadcasterWatcherStopDeadlock(t *testing.T) {
	done := make(chan bool)
	m := NewBroadcaster(0, WaitIfChannelFull)
	w, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	w2, err := m.Watch()
	if err != nil {
		t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
	}
	go func(w0, w1 Interface) {
		// We know Broadcaster is in the distribute loop once one watcher receives
		// an event. Stop the other watcher while distribute is trying to
		// send to it.
		select {
		case <-w0.ResultChan():
			w1.Stop()
		case <-w1.ResultChan():
			w0.Stop()
		}
		close(done)
	}(w, w2)
	m.Action(Added, &myType{})
	select {
	case <-time.After(wait.ForeverTestTimeout):
		t.Error("timeout: deadlocked")
	case <-done:
	}
	m.Shutdown()
}

func TestBroadcasterDropIfChannelFull(t *testing.T) {
	m := NewBroadcaster(1, DropIfChannelFull)

	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	event2 := Event{Type: Added, Object: &myType{"bar", "hello world 2"}}

	// Add a couple watchers
	watches := make([]Interface, 2)
	var err error
	for i := range watches {
		watches[i], err = m.Watch()
		if err != nil {
			t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
		}
	}

	// Send a couple events before closing the broadcast channel.
	t.Log("Sending event 1")
	m.Action(event1.Type, event1.Object)
	t.Log("Sending event 2")
	m.Action(event2.Type, event2.Object)
	m.Shutdown()

	// Pull events from the queue.
	wg := sync.WaitGroup{}
	wg.Add(len(watches))
	for i := range watches {
		// Verify that each watcher only gets the first event because its watch
		// queue of length one was full from the first one.
		go func(watcher int, w Interface) {
			defer wg.Done()
			e1, ok := <-w.ResultChan()
			if !ok {
				t.Errorf("Watcher %v failed to retrieve first event.", watcher)
			}
			if e, a := event1, e1; !reflect.DeepEqual(e, a) {
				t.Errorf("Watcher %v: Expected (%v, %#v), got (%v, %#v)",
					watcher, e.Type, e.Object, a.Type, a.Object)
			}
			t.Logf("Got (%v, %#v)", e1.Type, e1.Object)
			e2, ok := <-w.ResultChan()
			if ok {
				t.Errorf("Watcher %v received second event (%v, %#v) even though it shouldn't have.",
					watcher, e2.Type, e2.Object)
			}
		}(i, watches[i])
	}
	wg.Wait()
}

func BenchmarkBroadCaster(b *testing.B) {
	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	m := NewBroadcaster(0, WaitIfChannelFull)
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			m.Action(event1.Type, event1.Object)
		}
	})
	b.StopTimer()
}

func TestBroadcasterWatchAfterShutdown(t *testing.T) {
	event1 := Event{Type: Added, Object: &myType{"foo", "hello world 1"}}
	event2 := Event{Type: Added, Object: &myType{"bar", "hello world 2"}}

	m := NewBroadcaster(0, WaitIfChannelFull)
	m.Shutdown()

	_, err := m.Watch()
	assert.EqualError(t, err, "broadcaster already stopped", "Watch should report error id broadcaster is shutdown")

	_, err = m.WatchWithPrefix([]Event{event1, event2})
	assert.EqualError(t, err, "broadcaster already stopped", "WatchWithPrefix should report error id broadcaster is shutdown")
}

func TestBroadcasterSendEventAfterShutdown(t *testing.T) {
	m := NewBroadcaster(1, DropIfChannelFull)

	event := Event{Type: Added, Object: &myType{"foo", "hello world"}}

	// Add a couple watchers
	watches := make([]Interface, 2)
	for i := range watches {
		watches[i], _ = m.Watch()
	}
	m.Shutdown()

	// Send a couple events after closing the broadcast channel.
	t.Log("Sending event")

	err := m.Action(event.Type, event.Object)
	assert.EqualError(t, err, "broadcaster already stopped", "ActionOrDrop should report error id broadcaster is shutdown")

	sendOnClosed, err := m.ActionOrDrop(event.Type, event.Object)
	assert.Equal(t, sendOnClosed, false, "ActionOrDrop should return false if broadcaster is already shutdown")
	assert.EqualError(t, err, "broadcaster already stopped", "ActionOrDrop should report error id broadcaster is shutdown")
}

// Test this since we see usage patterns where the broadcaster and watchers are
// stopped simultaneously leading to races.
func TestBroadcasterShutdownRace(t *testing.T) {
	m := NewBroadcaster(1, WaitIfChannelFull)
	stopCh := make(chan struct{})

	// Add a bunch of watchers
	const testWatchers = 2
	for i := 0; i < testWatchers; i++ {
		i := i

		_, err := m.Watch()
		if err != nil {
			t.Fatalf("Unable start event watcher: '%v' (will not retry!)", err)
		}
		// This is how we force the watchers to close down independently of the
		// eventbroadcaster, see real usage pattern in startRecordingEvents()
		go func() {
			<-stopCh
			t.Log("Stopping Watchers")
			m.stopWatching(int64(i))
		}()
	}

	event := Event{Type: Added, Object: &myType{"foo", "hello world"}}
	err := m.Action(event.Type, event.Object)
	if err != nil {
		t.Fatalf("error sending event: %v", err)
	}

	// Manually simulate m.Shutdown() but change it to force a race scenario
	// 1. Close watcher stopchannel, so watchers are closed independently of the
	// eventBroadcaster
	// 2. Shutdown the m.incoming slightly Before m.stopped so that the watcher's
	// call of Blockqueue can pass the m.stopped check.
	m.blockQueue(func() {
		close(stopCh)
		close(m.incoming)
		time.Sleep(1 * time.Millisecond)
		close(m.stopped)
	})
	m.distributing.Wait()
}
<|endoftext|>


<commit_before>package datastructures

// Comparable represents a type which is comparable.
type Comparable interface {
	Compare(Comparable) int
}

// Comparables represents []Comparable
type Comparables []Comparable

// Len is to implements Sort.Interface .
func (cs Comparables) Len() int {
	return len(cs)
}

// Less is to implements Sort.Interface .
func (cs Comparables) Less(i, j int) bool {
	if cs[i].Compare(cs[j]) < 0 {
		return true
	}

	return false
}

// Shift removes the fisrt element and returns it.
func (cs *Comparables) Shift() Comparable {
	if len(*cs) == 0 {
		return nil
	}

	f := (*cs)[0]
	*cs = (*cs)[1:]
	return f
}

// Swap is to implements Sort.Interface .
func (cs Comparables) Swap(i, j int) {
	cs[i], cs[j] = cs[j], cs[i]
}

// IntComparable represents a comparable int.
type IntComparable int

// Compare implements the Comparable interface.
func (ic IntComparable) Compare(i Comparable) int {
	other, ok := i.(IntComparable)

	if !ok {
		panic("param should be an intComparable")
	}

	if int(ic) == int(other) {
		return 0
	} else if int(ic) < int(other) {
		return -1
	} else {
		return 1
	}
}
<commit_msg>fix a comment typo<commit_after>package datastructures

// Comparable represents a type which is comparable.
type Comparable interface {
	Compare(Comparable) int
}

// Comparables represents []Comparable .
type Comparables []Comparable

// Len is to implements Sort.Interface .
func (cs Comparables) Len() int {
	return len(cs)
}

// Less is to implements Sort.Interface .
func (cs Comparables) Less(i, j int) bool {
	if cs[i].Compare(cs[j]) < 0 {
		return true
	}

	return false
}

// Shift removes the fisrt element and returns it.
func (cs *Comparables) Shift() Comparable {
	if len(*cs) == 0 {
		return nil
	}

	f := (*cs)[0]
	*cs = (*cs)[1:]
	return f
}

// Swap is to implements Sort.Interface .
func (cs Comparables) Swap(i, j int) {
	cs[i], cs[j] = cs[j], cs[i]
}

// IntComparable represents a comparable int.
type IntComparable int

// Compare implements the Comparable interface.
func (ic IntComparable) Compare(i Comparable) int {
	other, ok := i.(IntComparable)

	if !ok {
		panic("param should be an intComparable")
	}

	if int(ic) == int(other) {
		return 0
	} else if int(ic) < int(other) {
		return -1
	} else {
		return 1
	}
}
<|endoftext|>


<commit_before>// Copyright 2013 tsuru 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 log provides logging utility.
//
// It abstracts the logger from the standard log package, allowing the
// developer to patck the logging target, changing this to a file, or syslog,
// for example.
package log

import (
	"github.com/globocom/config"
	"io"
	"log"
	"log/syslog"
	"os"
	"sync"
)

func getSysLogger() *log.Logger {
	logger, err := syslog.NewLogger(syslog.LOG_INFO, log.LstdFlags)
	if err != nil {
		log.Fatal(err)
	}
	return logger
}

func getFileLogger(fileName string) *log.Logger {
	file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
	if err != nil {
		log.Fatal(err)
	}
	return log.New(file, "", log.LstdFlags)
}

func Init() {
	logFileName, err := config.GetString("log:file")
	var logger *log.Logger
	if err != nil {
		logger = getSysLogger()
	} else {
		logger = getFileLogger(logFileName)
	}
	SetLogger(logger)
}

// Target is the current target for the log package.
type Target struct {
	logger *log.Logger
	mut    sync.RWMutex
}

// SetLogger defines a new logger for the current target.
//
// See the builtin log package for more details.
func (t *Target) SetLogger(l *log.Logger) {
	t.mut.Lock()
	defer t.mut.Unlock()
	t.logger = l
}

// Fatal is equivalent to Print() followed by os.Exit(1).
func (t *Target) Fatal(v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Fatal(v...)
	}
}

// Fatalf is equivalent to Printf followed by os.Exit(1).
func (t *Target) Fatalf(format string, v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Fatalf(format, v...)
	}
}

// Print is similar to fmt.Print, writing the given values to the Target
// logger.
func (t *Target) Print(v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Print(v...)
	}
}

// Printf is similar to fmt.Printf, writing the formatted string to the Target
// logger.
func (t *Target) Printf(format string, v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Printf(format, v...)
	}
}

// Panic is equivalent to Print() followed by panic().
func (t *Target) Panic(v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Panic(v...)
	}
}

func (t *Target) Panicf(format string, v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Panicf(format, v...)
	}
}

var DefaultTarget = new(Target)

// Fatal is a wrapper for DefaultTarget.Fatal.
func Fatal(v ...interface{}) {
	DefaultTarget.Fatal(v...)
}

// Fatalf is a wrapper for DefaultTarget.Fatalf.
func Fatalf(format string, v ...interface{}) {
	DefaultTarget.Fatalf(format, v...)
}

// Print is a wrapper for DefaultTarget.Print.
func Print(v ...interface{}) {
	DefaultTarget.Print(v...)
}

// Printf is a wrapper for DefaultTarget.Printf.
func Printf(format string, v ...interface{}) {
	DefaultTarget.Printf(format, v...)
}

// Panic is a wrapper for DefaultTarget.Panic.
func Panic(v ...interface{}) {
	DefaultTarget.Panic(v...)
}

// Panicf is a wrapper for DefaultTarget.Panicf.
func Panicf(format string, v ...interface{}) {
	DefaultTarget.Panicf(format, v...)
}

// SetLogger is a wrapper for DefaultTarget.SetLogger.
func SetLogger(logger *log.Logger) {
	DefaultTarget.SetLogger(logger)
}

func Write(w io.Writer, content []byte) error {
	n, err := w.Write(content)
	if err != nil {
		return err
	}
	if n != len(content) {
		return io.ErrShortWrite
	}
	return nil
}
<commit_msg>log: implemented log interface<commit_after>// Copyright 2013 tsuru 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 log provides logging utility.
//
// It abstracts the logger from the standard log package, allowing the
// developer to patck the logging target, changing this to a file, or syslog,
// for example.
package log

import (
	"github.com/globocom/config"
	"io"
	"log"
	"log/syslog"
	"os"
	"sync"
)

type Logger interface {
	Error(string)
	Errorf(string, ...interface{})
	Debug(string)
	Debugf(string, ...interface{})
}

func getSysLogger() *log.Logger {
	logger, err := syslog.NewLogger(syslog.LOG_INFO, log.LstdFlags)
	if err != nil {
		log.Fatal(err)
	}
	return logger
}

func getFileLogger(fileName string) *log.Logger {
	file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
	if err != nil {
		log.Fatal(err)
	}
	return log.New(file, "", log.LstdFlags)
}

func Init() {
	logFileName, err := config.GetString("log:file")
	var logger *log.Logger
	if err != nil {
		logger = getSysLogger()
	} else {
		logger = getFileLogger(logFileName)
	}
	SetLogger(logger)
}

// Target is the current target for the log package.
type Target struct {
	logger *log.Logger
	mut    sync.RWMutex
}

// SetLogger defines a new logger for the current target.
//
// See the builtin log package for more details.
func (t *Target) SetLogger(l *log.Logger) {
	t.mut.Lock()
	defer t.mut.Unlock()
	t.logger = l
}

// Fatal is equivalent to Print() followed by os.Exit(1).
func (t *Target) Fatal(v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Fatal(v...)
	}
}

// Fatalf is equivalent to Printf followed by os.Exit(1).
func (t *Target) Fatalf(format string, v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Fatalf(format, v...)
	}
}

// Print is similar to fmt.Print, writing the given values to the Target
// logger.
func (t *Target) Print(v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Print(v...)
	}
}

// Printf is similar to fmt.Printf, writing the formatted string to the Target
// logger.
func (t *Target) Printf(format string, v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Printf(format, v...)
	}
}

// Panic is equivalent to Print() followed by panic().
func (t *Target) Panic(v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Panic(v...)
	}
}

func (t *Target) Panicf(format string, v ...interface{}) {
	t.mut.RLock()
	defer t.mut.RUnlock()
	if t.logger != nil {
		t.logger.Panicf(format, v...)
	}
}

var DefaultTarget = new(Target)

// Fatal is a wrapper for DefaultTarget.Fatal.
func Fatal(v ...interface{}) {
	DefaultTarget.Fatal(v...)
}

// Fatalf is a wrapper for DefaultTarget.Fatalf.
func Fatalf(format string, v ...interface{}) {
	DefaultTarget.Fatalf(format, v...)
}

// Print is a wrapper for DefaultTarget.Print.
func Print(v ...interface{}) {
	DefaultTarget.Print(v...)
}

// Printf is a wrapper for DefaultTarget.Printf.
func Printf(format string, v ...interface{}) {
	DefaultTarget.Printf(format, v...)
}

// Panic is a wrapper for DefaultTarget.Panic.
func Panic(v ...interface{}) {
	DefaultTarget.Panic(v...)
}

// Panicf is a wrapper for DefaultTarget.Panicf.
func Panicf(format string, v ...interface{}) {
	DefaultTarget.Panicf(format, v...)
}

// SetLogger is a wrapper for DefaultTarget.SetLogger.
func SetLogger(logger *log.Logger) {
	DefaultTarget.SetLogger(logger)
}

func Write(w io.Writer, content []byte) error {
	n, err := w.Write(content)
	if err != nil {
		return err
	}
	if n != len(content) {
		return io.ErrShortWrite
	}
	return nil
}
<|endoftext|>


<commit_before>package log

import (
	"bitbucket.org/mundipagg/boletoapi/config"
	"github.com/mundipagg/goseq"
)

var logger *goseq.Logger
var messageType string
var Operation string
var Recipient string

//Install instala o "servico" de log do SEQ
func Install() error {
	_logger, err := goseq.GetLogger(config.Get().SEQUrl, config.Get().SEQAPIKey)
	if err != nil {
		return err
	}
	_logger.SetDefaultProperties(map[string]string{
		"Application": config.Get().ApplicationName,
		"Environment": config.Get().Environment,
		"Domain":      config.Get().SEQDomain,
	})
	logger = _logger
	return nil
}

func formatter(message string) string {
	return "[" + config.Get().ApplicationName + ": " + Operation + "] - " + messageType + " " + message
}

func Request(content string) {
	messageType = "Request"
	props := goseq.NewProperties()
	props.AddProperty("MessageType", messageType)
	props.AddProperty("Content", content)

	msg := formatter("to BancoDoBrasil (http://example.com)")

	logger.Information(msg, props)
}

//Info loga mensagem do level INFO
func Info(msg string) {
	logger.Information(msg, goseq.NewProperties())
}

//Warn loga mensagem do leve Warning
func Warn(msg string) {
	logger.Warning(msg, goseq.NewProperties())
}

//Fatal loga mensagem do leve fatal
func Fatal(msg string) {
	logger.Fatal(msg, goseq.NewProperties())
}

//Close fecha a conexao com o SEQ
func Close() {
	logger.Close()
}
<commit_msg>:heavy_plus_sign: Adiciona métodos helper de log<commit_after>package log

import (
	"net/http"

	"fmt"

	"bitbucket.org/mundipagg/boletoapi/config"
	"bitbucket.org/mundipagg/boletoapi/models"
	"github.com/mundipagg/goseq"
)

var logger *goseq.Logger
var messageType string

// Operation a operacao usada na API
var Operation string

//Install instala o "servico" de log do SEQ
func Install() error {
	_logger, err := goseq.GetLogger(config.Get().SEQUrl, config.Get().SEQAPIKey)
	if err != nil {
		return err
	}
	_logger.SetDefaultProperties(map[string]string{
		"Application": config.Get().ApplicationName,
		"Environment": config.Get().Environment,
		"Domain":      config.Get().SEQDomain,
	})
	logger = _logger
	return nil
}

func formatter(message string) string {
	return "[{Application}: {Operation}] - {MessageType} " + message
}

// Request loga o request para algum banco
func Request(content, url string, headers http.Header, bankNumber models.BankNumber) {
	messageType = "Request"
	props := goseq.NewProperties()
	props.AddProperty("MessageType", messageType)
	props.AddProperty("Content", content)
	props.AddProperty("BankName", bankNumber.BankName())
	props.AddProperty("Headers", formatHeaders(headers))
	props.AddProperty("Operation", Operation)
	props.AddProperty("URL", url)

	msg := formatter("to {BankName} ({URL})")

	logger.Information(msg, props)
}

// Response loga o response para algum banco
func Response(content string, bankNumber models.BankNumber) {
	messageType = "Response"
	props := goseq.NewProperties()
	props.AddProperty("MessageType", messageType)
	props.AddProperty("Content", content)
	props.AddProperty("BankName", bankNumber.BankName())
	props.AddProperty("Operation", Operation)

	msg := formatter("from {BankName}")

	logger.Information(msg, props)
}

//Info loga mensagem do level INFO
func Info(msg string) {
	logger.Information(msg, goseq.NewProperties())
}

//Warn loga mensagem do leve Warning
func Warn(msg string) {
	logger.Warning(msg, goseq.NewProperties())
}

//Fatal loga mensagem do leve fatal
func Fatal(msg string) {
	logger.Fatal(msg, goseq.NewProperties())
}

//Close fecha a conexao com o SEQ
func Close() {
	logger.Close()
}

func formatHeaders(headers http.Header) string {
	s := ""
	for name, headers := range headers {
		for _, h := range headers {
			s += fmt.Sprintf("%v: %v\n", name, h)
		}
	}
	return s
}
<|endoftext|>


<commit_before>package grohl

import (
	"fmt"
	"io"
)

// A really basic logger that builds lines and writes to any io.Writer.  This
// expects the writers to be threadsafe.
type IoLogger struct {
	stream  io.Writer
	AddTime bool
}

func (l *IoLogger) Log(data Data) error {
	_, err := l.stream.Write(l.BuildLog(data))
	return err
}

func (l *IoLogger) BuildLog(data Data) []byte {
	return []byte(fmt.Sprintf("%s\n", BuildLog(data, l.AddTime)))
}

type ChannelLogger struct {
	channel chan Data
}

func (l *ChannelLogger) Log(data Data) error {
	l.channel <- data
	return nil
}
<commit_msg>expose how an IoLogger writes to the stream<commit_after>package grohl

import (
	"fmt"
	"io"
)

// A really basic logger that builds lines and writes to any io.Writer.  This
// expects the writers to be threadsafe.
type IoLogger struct {
	stream  io.Writer
	AddTime bool
}

func (l *IoLogger) Log(data Data) error {
	return l.Write(l.BuildLog(data))
}

func (l *IoLogger) Write(data []byte) err {
	_, err := l.stream.Write(data)
	return err
}

func (l *IoLogger) BuildLog(data Data) []byte {
	return []byte(fmt.Sprintf("%s\n", BuildLog(data, l.AddTime)))
}

type ChannelLogger struct {
	channel chan Data
}

func (l *ChannelLogger) Log(data Data) error {
	l.channel <- data
	return nil
}
<|endoftext|>


<commit_before>package cpu

import "github.com/valep27/go-jeebie/jeebie/memory"

// Flag is one of the 4 possible flags used in the flag register (high part of AF)
type Flag uint8

const (
	zeroFlag      Flag = 0x80
	subFlag            = 0x40
	halfCarryFlag      = 0x20
	carryFlag          = 0x10
)

// CPU is the main struct holding Z80 state
type CPU struct {
	memory *memory.MMU
	af     Register16
	bc     Register16
	de     Register16
	hl     Register16
	sp     Register16
	pc     Register16
}

// New returns an uninitialized CPU instance
func New() CPU {
	return CPU{}
}

//Tick emulates a single step during the main loop for the cpu.
func (c *CPU) Tick() {

}

func (c *CPU) setFlag(flag Flag) {
	c.af.setHigh(uint8(flag))
}

func (c *CPU) resetFlag(flag Flag) {
	c.af.setHigh(uint8(flag) ^ 0xFF)
}

func (c CPU) isSetFlag(flag Flag) bool {
	return c.af.getHigh() & uint8(flag) != 0
}
<commit_msg>Fix use of incorrect register<commit_after>package cpu

import "github.com/valep27/go-jeebie/jeebie/memory"

// Flag is one of the 4 possible flags used in the flag register (high part of AF)
type Flag uint8

const (
	zeroFlag      Flag = 0x80
	subFlag            = 0x40
	halfCarryFlag      = 0x20
	carryFlag          = 0x10
)

// CPU is the main struct holding Z80 state
type CPU struct {
	memory *memory.MMU
	af     Register16
	bc     Register16
	de     Register16
	hl     Register16
	sp     Register16
	pc     Register16
}

// New returns an uninitialized CPU instance
func New() CPU {
	return CPU{}
}

//Tick emulates a single step during the main loop for the cpu.
func (c *CPU) Tick() {

}

func (c *CPU) setFlag(flag Flag) {
	c.af.setLow(uint8(flag))
}

func (c *CPU) resetFlag(flag Flag) {
	c.af.setLow(uint8(flag) ^ 0xFF)
}

func (c CPU) isSetFlag(flag Flag) bool {
	return c.af.getHigh() & uint8(flag) != 0
}
<|endoftext|>


<commit_before>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package context_test

import (
	"bytes"

	"github.com/juju/cmd"
	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"

	coretesting "github.com/juju/juju/testing"
	"github.com/juju/juju/worker/uniter/runner/jujuc"
)

type commandSuite struct {
	baseSuite

	cmdName string
	cmd     cmd.Command
	cmdCtx  *cmd.Context
}

func (s *commandSuite) SetUpTest(c *gc.C) {
	s.baseSuite.SetUpTest(c)

	s.cmdCtx = coretesting.Context(c)
}

func (s *commandSuite) checkStdout(c *gc.C, expected string) {
	c.Check(s.cmdCtx.Stdout.(*bytes.Buffer).String(), gc.Equals, expected)
}

func (s *commandSuite) checkStderr(c *gc.C, expected string) {
	c.Check(s.cmdCtx.Stderr.(*bytes.Buffer).String(), gc.Equals, expected)
}

func (s *commandSuite) checkCommandRegistered(c *gc.C) {
	// TODO(ericsnow) finish!
	panic("not finished")
	jujuc.NewCommand(s.Hctx, s.cmdName)
}

func (s *commandSuite) checkHelp(c *gc.C, expected string) {
	code := cmd.Main(s.cmd, s.cmdCtx, []string{"--help"})
	c.Assert(code, gc.Equals, 0)

	s.checkStdout(c, expected)
}

func (s *commandSuite) checkRun(c *gc.C, expectedOut, expectedErr string) {
	err := s.cmd.Run(s.cmdCtx)
	c.Assert(err, jc.ErrorIsNil)

	s.checkStdout(c, expectedOut)
	s.checkStderr(c, expectedErr)
}
<commit_msg>Set the component to a test double in checkRun.<commit_after>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package context_test

import (
	"bytes"

	"github.com/juju/cmd"
	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"

	"github.com/juju/juju/process/context"
	coretesting "github.com/juju/juju/testing"
	"github.com/juju/juju/worker/uniter/runner/jujuc"
	jujuctesting "github.com/juju/juju/worker/uniter/runner/jujuc/testing"
)

type commandSuite struct {
	baseSuite

	cmdName string
	cmd     cmd.Command
	cmdCtx  *cmd.Context
}

func (s *commandSuite) SetUpTest(c *gc.C) {
	s.baseSuite.SetUpTest(c)

	s.cmdCtx = coretesting.Context(c)
}

func (s *commandSuite) checkStdout(c *gc.C, expected string) {
	c.Check(s.cmdCtx.Stdout.(*bytes.Buffer).String(), gc.Equals, expected)
}

func (s *commandSuite) checkStderr(c *gc.C, expected string) {
	c.Check(s.cmdCtx.Stderr.(*bytes.Buffer).String(), gc.Equals, expected)
}

func (s *commandSuite) checkCommandRegistered(c *gc.C) {
	// TODO(ericsnow) finish!
	panic("not finished")
	jujuc.NewCommand(s.Hctx, s.cmdName)
}

func (s *commandSuite) checkHelp(c *gc.C, expected string) {
	code := cmd.Main(s.cmd, s.cmdCtx, []string{"--help"})
	c.Assert(code, gc.Equals, 0)

	s.checkStdout(c, expected)
}

func (s *commandSuite) checkRun(c *gc.C, expectedOut, expectedErr string) {
	context.SetComponent(s.cmd, jujuctesting.ContextComponent{s.Stub})

	err := s.cmd.Run(s.cmdCtx)
	c.Assert(err, jc.ErrorIsNil)

	s.checkStdout(c, expectedOut)
	s.checkStderr(c, expectedErr)
}
<|endoftext|>


<commit_before>// +build linux

package cni

import (
	"fmt"
	"net"
	"os"
	"path/filepath"

	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	utilwait "k8s.io/apimachinery/pkg/util/wait"
)

// Start the Server's local HTTP server on a root-owned Unix domain socket.
// requestFunc will be called to handle pod setup/teardown operations on each
// request to the Server's HTTP server, and should return a PodResult
// when the operation has completed.
func (s *Server) Start(requestFunc cniRequestFunc) error {
	if requestFunc == nil {
		return fmt.Errorf("no pod request handler")
	}
	s.requestFunc = requestFunc

	// Remove and re-create the socket directory with root-only permissions
	if err := os.RemoveAll(s.rundir); err != nil && !os.IsNotExist(err) {
		utilruntime.HandleError(fmt.Errorf("failed to remove old pod info socket: %v", err))
	}
	if err := os.MkdirAll(s.rundir, 0700); err != nil {
		return fmt.Errorf("failed to create pod info socket directory: %v", err)
	}

	// On Linux the socket is created with the permissions of the directory
	// it is in, so as long as the directory is root-only we can avoid
	// racy umask manipulation.
	socketPath := filepath.Join(s.rundir, serverSocketName)
	l, err := net.Listen("unix", socketPath)
	if err != nil {
		return fmt.Errorf("failed to listen on pod info socket: %v", err)
	}
	if err := os.Chmod(socketPath, 0600); err != nil {
		l.Close()
		return fmt.Errorf("failed to set pod info socket mode: %v", err)
	}

	s.SetKeepAlivesEnabled(false)
	go utilwait.Forever(func() {
		if err := s.Serve(l); err != nil {
			utilruntime.HandleError(fmt.Errorf("CNI server Serve() failed: %v", err))
		}
	}, 0)
	return nil
}
<commit_msg>cniserver: fail if cniserver socket cannot be removed<commit_after>// +build linux

package cni

import (
	"fmt"
	"net"
	"os"
	"path/filepath"

	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	utilwait "k8s.io/apimachinery/pkg/util/wait"
)

// Start the Server's local HTTP server on a root-owned Unix domain socket.
// requestFunc will be called to handle pod setup/teardown operations on each
// request to the Server's HTTP server, and should return a PodResult
// when the operation has completed.
func (s *Server) Start(requestFunc cniRequestFunc) error {
	if requestFunc == nil {
		return fmt.Errorf("no pod request handler")
	}
	s.requestFunc = requestFunc

	// Remove and re-create the socket directory with root-only permissions
	if err := os.RemoveAll(s.rundir); err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("failed to remove old pod info socket directory: %v", err)
	}
	if err := os.MkdirAll(s.rundir, 0700); err != nil {
		return fmt.Errorf("failed to create pod info socket directory: %v", err)
	}

	// On Linux the socket is created with the permissions of the directory
	// it is in, so as long as the directory is root-only we can avoid
	// racy umask manipulation.
	socketPath := filepath.Join(s.rundir, serverSocketName)
	l, err := net.Listen("unix", socketPath)
	if err != nil {
		return fmt.Errorf("failed to listen on pod info socket: %v", err)
	}
	if err := os.Chmod(socketPath, 0600); err != nil {
		l.Close()
		return fmt.Errorf("failed to set pod info socket mode: %v", err)
	}

	s.SetKeepAlivesEnabled(false)
	go utilwait.Forever(func() {
		if err := s.Serve(l); err != nil {
			utilruntime.HandleError(fmt.Errorf("CNI server Serve() failed: %v", err))
		}
	}, 0)
	return nil
}
<|endoftext|>


<commit_before>package kite

import (
	"errors"
	"fmt"
	"runtime/debug"
	"strings"

	"github.com/dgrijalva/jwt-go"
	"github.com/koding/kite/dnode"
	"github.com/koding/kite/kitekey"
)

// Request contains information about the incoming request.
type Request struct {
	Method         string
	Args           *dnode.Partial
	LocalKite      *Kite
	Client         *Client
	Username       string
	Authentication *Authentication
}

// Response is the type of the object that is returned from request handlers
// and the type of only argument that is passed to callback functions.
type Response struct {
	Error  *Error      `json:"error" dnode:"-"`
	Result interface{} `json:"result"`
}

// runMethod is called when a method is received from remote Kite.
func (c *Client) runMethod(method *Method, args *dnode.Partial) {
	var (
		callFunc func(interface{}, *Error)
		request  *Request
	)

	// Recover dnode argument errors and send them back. The caller can use
	// functions like MustString(), MustSlice()... without the fear of panic.
	defer func() {
		if r := recover(); r != nil {
			debug.PrintStack()
			callFunc(nil, createError(r))
		}
	}()

	// The request that will be constructed from incoming dnode message.
	request, callFunc = c.newRequest(method.name, args)
	if method.authenticate {
		if err := request.authenticate(); err != nil {
			callFunc(nil, err)
			return
		}
	} else {
		// if not valided accept any username it sends, also useful for test
		// cases.
		request.Username = request.Client.Kite.Username
	}

	// Call the handler function.
	result, err := method.handler.ServeKite(request)

	callFunc(result, createError(err))
}

// runCallback is called when a callback method call is received from remote Kite.
func (c *Client) runCallback(callback func(*dnode.Partial), args *dnode.Partial) {
	// Do not panic no matter what.
	defer func() {
		if err := recover(); err != nil {
			c.LocalKite.Log.Warning("Error in calling the callback function : %v", err)
		}
	}()

	// Call the callback function.
	callback(args)
}

// newRequest returns a new *Request from the method and arguments passed.
func (c *Client) newRequest(method string, args *dnode.Partial) (*Request, func(interface{}, *Error)) {
	// Parse dnode method arguments: [options]
	var options callOptions
	args.One().MustUnmarshal(&options)

	// Notify the handlers registered with Kite.OnFirstRequest().
	if c.RemoteAddr() != "" {
		c.firstRequestHandlersNotified.Do(func() {
			c.Kite = options.Kite
			c.LocalKite.callOnFirstRequestHandlers(c)
		})
	}

	request := &Request{
		Method:         method,
		Args:           options.WithArgs,
		LocalKite:      c.LocalKite,
		Client:         c,
		Authentication: options.Authentication,
	}

	// Call response callback function, send back our response
	callFunc := func(result interface{}, err *Error) {
		if options.ResponseCallback.Caller == nil {
			return
		}

		// Only argument to the callback.
		response := Response{
			Result: result,
			Error:  err,
		}

		if err := options.ResponseCallback.Call(response); err != nil {
			c.LocalKite.Log.Error(err.Error())
		}
	}

	return request, callFunc
}

// authenticate tries to authenticate the user by selecting appropriate
// authenticator function.
func (r *Request) authenticate() *Error {
	// Trust the Kite if we have initiated the connection.
	// RemoteAddr() returns "" if this is an outgoing connection.
	if r.Client.RemoteAddr() == "" {
		return nil
	}

	if r.Authentication == nil {
		return &Error{
			Type:    "authenticationError",
			Message: "No authentication information is provided",
		}
	}

	// Select authenticator function.
	f := r.LocalKite.Authenticators[r.Authentication.Type]
	if f == nil {
		return &Error{
			Type:    "authenticationError",
			Message: fmt.Sprintf("Unknown authentication type: %s", r.Authentication.Type),
		}
	}

	// Call authenticator function. It sets the Request.Username field.
	err := f(r)
	if err != nil {
		return &Error{
			Type:    "authenticationError",
			Message: err.Error(),
		}
	}

	// Fix username of the remote Kite if it is invalid.
	// This prevents a Kite to impersonate someone else's Kite.
	r.Client.Kite.Username = r.Username
	return nil
}

// AuthenticateFromToken is the default Authenticator for Kite.
func (k *Kite) AuthenticateFromToken(r *Request) error {
	token, err := jwt.Parse(r.Authentication.Key, r.LocalKite.RSAKey)
	if err != nil {
		return err
	}

	if !token.Valid {
		return errors.New("Invalid signature in token")
	}

	if audience, ok := token.Claims["aud"].(string); !ok || !strings.HasPrefix(k.Kite().String(), audience) {
		return fmt.Errorf("Invalid audience in token. \nHave: %s \nMust be a part of: %s", audience, k.Kite().String())
	}

	// We don't check for exp and nbf claims here because jwt-go package already checks them.
	if username, ok := token.Claims["sub"].(string); !ok {
		return errors.New("Username is not present in token")
	} else {
		r.Username = username
	}

	return nil
}

// AuthenticateFromKiteKey authenticates user from kite key.
func (k *Kite) AuthenticateFromKiteKey(r *Request) error {
	token, err := jwt.Parse(r.Authentication.Key, kitekey.GetKontrolKey)
	if err != nil {
		return err
	}

	if !token.Valid {
		return errors.New("Invalid signature in token")
	}

	if username, ok := token.Claims["sub"].(string); !ok {
		return errors.New("Username is not present in token")
	} else {
		r.Username = username
	}

	return nil
}
<commit_msg>kites: changes from github.com<commit_after>package kite

import (
	"errors"
	"fmt"
	"runtime/debug"
	"strings"

	"github.com/dgrijalva/jwt-go"
	"github.com/koding/kite/dnode"
	"github.com/koding/kite/kitekey"
)

// Request contains information about the incoming request.
type Request struct {
	Method         string
	Args           *dnode.Partial
	LocalKite      *Kite
	Client         *Client
	Username       string
	Authentication *Authentication
}

// Response is the type of the object that is returned from request handlers
// and the type of only argument that is passed to callback functions.
type Response struct {
	Error  *Error      `json:"error" dnode:"-"`
	Result interface{} `json:"result"`
}

// runMethod is called when a method is received from remote Kite.
func (c *Client) runMethod(method *Method, args *dnode.Partial) {
	var (
		callFunc func(interface{}, *Error)
		request  *Request
	)

	// Recover dnode argument errors and send them back. The caller can use
	// functions like MustString(), MustSlice()... without the fear of panic.
	defer func() {
		if r := recover(); r != nil {
			debug.PrintStack()
			callFunc(nil, createError(r))
		}
	}()

	// The request that will be constructed from incoming dnode message.
	request, callFunc = c.newRequest(method.name, args)
	if method.authenticate {
		if err := request.authenticate(); err != nil {
			callFunc(nil, err)
			return
		}
	} else {
		// if not valided accept any username it sends, also useful for test
		// cases.
		request.Username = request.Client.Kite.Username
	}

	// Call the handler function.
	result, err := method.handler.ServeKite(request)

	callFunc(result, createError(err))
}

// runCallback is called when a callback method call is received from remote Kite.
func (c *Client) runCallback(callback func(*dnode.Partial), args *dnode.Partial) {
	// Do not panic no matter what.
	defer func() {
		if err := recover(); err != nil {
			c.LocalKite.Log.Warning("Error in calling the callback function : %v", err)
		}
	}()

	// Call the callback function.
	callback(args)
}

// newRequest returns a new *Request from the method and arguments passed.
func (c *Client) newRequest(method string, args *dnode.Partial) (*Request, func(interface{}, *Error)) {
	// Parse dnode method arguments: [options]
	var options callOptions
	args.One().MustUnmarshal(&options)

	// Notify the handlers registered with Kite.OnFirstRequest().
	if c.RemoteAddr() != "" {
		c.firstRequestHandlersNotified.Do(func() {
			c.Kite = options.Kite
			c.LocalKite.callOnFirstRequestHandlers(c)
		})
	}

	request := &Request{
		Method:         method,
		Args:           options.WithArgs,
		LocalKite:      c.LocalKite,
		Client:         c,
		Authentication: options.Authentication,
	}

	// Call response callback function, send back our response
	callFunc := func(result interface{}, err *Error) {
		if options.ResponseCallback.Caller == nil {
			return
		}

		// Only argument to the callback.
		response := Response{
			Result: result,
			Error:  err,
		}

		if err := options.ResponseCallback.Call(response); err != nil {
			c.LocalKite.Log.Error(err.Error())
		}
	}

	return request, callFunc
}

// authenticate tries to authenticate the user by selecting appropriate
// authenticator function.
func (r *Request) authenticate() *Error {
	// Trust the Kite if we have initiated the connection.
	// RemoteAddr() returns "" if this is an outgoing connection.
	if r.Client.RemoteAddr() == "" {
		return nil
	}

	if r.Authentication == nil {
		return &Error{
			Type:    "authenticationError",
			Message: "No authentication information is provided",
		}
	}

	// Select authenticator function.
	f := r.LocalKite.Authenticators[r.Authentication.Type]
	if f == nil {
		return &Error{
			Type:    "authenticationError",
			Message: fmt.Sprintf("Unknown authentication type: %s", r.Authentication.Type),
		}
	}

	// Call authenticator function. It sets the Request.Username field.
	err := f(r)
	if err != nil {
		return &Error{
			Type:    "authenticationError",
			Message: err.Error(),
		}
	}

	// Replace username of the remote Kite with the username that client send
	// us. This prevents a Kite to impersonate someone else's Kite.
	r.Client.Kite.Username = r.Username
	return nil
}

// AuthenticateFromToken is the default Authenticator for Kite.
func (k *Kite) AuthenticateFromToken(r *Request) error {
	token, err := jwt.Parse(r.Authentication.Key, r.LocalKite.RSAKey)
	if err != nil {
		return err
	}

	if !token.Valid {
		return errors.New("Invalid signature in token")
	}

	if audience, ok := token.Claims["aud"].(string); !ok || !strings.HasPrefix(k.Kite().String(), audience) {
		return fmt.Errorf("Invalid audience in token. \nHave: %s \nMust be a part of: %s", audience, k.Kite().String())
	}

	// We don't check for exp and nbf claims here because jwt-go package already checks them.
	if username, ok := token.Claims["sub"].(string); !ok {
		return errors.New("Username is not present in token")
	} else {
		r.Username = username
	}

	return nil
}

// AuthenticateFromKiteKey authenticates user from kite key.
func (k *Kite) AuthenticateFromKiteKey(r *Request) error {
	token, err := jwt.Parse(r.Authentication.Key, kitekey.GetKontrolKey)
	if err != nil {
		return err
	}

	if !token.Valid {
		return errors.New("Invalid signature in token")
	}

	if username, ok := token.Claims["sub"].(string); !ok {
		return errors.New("Username is not present in token")
	} else {
		r.Username = username
	}

	return nil
}
<|endoftext|>