<commit_before>package tensor3

import "testing"
import "fmt"

func TestVecRefsNewFromIndexes(t *testing.T) {
	vr := NewVectorRefsFromIndexes(NewVectors(1, 2, 3, 4, 5, 6, 7), 1, 3, 2)
	if fmt.Sprint(*vr[2]) != "{4 5 6}" {
		t.Error(fmt.Sprint(*vr[2]))
	}
}

func TestVecRefsNewFromEmplyIndexes(t *testing.T) {
	vr := NewVectorRefsFromIndexes(NewVectors(1, 2, 3, 4, 5, 6, 7))
	if fmt.Sprint(*vr[2]) != "{7 0 0}" {
		t.Error(fmt.Sprint(*vr[2]))
	}
}

func TestVecRefsDereferenceNew(t *testing.T) {
	vr := NewVectorRefsFromIndexes(NewVectors(1, 2, 3, 4, 5, 6, 7), 1, 3, 2)
	if fmt.Sprint(vr.Dereference()) != "[{1 2 3} {7 0 0} {4 5 6}]" {
		t.Error(fmt.Sprint(vr.Dereference()))
	}
}

func TestVecRefsDereference(t *testing.T) {
	vr := NewVectorRefsFromIndexes(NewVectors(1, 2, 3, 4, 5, 6, 7), 1, 3, 2)
	v := make(Vectors, 2)
	v.Reference(vr)
	if fmt.Sprint(v) != "[{1 2 3} {7 0 0}]" {
		t.Error(fmt.Sprint(v))
	}
}

func TestVecsFromVectorRefs(t *testing.T) {
	vs := NewVectors(1, 2, 3, 4, 5, 6, 7)
	vr1 := NewVectorRefsFromIndexes(vs, 1)
	vr2 := NewVectorRefsFromIndexes(vs, 3)
	vr := NewVectorsFromVectorRefs(vr1, vr2)
	if fmt.Sprint(vr) != "[{1 2 3} {7 0 0}]" {
		t.Error(fmt.Sprint(vr))
	}
	if fmt.Sprint(vr1.Dereference()) != "[{1 2 3}]" {
		t.Error(fmt.Sprint(vr1.Dereference()))
	}
	if fmt.Sprint(vr2.Dereference()) != "[{7 0 0}]" {
		t.Error(fmt.Sprint(vr2.Dereference()))
	}
}

func TestVecsIndexesFromVectorRefs(t *testing.T) {
	vs := NewVectors(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
	vr1 := NewVectorRefsFromIndexes(vs, 3, 5, 1)
	vr2 := NewVectorRefsFromIndexes(vs, 2, 5)
	vr := NewVectorsFromVectorRefs(vr2, vr1)
	is1 := vr1.Indexes(vr)
	is2 := vr2.Indexes(vr)
	if fmt.Sprint(is1, is2, vr) != "[3 2 4] [1 2] [{4 5 6} {13 14 15} {7 8 9} {1 2 3}]" {
		t.Error(fmt.Sprint(is1, is2, vr))
	}

}

func TestVecRefsPrint(t *testing.T) {
	vr := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}}
	if fmt.Sprint(*vr[0]) != "{1 2 3}" {
		t.Error(fmt.Sprint(*vr[0]))
	}
}

func TestVecRefsSum(t *testing.T) {
	vs := VectorRefs{&Vector{7 * scale, 8 * scale, 9 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	if fmt.Sprint(vs.Sum()) != "{21 24 27}" {
		t.Error(vs.Sum())
	}
}

func TestVecRefsAddRefs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.AddAllRefs(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{10 10 10} {10 10 10} {10 10 10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func TestVecRefsCrossRefs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.CrossAllRefs(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{-10 20 -10} {-10 20 -10} {-10 20 -10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func TestVecsAddVecRefs(t *testing.T) {
	vs := Vectors{NewVector(1, 2, 3), NewVector(4, 5, 6), NewVector(7, 8, 9)}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.AddAllRefs(vs2)
	if fmt.Sprint(vs[0], vs[1], vs[2]) != "{10 10 10} {10 10 10} {10 10 10}" {
		t.Error(fmt.Sprint(vs[0], vs[1], vs[2]))
	}
}

func TestVecsCrossVecRefs(t *testing.T) {
	vs := Vectors{NewVector(1, 2, 3), NewVector(4, 5, 6), NewVector(7, 8, 9)}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.CrossAllRefs(vs2)
	if fmt.Sprint(vs[0], vs[1], vs[2]) != "{-10 20 -10} {-10 20 -10} {-10 20 -10}" {
		t.Error(fmt.Sprint(vs[0], vs[1], vs[2]))
	}
}
func TestVecsRefsAddVecs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := Vectors{NewVector(9, 8, 7), NewVector(6, 5, 4), NewVector(3, 2, 1)}
	vs.AddAll(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{10 10 10} {10 10 10} {10 10 10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func TestVecRefsCrossVecs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := Vectors{NewVector(9, 8, 7), NewVector(6, 5, 4), NewVector(3, 2, 1)}
	vs.CrossAll(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{-10 20 -10} {-10 20 -10} {-10 20 -10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func TestVecsRefsAddVecRefs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.AddAllRefs(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{10 10 10} {10 10 10} {10 10 10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func TestVecRefsCrossVecRefs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.CrossAllRefs(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{-10 20 -10} {-10 20 -10} {-10 20 -10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func BenchmarkVecRefsProduct(b *testing.B) {
	b.StopTimer()
	vrs := make(VectorRefs, 100000)
	for i := range vrs {
		vrs[i] = &Vector{1 * scale, 2 * scale, 3 * scale}
	}
	m := Matrix{}
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		vrs.Product(m)
	}

}

func BenchmarkVecRefsProductParallel(b *testing.B) {
	b.StopTimer()
	vrs := make(VectorRefs, 100000)
	for i := range vrs {
		vrs[i] = &Vector{1 * scale, 2 * scale, 3 * scale}
	}
	m := Matrix{}
	Parallel = true
	defer func() {
		Parallel = false
	}()
	Hints.ChunkSizeFixed = true
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		vrs.Product(m)
	}
}

/*  Hal3 Wed 25 Apr 22:19:09 BST 2018  go version go1.10 linux/amd64

goos: linux
goarch: amd64
BenchmarkVecRefsProduct-2           	    1000	   2123885 ns/op
BenchmarkVecRefsProductParallel-2   	    1000	   1940512 ns/op
PASS
ok  	_/home/simon/Dropbox/github/working/tensor3	4.556s
Wed 25 Apr 22:19:15 BST 2018
*/

<commit_msg>vecrefs.(Sum|Min|Max) weren't using parallel<commit_after>package tensor3

import "testing"
import "fmt"

func TestVecRefsNewFromIndexes(t *testing.T) {
	vr := NewVectorRefsFromIndexes(NewVectors(1, 2, 3, 4, 5, 6, 7), 1, 3, 2)
	if fmt.Sprint(*vr[2]) != "{4 5 6}" {
		t.Error(fmt.Sprint(*vr[2]))
	}
}

func TestVecRefsNewFromEmplyIndexes(t *testing.T) {
	vr := NewVectorRefsFromIndexes(NewVectors(1, 2, 3, 4, 5, 6, 7))
	if fmt.Sprint(*vr[2]) != "{7 0 0}" {
		t.Error(fmt.Sprint(*vr[2]))
	}
}

func TestVecRefsDereferenceNew(t *testing.T) {
	vr := NewVectorRefsFromIndexes(NewVectors(1, 2, 3, 4, 5, 6, 7), 1, 3, 2)
	if fmt.Sprint(vr.Dereference()) != "[{1 2 3} {7 0 0} {4 5 6}]" {
		t.Error(fmt.Sprint(vr.Dereference()))
	}
}

func TestVecRefsDereference(t *testing.T) {
	vr := NewVectorRefsFromIndexes(NewVectors(1, 2, 3, 4, 5, 6, 7), 1, 3, 2)
	v := make(Vectors, 2)
	v.Reference(vr)
	if fmt.Sprint(v) != "[{1 2 3} {7 0 0}]" {
		t.Error(fmt.Sprint(v))
	}
}

func TestVecsFromVectorRefs(t *testing.T) {
	vs := NewVectors(1, 2, 3, 4, 5, 6, 7)
	vr1 := NewVectorRefsFromIndexes(vs, 1)
	vr2 := NewVectorRefsFromIndexes(vs, 3)
	vr := NewVectorsFromVectorRefs(vr1, vr2)
	if fmt.Sprint(vr) != "[{1 2 3} {7 0 0}]" {
		t.Error(fmt.Sprint(vr))
	}
	if fmt.Sprint(vr1.Dereference()) != "[{1 2 3}]" {
		t.Error(fmt.Sprint(vr1.Dereference()))
	}
	if fmt.Sprint(vr2.Dereference()) != "[{7 0 0}]" {
		t.Error(fmt.Sprint(vr2.Dereference()))
	}
}

func TestVecsIndexesFromVectorRefs(t *testing.T) {
	vs := NewVectors(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
	vr1 := NewVectorRefsFromIndexes(vs, 3, 5, 1)
	vr2 := NewVectorRefsFromIndexes(vs, 2, 5)
	vr := NewVectorsFromVectorRefs(vr2, vr1)
	is1 := vr1.Indexes(vr)
	is2 := vr2.Indexes(vr)
	if fmt.Sprint(is1, is2, vr) != "[3 2 4] [1 2] [{4 5 6} {13 14 15} {7 8 9} {1 2 3}]" {
		t.Error(fmt.Sprint(is1, is2, vr))
	}

}

func TestVecRefsPrint(t *testing.T) {
	vr := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}}
	if fmt.Sprint(*vr[0]) != "{1 2 3}" {
		t.Error(fmt.Sprint(*vr[0]))
	}
}

func TestVecRefsSum(t *testing.T) {
	vs := VectorRefs{&Vector{7 * scale, 8 * scale, 9 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	if fmt.Sprint(vs.Sum()) != "{21 24 27}" {
		t.Error(vs.Sum())
	}
}

func TestVecRefsAddRefs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.AddAllRefs(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{10 10 10} {10 10 10} {10 10 10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func TestVecRefsCrossRefs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.CrossAllRefs(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{-10 20 -10} {-10 20 -10} {-10 20 -10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func TestVecsAddVecRefs(t *testing.T) {
	vs := Vectors{NewVector(1, 2, 3), NewVector(4, 5, 6), NewVector(7, 8, 9)}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.AddAllRefs(vs2)
	if fmt.Sprint(vs[0], vs[1], vs[2]) != "{10 10 10} {10 10 10} {10 10 10}" {
		t.Error(fmt.Sprint(vs[0], vs[1], vs[2]))
	}
}

func TestVecsCrossVecRefs(t *testing.T) {
	vs := Vectors{NewVector(1, 2, 3), NewVector(4, 5, 6), NewVector(7, 8, 9)}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.CrossAllRefs(vs2)
	if fmt.Sprint(vs[0], vs[1], vs[2]) != "{-10 20 -10} {-10 20 -10} {-10 20 -10}" {
		t.Error(fmt.Sprint(vs[0], vs[1], vs[2]))
	}
}
func TestVecsRefsAddVecs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := Vectors{NewVector(9, 8, 7), NewVector(6, 5, 4), NewVector(3, 2, 1)}
	vs.AddAll(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{10 10 10} {10 10 10} {10 10 10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func TestVecRefsCrossVecs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := Vectors{NewVector(9, 8, 7), NewVector(6, 5, 4), NewVector(3, 2, 1)}
	vs.CrossAll(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{-10 20 -10} {-10 20 -10} {-10 20 -10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func TestVecsRefsAddVecRefs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.AddAllRefs(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{10 10 10} {10 10 10} {10 10 10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func TestVecRefsCrossVecRefs(t *testing.T) {
	vs := VectorRefs{&Vector{1 * scale, 2 * scale, 3 * scale}, &Vector{4 * scale, 5 * scale, 6 * scale}, &Vector{7 * scale, 8 * scale, 9 * scale}}
	vs2 := VectorRefs{&Vector{9 * scale, 8 * scale, 7 * scale}, &Vector{6 * scale, 5 * scale, 4 * scale}, &Vector{3 * scale, 2 * scale, 1 * scale}}
	vs.CrossAllRefs(vs2)
	if fmt.Sprint(*vs[0], *vs[1], *vs[2]) != "{-10 20 -10} {-10 20 -10} {-10 20 -10}" {
		t.Error(fmt.Sprint(*vs[0], *vs[1], *vs[2]))
	}
}

func BenchmarkVecRefsProduct(b *testing.B) {
	b.StopTimer()
	vrs := make(VectorRefs, 100000)
	for i := range vrs {
		vrs[i] = &Vector{1 * scale, 2 * scale, 3 * scale}
	}
	m := Matrix{}
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		vrs.Product(m)
	}

}

func BenchmarkVecRefsProductParallel(b *testing.B) {
	b.StopTimer()
	vrs := make(VectorRefs, 100000)
	for i := range vrs {
		vrs[i] = &Vector{1 * scale, 2 * scale, 3 * scale}
	}
	m := Matrix{}
	Parallel = true
	defer func() {
		Parallel = false
	}()
	Hints.ChunkSizeFixed = true
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		vrs.Product(m)
	}
}

<|endoftext|>


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

// +build !gccgo

package vsphere

import (
	"archive/tar"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"path/filepath"

	"github.com/juju/errors"
	"github.com/juju/govmomi/object"
	"github.com/juju/govmomi/vim25/progress"
	"github.com/juju/govmomi/vim25/soap"
	"github.com/juju/govmomi/vim25/types"
	"golang.org/x/net/context"

	"github.com/juju/juju/juju/osenv"
)

/*
This file contains implementation of the process of importing OVF template using vsphere API.  This process can be splitted in the following steps
1. Download OVA template
2. Extract it to a temp folder and load ovf file from it.
3. Call CreateImportSpec method from vsphere API https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/. This method validates the OVF descriptor against the hardware supported by the host system. If the validation succeeds, return a result containing:
  * An ImportSpec to use when importing the entity.
  * A list of items to upload (for example disk backing files, ISO images etc.)
4. Prepare all necessary parameters (CPU, mem, etc.) and call ImportVApp method https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/. This method is responsible for actually creating VM. This method return HttpNfcLease (https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.HttpNfcLease.html) object, that is used to monitor status of the process.
5. Upload virtual disk contents (that usually consist of a single vmdk file)
6. Call HttpNfcLeaseComplete https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/ and indicate that the process of uploading is finished - this step finishes the process.
*/

//this type implements progress.Sinker interface, that is requred to obtain the status of uploading an item to vspehere
type ovaFileItem struct {
	url  *url.URL
	item types.OvfFileItem
	ch   chan progress.Report
}

func (o ovaFileItem) Sink() chan<- progress.Report {
	return o.ch
}

type ovaImportManager struct {
	client *client
}

func (m *ovaImportManager) importOva(ecfg *environConfig, instSpec *instanceSpec) (*object.VirtualMachine, error) {
	folders, err := m.client.datacenter.Folders(context.TODO())
	if err != nil {
		return nil, errors.Trace(err)
	}

	basePath, err := ioutil.TempDir(osenv.JujuHome(), "")
	if err != nil {
		return nil, errors.Trace(err)
	}
	defer func() {
		if err := os.RemoveAll(basePath); err != nil {
			logger.Errorf("can't remove temp directory, error: %s", err.Error())
		}
	}()
	ovf, err := m.downloadOva(basePath, instSpec.img.Url)
	if err != nil {
		return nil, errors.Trace(err)
	}

	cisp := types.OvfCreateImportSpecParams{
		EntityName: instSpec.machineID,
		PropertyMapping: []types.KeyValue{
			types.KeyValue{Key: "public-keys", Value: instSpec.sshKey},
			types.KeyValue{Key: "user-data", Value: string(instSpec.userData)},
		},
	}

	ovfManager := object.NewOvfManager(m.client.connection.Client)
	resourcePool := object.NewReference(m.client.connection.Client, *instSpec.zone.r.ResourcePool)
	datastore := object.NewReference(m.client.connection.Client, instSpec.zone.r.Datastore[0])
	spec, err := ovfManager.CreateImportSpec(context.TODO(), string(ovf), resourcePool, datastore, cisp)
	if err != nil {
		return nil, errors.Trace(err)
	}

	if spec.Error != nil {
		return nil, errors.New(spec.Error[0].LocalizedMessage)
	}
	s := &spec.ImportSpec.(*types.VirtualMachineImportSpec).ConfigSpec
	s.NumCPUs = int(*instSpec.hwc.CpuCores)
	s.MemoryMB = int64(*instSpec.hwc.Mem)
	s.CpuAllocation = &types.ResourceAllocationInfo{
		Limit:       int64(*instSpec.hwc.CpuPower),
		Reservation: int64(*instSpec.hwc.CpuPower),
	}
	if instSpec.isState {
		s.ExtraConfig = append(s.ExtraConfig, &types.OptionValue{Key: metadataKeyIsState, Value: metadataValueIsState})
	}
	for _, d := range s.DeviceChange {
		if disk, ok := d.GetVirtualDeviceConfigSpec().Device.(*types.VirtualDisk); ok {
			if disk.CapacityInKB < int64(*instSpec.hwc.RootDisk*1024) {
				disk.CapacityInKB = int64(*instSpec.hwc.RootDisk * 1024)
			}
			//Set UnitNumber to -1 if it is unset in ovf file template (in this case it is parces as 0)
			//but 0 causes an error for disk devices
			if disk.UnitNumber == 0 {
				disk.UnitNumber = -1
			}
		}
	}
	if ecfg.externalNetwork() != "" {
		s.DeviceChange = append(s.DeviceChange, &types.VirtualDeviceConfigSpec{
			Operation: types.VirtualDeviceConfigSpecOperationAdd,
			Device: &types.VirtualE1000{
				VirtualEthernetCard: types.VirtualEthernetCard{
					VirtualDevice: types.VirtualDevice{
						Backing: &types.VirtualEthernetCardNetworkBackingInfo{
							VirtualDeviceDeviceBackingInfo: types.VirtualDeviceDeviceBackingInfo{
								DeviceName: ecfg.externalNetwork(),
							},
						},
						Connectable: &types.VirtualDeviceConnectInfo{
							StartConnected:    true,
							AllowGuestControl: true,
						},
					},
				},
			},
		})
	}
	rp := object.NewResourcePool(m.client.connection.Client, *instSpec.zone.r.ResourcePool)
	lease, err := rp.ImportVApp(context.TODO(), spec.ImportSpec, folders.VmFolder, nil)
	if err != nil {
		return nil, errors.Annotatef(err, "failed to import vapp")
	}

	info, err := lease.Wait(context.TODO())
	if err != nil {
		return nil, errors.Trace(err)
	}
	items := []ovaFileItem{}
	for _, device := range info.DeviceUrl {
		for _, item := range spec.FileItem {
			if device.ImportKey != item.DeviceId {
				continue
			}

			u, err := m.client.connection.Client.ParseURL(device.Url)
			if err != nil {
				return nil, errors.Trace(err)
			}

			i := ovaFileItem{
				url:  u,
				item: item,
				ch:   make(chan progress.Report),
			}
			items = append(items, i)
		}
	}

	for _, i := range items {
		err = m.uploadImage(i, basePath)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	lease.HttpNfcLeaseComplete(context.TODO())
	return object.NewVirtualMachine(m.client.connection.Client, info.Entity), nil
}

func (m *ovaImportManager) downloadOva(basePath, url string) (string, error) {
	logger.Debugf("Downloading ova file from url: %s", url)
	resp, err := http.Get(url)
	if err != nil {
		return "", errors.Trace(err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return "", errors.Errorf("can't download ova file from url: %s, status: %d", url, resp.StatusCode)
	}

	ovfFilePath, err := m.extractOva(basePath, resp.Body)
	if err != nil {
		return "", errors.Trace(err)
	}

	file, err := os.Open(ovfFilePath)
	defer file.Close()
	if err != nil {
		return "", errors.Trace(err)
	}
	bytes, err := ioutil.ReadAll(file)
	if err != nil {
		return "", errors.Trace(err)
	}
	return string(bytes), nil
}

func (m *ovaImportManager) extractOva(basePath string, body io.Reader) (string, error) {
	logger.Debugf("Extracting ova to path: %s", basePath)
	tarBallReader := tar.NewReader(body)
	var ovfFileName string

	for {
		header, err := tarBallReader.Next()
		if err != nil {
			if err == io.EOF {
				break
			}
			return "", errors.Trace(err)
		}
		filename := header.Name
		if filepath.Ext(filename) == ".ovf" {
			ovfFileName = filename
		}
		logger.Debugf("Writing file %s", filename)
		err = func() error {
			writer, err := os.Create(filepath.Join(basePath, filename))
			defer writer.Close()
			if err != nil {
				return errors.Trace(err)
			}
			_, err = io.Copy(writer, tarBallReader)
			if err != nil {
				return errors.Trace(err)
			}
			return nil
		}()
		if err != nil {
			return "", errors.Trace(err)
		}
	}
	if ovfFileName == "" {
		return "", errors.Errorf("no ovf file found in the archive")
	}
	logger.Debugf("Ova extracted successfully")
	return filepath.Join(basePath, ovfFileName), nil
}

func (m *ovaImportManager) uploadImage(ofi ovaFileItem, basePath string) error {
	filepath := filepath.Join(basePath, ofi.item.Path)
	logger.Debugf("Uploading item from path: %s", filepath)
	f, err := os.Open(filepath)
	if err != nil {
		return errors.Trace(err)
	}

	defer f.Close()

	opts := soap.Upload{
		ContentLength: ofi.item.Size,
		Progress:      ofi,
	}

	opts.Method = "POST"
	opts.Type = "application/x-vnd.vmware-streamVmdk"
	logger.Debugf("Uploading image to %s", ofi.url)
	go func() {
		lastPercent := 0
		for pr := <-ofi.ch; pr != nil; pr = <-ofi.ch {
			curPercent := int(pr.Percentage())
			if curPercent-lastPercent >= 10 {
				lastPercent = curPercent
				logger.Debugf("Progress: %d%%", lastPercent)
			}
		}
	}()
	err = m.client.connection.Client.Upload(f, ofi.url, &opts)
	if err == nil {
		logger.Debugf("Image uploaded")
	}
	return errors.Trace(err)
}
<commit_msg>Temp directory location for unpacking ova images changed. Prvious location don't exist on juju state server, and this leads to a panic.<commit_after>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

// +build !gccgo

package vsphere

import (
	"archive/tar"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"path/filepath"

	"github.com/juju/errors"
	"github.com/juju/govmomi/object"
	"github.com/juju/govmomi/vim25/progress"
	"github.com/juju/govmomi/vim25/soap"
	"github.com/juju/govmomi/vim25/types"
	"golang.org/x/net/context"
)

/*
This file contains implementation of the process of importing OVF template using vsphere API.  This process can be splitted in the following steps
1. Download OVA template
2. Extract it to a temp folder and load ovf file from it.
3. Call CreateImportSpec method from vsphere API https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/. This method validates the OVF descriptor against the hardware supported by the host system. If the validation succeeds, return a result containing:
  * An ImportSpec to use when importing the entity.
  * A list of items to upload (for example disk backing files, ISO images etc.)
4. Prepare all necessary parameters (CPU, mem, etc.) and call ImportVApp method https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/. This method is responsible for actually creating VM. This method return HttpNfcLease (https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.HttpNfcLease.html) object, that is used to monitor status of the process.
5. Upload virtual disk contents (that usually consist of a single vmdk file)
6. Call HttpNfcLeaseComplete https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/ and indicate that the process of uploading is finished - this step finishes the process.
*/

//this type implements progress.Sinker interface, that is requred to obtain the status of uploading an item to vspehere
type ovaFileItem struct {
	url  *url.URL
	item types.OvfFileItem
	ch   chan progress.Report
}

func (o ovaFileItem) Sink() chan<- progress.Report {
	return o.ch
}

type ovaImportManager struct {
	client *client
}

func (m *ovaImportManager) importOva(ecfg *environConfig, instSpec *instanceSpec) (*object.VirtualMachine, error) {
	folders, err := m.client.datacenter.Folders(context.TODO())
	if err != nil {
		return nil, errors.Trace(err)
	}

	basePath, err := ioutil.TempDir("", "")
	if err != nil {
		return nil, errors.Trace(err)
	}
	defer func() {
		if err := os.RemoveAll(basePath); err != nil {
			logger.Errorf("can't remove temp directory, error: %s", err.Error())
		}
	}()
	ovf, err := m.downloadOva(basePath, instSpec.img.Url)
	if err != nil {
		return nil, errors.Trace(err)
	}

	cisp := types.OvfCreateImportSpecParams{
		EntityName: instSpec.machineID,
		PropertyMapping: []types.KeyValue{
			types.KeyValue{Key: "public-keys", Value: instSpec.sshKey},
			types.KeyValue{Key: "user-data", Value: string(instSpec.userData)},
		},
	}

	ovfManager := object.NewOvfManager(m.client.connection.Client)
	resourcePool := object.NewReference(m.client.connection.Client, *instSpec.zone.r.ResourcePool)
	datastore := object.NewReference(m.client.connection.Client, instSpec.zone.r.Datastore[0])
	spec, err := ovfManager.CreateImportSpec(context.TODO(), string(ovf), resourcePool, datastore, cisp)
	if err != nil {
		return nil, errors.Trace(err)
	}

	if spec.Error != nil {
		return nil, errors.New(spec.Error[0].LocalizedMessage)
	}
	s := &spec.ImportSpec.(*types.VirtualMachineImportSpec).ConfigSpec
	s.NumCPUs = int(*instSpec.hwc.CpuCores)
	s.MemoryMB = int64(*instSpec.hwc.Mem)
	s.CpuAllocation = &types.ResourceAllocationInfo{
		Limit:       int64(*instSpec.hwc.CpuPower),
		Reservation: int64(*instSpec.hwc.CpuPower),
	}
	if instSpec.isState {
		s.ExtraConfig = append(s.ExtraConfig, &types.OptionValue{Key: metadataKeyIsState, Value: metadataValueIsState})
	}
	for _, d := range s.DeviceChange {
		if disk, ok := d.GetVirtualDeviceConfigSpec().Device.(*types.VirtualDisk); ok {
			if disk.CapacityInKB < int64(*instSpec.hwc.RootDisk*1024) {
				disk.CapacityInKB = int64(*instSpec.hwc.RootDisk * 1024)
			}
			//Set UnitNumber to -1 if it is unset in ovf file template (in this case it is parces as 0)
			//but 0 causes an error for disk devices
			if disk.UnitNumber == 0 {
				disk.UnitNumber = -1
			}
		}
	}
	if ecfg.externalNetwork() != "" {
		s.DeviceChange = append(s.DeviceChange, &types.VirtualDeviceConfigSpec{
			Operation: types.VirtualDeviceConfigSpecOperationAdd,
			Device: &types.VirtualE1000{
				VirtualEthernetCard: types.VirtualEthernetCard{
					VirtualDevice: types.VirtualDevice{
						Backing: &types.VirtualEthernetCardNetworkBackingInfo{
							VirtualDeviceDeviceBackingInfo: types.VirtualDeviceDeviceBackingInfo{
								DeviceName: ecfg.externalNetwork(),
							},
						},
						Connectable: &types.VirtualDeviceConnectInfo{
							StartConnected:    true,
							AllowGuestControl: true,
						},
					},
				},
			},
		})
	}
	rp := object.NewResourcePool(m.client.connection.Client, *instSpec.zone.r.ResourcePool)
	lease, err := rp.ImportVApp(context.TODO(), spec.ImportSpec, folders.VmFolder, nil)
	if err != nil {
		return nil, errors.Annotatef(err, "failed to import vapp")
	}

	info, err := lease.Wait(context.TODO())
	if err != nil {
		return nil, errors.Trace(err)
	}
	items := []ovaFileItem{}
	for _, device := range info.DeviceUrl {
		for _, item := range spec.FileItem {
			if device.ImportKey != item.DeviceId {
				continue
			}

			u, err := m.client.connection.Client.ParseURL(device.Url)
			if err != nil {
				return nil, errors.Trace(err)
			}

			i := ovaFileItem{
				url:  u,
				item: item,
				ch:   make(chan progress.Report),
			}
			items = append(items, i)
		}
	}

	for _, i := range items {
		err = m.uploadImage(i, basePath)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	lease.HttpNfcLeaseComplete(context.TODO())
	return object.NewVirtualMachine(m.client.connection.Client, info.Entity), nil
}

func (m *ovaImportManager) downloadOva(basePath, url string) (string, error) {
	logger.Debugf("Downloading ova file from url: %s", url)
	resp, err := http.Get(url)
	if err != nil {
		return "", errors.Trace(err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return "", errors.Errorf("can't download ova file from url: %s, status: %d", url, resp.StatusCode)
	}

	ovfFilePath, err := m.extractOva(basePath, resp.Body)
	if err != nil {
		return "", errors.Trace(err)
	}

	file, err := os.Open(ovfFilePath)
	defer file.Close()
	if err != nil {
		return "", errors.Trace(err)
	}
	bytes, err := ioutil.ReadAll(file)
	if err != nil {
		return "", errors.Trace(err)
	}
	return string(bytes), nil
}

func (m *ovaImportManager) extractOva(basePath string, body io.Reader) (string, error) {
	logger.Debugf("Extracting ova to path: %s", basePath)
	tarBallReader := tar.NewReader(body)
	var ovfFileName string

	for {
		header, err := tarBallReader.Next()
		if err != nil {
			if err == io.EOF {
				break
			}
			return "", errors.Trace(err)
		}
		filename := header.Name
		if filepath.Ext(filename) == ".ovf" {
			ovfFileName = filename
		}
		logger.Debugf("Writing file %s", filename)
		err = func() error {
			writer, err := os.Create(filepath.Join(basePath, filename))
			defer writer.Close()
			if err != nil {
				return errors.Trace(err)
			}
			_, err = io.Copy(writer, tarBallReader)
			if err != nil {
				return errors.Trace(err)
			}
			return nil
		}()
		if err != nil {
			return "", errors.Trace(err)
		}
	}
	if ovfFileName == "" {
		return "", errors.Errorf("no ovf file found in the archive")
	}
	logger.Debugf("Ova extracted successfully")
	return filepath.Join(basePath, ovfFileName), nil
}

func (m *ovaImportManager) uploadImage(ofi ovaFileItem, basePath string) error {
	filepath := filepath.Join(basePath, ofi.item.Path)
	logger.Debugf("Uploading item from path: %s", filepath)
	f, err := os.Open(filepath)
	if err != nil {
		return errors.Trace(err)
	}

	defer f.Close()

	opts := soap.Upload{
		ContentLength: ofi.item.Size,
		Progress:      ofi,
	}

	opts.Method = "POST"
	opts.Type = "application/x-vnd.vmware-streamVmdk"
	logger.Debugf("Uploading image to %s", ofi.url)
	go func() {
		lastPercent := 0
		for pr := <-ofi.ch; pr != nil; pr = <-ofi.ch {
			curPercent := int(pr.Percentage())
			if curPercent-lastPercent >= 10 {
				lastPercent = curPercent
				logger.Debugf("Progress: %d%%", lastPercent)
			}
		}
	}()
	err = m.client.connection.Client.Upload(f, ofi.url, &opts)
	if err == nil {
		logger.Debugf("Image uploaded")
	}
	return errors.Trace(err)
}
<|endoftext|>


<commit_before>package chroot

//  pf := func () { somefunc("a str", 1) }

import (
	"fmt"
	"github.com/mitchellh/packer/packer"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"syscall"
)

// Communicator is a special communicator that works by executing
// commands locally but within a chroot.
type Communicator struct {
	Chroot     string
	CmdWrapper CommandWrapper
}

func (c *Communicator) Start(cmd *packer.RemoteCmd) error {
	command, err := c.CmdWrapper(
		fmt.Sprintf("sudo chroot %s '%s'", c.Chroot, cmd.Command))
	if err != nil {
		return err
	}

	localCmd := ShellCommand(command)
	localCmd.Stdin = cmd.Stdin
	localCmd.Stdout = cmd.Stdout
	localCmd.Stderr = cmd.Stderr
	log.Printf("Executing: %s %#v", localCmd.Path, localCmd.Args)
	if err := localCmd.Start(); err != nil {
		return err
	}

	go func() {
		exitStatus := 0
		if err := localCmd.Wait(); err != nil {
			if exitErr, ok := err.(*exec.ExitError); ok {
				exitStatus = 1

				// There is no process-independent way to get the REAL
				// exit status so we just try to go deeper.
				if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
					exitStatus = status.ExitStatus()
				}
			}
		}

		log.Printf(
			"Chroot executation exited with '%d': '%s'",
			exitStatus, cmd.Command)
		cmd.SetExited(exitStatus)
	}()

	return nil
}

func (c *Communicator) Upload(dst string, r io.Reader) error {
	dst = filepath.Join(c.Chroot, dst)
	log.Printf("Uploading to chroot dir: %s", dst)
	tf, err := ioutil.TempFile("", "packer-amazon-chroot")
	if err != nil {
		return fmt.Errorf("Error preparing shell script: %s", err)
	}
	defer os.Remove(tf.Name())
	io.Copy(tf, r)

	cpCmd, err := c.CmdWrapper(fmt.Sprintf("cp %s %s", tf.Name(), dst))
	if err != nil {
		return err
	}

	return ShellCommand(cpCmd).Run()
}

func (c *Communicator) UploadDir(dst string, src string, exclude []string) error {
	// TODO: remove any file copied if it appears in `exclude`
	chrootDest := filepath.Join(c.Chroot, dst)
	log.Printf("Uploading directory '%s' to '%s'", src, chrootDest)
	cpCmd, err := c.CmdWrapper(fmt.Sprintf("cp -R %s* %s", src, chrootDest))
	if err != nil {
		return err
	}

	return ShellCommand(cpCmd).Run()
}

func (c *Communicator) Download(src string, w io.Writer) error {
	src = filepath.Join(c.Chroot, src)
	log.Printf("Downloading from chroot dir: %s", src)
	f, err := os.Open(src)
	if err != nil {
		return err
	}
	defer f.Close()

	if _, err := io.Copy(w, f); err != nil {
		return err
	}

	return nil
}
<commit_msg>builder/amazon/chroot: remove unused comment<commit_after>package chroot

import (
	"fmt"
	"github.com/mitchellh/packer/packer"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"syscall"
)

// Communicator is a special communicator that works by executing
// commands locally but within a chroot.
type Communicator struct {
	Chroot     string
	CmdWrapper CommandWrapper
}

func (c *Communicator) Start(cmd *packer.RemoteCmd) error {
	command, err := c.CmdWrapper(
		fmt.Sprintf("sudo chroot %s '%s'", c.Chroot, cmd.Command))
	if err != nil {
		return err
	}

	localCmd := ShellCommand(command)
	localCmd.Stdin = cmd.Stdin
	localCmd.Stdout = cmd.Stdout
	localCmd.Stderr = cmd.Stderr
	log.Printf("Executing: %s %#v", localCmd.Path, localCmd.Args)
	if err := localCmd.Start(); err != nil {
		return err
	}

	go func() {
		exitStatus := 0
		if err := localCmd.Wait(); err != nil {
			if exitErr, ok := err.(*exec.ExitError); ok {
				exitStatus = 1

				// There is no process-independent way to get the REAL
				// exit status so we just try to go deeper.
				if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
					exitStatus = status.ExitStatus()
				}
			}
		}

		log.Printf(
			"Chroot executation exited with '%d': '%s'",
			exitStatus, cmd.Command)
		cmd.SetExited(exitStatus)
	}()

	return nil
}

func (c *Communicator) Upload(dst string, r io.Reader) error {
	dst = filepath.Join(c.Chroot, dst)
	log.Printf("Uploading to chroot dir: %s", dst)
	tf, err := ioutil.TempFile("", "packer-amazon-chroot")
	if err != nil {
		return fmt.Errorf("Error preparing shell script: %s", err)
	}
	defer os.Remove(tf.Name())
	io.Copy(tf, r)

	cpCmd, err := c.CmdWrapper(fmt.Sprintf("cp %s %s", tf.Name(), dst))
	if err != nil {
		return err
	}

	return ShellCommand(cpCmd).Run()
}

func (c *Communicator) UploadDir(dst string, src string, exclude []string) error {
	// TODO: remove any file copied if it appears in `exclude`
	chrootDest := filepath.Join(c.Chroot, dst)
	log.Printf("Uploading directory '%s' to '%s'", src, chrootDest)
	cpCmd, err := c.CmdWrapper(fmt.Sprintf("cp -R %s* %s", src, chrootDest))
	if err != nil {
		return err
	}

	return ShellCommand(cpCmd).Run()
}

func (c *Communicator) Download(src string, w io.Writer) error {
	src = filepath.Join(c.Chroot, src)
	log.Printf("Downloading from chroot dir: %s", src)
	f, err := os.Open(src)
	if err != nil {
		return err
	}
	defer f.Close()

	if _, err := io.Copy(w, f); err != nil {
		return err
	}

	return nil
}
<|endoftext|>


<commit_before>package arm

// these tests require the following variables to be set,
// although some test will only use a subset:
//
// * ARM_CLIENT_ID
// * ARM_CLIENT_SECRET
// * ARM_SUBSCRIPTION_ID
// * ARM_OBJECT_ID
// * ARM_STORAGE_ACCOUNT
//
// The subscription in question should have a resource group
// called "packer-acceptance-test" in "South Central US" region. The
// storage account referred to in the above variable should
// be inside this resource group and in "South Central US" as well.
//
// In addition, the PACKER_ACC variable should also be set to
// a non-empty value to enable Packer acceptance tests and the
// options "-v -timeout 90m" should be provided to the test
// command, e.g.:
//   go test -v -timeout 90m -run TestBuilderAcc_.*

import (
	"testing"

	"fmt"
	"os"

	builderT "github.com/hashicorp/packer/helper/builder/testing"
)

const DeviceLoginAcceptanceTest = "DEVICELOGIN_TEST"

func TestBuilderAcc_ManagedDisk_Windows(t *testing.T) {
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccManagedDiskWindows,
	})
}

func TestBuilderAcc_ManagedDisk_Windows_Build_Resource_Group(t *testing.T) {
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccManagedDiskWindowsBuildResourceGroup,
	})
}

func TestBuilderAcc_ManagedDisk_Windows_DeviceLogin(t *testing.T) {
	if os.Getenv(DeviceLoginAcceptanceTest) == "" {
		t.Skip(fmt.Sprintf(
			"Device Login Acceptance tests skipped unless env '%s' set, as its requires manual step during execution",
			DeviceLoginAcceptanceTest))
		return
	}
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccManagedDiskWindowsDeviceLogin,
	})
}

func TestBuilderAcc_ManagedDisk_Linux(t *testing.T) {
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccManagedDiskLinux,
	})
}

func TestBuilderAcc_ManagedDisk_Linux_DeviceLogin(t *testing.T) {
	if os.Getenv(DeviceLoginAcceptanceTest) == "" {
		t.Skip(fmt.Sprintf(
			"Device Login Acceptance tests skipped unless env '%s' set, as its requires manual step during execution",
			DeviceLoginAcceptanceTest))
		return
	}
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccManagedDiskLinuxDeviceLogin,
	})
}

func TestBuilderAcc_Blob_Windows(t *testing.T) {
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccBlobWindows,
	})
}

func TestBuilderAcc_Blob_Linux(t *testing.T) {
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccBlobLinux,
	})
}

func testAccPreCheck(*testing.T) {}

const testBuilderAccManagedDiskWindows = `
{
	"variables": {
	  "client_id": "{{env ` + "`ARM_CLIENT_ID`" + `}}",
	  "client_secret": "{{env ` + "`ARM_CLIENT_SECRET`" + `}}",
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "client_id": "{{user ` + "`client_id`" + `}}",
	  "client_secret": "{{user ` + "`client_secret`" + `}}",
	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "managed_image_resource_group_name": "packer-acceptance-test",
	  "managed_image_name": "testBuilderAccManagedDiskWindows-{{timestamp}}",

	  "os_type": "Windows",
	  "image_publisher": "MicrosoftWindowsServer",
	  "image_offer": "WindowsServer",
	  "image_sku": "2012-R2-Datacenter",

	  "communicator": "winrm",
	  "winrm_use_ssl": "true",
	  "winrm_insecure": "true",
	  "winrm_timeout": "3m",
	  "winrm_username": "packer",
	  "async_resourcegroup_delete": "true",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2"
	}]
}
`
const testBuilderAccManagedDiskWindowsBuildResourceGroup = `
{
	"variables": {
	  "client_id": "{{env ` + "`ARM_CLIENT_ID`" + `}}",
	  "client_secret": "{{env ` + "`ARM_CLIENT_SECRET`" + `}}",
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "client_id": "{{user ` + "`client_id`" + `}}",
	  "client_secret": "{{user ` + "`client_secret`" + `}}",
	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "build_resource_group_name" : "packer-acceptance-test",
	  "managed_image_resource_group_name": "packer-acceptance-test",
	  "managed_image_name": "testBuilderAccManagedDiskWindows-{{timestamp}}",

	  "os_type": "Windows",
	  "image_publisher": "MicrosoftWindowsServer",
	  "image_offer": "WindowsServer",
	  "image_sku": "2012-R2-Datacenter",

	  "communicator": "winrm",
	  "winrm_use_ssl": "true",
	  "winrm_insecure": "true",
	  "winrm_timeout": "3m",
	  "winrm_username": "packer",
	  "async_resourcegroup_delete": "true",

	  "vm_size": "Standard_DS2_v2"
	}]
}
`

const testBuilderAccManagedDiskWindowsDeviceLogin = `
{
	"variables": {
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "managed_image_resource_group_name": "packer-acceptance-test",
	  "managed_image_name": "testBuilderAccManagedDiskWindowsDeviceLogin-{{timestamp}}",

	  "os_type": "Windows",
	  "image_publisher": "MicrosoftWindowsServer",
	  "image_offer": "WindowsServer",
	  "image_sku": "2012-R2-Datacenter",

	  "communicator": "winrm",
	  "winrm_use_ssl": "true",
	  "winrm_insecure": "true",
	  "winrm_timeout": "3m",
	  "winrm_username": "packer",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2"
	}]
}
`

const testBuilderAccManagedDiskLinux = `
{
	"variables": {
	  "client_id": "{{env ` + "`ARM_CLIENT_ID`" + `}}",
	  "client_secret": "{{env ` + "`ARM_CLIENT_SECRET`" + `}}",
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "client_id": "{{user ` + "`client_id`" + `}}",
	  "client_secret": "{{user ` + "`client_secret`" + `}}",
	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "managed_image_resource_group_name": "packer-acceptance-test",
	  "managed_image_name": "testBuilderAccManagedDiskLinux-{{timestamp}}",

	  "os_type": "Linux",
	  "image_publisher": "Canonical",
	  "image_offer": "UbuntuServer",
	  "image_sku": "16.04-LTS",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2",
	  "azure_tags": {
	    "env": "testing",
	    "builder": "packer"
	   }
	}]
}
`
const testBuilderAccManagedDiskLinuxDeviceLogin = `
{
	"variables": {
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "managed_image_resource_group_name": "packer-acceptance-test",
	  "managed_image_name": "testBuilderAccManagedDiskLinuxDeviceLogin-{{timestamp}}",

	  "os_type": "Linux",
	  "image_publisher": "Canonical",
	  "image_offer": "UbuntuServer",
	  "image_sku": "16.04-LTS",
	  "async_resourcegroup_delete": "true",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2"
	}]
}
`

const testBuilderAccBlobWindows = `
{
	"variables": {
	  "client_id": "{{env ` + "`ARM_CLIENT_ID`" + `}}",
	  "client_secret": "{{env ` + "`ARM_CLIENT_SECRET`" + `}}",
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}",
	  "object_id": "{{env ` + "`ARM_OBJECT_ID`" + `}}",
	  "storage_account": "{{env ` + "`ARM_STORAGE_ACCOUNT`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "client_id": "{{user ` + "`client_id`" + `}}",
	  "client_secret": "{{user ` + "`client_secret`" + `}}",
	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",
	  "object_id": "{{user ` + "`object_id`" + `}}",

	  "storage_account": "{{user ` + "`storage_account`" + `}}",
	  "resource_group_name": "packer-acceptance-test",
	  "capture_container_name": "test",
	  "capture_name_prefix": "testBuilderAccBlobWin",

	  "os_type": "Windows",
	  "image_publisher": "MicrosoftWindowsServer",
	  "image_offer": "WindowsServer",
	  "image_sku": "2012-R2-Datacenter",

	  "communicator": "winrm",
	  "winrm_use_ssl": "true",
	  "winrm_insecure": "true",
	  "winrm_timeout": "3m",
	  "winrm_username": "packer",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2"
	}]
}
`

const testBuilderAccBlobLinux = `
{
	"variables": {
	  "client_id": "{{env ` + "`ARM_CLIENT_ID`" + `}}",
	  "client_secret": "{{env ` + "`ARM_CLIENT_SECRET`" + `}}",
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}",
	  "storage_account": "{{env ` + "`ARM_STORAGE_ACCOUNT`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "client_id": "{{user ` + "`client_id`" + `}}",
	  "client_secret": "{{user ` + "`client_secret`" + `}}",
	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "storage_account": "{{user ` + "`storage_account`" + `}}",
	  "resource_group_name": "packer-acceptance-test",
	  "capture_container_name": "test",
	  "capture_name_prefix": "testBuilderAccBlobLinux",

	  "os_type": "Linux",
	  "image_publisher": "Canonical",
	  "image_offer": "UbuntuServer",
	  "image_sku": "16.04-LTS",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2"
	}]
}
`
<commit_msg>tests/azure/arm: Update test image name (#10133)<commit_after>package arm

// these tests require the following variables to be set,
// although some test will only use a subset:
//
// * ARM_CLIENT_ID
// * ARM_CLIENT_SECRET
// * ARM_SUBSCRIPTION_ID
// * ARM_OBJECT_ID
// * ARM_STORAGE_ACCOUNT
//
// The subscription in question should have a resource group
// called "packer-acceptance-test" in "South Central US" region. The
// storage account referred to in the above variable should
// be inside this resource group and in "South Central US" as well.
//
// In addition, the PACKER_ACC variable should also be set to
// a non-empty value to enable Packer acceptance tests and the
// options "-v -timeout 90m" should be provided to the test
// command, e.g.:
//   go test -v -timeout 90m -run TestBuilderAcc_.*

import (
	"testing"

	"fmt"
	"os"

	builderT "github.com/hashicorp/packer/helper/builder/testing"
)

const DeviceLoginAcceptanceTest = "DEVICELOGIN_TEST"

func TestBuilderAcc_ManagedDisk_Windows(t *testing.T) {
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccManagedDiskWindows,
	})
}

func TestBuilderAcc_ManagedDisk_Windows_Build_Resource_Group(t *testing.T) {
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccManagedDiskWindowsBuildResourceGroup,
	})
}

func TestBuilderAcc_ManagedDisk_Windows_DeviceLogin(t *testing.T) {
	if os.Getenv(DeviceLoginAcceptanceTest) == "" {
		t.Skip(fmt.Sprintf(
			"Device Login Acceptance tests skipped unless env '%s' set, as its requires manual step during execution",
			DeviceLoginAcceptanceTest))
		return
	}
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccManagedDiskWindowsDeviceLogin,
	})
}

func TestBuilderAcc_ManagedDisk_Linux(t *testing.T) {
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccManagedDiskLinux,
	})
}

func TestBuilderAcc_ManagedDisk_Linux_DeviceLogin(t *testing.T) {
	if os.Getenv(DeviceLoginAcceptanceTest) == "" {
		t.Skip(fmt.Sprintf(
			"Device Login Acceptance tests skipped unless env '%s' set, as its requires manual step during execution",
			DeviceLoginAcceptanceTest))
		return
	}
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccManagedDiskLinuxDeviceLogin,
	})
}

func TestBuilderAcc_Blob_Windows(t *testing.T) {
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccBlobWindows,
	})
}

func TestBuilderAcc_Blob_Linux(t *testing.T) {
	builderT.Test(t, builderT.TestCase{
		PreCheck: func() { testAccPreCheck(t) },
		Builder:  &Builder{},
		Template: testBuilderAccBlobLinux,
	})
}

func testAccPreCheck(*testing.T) {}

const testBuilderAccManagedDiskWindows = `
{
	"variables": {
	  "client_id": "{{env ` + "`ARM_CLIENT_ID`" + `}}",
	  "client_secret": "{{env ` + "`ARM_CLIENT_SECRET`" + `}}",
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "client_id": "{{user ` + "`client_id`" + `}}",
	  "client_secret": "{{user ` + "`client_secret`" + `}}",
	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "managed_image_resource_group_name": "packer-acceptance-test",
	  "managed_image_name": "testBuilderAccManagedDiskWindows-{{timestamp}}",

	  "os_type": "Windows",
	  "image_publisher": "MicrosoftWindowsServer",
	  "image_offer": "WindowsServer",
	  "image_sku": "2012-R2-Datacenter",

	  "communicator": "winrm",
	  "winrm_use_ssl": "true",
	  "winrm_insecure": "true",
	  "winrm_timeout": "3m",
	  "winrm_username": "packer",
	  "async_resourcegroup_delete": "true",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2"
	}]
}
`
const testBuilderAccManagedDiskWindowsBuildResourceGroup = `
{
	"variables": {
	  "client_id": "{{env ` + "`ARM_CLIENT_ID`" + `}}",
	  "client_secret": "{{env ` + "`ARM_CLIENT_SECRET`" + `}}",
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "client_id": "{{user ` + "`client_id`" + `}}",
	  "client_secret": "{{user ` + "`client_secret`" + `}}",
	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "build_resource_group_name" : "packer-acceptance-test",
	  "managed_image_resource_group_name": "packer-acceptance-test",
	  "managed_image_name": "testBuilderAccManagedDiskWindowsBuildResourceGroup-{{timestamp}}",

	  "os_type": "Windows",
	  "image_publisher": "MicrosoftWindowsServer",
	  "image_offer": "WindowsServer",
	  "image_sku": "2012-R2-Datacenter",

	  "communicator": "winrm",
	  "winrm_use_ssl": "true",
	  "winrm_insecure": "true",
	  "winrm_timeout": "3m",
	  "winrm_username": "packer",
	  "async_resourcegroup_delete": "true",

	  "vm_size": "Standard_DS2_v2"
	}]
}
`

const testBuilderAccManagedDiskWindowsDeviceLogin = `
{
	"variables": {
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "managed_image_resource_group_name": "packer-acceptance-test",
	  "managed_image_name": "testBuilderAccManagedDiskWindowsDeviceLogin-{{timestamp}}",

	  "os_type": "Windows",
	  "image_publisher": "MicrosoftWindowsServer",
	  "image_offer": "WindowsServer",
	  "image_sku": "2012-R2-Datacenter",

	  "communicator": "winrm",
	  "winrm_use_ssl": "true",
	  "winrm_insecure": "true",
	  "winrm_timeout": "3m",
	  "winrm_username": "packer",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2"
	}]
}
`

const testBuilderAccManagedDiskLinux = `
{
	"variables": {
	  "client_id": "{{env ` + "`ARM_CLIENT_ID`" + `}}",
	  "client_secret": "{{env ` + "`ARM_CLIENT_SECRET`" + `}}",
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "client_id": "{{user ` + "`client_id`" + `}}",
	  "client_secret": "{{user ` + "`client_secret`" + `}}",
	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "managed_image_resource_group_name": "packer-acceptance-test",
	  "managed_image_name": "testBuilderAccManagedDiskLinux-{{timestamp}}",

	  "os_type": "Linux",
	  "image_publisher": "Canonical",
	  "image_offer": "UbuntuServer",
	  "image_sku": "16.04-LTS",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2",
	  "azure_tags": {
	    "env": "testing",
	    "builder": "packer"
	   }
	}]
}
`
const testBuilderAccManagedDiskLinuxDeviceLogin = `
{
	"variables": {
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "managed_image_resource_group_name": "packer-acceptance-test",
	  "managed_image_name": "testBuilderAccManagedDiskLinuxDeviceLogin-{{timestamp}}",

	  "os_type": "Linux",
	  "image_publisher": "Canonical",
	  "image_offer": "UbuntuServer",
	  "image_sku": "16.04-LTS",
	  "async_resourcegroup_delete": "true",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2"
	}]
}
`

const testBuilderAccBlobWindows = `
{
	"variables": {
	  "client_id": "{{env ` + "`ARM_CLIENT_ID`" + `}}",
	  "client_secret": "{{env ` + "`ARM_CLIENT_SECRET`" + `}}",
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}",
	  "object_id": "{{env ` + "`ARM_OBJECT_ID`" + `}}",
	  "storage_account": "{{env ` + "`ARM_STORAGE_ACCOUNT`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "client_id": "{{user ` + "`client_id`" + `}}",
	  "client_secret": "{{user ` + "`client_secret`" + `}}",
	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",
	  "object_id": "{{user ` + "`object_id`" + `}}",

	  "storage_account": "{{user ` + "`storage_account`" + `}}",
	  "resource_group_name": "packer-acceptance-test",
	  "capture_container_name": "test",
	  "capture_name_prefix": "testBuilderAccBlobWin",

	  "os_type": "Windows",
	  "image_publisher": "MicrosoftWindowsServer",
	  "image_offer": "WindowsServer",
	  "image_sku": "2012-R2-Datacenter",

	  "communicator": "winrm",
	  "winrm_use_ssl": "true",
	  "winrm_insecure": "true",
	  "winrm_timeout": "3m",
	  "winrm_username": "packer",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2"
	}]
}
`

const testBuilderAccBlobLinux = `
{
	"variables": {
	  "client_id": "{{env ` + "`ARM_CLIENT_ID`" + `}}",
	  "client_secret": "{{env ` + "`ARM_CLIENT_SECRET`" + `}}",
	  "subscription_id": "{{env ` + "`ARM_SUBSCRIPTION_ID`" + `}}",
	  "storage_account": "{{env ` + "`ARM_STORAGE_ACCOUNT`" + `}}"
	},
	"builders": [{
	  "type": "test",

	  "client_id": "{{user ` + "`client_id`" + `}}",
	  "client_secret": "{{user ` + "`client_secret`" + `}}",
	  "subscription_id": "{{user ` + "`subscription_id`" + `}}",

	  "storage_account": "{{user ` + "`storage_account`" + `}}",
	  "resource_group_name": "packer-acceptance-test",
	  "capture_container_name": "test",
	  "capture_name_prefix": "testBuilderAccBlobLinux",

	  "os_type": "Linux",
	  "image_publisher": "Canonical",
	  "image_offer": "UbuntuServer",
	  "image_sku": "16.04-LTS",

	  "location": "South Central US",
	  "vm_size": "Standard_DS2_v2"
	}]
}
`
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	log "github.com/cihub/seelog"
	"github.com/crufter/borg/types"
	httpr "github.com/julienschmidt/httprouter"
	"github.com/olivere/elastic"
	"io/ioutil"
	"net/http"
	"reflect"
)

var (
	client *elastic.Client
)

func init() {
	cl, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL("http://127.0.0.1:9200"))
	if err != nil {
		panic(err)
	}
	client = cl
}

func main() {
	r := httpr.New()
	r.GET("/v1/query", query)
	//r.PUT("/v1/problem/:id", update)
	//r.POST("/v1/problem", save)
	//r.POST()
	log.Info("Starting http server")
	log.Critical(http.ListenAndServe(fmt.Sprintf(":%v", 9992), r))
}

func query(w http.ResponseWriter, r *http.Request, p httpr.Params) {
	res, err := client.Search().Index("borg").Type("problem").From(0).Size(5).Query(
		elastic.NewQueryStringQuery(r.FormValue("q"))).Do()
	if err != nil {
		panic(err)
	}
	all := []types.Problem{}
	var ttyp types.Problem
	for _, item := range res.Each(reflect.TypeOf(ttyp)) {
		if t, ok := item.(types.Problem); ok {
			all = append(all, t)
		}
	}
	bs, err := json.Marshal(all)
	if err != nil {
		panic(err)
	}
	fmt.Fprint(w, string(bs))
}

func save(w http.ResponseWriter, r *http.Request, p httpr.Params) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		panic(err)
	}
	b64encoded := []string{}
	err = json.Unmarshal(body, &b64encoded)
	if err != nil {
		panic(err)
	}
	ss := []types.Solution{}
	log.Debugf("Putting services %v", ss)
}
<commit_msg>Parse limit on query on backend<commit_after>package main

import (
	"encoding/json"
	"fmt"
	log "github.com/cihub/seelog"
	"github.com/crufter/borg/types"
	httpr "github.com/julienschmidt/httprouter"
	"github.com/olivere/elastic"
	"net/http"
	"reflect"
	"strconv"
)

var (
	client *elastic.Client
)

func init() {
	cl, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL("http://127.0.0.1:9200"))
	if err != nil {
		panic(err)
	}
	client = cl
}

func main() {
	r := httpr.New()
	r.GET("/v1/query", query)
	log.Info("Starting http server")
	log.Critical(http.ListenAndServe(fmt.Sprintf(":%v", 9992), r))
}

func query(w http.ResponseWriter, r *http.Request, p httpr.Params) {
	size := 5
	s, err := strconv.ParseInt(r.FormValue("l"), 10, 32)
	if err == nil && s > 0 {
		size = int(s)
	}
	if s > 50 {
		s = 50
	}
	res, err := client.Search().Index("borg").Type("problem").From(0).Size(size).Query(
		elastic.NewQueryStringQuery(r.FormValue("q"))).Do()
	if err != nil {
		panic(err)
	}
	all := []types.Problem{}
	var ttyp types.Problem
	for _, item := range res.Each(reflect.TypeOf(ttyp)) {
		if t, ok := item.(types.Problem); ok {
			all = append(all, t)
		}
	}
	bs, err := json.Marshal(all)
	if err != nil {
		panic(err)
	}
	fmt.Fprint(w, string(bs))
}
<|endoftext|>


<commit_before>// +build acceptance

package v2

import (
	"testing"

	"github.com/gophercloud/gophercloud/acceptance/tools"
	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
	th "github.com/gophercloud/gophercloud/testhelper"
)

func TestBootFromVolume(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	choices, err := ComputeChoicesFromEnv()
	if err != nil {
		t.Fatal(err)
	}

	name := tools.RandomString("Gophercloud-", 8)
	t.Logf("Creating server [%s].", name)

	bd := []bootfromvolume.BlockDevice{
		bootfromvolume.BlockDevice{
			UUID:       choices.ImageID,
			SourceType: bootfromvolume.Image,
			VolumeSize: 10,
		},
	}

	serverCreateOpts := servers.CreateOpts{
		Name:      name,
		FlavorRef: choices.FlavorID,
		ImageRef:  choices.ImageID,
	}
	server, err := bootfromvolume.Create(client, bootfromvolume.CreateOptsExt{
		serverCreateOpts,
		bd,
	}).Extract()
	th.AssertNoErr(t, err)
	if err = waitForStatus(client, server, "ACTIVE"); err != nil {
		t.Fatal(err)
	}

	t.Logf("Created server: %+v\n", server)
	defer servers.Delete(client, server.ID)
	t.Logf("Deleting server [%s]...", name)
}

func TestMultiEphemeral(t *testing.T) {
	client, err := newClient()
	th.AssertNoErr(t, err)

	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	choices, err := ComputeChoicesFromEnv()
	if err != nil {
		t.Fatal(err)
	}

	name := tools.RandomString("Gophercloud-", 8)
	t.Logf("Creating server [%s].", name)

	bd := []bootfromvolume.BlockDevice{
		bootfromvolume.BlockDevice{
			BootIndex:           0,
			UUID:                choices.ImageID,
			SourceType:          bootfromvolume.Image,
			DestinationType:     "local",
			DeleteOnTermination: true,
		},
		bootfromvolume.BlockDevice{
			BootIndex:           -1,
			SourceType:          bootfromvolume.Blank,
			DestinationType:     "local",
			DeleteOnTermination: true,
			GuestFormat:         "ext4",
			VolumeSize:          1,
		},
		bootfromvolume.BlockDevice{
			BootIndex:           -1,
			SourceType:          bootfromvolume.Blank,
			DestinationType:     "local",
			DeleteOnTermination: true,
			GuestFormat:         "ext4",
			VolumeSize:          1,
		},
	}

	serverCreateOpts := servers.CreateOpts{
		Name:      name,
		FlavorRef: choices.FlavorID,
		ImageRef:  choices.ImageID,
	}
	server, err := bootfromvolume.Create(client, bootfromvolume.CreateOptsExt{
		serverCreateOpts,
		bd,
	}).Extract()
	th.AssertNoErr(t, err)
	if err = waitForStatus(client, server, "ACTIVE"); err != nil {
		t.Fatal(err)
	}

	t.Logf("Created server: %+v\n", server)
	defer servers.Delete(client, server.ID)
	t.Logf("Deleting server [%s]...", name)
}
<commit_msg>Boot From Volume Acceptance Test Cleanup<commit_after>// +build acceptance compute bootfromvolume

package v2

import (
	"testing"

	"github.com/gophercloud/gophercloud"
	"github.com/gophercloud/gophercloud/acceptance/tools"
	"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
)

func TestBootFromVolumeSingleVolume(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	client, err := newClient()
	if err != nil {
		t.Fatalf("Unable to create a compute client: %v", err)
	}

	choices, err := ComputeChoicesFromEnv()
	if err != nil {
		t.Fatal(err)
	}

	blockDevices := []bootfromvolume.BlockDevice{
		bootfromvolume.BlockDevice{
			UUID:       choices.ImageID,
			SourceType: bootfromvolume.Image,
			VolumeSize: 10,
		},
	}

	server, err := createBootableVolumeServer(t, client, blockDevices, choices)
	if err != nil {
		t.Fatal("Unable to create server: %v", err)
	}
	if err = waitForStatus(client, server, "ACTIVE"); err != nil {
		t.Fatal("Unable to wait for server: %v", err)
	}
	defer deleteServer(t, client, server)

	printServer(t, server)
}

func TestBootFromVolumeMultiEphemeral(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	client, err := newClient()
	if err != nil {
		t.Fatalf("Unable to create a compute client: %v", err)
	}

	choices, err := ComputeChoicesFromEnv()
	if err != nil {
		t.Fatal(err)
	}

	blockDevices := []bootfromvolume.BlockDevice{
		bootfromvolume.BlockDevice{
			BootIndex:           0,
			UUID:                choices.ImageID,
			SourceType:          bootfromvolume.Image,
			DestinationType:     "local",
			DeleteOnTermination: true,
			VolumeSize:          5,
		},
		bootfromvolume.BlockDevice{
			BootIndex:           -1,
			SourceType:          bootfromvolume.Blank,
			DestinationType:     "local",
			DeleteOnTermination: true,
			GuestFormat:         "ext4",
			VolumeSize:          1,
		},
		bootfromvolume.BlockDevice{
			BootIndex:           -1,
			SourceType:          bootfromvolume.Blank,
			DestinationType:     "local",
			DeleteOnTermination: true,
			GuestFormat:         "ext4",
			VolumeSize:          1,
		},
	}

	server, err := createBootableVolumeServer(t, client, blockDevices, choices)
	if err != nil {
		t.Fatalf("Unable to create server: %v", err)
	}
	if err = waitForStatus(client, server, "ACTIVE"); err != nil {
		t.Fatalf("Unable to wait for server: %v", err)
	}
	defer deleteServer(t, client, server)

	printServer(t, server)
}

func createBootableVolumeServer(t *testing.T, client *gophercloud.ServiceClient, blockDevices []bootfromvolume.BlockDevice, choices *ComputeChoices) (*servers.Server, error) {
	if testing.Short() {
		t.Skip("Skipping test that requires server creation in short mode.")
	}

	networkID, err := getNetworkIDFromTenantNetworks(t, client, choices.NetworkName)
	if err != nil {
		t.Fatalf("Failed to obtain network ID: %v", err)
	}

	name := tools.RandomString("ACPTTEST", 16)
	t.Logf("Attempting to create bootable volume server: %s", name)

	serverCreateOpts := servers.CreateOpts{
		Name:      name,
		FlavorRef: choices.FlavorID,
		ImageRef:  choices.ImageID,
		Networks: []servers.Network{
			servers.Network{UUID: networkID},
		},
	}

	server, err := bootfromvolume.Create(client, bootfromvolume.CreateOptsExt{
		serverCreateOpts,
		blockDevices,
	}).Extract()

	if err != nil {
		return server, err
	}

	return server, nil
}
<|endoftext|>


<commit_before>// Copyright 2018 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.

package client

import (
	"fmt"
	"path"
	"strings"

	"github.com/keybase/cli"
	"github.com/keybase/client/go/libcmdline"
	"github.com/keybase/client/go/libkb"
	keybase1 "github.com/keybase/client/go/protocol/keybase1"
	"golang.org/x/net/context"
)

// CmdSimpleFSSyncEnable is the 'fs sync enable' command.
type CmdSimpleFSSyncEnable struct {
	libkb.Contextified
	path keybase1.Path
}

// NewCmdSimpleFSSyncEnable creates a new cli.Command.
func NewCmdSimpleFSSyncEnable(
	cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
	return cli.Command{
		Name:         "enable",
		ArgumentHelp: "[path-to-folder]",
		Usage:        "syncs the given folder to local storage, for offline access",
		Action: func(c *cli.Context) {
			cl.ChooseCommand(&CmdSimpleFSSyncEnable{
				Contextified: libkb.NewContextified(g)}, "enable", c)
			cl.SetNoStandalone()
		},
	}
}

func toTlfPath(p keybase1.Path) (keybase1.Path, error) {
	split := strings.SplitN(p.String(), "/", 4)
	if len(split) < 4 {
		return p, nil
	}
	return makeSimpleFSPath(
		path.Join(append([]string{mountDir}, split[0:3]...)...))
}

func pathMinusTlf(p keybase1.Path) string {
	split := strings.SplitN(p.String(), "/", 4)
	if len(split) < 4 {
		return ""
	}
	return split[3]
}

// Run runs the command in client/server mode.
func (c *CmdSimpleFSSyncEnable) Run() error {
	cli, err := GetSimpleFSClient(c.G())
	if err != nil {
		return err
	}

	ctx := context.TODO()
	arg := keybase1.SimpleFSSetFolderSyncConfigArg{
		Config: keybase1.FolderSyncConfig{
			Mode: keybase1.FolderSyncMode_ENABLED,
		},
		Path: c.path,
	}

	subpath := pathMinusTlf(c.path)
	if subpath != "" {
		arg.Path, err = toTlfPath(c.path)
		if err != nil {
			return err
		}
		res, err := cli.SimpleFSFolderSyncConfigAndStatus(ctx, arg.Path)
		if err != nil {
			return err
		}

		if res.Config.Mode == keybase1.FolderSyncMode_ENABLED {
			return fmt.Errorf("Must disable full syncing on %s first", arg.Path)
		}

		for _, p := range res.Config.Paths {
			if p == subpath {
				// Already enabled.
				return nil
			}
		}

		arg.Config.Mode = keybase1.FolderSyncMode_PARTIAL
		arg.Config.Paths = append(res.Config.Paths, subpath)
	}

	return cli.SimpleFSSetFolderSyncConfig(ctx, arg)
}

// ParseArgv gets the required path.
func (c *CmdSimpleFSSyncEnable) ParseArgv(ctx *cli.Context) error {
	if len(ctx.Args()) != 1 {
		return fmt.Errorf("wrong number of arguments")
	}

	p, err := makeSimpleFSPath(ctx.Args()[0])
	if err != nil {
		return err
	}
	c.path = p
	return nil
}

// GetUsage says what this command needs to operate.
func (c *CmdSimpleFSSyncEnable) GetUsage() libkb.Usage {
	return libkb.Usage{
		Config:    true,
		KbKeyring: true,
		API:       true,
	}
}
<commit_msg>client: allow non-abs paths when splitting keybase paths<commit_after>// Copyright 2018 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.

package client

import (
	"fmt"
	"path"
	"strings"

	"github.com/keybase/cli"
	"github.com/keybase/client/go/libcmdline"
	"github.com/keybase/client/go/libkb"
	keybase1 "github.com/keybase/client/go/protocol/keybase1"
	"golang.org/x/net/context"
)

// CmdSimpleFSSyncEnable is the 'fs sync enable' command.
type CmdSimpleFSSyncEnable struct {
	libkb.Contextified
	path keybase1.Path
}

// NewCmdSimpleFSSyncEnable creates a new cli.Command.
func NewCmdSimpleFSSyncEnable(
	cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
	return cli.Command{
		Name:         "enable",
		ArgumentHelp: "[path-to-sync]",
		Usage:        "syncs the given folder to local storage, for offline access",
		Action: func(c *cli.Context) {
			cl.ChooseCommand(&CmdSimpleFSSyncEnable{
				Contextified: libkb.NewContextified(g)}, "enable", c)
			cl.SetNoStandalone()
		},
	}
}

const minNumKeybasePathElems = 4

func splitKeybasePath(p keybase1.Path) []string {
	toSplit := p.String()
	// Just in case the path isn't absolute.
	if !strings.HasPrefix(toSplit, "/") {
		toSplit = "/" + toSplit
	}
	return strings.SplitN(toSplit, "/", minNumKeybasePathElems)
}

func toTlfPath(p keybase1.Path) (keybase1.Path, error) {
	split := splitKeybasePath(p)
	if len(split) < minNumKeybasePathElems {
		return p, nil
	}
	return makeSimpleFSPath(
		path.Join(append([]string{mountDir}, split[0:3]...)...))
}

func pathMinusTlf(p keybase1.Path) string {
	split := splitKeybasePath(p)
	if len(split) < minNumKeybasePathElems {
		return ""
	}
	return split[3]
}

// Run runs the command in client/server mode.
func (c *CmdSimpleFSSyncEnable) Run() error {
	cli, err := GetSimpleFSClient(c.G())
	if err != nil {
		return err
	}

	ctx := context.TODO()
	arg := keybase1.SimpleFSSetFolderSyncConfigArg{
		Config: keybase1.FolderSyncConfig{
			Mode: keybase1.FolderSyncMode_ENABLED,
		},
		Path: c.path,
	}

	subpath := pathMinusTlf(c.path)
	if subpath != "" {
		arg.Path, err = toTlfPath(c.path)
		if err != nil {
			return err
		}
		res, err := cli.SimpleFSFolderSyncConfigAndStatus(ctx, arg.Path)
		if err != nil {
			return err
		}

		if res.Config.Mode == keybase1.FolderSyncMode_ENABLED {
			return fmt.Errorf("Must disable full syncing on %s first", arg.Path)
		}

		for _, p := range res.Config.Paths {
			if p == subpath {
				// Already enabled.
				return nil
			}
		}

		arg.Config.Mode = keybase1.FolderSyncMode_PARTIAL
		arg.Config.Paths = append(res.Config.Paths, subpath)
	}

	return cli.SimpleFSSetFolderSyncConfig(ctx, arg)
}

// ParseArgv gets the required path.
func (c *CmdSimpleFSSyncEnable) ParseArgv(ctx *cli.Context) error {
	if len(ctx.Args()) != 1 {
		return fmt.Errorf("wrong number of arguments")
	}

	p, err := makeSimpleFSPath(ctx.Args()[0])
	if err != nil {
		return err
	}
	c.path = p
	return nil
}

// GetUsage says what this command needs to operate.
func (c *CmdSimpleFSSyncEnable) GetUsage() libkb.Usage {
	return libkb.Usage{
		Config:    true,
		KbKeyring: true,
		API:       true,
	}
}
<|endoftext|>


<commit_before>package print_store

import (
	"strings"
	"time"

	"github.com/cloudfoundry-incubator/runtime-schema/models"
	"github.com/onsi/gomega/format"
	"github.com/pivotal-cf-experimental/veritas/say"
	"github.com/pivotal-cf-experimental/veritas/veritas_models"
)

func printLRPS(verbose bool, lrps veritas_models.VeritasLRPS) {
	say.Println(0, say.Green("LRPs"))

	sortedLRPS := lrps.SortedByProcessGuid()
	for _, lrp := range sortedLRPS {
		if verbose {
			printVerboseLRP(lrp)
		} else {
			printLRP(lrp)
		}
	}
}

func printDomains(domains []string) {
	say.Println(0, say.Green("Domains"))
	if len(domains) == 0 {
		say.Println(1, say.Red("None"))
		return
	}
	for _, domain := range domains {
		say.Println(1, say.Green("%s", domain))
	}
}

func printVerboseLRP(lrp *veritas_models.VeritasLRP) {
	say.Println(0, format.Object(lrp, 1))
}

func printLRP(lrp *veritas_models.VeritasLRP) {
	say.Println(1, say.Green(lrp.ProcessGuid))
	if lrp.DesiredLRP.ProcessGuid != "" {
		privileged := ""
		if lrp.DesiredLRP.Privileged {
			privileged = say.Red(" PRIVILEGED")
		}
		say.Println(
			2,
			"%s on %s%s (%d MB, %d MB, %d CPU) %s",
			say.Green("%d", lrp.DesiredLRP.Instances),
			say.Green(lrp.DesiredLRP.Stack),
			privileged,
			lrp.DesiredLRP.MemoryMB,
			lrp.DesiredLRP.DiskMB,
			lrp.DesiredLRP.CPUWeight,
			say.Yellow(strings.Join(lrp.DesiredLRP.Routes, ", ")),
		)
	} else {
		say.Println(2, say.Red("UNDESIRED"))
	}

	orderedActualIndices := lrp.OrderedActualLRPIndices()
	for _, index := range orderedActualIndices {
		actual := lrp.ActualLRPsByIndex[index]
		if actual.State == models.ActualLRPStateUnclaimed || actual.State == models.ActualLRPStateCrashed {
			say.Println(
				3,
				"%2s: [%s for %s]",
				index,
				actualState(actual),
				time.Since(time.Unix(0, actual.Since)),
			)
		} else {
			say.Println(
				3,
				"%2s: %s %s [%s for %s]",
				index,
				actual.InstanceGuid,
				say.Yellow(actual.CellID),
				actualState(actual),
				time.Since(time.Unix(0, actual.Since)),
			)
		}
	}
}

func actualState(actual models.ActualLRP) string {
	switch actual.State {
	case models.ActualLRPStateUnclaimed:
		return say.LightGray("UNCLAIMED")
	case models.ActualLRPStateClaimed:
		return say.Yellow("CLAIMED")
	case models.ActualLRPStateRunning:
		return say.Green("RUNNING")
	case models.ActualLRPStateCrashed:
		return say.Red("CRASHED (%d)", actual.CrashCount)
	default:
		return say.Red("INVALID")
	}
}
<commit_msg>mention placement error<commit_after>package print_store

import (
	"strings"
	"time"

	"github.com/cloudfoundry-incubator/runtime-schema/models"
	"github.com/onsi/gomega/format"
	"github.com/pivotal-cf-experimental/veritas/say"
	"github.com/pivotal-cf-experimental/veritas/veritas_models"
)

func printLRPS(verbose bool, lrps veritas_models.VeritasLRPS) {
	say.Println(0, say.Green("LRPs"))

	sortedLRPS := lrps.SortedByProcessGuid()
	for _, lrp := range sortedLRPS {
		if verbose {
			printVerboseLRP(lrp)
		} else {
			printLRP(lrp)
		}
	}
}

func printDomains(domains []string) {
	say.Println(0, say.Green("Domains"))
	if len(domains) == 0 {
		say.Println(1, say.Red("None"))
		return
	}
	for _, domain := range domains {
		say.Println(1, say.Green("%s", domain))
	}
}

func printVerboseLRP(lrp *veritas_models.VeritasLRP) {
	say.Println(0, format.Object(lrp, 1))
}

func printLRP(lrp *veritas_models.VeritasLRP) {
	say.Println(1, say.Green(lrp.ProcessGuid))
	if lrp.DesiredLRP.ProcessGuid != "" {
		privileged := ""
		if lrp.DesiredLRP.Privileged {
			privileged = say.Red(" PRIVILEGED")
		}
		say.Println(
			2,
			"%s on %s%s (%d MB, %d MB, %d CPU) %s",
			say.Green("%d", lrp.DesiredLRP.Instances),
			say.Green(lrp.DesiredLRP.Stack),
			privileged,
			lrp.DesiredLRP.MemoryMB,
			lrp.DesiredLRP.DiskMB,
			lrp.DesiredLRP.CPUWeight,
			say.Yellow(strings.Join(lrp.DesiredLRP.Routes, ", ")),
		)
	} else {
		say.Println(2, say.Red("UNDESIRED"))
	}

	orderedActualIndices := lrp.OrderedActualLRPIndices()
	for _, index := range orderedActualIndices {
		actual := lrp.ActualLRPsByIndex[index]
		if actual.State == models.ActualLRPStateUnclaimed || actual.State == models.ActualLRPStateCrashed {
			say.Println(
				3,
				"%2s: [%s for %s]",
				index,
				actualState(actual),
				time.Since(time.Unix(0, actual.Since)),
			)
		} else {
			say.Println(
				3,
				"%2s: %s %s [%s for %s]",
				index,
				actual.InstanceGuid,
				say.Yellow(actual.CellID),
				actualState(actual),
				time.Since(time.Unix(0, actual.Since)),
			)
		}
	}
}

func actualState(actual models.ActualLRP) string {
	switch actual.State {
	case models.ActualLRPStateUnclaimed:
		if actual.PlacementError == "" {
			return say.LightGray("UNCLAIMED")
		} else {
			return say.Red("UNCLAIMED (%s)", actual.PlacementError)
		}
	case models.ActualLRPStateClaimed:
		return say.Yellow("CLAIMED")
	case models.ActualLRPStateRunning:
		return say.Green("RUNNING")
	case models.ActualLRPStateCrashed:
		return say.Red("CRASHED (%d)", actual.CrashCount)
	default:
		return say.Red("INVALID")
	}
}
<|endoftext|>


<commit_before>package testdemo

import (
    . "github.com/getwe/goose/utils"
    "github.com/laurent22/toml-go"
    "github.com/getwe/scws4go"
	"encoding/json"
    "reflect"
    "runtime"
)

type oneDocJson struct {
    title   string
    docid   uint32
    hot     uint8
    desc    string
}

// 建库的时候,goose框架建静态库读取文件认为一行是一个doc,动态库一个网络请求是一
// 个doc,这是框架设计.StyIndexer每一个doc是一个json结构,只关注其中4个字段:
//  title:doc的标题,建立索引的字段
//  docid:唯一的外部标志符
//  hot:作为Value,只使用这一个字段,实际情况下可以在Value中存储多个字段
//  desc:附加描述信息,不参与检索
// 这构成了一个最简单的检索元素.
type StyIndexer struct {
    // 共用切词工具
    scws    *scws4go.Scws
}

// 分析一个doc,返回其中的term列表,Value,Data.(必须保证框架可并发调用ParseDoc)
func (this *StyIndexer) ParseDoc(doc interface{}) (
    outId OutIdType,termList []TermInDoc,value *Value,data *Data,err error) {
    // ParseDoc的功能实现需要注意的是,这个函数是可并发的,使用StyIndexer.*需要注意安全
    defer func() {
        if r := recover();r != nil {
            err = NewGooseError("ParseDoc","Catch Exception","")
        }
    }()

    // 策略假设每一个doc就是一个[]buf
    realValue := reflect.ValueOf(doc)
    docbuf := realValue.Bytes()
    docJson := oneDocJson{}
    err = json.Unmarshal(docbuf,docJson)
    if err != nil {
        return
    }

    // 对title进行切词
    segResult,err := this.scws.Segment(docJson.title)
    if err != nil {
        return
    }

    // 对doc的term进行基础赋权
    // (在一个成熟的检索系统里面,需要一个复杂的子系统来完成工作)
    // 这个测试例子中直接取scws中term的idf
    // 同时,对term也做去重
    termmap := make(map[TermSign]TermWeight)
    for _,term := range segResult {
        tsign := TermSign(StringSignMd5(term.Term))
        tweight := TermWeight(term.Idf * 100)
        if tweight < 1 {
            tweight = 1
        }

        oldwei,ok := termmap[tsign]
        if ok {
            // 取大
            if tweight < oldwei {
                tweight = oldwei
            }
        }
        termmap[tsign] = tweight
    }
    termList = termList[0:0]
    for k,v := range termmap {
        termList = append(termList,TermInDoc{
            Sign : k,Weight : v})
    }

    // 从doc中提取需要写入Value的数据
    // 这个策略只使用value的一个字节,写入hot值
    // 合理情况这里应该从配置读取(或者在Init阶段提前读取)Value的长度
    *value = make([]byte,1)
    (*value)[0] = docJson.hot

    // 从doc中提取需要写入Data的数据
    // 简单把全部传入的数据当成data返回
    *data = make([]byte,len(docbuf))
    copy(*data,docbuf)

    return
}

// 调用一次初始化
func (this *StyIndexer) Init(conf toml.Document) (error) {

    // scws初始化
    scwsDictPath := conf.GetString("Strategy.Indexer.Scws.xdbdict")
    scwsRulePath := conf.GetString("Strategy.Indexer.Scws.rules")
    scwsForkCnt  := runtime.NumCPU()
    this.scws = scws4go.NewScws()
    this.scws.SetDict(scwsDictPath, scws4go.SCWS_XDICT_XDB|scws4go.SCWS_XDICT_MEM)
    this.scws.SetRule(scwsRulePath)
    this.scws.SetCharset("utf8")
    this.scws.SetIgnore(1)
    this.scws.SetMulti(scws4go.SCWS_MULTI_SHORT & scws4go.SCWS_MULTI_DUALITY & scws4go.SCWS_MULTI_ZMAIN)
    this.scws.Init(scwsForkCnt)

    return nil
}

<commit_msg>testdemo strategy add error handle<commit_after>package testdemo

import (
    . "github.com/getwe/goose/utils"
    "github.com/laurent22/toml-go"
    "github.com/getwe/scws4go"
	"encoding/json"
    "reflect"
    "runtime"
)

type oneDocJson struct {
    title   string
    docid   uint32
    hot     uint8
    desc    string
}

// 建库的时候,goose框架建静态库读取文件认为一行是一个doc,动态库一个网络请求是一
// 个doc,这是框架设计.StyIndexer每一个doc是一个json结构,只关注其中4个字段:
//  title:doc的标题,建立索引的字段
//  docid:唯一的外部标志符
//  hot:作为Value,只使用这一个字段,实际情况下可以在Value中存储多个字段
//  desc:附加描述信息,不参与检索
// 这构成了一个最简单的检索元素.
type StyIndexer struct {
    // 共用切词工具
    scws    *scws4go.Scws
}

// 分析一个doc,返回其中的term列表,Value,Data.(必须保证框架可并发调用ParseDoc)
func (this *StyIndexer) ParseDoc(doc interface{}) (
    outId OutIdType,termList []TermInDoc,value *Value,data *Data,err error) {
    // ParseDoc的功能实现需要注意的是,这个函数是可并发的,使用StyIndexer.*需要注意安全
    defer func() {
        if r := recover();r != nil {
            err = NewGooseError("ParseDoc","Catch Exception","")
        }
    }()

    // 策略假设每一个doc就是一个[]buf
    realValue := reflect.ValueOf(doc)
    docbuf := realValue.Bytes()
    docJson := oneDocJson{}
    err = json.Unmarshal(docbuf,docJson)
    if err != nil {
        return
    }

    // 对title进行切词
    segResult,err := this.scws.Segment(docJson.title)
    if err != nil {
        return
    }

    // 对doc的term进行基础赋权
    // (在一个成熟的检索系统里面,需要一个复杂的子系统来完成工作)
    // 这个测试例子中直接取scws中term的idf
    // 同时,对term也做去重
    termmap := make(map[TermSign]TermWeight)
    for _,term := range segResult {
        tsign := TermSign(StringSignMd5(term.Term))
        tweight := TermWeight(term.Idf * 100)
        if tweight < 1 {
            tweight = 1
        }

        oldwei,ok := termmap[tsign]
        if ok {
            // 取大
            if tweight < oldwei {
                tweight = oldwei
            }
        }
        termmap[tsign] = tweight
    }
    termList = termList[0:0]
    for k,v := range termmap {
        termList = append(termList,TermInDoc{
            Sign : k,Weight : v})
    }

    // 从doc中提取需要写入Value的数据
    // 这个策略只使用value的一个字节,写入hot值
    // 合理情况这里应该从配置读取(或者在Init阶段提前读取)Value的长度
    *value = make([]byte,1)
    (*value)[0] = docJson.hot

    // 从doc中提取需要写入Data的数据
    // 简单把全部传入的数据当成data返回
    *data = make([]byte,len(docbuf))
    copy(*data,docbuf)

    return
}

// 调用一次初始化
func (this *StyIndexer) Init(conf toml.Document) (err error) {

    // scws初始化
    scwsDictPath := conf.GetString("Strategy.Indexer.Scws.xdbdict")
    scwsRulePath := conf.GetString("Strategy.Indexer.Scws.rules")
    scwsForkCnt  := runtime.NumCPU()
    this.scws = scws4go.NewScws()
    err = this.scws.SetDict(scwsDictPath, scws4go.SCWS_XDICT_XDB|scws4go.SCWS_XDICT_MEM)
    if err != nil { return }
    err = this.scws.SetRule(scwsRulePath)
    if err != nil { return }
    this.scws.SetCharset("utf8")
    this.scws.SetIgnore(1)
    this.scws.SetMulti(scws4go.SCWS_MULTI_SHORT & scws4go.SCWS_MULTI_DUALITY & scws4go.SCWS_MULTI_ZMAIN)
    err = this.scws.Init(scwsForkCnt)
    if err != nil { return }

    return nil
}

<|endoftext|>


<commit_before>package imageplugin

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"

	"code.cloudfoundry.org/garden"
	"code.cloudfoundry.org/garden-shed/rootfs_provider"
	"code.cloudfoundry.org/lager"
	"github.com/cloudfoundry/gunk/command_runner"
	specs "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/tscolari/lagregator"
)

func New(binPath string, commandRunner command_runner.CommandRunner, defaultBaseImage *url.URL, mappings []specs.LinuxIDMapping) *ExternalImageManager {
	return &ExternalImageManager{
		binPath:          binPath,
		commandRunner:    commandRunner,
		defaultBaseImage: defaultBaseImage,
		mappings:         mappings,
	}
}

type ExternalImageManager struct {
	binPath          string
	commandRunner    command_runner.CommandRunner
	defaultBaseImage *url.URL
	mappings         []specs.LinuxIDMapping
}

func (p *ExternalImageManager) Create(log lager.Logger, handle string, spec rootfs_provider.Spec) (string, []string, error) {
	log = log.Session("image-plugin-create")
	log.Debug("start")
	defer log.Debug("end")

	args := []string{"create"}
	if spec.QuotaSize != 0 {
		if spec.QuotaScope == garden.DiskLimitScopeExclusive {
			args = append(args, "--exclude-image-from-quota")
		}
		args = append(args, "--disk-limit-size-bytes", strconv.FormatInt(spec.QuotaSize, 10))
	}

	if spec.Namespaced {
		for _, mapping := range p.mappings {
			args = append(args, "--uid-mapping", stringifyMapping(mapping))
			args = append(args, "--gid-mapping", stringifyMapping(mapping))
		}
	}

	if spec.RootFS == nil || spec.RootFS.String() == "" {
		args = append(args, p.defaultBaseImage.String())
	} else {
		args = append(args, spec.RootFS.String())
	}

	args = append(args, handle)

	cmd := exec.Command(p.binPath, args...)

	cmd.Stderr = lagregator.NewRelogger(log)
	outBuffer := bytes.NewBuffer([]byte{})
	cmd.Stdout = outBuffer

	if spec.Namespaced {
		cmd.SysProcAttr = &syscall.SysProcAttr{
			Credential: &syscall.Credential{
				Uid: p.mappings[0].HostID,
				Gid: p.mappings[0].HostID,
			},
		}
	}

	if err := p.commandRunner.Run(cmd); err != nil {
		logData := lager.Data{"action": "create", "stdout": outBuffer.String()}
		log.Error("external-image-manager-result", err, logData)
		return "", nil, fmt.Errorf("external image manager create failed: %s (%s)", outBuffer.String(), err)
	}

	imagePath := strings.TrimSpace(outBuffer.String())
	envVars, err := p.readEnvVars(imagePath)
	if err != nil {
		return "", nil, err
	}

	rootFSPath := filepath.Join(imagePath, "rootfs")
	return rootFSPath, envVars, nil
}

func (p *ExternalImageManager) Destroy(log lager.Logger, handle, rootFSPath string) error {
	log = log.Session("image-plugin-destroy")
	log.Debug("start")
	defer log.Debug("end")

	imagePath := filepath.Dir(rootFSPath)
	cmd := exec.Command(p.binPath, "delete", imagePath)

	cmd.Stderr = lagregator.NewRelogger(log)
	outBuffer := bytes.NewBuffer([]byte{})
	cmd.Stdout = outBuffer

	if err := p.commandRunner.Run(cmd); err != nil {
		logData := lager.Data{"action": "delete", "stdout": outBuffer.String()}
		log.Error("external-image-manager-result", err, logData)
		return fmt.Errorf("external image manager destroy failed: %s (%s)", outBuffer.String(), err)
	}

	return nil
}

func (p *ExternalImageManager) Metrics(log lager.Logger, _, rootfs string) (garden.ContainerDiskStat, error) {
	log = log.Session("image-plugin-metrics", lager.Data{"rootfs": rootfs})
	log.Debug("start")
	defer log.Debug("end")

	imagePath := filepath.Dir(rootfs)
	cmd := exec.Command(p.binPath, "stats", imagePath)
	cmd.Stderr = lagregator.NewRelogger(log)
	outBuffer := bytes.NewBuffer([]byte{})
	cmd.Stdout = outBuffer

	if err := p.commandRunner.Run(cmd); err != nil {
		logData := lager.Data{"action": "stats", "stderr": errBuffer.String()}
		log.Error("external-image-manager-result", err, logData)
		return garden.ContainerDiskStat{}, fmt.Errorf("external image manager metrics failed: %s (%s)", outBuffer.String(), err)
	}

	var metrics map[string]map[string]uint64
	if err := json.NewDecoder(outBuffer).Decode(&metrics); err != nil {
		return garden.ContainerDiskStat{}, fmt.Errorf("parsing metrics: %s", err)
	}

	return garden.ContainerDiskStat{
		TotalBytesUsed:     metrics["disk_usage"]["total_bytes_used"],
		ExclusiveBytesUsed: metrics["disk_usage"]["exclusive_bytes_used"],
	}, nil
}

func (p *ExternalImageManager) GC(log lager.Logger) error {
	log = log.Session("image-plugin-gc")
	log.Debug("start")
	defer log.Debug("end")

	cmd := exec.Command(p.binPath, "clean")
	cmd.Stderr = lagregator.NewRelogger(log)
	outBuffer := bytes.NewBuffer([]byte{})
	cmd.Stdout = outBuffer

	if err := p.commandRunner.Run(cmd); err != nil {
		logData := lager.Data{"action": "clean", "stdout": outBuffer.String()}
		log.Error("external-image-manager-result", err, logData)
		return fmt.Errorf("external image manager clean failed: %s (%s)", outBuffer.String(), err)
	}

	return nil
}

func stringifyMapping(mapping specs.LinuxIDMapping) string {
	return fmt.Sprintf("%d:%d:%d", mapping.ContainerID, mapping.HostID, mapping.Size)
}

func (p *ExternalImageManager) readEnvVars(imagePath string) ([]string, error) {
	imageConfigFile, err := os.Open(filepath.Join(imagePath, "image.json"))
	if err != nil {
		if os.IsNotExist(err) {
			return []string{}, nil
		}

		return nil, fmt.Errorf("could not open image configuration: %s", err)
	}

	var imageConfig Image
	if err := json.NewDecoder(imageConfigFile).Decode(&imageConfig); err != nil {
		return nil, fmt.Errorf("parsing image config: %s", err)
	}

	return imageConfig.Config.Env, nil
}
<commit_msg>Fix compilation error<commit_after>package imageplugin

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/url"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"

	"code.cloudfoundry.org/garden"
	"code.cloudfoundry.org/garden-shed/rootfs_provider"
	"code.cloudfoundry.org/lager"
	"github.com/cloudfoundry/gunk/command_runner"
	specs "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/tscolari/lagregator"
)

func New(binPath string, commandRunner command_runner.CommandRunner, defaultBaseImage *url.URL, mappings []specs.LinuxIDMapping) *ExternalImageManager {
	return &ExternalImageManager{
		binPath:          binPath,
		commandRunner:    commandRunner,
		defaultBaseImage: defaultBaseImage,
		mappings:         mappings,
	}
}

type ExternalImageManager struct {
	binPath          string
	commandRunner    command_runner.CommandRunner
	defaultBaseImage *url.URL
	mappings         []specs.LinuxIDMapping
}

func (p *ExternalImageManager) Create(log lager.Logger, handle string, spec rootfs_provider.Spec) (string, []string, error) {
	log = log.Session("image-plugin-create")
	log.Debug("start")
	defer log.Debug("end")

	args := []string{"create"}
	if spec.QuotaSize != 0 {
		if spec.QuotaScope == garden.DiskLimitScopeExclusive {
			args = append(args, "--exclude-image-from-quota")
		}
		args = append(args, "--disk-limit-size-bytes", strconv.FormatInt(spec.QuotaSize, 10))
	}

	if spec.Namespaced {
		for _, mapping := range p.mappings {
			args = append(args, "--uid-mapping", stringifyMapping(mapping))
			args = append(args, "--gid-mapping", stringifyMapping(mapping))
		}
	}

	if spec.RootFS == nil || spec.RootFS.String() == "" {
		args = append(args, p.defaultBaseImage.String())
	} else {
		args = append(args, spec.RootFS.String())
	}

	args = append(args, handle)

	cmd := exec.Command(p.binPath, args...)

	cmd.Stderr = lagregator.NewRelogger(log)
	outBuffer := bytes.NewBuffer([]byte{})
	cmd.Stdout = outBuffer

	if spec.Namespaced {
		cmd.SysProcAttr = &syscall.SysProcAttr{
			Credential: &syscall.Credential{
				Uid: p.mappings[0].HostID,
				Gid: p.mappings[0].HostID,
			},
		}
	}

	if err := p.commandRunner.Run(cmd); err != nil {
		logData := lager.Data{"action": "create", "stdout": outBuffer.String()}
		log.Error("external-image-manager-result", err, logData)
		return "", nil, fmt.Errorf("external image manager create failed: %s (%s)", outBuffer.String(), err)
	}

	imagePath := strings.TrimSpace(outBuffer.String())
	envVars, err := p.readEnvVars(imagePath)
	if err != nil {
		return "", nil, err
	}

	rootFSPath := filepath.Join(imagePath, "rootfs")
	return rootFSPath, envVars, nil
}

func (p *ExternalImageManager) Destroy(log lager.Logger, handle, rootFSPath string) error {
	log = log.Session("image-plugin-destroy")
	log.Debug("start")
	defer log.Debug("end")

	imagePath := filepath.Dir(rootFSPath)
	cmd := exec.Command(p.binPath, "delete", imagePath)

	cmd.Stderr = lagregator.NewRelogger(log)
	outBuffer := bytes.NewBuffer([]byte{})
	cmd.Stdout = outBuffer

	if err := p.commandRunner.Run(cmd); err != nil {
		logData := lager.Data{"action": "delete", "stdout": outBuffer.String()}
		log.Error("external-image-manager-result", err, logData)
		return fmt.Errorf("external image manager destroy failed: %s (%s)", outBuffer.String(), err)
	}

	return nil
}

func (p *ExternalImageManager) Metrics(log lager.Logger, _, rootfs string) (garden.ContainerDiskStat, error) {
	log = log.Session("image-plugin-metrics", lager.Data{"rootfs": rootfs})
	log.Debug("start")
	defer log.Debug("end")

	imagePath := filepath.Dir(rootfs)
	cmd := exec.Command(p.binPath, "stats", imagePath)
	cmd.Stderr = lagregator.NewRelogger(log)
	outBuffer := bytes.NewBuffer([]byte{})
	cmd.Stdout = outBuffer

	if err := p.commandRunner.Run(cmd); err != nil {
		logData := lager.Data{"action": "stats", "stderr": outBuffer.String()}
		log.Error("external-image-manager-result", err, logData)
		return garden.ContainerDiskStat{}, fmt.Errorf("external image manager metrics failed: %s (%s)", outBuffer.String(), err)
	}

	var metrics map[string]map[string]uint64
	if err := json.NewDecoder(outBuffer).Decode(&metrics); err != nil {
		return garden.ContainerDiskStat{}, fmt.Errorf("parsing metrics: %s", err)
	}

	return garden.ContainerDiskStat{
		TotalBytesUsed:     metrics["disk_usage"]["total_bytes_used"],
		ExclusiveBytesUsed: metrics["disk_usage"]["exclusive_bytes_used"],
	}, nil
}

func (p *ExternalImageManager) GC(log lager.Logger) error {
	log = log.Session("image-plugin-gc")
	log.Debug("start")
	defer log.Debug("end")

	cmd := exec.Command(p.binPath, "clean")
	cmd.Stderr = lagregator.NewRelogger(log)
	outBuffer := bytes.NewBuffer([]byte{})
	cmd.Stdout = outBuffer

	if err := p.commandRunner.Run(cmd); err != nil {
		logData := lager.Data{"action": "clean", "stdout": outBuffer.String()}
		log.Error("external-image-manager-result", err, logData)
		return fmt.Errorf("external image manager clean failed: %s (%s)", outBuffer.String(), err)
	}

	return nil
}

func stringifyMapping(mapping specs.LinuxIDMapping) string {
	return fmt.Sprintf("%d:%d:%d", mapping.ContainerID, mapping.HostID, mapping.Size)
}

func (p *ExternalImageManager) readEnvVars(imagePath string) ([]string, error) {
	imageConfigFile, err := os.Open(filepath.Join(imagePath, "image.json"))
	if err != nil {
		if os.IsNotExist(err) {
			return []string{}, nil
		}

		return nil, fmt.Errorf("could not open image configuration: %s", err)
	}

	var imageConfig Image
	if err := json.NewDecoder(imageConfigFile).Decode(&imageConfig); err != nil {
		return nil, fmt.Errorf("parsing image config: %s", err)
	}

	return imageConfig.Config.Env, nil
}
<|endoftext|>


<commit_before>package helpers

import (
	"fmt"
	"sort"
	"strings"

	uuid "github.com/nu7hatch/gouuid"
)

// TODO: Is this working???

// GenerateHigherName will use the passed randomNameGenerator to generate a name with a higher
// sort value than all the passed names
func GenerateHigherName(randomNameGenerator func() string, names ...string) string {
	sort.Strings(names)

	maxName := names[len(names)-1]

	for {
		name := randomNameGenerator()
		// regenerate name if name is NOT higher
		if strings.Compare(name, maxName) > 0 {
			return name
		}
	}
}

// TODO: Is this working???

// GenerateLowerName will use the passed randomNameGenerator to generate a name with a lower
// sort value than all the passed names
func GenerateLowerName(randomNameGenerator func() string, names ...string) string {
	if len(names) == 0 {
		return randomNameGenerator()
	}

	sort.Strings(names)

	minName := names[0]

	for {
		name := randomNameGenerator()
		// regenerate name if name is NOT higher
		if strings.Compare(name, minName) < 0 {
			return name
		}
	}
}

// NewAppName provides a random name prefixed with INTEGRATION-APP
func NewAppName() string {
	return PrefixedRandomName("INTEGRATION-APP")
}

// NewIsolationSegmentName provides a random name prefixed with INTEGRATION-ISOLATION-SEGMENT
func NewIsolationSegmentName(infix ...string) string {
	return PrefixedRandomName("INTEGRATION-ISOLATION-SEGMENT")
}

// NewOrgName provides a random name prefixed with INTEGRATION-ORG
func NewOrgName() string {
	return PrefixedRandomName("INTEGRATION-ORG")
}

// NewServiceBrokerName provides a random name prefixed with INTEGRATION-SERVICE-BROKER
func NewServiceBrokerName() string {
	return PrefixedRandomName("INTEGRATION-SERVICE-BROKER")
}

// NewPlanName provides a random name prefixed with INTEGRATION-PLAN
func NewPlanName() string {
	return PrefixedRandomName("INTEGRATION-PLAN")
}

// NewPassword provides a random string prefixed with INTEGRATION-PASSWORD
func NewPassword() string {
	return PrefixedRandomName("INTEGRATION-PASSWORD")
}

// NewSecurityGroupName provides a random name prefixed with INTEGRATION-SEC-GROUP. If an infix is provided, it
// is placed between INTEGRATION-SEC-GROUP and the random string.
func NewSecurityGroupName(infix ...string) string {
	if len(infix) > 0 {
		return PrefixedRandomName("INTEGRATION-SEC-GROUP-" + infix[0])
	}

	return PrefixedRandomName("INTEGRATION-SEC-GROUP")
}

// NewSpaceName provides a random name prefixed with INTEGRATION-SPACE
func NewSpaceName() string {
	return PrefixedRandomName("INTEGRATION-SPACE")
}

// NewUsername provides a random name prefixed with INTEGRATION-USER
func NewUsername() string {
	return PrefixedRandomName("INTEGRATION-USER")
}

// NewBuildpackName provides a random name prefixed with INTEGRATION-BUILDPACK
func NewBuildpackName() string {
	return PrefixedRandomName("INTEGRATION-BUILDPACK")
}

// NewStackName provides a random name prefixed with INTEGRATION-STACK
func NewStackName() string {
	return PrefixedRandomName("INTEGRATION-STACK")
}

// NewDomainName provides a random domain name prefixed with integration. If prefix is provided the domain name
// will have structure "integration-prefix-randomstring.com" else it will have structure "integration-randomstring.com"
func NewDomainName(prefix ...string) string {
	if len(prefix) > 0 {
		return fmt.Sprintf("integration-%s.com", PrefixedRandomName(prefix[0]))
	}
	return fmt.Sprintf("integration%s.com", PrefixedRandomName(""))
}

// PrefixedRandomName provides a random name with structure "namePrefix-randomstring"
func PrefixedRandomName(namePrefix string) string {
	return namePrefix + "-" + RandomName()
}

// RandomName provides a random string
func RandomName() string {
	guid, err := uuid.NewV4()
	if err != nil {
		panic(err)
	}

	return guid.String()
}
<commit_msg>Avoid index out of bounds<commit_after>package helpers

import (
	"fmt"
	"sort"
	"strings"

	uuid "github.com/nu7hatch/gouuid"
)

// TODO: Is this working???

// GenerateHigherName will use the passed randomNameGenerator to generate a name with a higher
// sort value than all the passed names
func GenerateHigherName(randomNameGenerator func() string, names ...string) string {
	if len(names) == 0 {
		return randomNameGenerator()
	}
	sort.Strings(names)

	maxName := names[len(names)-1]

	for {
		name := randomNameGenerator()
		// regenerate name if name is NOT higher
		if strings.Compare(name, maxName) > 0 {
			return name
		}
	}
}

// TODO: Is this working???

// GenerateLowerName will use the passed randomNameGenerator to generate a name with a lower
// sort value than all the passed names
func GenerateLowerName(randomNameGenerator func() string, names ...string) string {
	if len(names) == 0 {
		return randomNameGenerator()
	}

	sort.Strings(names)

	minName := names[0]

	for {
		name := randomNameGenerator()
		// regenerate name if name is NOT higher
		if strings.Compare(name, minName) < 0 {
			return name
		}
	}
}

// NewAppName provides a random name prefixed with INTEGRATION-APP
func NewAppName() string {
	return PrefixedRandomName("INTEGRATION-APP")
}

// NewIsolationSegmentName provides a random name prefixed with INTEGRATION-ISOLATION-SEGMENT
func NewIsolationSegmentName(infix ...string) string {
	return PrefixedRandomName("INTEGRATION-ISOLATION-SEGMENT")
}

// NewOrgName provides a random name prefixed with INTEGRATION-ORG
func NewOrgName() string {
	return PrefixedRandomName("INTEGRATION-ORG")
}

// NewServiceBrokerName provides a random name prefixed with INTEGRATION-SERVICE-BROKER
func NewServiceBrokerName() string {
	return PrefixedRandomName("INTEGRATION-SERVICE-BROKER")
}

// NewPlanName provides a random name prefixed with INTEGRATION-PLAN
func NewPlanName() string {
	return PrefixedRandomName("INTEGRATION-PLAN")
}

// NewPassword provides a random string prefixed with INTEGRATION-PASSWORD
func NewPassword() string {
	return PrefixedRandomName("INTEGRATION-PASSWORD")
}

// NewSecurityGroupName provides a random name prefixed with INTEGRATION-SEC-GROUP. If an infix is provided, it
// is placed between INTEGRATION-SEC-GROUP and the random string.
func NewSecurityGroupName(infix ...string) string {
	if len(infix) > 0 {
		return PrefixedRandomName("INTEGRATION-SEC-GROUP-" + infix[0])
	}

	return PrefixedRandomName("INTEGRATION-SEC-GROUP")
}

// NewSpaceName provides a random name prefixed with INTEGRATION-SPACE
func NewSpaceName() string {
	return PrefixedRandomName("INTEGRATION-SPACE")
}

// NewUsername provides a random name prefixed with INTEGRATION-USER
func NewUsername() string {
	return PrefixedRandomName("INTEGRATION-USER")
}

// NewBuildpackName provides a random name prefixed with INTEGRATION-BUILDPACK
func NewBuildpackName() string {
	return PrefixedRandomName("INTEGRATION-BUILDPACK")
}

// NewStackName provides a random name prefixed with INTEGRATION-STACK
func NewStackName() string {
	return PrefixedRandomName("INTEGRATION-STACK")
}

// NewDomainName provides a random domain name prefixed with integration. If prefix is provided the domain name
// will have structure "integration-prefix-randomstring.com" else it will have structure "integration-randomstring.com"
func NewDomainName(prefix ...string) string {
	if len(prefix) > 0 {
		return fmt.Sprintf("integration-%s.com", PrefixedRandomName(prefix[0]))
	}
	return fmt.Sprintf("integration%s.com", PrefixedRandomName(""))
}

// PrefixedRandomName provides a random name with structure "namePrefix-randomstring"
func PrefixedRandomName(namePrefix string) string {
	return namePrefix + "-" + RandomName()
}

// RandomName provides a random string
func RandomName() string {
	guid, err := uuid.NewV4()
	if err != nil {
		panic(err)
	}

	return guid.String()
}
<|endoftext|>