<commit_before>package runners

import "text/template"

func Defaults() Runners {
	return Runners{
		"default": Runner{
			Description: `Print all check results in a short format and human readable`,
			ForEach:     true,
			Template: template.Must(template.New("default").Funcs(getFuncs()).Parse(`{{- range $index, $bp := .BP }}  {{ $bp.Status.Colorize $bp.Name }} {{ $bp.Status.Colorize "is" }} {{ $bp.Status.Colorize $bp.Status.String }}
  {{- range $index, $kpi := .Children }}
    {{ $kpi.Status.Colorize $kpi.Name }} {{ $kpi.Status.Colorize "is" }} {{ $kpi.Status.Colorize $kpi.Status.String }}
    {{- range $index, $svc := .Children }}
      {{ $svc.Status.Colorize $svc.Name }} {{ $svc.Status.Colorize "is" }} {{ $svc.Status.Colorize $svc.Status.String }}
    {{- end -}}
  {{ end }}
{{ end }}
`)),
		},
		"dump": Runner{
			Description: `Print all check results in a short format and human readable`,
			ForEach:     false,
			Template:    template.Must(template.New("dump").Funcs(getFuncs()).Parse(`{{ describe .}}`)),
		},
		"verbose": Runner{
			Description: `Print all check results in a long format and human readable`,
			ForEach:     true,
			Template: template.Must(template.New("verbose").Funcs(getFuncs()).Parse(`{{- range $index, $bp := .BP }}  {{ $bp.Status.Colorize $bp.Name }} {{ $bp.Status.Colorize "is" }} {{ $bp.Status.Colorize $bp.Status.String }}
            since: {{ $bp.Start.Format "2006-01-02 15:04:05" }}
      responsible: {{ $bp.Responsible }}
           values: {{ range $key, $val := $bp.Vals }}{{$key}}={{$val}} {{ end }}
  {{- range $index, $kpi := .Children }}
    {{ $kpi.Status.Colorize $kpi.Name }} {{ $kpi.Status.Colorize "is" }} {{ $kpi.Status.Colorize $kpi.Status.String }}
              since: {{ $kpi.Start.Format "2006-01-02 15:04:05" }}
        responsible: {{ $kpi.Responsible }}
    {{- range $index, $svc := .Children }}
      {{ $svc.Status.Colorize $svc.Name }} {{ $svc.Status.Colorize "is" }} {{ $svc.Status.Colorize $svc.Status.String }}
                since: {{ $svc.Start.Format "2006-01-02 15:04:05" }}
          responsible: {{ $svc.Responsible }}
               values: {{ range $key, $val := $svc.Vals }}{{$key}}={{$val}} {{ end }}
    {{- end -}}
  {{ end }}
{{ end }}
`)),
		},
		"issues": Runner{
			Description: `Print failed check results in a short format and human readable`,
			Template: template.Must(template.New("issues").Funcs(getFuncs()).Parse(`
{{- range $index, $bp := .BP -}}
  {{- if and (index $bp.Vals "in_availability") (ne $bp.Status 0) }}
  {{ $bp.Status.Colorize $bp.Name }} {{ $bp.Status.Colorize "is" }} {{ $bp.Status.Colorize $bp.Status.String }}
    {{- range $index, $kpi := .Children -}}
      {{- if ne $kpi.Status 0 }}
    {{ $kpi.Status.Colorize $kpi.Name }} {{ $kpi.Status.Colorize "is" }} {{ $kpi.Status.Colorize $kpi.Status.String }}
        {{- range $index, $svc := .Children -}}
          {{- if ne $svc.Status 0 }}
      {{ $svc.Status.Colorize $svc.Name }} {{ $svc.Status.Colorize "is" }} {{ $svc.Status.Colorize $svc.Status.String }}
          {{- end -}}
        {{ end }}
      {{- end -}}
    {{ end }}
  {{ end }}
{{- end -}}`)),
		},
		"issues_verbose": Runner{
			Description: `Print failed check results in a long format and human readable`,
			Template: template.Must(template.New("issues_verbose").Funcs(getFuncs()).Parse(`
{{- range $index, $bp := .BP -}}
  {{- if and (index $bp.Vals "in_availability") (ne $bp.Status 0) }}
  {{ $bp.Status.Colorize $bp.Name }} {{ $bp.Status.Colorize "is" }} {{ $bp.Status.Colorize $bp.Status.String }}
            since: {{ $bp.Start.Format "2006-01-02 15:04:05" }}
      responsible: {{ $bp.Responsible }}
           values: {{ range $key, $val := $bp.Vals }}{{$key}}={{$val}} {{ end }}
    {{- range $index, $kpi := .Children -}}
      {{- if ne $kpi.Status 0 }}
    {{ $kpi.Status.Colorize $kpi.Name }} {{ $kpi.Status.Colorize "is" }} {{ $kpi.Status.Colorize $kpi.Status.String }}
              since: {{ $kpi.Start.Format "2006-01-02 15:04:05" }}
        responsible: {{ $kpi.Responsible }}
        {{- range $index, $svc := .Children -}}
          {{- if ne $svc.Status 0 }}
      {{ $svc.Status.Colorize $svc.Name }} {{ $svc.Status.Colorize "is" }} {{ $svc.Status.Colorize $svc.Status.String }}
                since: {{ $svc.Start.Format "2006-01-02 15:04:05" }}
          responsible: {{ $svc.Responsible }}
               values: {{ range $key, $val := $svc.Vals }}{{$key}}={{$val}} {{ end }}
              message: {{ $svc.Output }}
                error: {{ $svc.Err }}
          {{- end -}}
        {{ end }}
      {{- end -}}
    {{ end }}
  {{ end }}
{{- end -}}`)),
		},
	}
}
<commit_msg>cleanup<commit_after>package runners

import "text/template"

func Defaults() Runners {
	return Runners{
		"default": Runner{
			Description: `Print all check results in a short format and human readable`,
			ForEach:     true,
			Template: template.Must(template.New("default").Funcs(getFuncs()).Parse(`{{- range $index, $bp := .BP }}  {{ $bp.Status.Colorize $bp.Name }} {{ $bp.Status.Colorize "is" }} {{ $bp.Status.Colorize $bp.Status.String }}
  {{- range $index, $kpi := .Children }}
    {{ $kpi.Status.Colorize $kpi.Name }} {{ $kpi.Status.Colorize "is" }} {{ $kpi.Status.Colorize $kpi.Status.String }}
    {{- range $index, $svc := .Children }}
      {{ $svc.Status.Colorize $svc.Name }} {{ $svc.Status.Colorize "is" }} {{ $svc.Status.Colorize $svc.Status.String }}
    {{- end -}}
  {{ end }}
{{ end }}
`)),
		},
		"verbose": Runner{
			Description: `Print all check results in a long format and human readable`,
			ForEach:     true,
			Template: template.Must(template.New("verbose").Funcs(getFuncs()).Parse(`{{- range $index, $bp := .BP }}  {{ $bp.Status.Colorize $bp.Name }} {{ $bp.Status.Colorize "is" }} {{ $bp.Status.Colorize $bp.Status.String }}
            since: {{ $bp.Start.Format "2006-01-02 15:04:05" }}
      responsible: {{ $bp.Responsible }}
           values: {{ range $key, $val := $bp.Vals }}{{$key}}={{$val}} {{ end }}
  {{- range $index, $kpi := .Children }}
    {{ $kpi.Status.Colorize $kpi.Name }} {{ $kpi.Status.Colorize "is" }} {{ $kpi.Status.Colorize $kpi.Status.String }}
              since: {{ $kpi.Start.Format "2006-01-02 15:04:05" }}
        responsible: {{ $kpi.Responsible }}
    {{- range $index, $svc := .Children }}
      {{ $svc.Status.Colorize $svc.Name }} {{ $svc.Status.Colorize "is" }} {{ $svc.Status.Colorize $svc.Status.String }}
                since: {{ $svc.Start.Format "2006-01-02 15:04:05" }}
          responsible: {{ $svc.Responsible }}
               values: {{ range $key, $val := $svc.Vals }}{{$key}}={{$val}} {{ end }}
    {{- end -}}
  {{ end }}
{{ end }}
`)),
		},
		"issues": Runner{
			Description: `Print failed check results in a short format and human readable`,
			Template: template.Must(template.New("issues").Funcs(getFuncs()).Parse(`
{{- range $index, $bp := .BP -}}
  {{- if and (index $bp.Vals "in_availability") (ne $bp.Status 0) }}
  {{ $bp.Status.Colorize $bp.Name }} {{ $bp.Status.Colorize "is" }} {{ $bp.Status.Colorize $bp.Status.String }}
    {{- range $index, $kpi := .Children -}}
      {{- if ne $kpi.Status 0 }}
    {{ $kpi.Status.Colorize $kpi.Name }} {{ $kpi.Status.Colorize "is" }} {{ $kpi.Status.Colorize $kpi.Status.String }}
        {{- range $index, $svc := .Children -}}
          {{- if ne $svc.Status 0 }}
      {{ $svc.Status.Colorize $svc.Name }} {{ $svc.Status.Colorize "is" }} {{ $svc.Status.Colorize $svc.Status.String }}
          {{- end -}}
        {{ end }}
      {{- end -}}
    {{ end }}
  {{ end }}
{{- end -}}`)),
		},
		"issues_verbose": Runner{
			Description: `Print failed check results in a long format and human readable`,
			Template: template.Must(template.New("issues_verbose").Funcs(getFuncs()).Parse(`
{{- range $index, $bp := .BP -}}
  {{- if and (index $bp.Vals "in_availability") (ne $bp.Status 0) }}
  {{ $bp.Status.Colorize $bp.Name }} {{ $bp.Status.Colorize "is" }} {{ $bp.Status.Colorize $bp.Status.String }}
            since: {{ $bp.Start.Format "2006-01-02 15:04:05" }}
      responsible: {{ $bp.Responsible }}
           values: {{ range $key, $val := $bp.Vals }}{{$key}}={{$val}} {{ end }}
    {{- range $index, $kpi := .Children -}}
      {{- if ne $kpi.Status 0 }}
    {{ $kpi.Status.Colorize $kpi.Name }} {{ $kpi.Status.Colorize "is" }} {{ $kpi.Status.Colorize $kpi.Status.String }}
              since: {{ $kpi.Start.Format "2006-01-02 15:04:05" }}
        responsible: {{ $kpi.Responsible }}
        {{- range $index, $svc := .Children -}}
          {{- if ne $svc.Status 0 }}
      {{ $svc.Status.Colorize $svc.Name }} {{ $svc.Status.Colorize "is" }} {{ $svc.Status.Colorize $svc.Status.String }}
                since: {{ $svc.Start.Format "2006-01-02 15:04:05" }}
          responsible: {{ $svc.Responsible }}
               values: {{ range $key, $val := $svc.Vals }}{{$key}}={{$val}} {{ end }}
              message: {{ $svc.Output }}
                error: {{ $svc.Err }}
          {{- end -}}
        {{ end }}
      {{- end -}}
    {{ end }}
  {{ end }}
{{- end -}}`)),
		},
	}
}
<|endoftext|>


<commit_before>package ast

import (
	"bytes"
	"monkey/token"
)

type BlockStatement struct {
	Token      token.Token
	Statements []Statement
}

func (bs *BlockStatement) statementNode() {}

func (bs *BlockStatement) TokenLiteral() string {
	return bs.Token.Literal
}

func (bs *BlockStatement) String() string {
	var out bytes.Buffer

	for _, s := range bs.Statements {
		out.WriteString(s.String())
	}

	return out.String()
}
<commit_msg>added block statement doc comments<commit_after>package ast

import (
	"bytes"
	"monkey/token"
)

// BlockStatement : Statement node representing a block of statements
type BlockStatement struct {
	Token      token.Token
	Statements []Statement
}

func (bs *BlockStatement) statementNode() {}

// TokenLiteral : Returns the literal representation of the token
func (bs *BlockStatement) TokenLiteral() string {
	return bs.Token.Literal
}

func (bs *BlockStatement) String() string {
	var out bytes.Buffer

	for _, s := range bs.Statements {
		out.WriteString(s.String())
	}

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


<commit_before>
package main

import (
	"fmt"
	"strings"
	"strconv"
	"errors"
	"encoding/json"
	"github.com/hyperledger/fabric/core/chaincode/shim"
)

// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}

type Employee struct {
	Name string `json:"name"`
	EmployeeId int `json:"employeeId"`
	Project string `json:"project"`
}

type Customer struct {
	Name string `json:"name"`
	CustomerId int `json:"customerId"`
}
type Project struct {
	Name string `json:"name"`
	ProjectId int `json:"projectId"`
	CustomerOf string `json:"customerOf"`
	StartTime string `json:"startDate"`
	EndTime string `json:"endDate"`
}

type EmployeeEfforts struct{
	Efforts string
	Project string
}

type ProjectEfforts struct{
	Efforts string
	Employee string
}

type EmployeeInvoice struct{
	List []EmployeeEfforts
}
type ProjectInvoice struct{
	List []ProjectEfforts
}

func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte,error) {
    fmt.Println("Init is running " + function)
    return nil,nil
}

func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte,error) {

    fmt.Println("Invoke is running " + function)

    if function =="initEmployee"{
		return t.initEmployee(stub,args)
	}else if function =="getEmployee"{
		return t.getEmployee(stub,args)
	}else if function =="initCustomer"{
		return t.initCustomer(stub,args)
	}else if function =="getCustomer"{
		return t.getCustomer(stub,args)
	}else if function =="initProject"{
		return t.initProject(stub,args)
	}else if function =="getProject"{
		return t.getProject(stub,args)
	}else if function =="changeProject"{
		return t.changeProject(stub,args)
	}else if function =="submitEfforts"{
		return t.submitEfforts(stub,args)
	}
	return nil,errors.New("Received unknown function invocation")
}

func (t *SimpleChaincode) submitEfforts(stub shim.ChaincodeStubInterface, args []string)([]byte,error){
	if len(args)!=4{
		return nil,errors.New("Incorrect Number of arguments. Expecting 4")
	}
	fmt.Println("- start submitEfforts -")
	employeeId := args[0]
	timestamp := args[1]
	employeeIdAndTimestamp := args[2]
	efforts := args[3]
	employeeJSONasBytes,err := stub.GetState(employeeId)

	if err != nil {
		return nil,err
	}
	var e Employee
	json.Unmarshal(employeeJSONasBytes,&e)

	var ei EmployeeInvoice
	var pi ProjectInvoice

	employeeInvoiceAsBytes,err := stub.GetState(employeeIdAndTimestamp)

	if err != nil{
		return nil,err
	}
	json.Unmarshal(employeeInvoiceAsBytes,&ei)

	var ef EmployeeEfforts
	ef = EmployeeEfforts{efforts,e.Project}
	ei.List = append(ei.List,ef)

	employeeInvoiceAsBytes,err = json.Marshal(ei)
	if err!= nil{
		return nil,err
	}
	err = stub.PutState(employeeIdAndTimestamp,employeeInvoiceAsBytes)
	if err !=nil{
		return nil,err
	}

	var projectIdAndTimestamp string
	projectIdAndTimestamp = e.Project+""+timestamp

	projectInvoiceAsBytes,err := stub.GetState(projectIdAndTimestamp)

	json.Unmarshal(projectInvoiceAsBytes,&pi)

	var pf ProjectEfforts
	pf = ProjectEfforts{efforts,e.Project}
	pi.List = append(pi.List,pf)

	projectInvoiceAsBytes,err = json.Marshal(pi)

	if err!= nil{
		return nil,err
	}

	err = stub.PutState(projectIdAndTimestamp,projectInvoiceAsBytes)

	if err!= nil{
		return nil,err
	}

	return nil,nil
}
func (t *SimpleChaincode) changeProject(stub shim.ChaincodeStubInterface, args []string)([]byte,error){
	if len(args)!=2 {
		return nil,errors.New("Incorrect Number of arguments. Expecting 2")
	}
	// ==== Input sanitation ====
	fmt.Println("- start changeProject")
	if len(args[0]) <= 0 {
		return nil,errors.New("1st argument must be a non-empty string")
	}
	if len(args[1]) <= 0 {
		return nil,errors.New("2nd argument must be a non-empty string")
	}
	employeeId := args[0]
	project := strings.ToLower(args[1])
	_, err := strconv.Atoi(project)

	if err != nil {
		return nil,errors.New("2nd argument must be a numeric string")
	}
	_, err = strconv.Atoi(employeeId)

	if err != nil {
		return nil,errors.New("1st argument must be a numeric string")
	}

	_, err = stub.GetState(project)
	if err != nil {
		return nil,err
	}

	employee, err := stub.GetState(employeeId)
	if err != nil {
		return nil,err
	}

	var e Employee

	json.Unmarshal(employee,&e)
	e.Project = project
	fmt.Println(e)

	employeeJSONasBytes, err := json.Marshal(e)
	fmt.Println(employeeJSONasBytes)
	if err != nil {
		return nil,err
	}
	err = stub.PutState(employeeId, employeeJSONasBytes)

	if err != nil {
		return nil,err
	}
	return nil,nil
}
func (t *SimpleChaincode) initProject(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){

	if len(args) != 5 {
		return nil,errors.New("Incorrect number of arguments. Expecting 5")
	}
	// ==== Input sanitation ====
	fmt.Println("- start initProject")
	if len(args[0]) <= 0 {
		return nil,errors.New("1st argument must be a non-empty string")
	}
	if len(args[1]) <= 0 {
		return nil,errors.New("2nd argument must be a non-empty string")
	}
	if len(args[2]) <= 0 {
		return nil,errors.New("3rd argument must be a non-empty string")
	}
	if len(args[3]) <= 0 {
		return nil,errors.New("4th argument must be a non-empty string")
	}
	if len(args[4]) <= 0 {
		return nil,errors.New("5th argument must be a non-empty string")
	}

	projectName := args[0]
	customerOf := strings.ToLower(args[2])
	projectId, err := strconv.Atoi(args[1])
	projectIdAsString := args[1]
	startDate:= args[3]
	endDate:= args[4]
	if err != nil {
		return nil,errors.New("2nd argument must be a numeric string")
	}

	projectAsBytes, err := stub.GetState(projectIdAsString)
	if err != nil {
		return nil,err
	} else if projectAsBytes != nil {
		fmt.Println("This project already exists: " + projectIdAsString)
		return nil, errors.New("This project already exists "+projectIdAsString)
	}

	project:= Project{projectName,projectId,customerOf,startDate,endDate}

	projectJSONasBytes, err := json.Marshal(project)

	if err != nil {
		return nil,err
	}
	err = stub.PutState(projectIdAsString, projectJSONasBytes)

	if err != nil {
		return nil,err
	}

	// composite key creation for searching projects according to customer
	/*
	indexName := "customerOf"
	customerOfIndexKey, err := stub.CreateCompositeKey(indexName, []string{customerOf, projectIdAsString})
	if err != nil {
		return nil,err
	}

	value := []byte{0x00}
	stub.PutState(customerOfIndexKey, value)
    */
	fmt.Println("- end initProject")
	return nil,nil
}

func (t *SimpleChaincode) getProject(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){
	if len(args) !=1{
		return nil,errors.New("Incorrect number of arguments. Expecting 1")
	}
	projectId := args[0]
	project, err := stub.GetState(projectId)
	if err != nil {
		return nil,err
	}

	return project,nil
}

func (t *SimpleChaincode) getCustomer(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){
	if len(args) !=1{
		return nil,errors.New("Incorrect number of arguments. Expecting 1")
	}
	customerId := args[0]
	customer, err := stub.GetState(customerId)
	if err != nil {
		return nil,err
	}

	return customer,nil
}

func (t *SimpleChaincode) initCustomer(stub shim.ChaincodeStubInterface,args []string) ([]byte,error) {
		if len(args) != 2{
			return nil,errors.New("Incorrect number of arguments. Expecting 2")
		}
			// ==== Input sanitation ====
		fmt.Println("- start initCustomer")
		if len(args[0]) <= 0 {
			return nil,errors.New("1st argument must be a non-empty string")
		}
		if len(args[1]) <= 0 {
			return nil,errors.New("2nd argument must be a non-empty string")
		}

		customerName := args[0]
		customerId, err := strconv.Atoi(args[1])
		customerIdAsString := args[1]
		if err != nil {
			return nil,errors.New("2nd argument must be a numeric string")
		}
		customerAsBytes, err := stub.GetState(customerIdAsString)
		if err != nil {
			 return nil,err
		} else if customerAsBytes != nil {
			fmt.Println("This customer already exists: " + customerIdAsString)
			return nil,errors.New("This customer already exists: "+customerIdAsString)
		}

		customer:= Customer{customerName,customerId}

		customerJSONasBytes, err := json.Marshal(customer)

		if err != nil {
			return nil,err
		}
		err = stub.PutState(customerIdAsString, customerJSONasBytes)

		if err != nil {
			return nil,err
		}

		fmt.Println("- end initCustomer")
		return nil,nil
}

func (t *SimpleChaincode) initEmployee(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){

	if len(args) != 3 {
		return nil,errors.New("Incorrect number of arguments. Expecting 3")
	}
	// ==== Input sanitation ====
	fmt.Println("- start initEmployee")
	if len(args[0]) <= 0 {
		return nil,errors.New("1st argument must be a non-empty string")
	}
	if len(args[1]) <= 0 {
		return nil,errors.New("2nd argument must be a non-empty string")
	}
	if len(args[2]) <= 0 {
		return nil,errors.New("3rd argument must be a non-empty string")
	}

	employeeName := args[0]
	project := strings.ToLower(args[2])
	employeeId, err := strconv.Atoi(args[1])
	employeeIdAsString := args[1]
	if err != nil {
		return nil,errors.New("2nd argument must be a numeric string")
	}

	employeeAsBytes, err := stub.GetState(employeeIdAsString)
	if err != nil {
		return nil,err
	} else if employeeAsBytes != nil {
		fmt.Println("This employee already exists: " + employeeIdAsString)
		return []byte("duplicate"),errors.New("This employee already exists: "+employeeIdAsString)
	}

	employee:= Employee{employeeName,employeeId,project}
	fmt.Println(employee)
	employeeJSONasBytes, err := json.Marshal(employee)
	fmt.Println(employeeJSONasBytes)
	if err != nil {
		return nil,err
	}
	err = stub.PutState(employeeIdAsString, employeeJSONasBytes)

	if err != nil {
		return nil,err
	}
	employee_temp,_ := stub.GetState(employeeIdAsString)
	fmt.Println(employee_temp)
	// composite key to get employees by project

	/*
	indexName := "project"
	projectIndexKey, err := stub.CreateCompositeKey(indexName, []string{project, employeeIdAsString})
	if err != nil {
		return nil,err
	}

	value := []byte{0x00}
	stub.PutState(projectIndexKey, value)
	*/
	fmt.Println("- end initEmployee")
	return nil,nil
}

func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte,error) {
    fmt.Println("Query is running " + function)

    if function =="getEmployee"{
		return t.getEmployee(stub,args)
	}else if function =="getCustomer"{
		return t.getCustomer(stub,args)
	}else if function =="getProject"{
		return t.getProject(stub,args)
	}else if function == "getEfforts"{
		return t.getEfforts(stub,args)
	}

	return nil,errors.New("Received unknown function query")
}
func (t *SimpleChaincode) getEfforts(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){
	fmt.Printf("getEfforts called")
	if len(args) !=1{
		return nil,errors.New("Incorrect number of arguments. Expecting 1")
	}
	idAndTimestamp := args[0]
	invoice, err := stub.GetState(idAndTimestamp)
	fmt.Println(invoice)
	if err != nil {
		return nil,err
	}

	return invoice,nil
}
func (t *SimpleChaincode) getEmployee(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){
	fmt.Printf("getEmployee called")
	if len(args) !=1{
		return nil,errors.New("Incorrect number of arguments. Expecting 1")
	}
	employeeId := args[0]
	employee, err := stub.GetState(employeeId)
	fmt.Println(employee)
	if err != nil {
		return nil,err
	}

	return employee,nil
}

func main() {
    	err := shim.Start(new(SimpleChaincode))
    if err != nil {
        fmt.Printf("Error starting Simple chaincode: %s", err)
    }
}
<commit_msg>project employee correction<commit_after>
package main

import (
	"fmt"
	"strings"
	"strconv"
	"errors"
	"encoding/json"
	"github.com/hyperledger/fabric/core/chaincode/shim"
)

// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}

type Employee struct {
	Name string `json:"name"`
	EmployeeId int `json:"employeeId"`
	Project string `json:"project"`
}

type Customer struct {
	Name string `json:"name"`
	CustomerId int `json:"customerId"`
}
type Project struct {
	Name string `json:"name"`
	ProjectId int `json:"projectId"`
	CustomerOf string `json:"customerOf"`
	StartTime string `json:"startDate"`
	EndTime string `json:"endDate"`
}

type EmployeeEfforts struct{
	Efforts string
	Project string
}

type ProjectEfforts struct{
	Efforts string
	Employee string
}

type EmployeeInvoice struct{
	List []EmployeeEfforts
}
type ProjectInvoice struct{
	List []ProjectEfforts
}

func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte,error) {
    fmt.Println("Init is running " + function)
    return nil,nil
}

func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte,error) {

    fmt.Println("Invoke is running " + function)

    if function =="initEmployee"{
		return t.initEmployee(stub,args)
	}else if function =="getEmployee"{
		return t.getEmployee(stub,args)
	}else if function =="initCustomer"{
		return t.initCustomer(stub,args)
	}else if function =="getCustomer"{
		return t.getCustomer(stub,args)
	}else if function =="initProject"{
		return t.initProject(stub,args)
	}else if function =="getProject"{
		return t.getProject(stub,args)
	}else if function =="changeProject"{
		return t.changeProject(stub,args)
	}else if function =="submitEfforts"{
		return t.submitEfforts(stub,args)
	}
	return nil,errors.New("Received unknown function invocation")
}

func (t *SimpleChaincode) submitEfforts(stub shim.ChaincodeStubInterface, args []string)([]byte,error){
	if len(args)!=4{
		return nil,errors.New("Incorrect Number of arguments. Expecting 4")
	}
	fmt.Println("- start submitEfforts -")
	employeeId := args[0]
	timestamp := args[1]
	employeeIdAndTimestamp := args[2]
	efforts := args[3]
	employeeJSONasBytes,err := stub.GetState(employeeId)

	if err != nil {
		return nil,err
	}
	var e Employee
	json.Unmarshal(employeeJSONasBytes,&e)

	var ei EmployeeInvoice
	var pi ProjectInvoice

	employeeInvoiceAsBytes,err := stub.GetState(employeeIdAndTimestamp)

	if err != nil{
		return nil,err
	}
	json.Unmarshal(employeeInvoiceAsBytes,&ei)

	var ef EmployeeEfforts
	ef = EmployeeEfforts{efforts,e.Project}
	ei.List = append(ei.List,ef)

	employeeInvoiceAsBytes,err = json.Marshal(ei)
	if err!= nil{
		return nil,err
	}
	err = stub.PutState(employeeIdAndTimestamp,employeeInvoiceAsBytes)
	if err !=nil{
		return nil,err
	}

	var projectIdAndTimestamp string
	projectIdAndTimestamp = e.Project+""+timestamp

	projectInvoiceAsBytes,err := stub.GetState(projectIdAndTimestamp)

	json.Unmarshal(projectInvoiceAsBytes,&pi)

	var pf ProjectEfforts
	pf = ProjectEfforts{efforts,e.EmployeeId}
	pi.List = append(pi.List,pf)

	projectInvoiceAsBytes,err = json.Marshal(pi)

	if err!= nil{
		return nil,err
	}

	err = stub.PutState(projectIdAndTimestamp,projectInvoiceAsBytes)

	if err!= nil{
		return nil,err
	}

	return nil,nil
}
func (t *SimpleChaincode) changeProject(stub shim.ChaincodeStubInterface, args []string)([]byte,error){
	if len(args)!=2 {
		return nil,errors.New("Incorrect Number of arguments. Expecting 2")
	}
	// ==== Input sanitation ====
	fmt.Println("- start changeProject")
	if len(args[0]) <= 0 {
		return nil,errors.New("1st argument must be a non-empty string")
	}
	if len(args[1]) <= 0 {
		return nil,errors.New("2nd argument must be a non-empty string")
	}
	employeeId := args[0]
	project := strings.ToLower(args[1])
	_, err := strconv.Atoi(project)

	if err != nil {
		return nil,errors.New("2nd argument must be a numeric string")
	}
	_, err = strconv.Atoi(employeeId)

	if err != nil {
		return nil,errors.New("1st argument must be a numeric string")
	}

	_, err = stub.GetState(project)
	if err != nil {
		return nil,err
	}

	employee, err := stub.GetState(employeeId)
	if err != nil {
		return nil,err
	}

	var e Employee

	json.Unmarshal(employee,&e)
	e.Project = project
	fmt.Println(e)

	employeeJSONasBytes, err := json.Marshal(e)
	fmt.Println(employeeJSONasBytes)
	if err != nil {
		return nil,err
	}
	err = stub.PutState(employeeId, employeeJSONasBytes)

	if err != nil {
		return nil,err
	}
	return nil,nil
}
func (t *SimpleChaincode) initProject(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){

	if len(args) != 5 {
		return nil,errors.New("Incorrect number of arguments. Expecting 5")
	}
	// ==== Input sanitation ====
	fmt.Println("- start initProject")
	if len(args[0]) <= 0 {
		return nil,errors.New("1st argument must be a non-empty string")
	}
	if len(args[1]) <= 0 {
		return nil,errors.New("2nd argument must be a non-empty string")
	}
	if len(args[2]) <= 0 {
		return nil,errors.New("3rd argument must be a non-empty string")
	}
	if len(args[3]) <= 0 {
		return nil,errors.New("4th argument must be a non-empty string")
	}
	if len(args[4]) <= 0 {
		return nil,errors.New("5th argument must be a non-empty string")
	}

	projectName := args[0]
	customerOf := strings.ToLower(args[2])
	projectId, err := strconv.Atoi(args[1])
	projectIdAsString := args[1]
	startDate:= args[3]
	endDate:= args[4]
	if err != nil {
		return nil,errors.New("2nd argument must be a numeric string")
	}

	projectAsBytes, err := stub.GetState(projectIdAsString)
	if err != nil {
		return nil,err
	} else if projectAsBytes != nil {
		fmt.Println("This project already exists: " + projectIdAsString)
		return nil, errors.New("This project already exists "+projectIdAsString)
	}

	project:= Project{projectName,projectId,customerOf,startDate,endDate}

	projectJSONasBytes, err := json.Marshal(project)

	if err != nil {
		return nil,err
	}
	err = stub.PutState(projectIdAsString, projectJSONasBytes)

	if err != nil {
		return nil,err
	}

	// composite key creation for searching projects according to customer
	/*
	indexName := "customerOf"
	customerOfIndexKey, err := stub.CreateCompositeKey(indexName, []string{customerOf, projectIdAsString})
	if err != nil {
		return nil,err
	}

	value := []byte{0x00}
	stub.PutState(customerOfIndexKey, value)
    */
	fmt.Println("- end initProject")
	return nil,nil
}

func (t *SimpleChaincode) getProject(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){
	if len(args) !=1{
		return nil,errors.New("Incorrect number of arguments. Expecting 1")
	}
	projectId := args[0]
	project, err := stub.GetState(projectId)
	if err != nil {
		return nil,err
	}

	return project,nil
}

func (t *SimpleChaincode) getCustomer(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){
	if len(args) !=1{
		return nil,errors.New("Incorrect number of arguments. Expecting 1")
	}
	customerId := args[0]
	customer, err := stub.GetState(customerId)
	if err != nil {
		return nil,err
	}

	return customer,nil
}

func (t *SimpleChaincode) initCustomer(stub shim.ChaincodeStubInterface,args []string) ([]byte,error) {
		if len(args) != 2{
			return nil,errors.New("Incorrect number of arguments. Expecting 2")
		}
			// ==== Input sanitation ====
		fmt.Println("- start initCustomer")
		if len(args[0]) <= 0 {
			return nil,errors.New("1st argument must be a non-empty string")
		}
		if len(args[1]) <= 0 {
			return nil,errors.New("2nd argument must be a non-empty string")
		}

		customerName := args[0]
		customerId, err := strconv.Atoi(args[1])
		customerIdAsString := args[1]
		if err != nil {
			return nil,errors.New("2nd argument must be a numeric string")
		}
		customerAsBytes, err := stub.GetState(customerIdAsString)
		if err != nil {
			 return nil,err
		} else if customerAsBytes != nil {
			fmt.Println("This customer already exists: " + customerIdAsString)
			return nil,errors.New("This customer already exists: "+customerIdAsString)
		}

		customer:= Customer{customerName,customerId}

		customerJSONasBytes, err := json.Marshal(customer)

		if err != nil {
			return nil,err
		}
		err = stub.PutState(customerIdAsString, customerJSONasBytes)

		if err != nil {
			return nil,err
		}

		fmt.Println("- end initCustomer")
		return nil,nil
}

func (t *SimpleChaincode) initEmployee(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){

	if len(args) != 3 {
		return nil,errors.New("Incorrect number of arguments. Expecting 3")
	}
	// ==== Input sanitation ====
	fmt.Println("- start initEmployee")
	if len(args[0]) <= 0 {
		return nil,errors.New("1st argument must be a non-empty string")
	}
	if len(args[1]) <= 0 {
		return nil,errors.New("2nd argument must be a non-empty string")
	}
	if len(args[2]) <= 0 {
		return nil,errors.New("3rd argument must be a non-empty string")
	}

	employeeName := args[0]
	project := strings.ToLower(args[2])
	employeeId, err := strconv.Atoi(args[1])
	employeeIdAsString := args[1]
	if err != nil {
		return nil,errors.New("2nd argument must be a numeric string")
	}

	employeeAsBytes, err := stub.GetState(employeeIdAsString)
	if err != nil {
		return nil,err
	} else if employeeAsBytes != nil {
		fmt.Println("This employee already exists: " + employeeIdAsString)
		return []byte("duplicate"),errors.New("This employee already exists: "+employeeIdAsString)
	}

	employee:= Employee{employeeName,employeeId,project}
	fmt.Println(employee)
	employeeJSONasBytes, err := json.Marshal(employee)
	fmt.Println(employeeJSONasBytes)
	if err != nil {
		return nil,err
	}
	err = stub.PutState(employeeIdAsString, employeeJSONasBytes)

	if err != nil {
		return nil,err
	}
	employee_temp,_ := stub.GetState(employeeIdAsString)
	fmt.Println(employee_temp)
	// composite key to get employees by project

	/*
	indexName := "project"
	projectIndexKey, err := stub.CreateCompositeKey(indexName, []string{project, employeeIdAsString})
	if err != nil {
		return nil,err
	}

	value := []byte{0x00}
	stub.PutState(projectIndexKey, value)
	*/
	fmt.Println("- end initEmployee")
	return nil,nil
}

func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte,error) {
    fmt.Println("Query is running " + function)

    if function =="getEmployee"{
		return t.getEmployee(stub,args)
	}else if function =="getCustomer"{
		return t.getCustomer(stub,args)
	}else if function =="getProject"{
		return t.getProject(stub,args)
	}else if function == "getEfforts"{
		return t.getEfforts(stub,args)
	}

	return nil,errors.New("Received unknown function query")
}
func (t *SimpleChaincode) getEfforts(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){
	fmt.Printf("getEfforts called")
	if len(args) !=1{
		return nil,errors.New("Incorrect number of arguments. Expecting 1")
	}
	idAndTimestamp := args[0]
	invoice, err := stub.GetState(idAndTimestamp)
	fmt.Println(invoice)
	if err != nil {
		return nil,err
	}

	return invoice,nil
}
func (t *SimpleChaincode) getEmployee(stub shim.ChaincodeStubInterface, args []string) ([]byte,error){
	fmt.Printf("getEmployee called")
	if len(args) !=1{
		return nil,errors.New("Incorrect number of arguments. Expecting 1")
	}
	employeeId := args[0]
	employee, err := stub.GetState(employeeId)
	fmt.Println(employee)
	if err != nil {
		return nil,err
	}

	return employee,nil
}

func main() {
    	err := shim.Start(new(SimpleChaincode))
    if err != nil {
        fmt.Printf("Error starting Simple chaincode: %s", err)
    }
}
<|endoftext|>


<commit_before>// Copyright (c) Ilia Kravets, 2015. All rights reserved. PROVIDED "AS IS"
// WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. See LICENSE file for details.

package cmd

import (
	"github.com/ikravets/errs"
	"github.com/jessevdk/go-flags"

	"my/ev/packet"
)

type cmdReplay struct {
	InputFileName   string `long:"input" short:"i" required:"y" value-name:"PCAP_FILE" description:"input pcap file to read"`
	OutputInterface string `long:"iface" required:"y" value-name:"IFACE" description:"output interface name"`
	Pps             int    `long:"pps"   short:"p" value-name:"NUM" description:"packets per second"`
	Limit           int    `long:"limit" short:"L" value-name:"NUM" description:"stop after NUM packets"`
	Loop            int    `long:"loop"  short:"l" value-name:"NUM" description:"loop NUM times"`
	shouldExecute   bool
}

func (c *cmdReplay) Execute(args []string) error {
	c.shouldExecute = true
	return nil
}

func (c *cmdReplay) ConfigParser(parser *flags.Parser) {
	parser.AddCommand("replay", "replay pcap dump", "", c)
}

func (c *cmdReplay) ParsingFinished() (err error) {
	if !c.shouldExecute {
		return
	}
	r := packet.Replay{
		IfaceName: c.OutputInterface,
		DumpName:  c.InputFileName,
		Limit:     c.Limit,
		Pps:       c.Pps,
	}
	loop := c.Loop
	if loop == 0 {
		loop = 1
	}
	for i := 0; i < loop; i++ {
		errs.CheckE(r.Run())
	}
	return
}

func init() {
	var c cmdReplay
	Registry.Register(&c)
}
<commit_msg>cmd:replay: use new packet.Replay<commit_after>// Copyright (c) Ilia Kravets, 2015. All rights reserved. PROVIDED "AS IS"
// WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. See LICENSE file for details.

package cmd

import (
	"github.com/ikravets/errs"
	"github.com/jessevdk/go-flags"

	"my/ev/packet"
)

type cmdReplay struct {
	InputFileName   string `long:"input" short:"i" required:"y" value-name:"PCAP_FILE" description:"input pcap file to read"`
	OutputInterface string `long:"iface" required:"y" value-name:"IFACE" description:"output interface name"`
	Pps             int    `long:"pps"   short:"p" value-name:"NUM" description:"packets per second"`
	Limit           int    `long:"limit" short:"L" value-name:"NUM" description:"stop after NUM packets"`
	Loop            int    `long:"loop"  short:"l" value-name:"NUM" description:"loop NUM times"`
	shouldExecute   bool
}

func (c *cmdReplay) Execute(args []string) error {
	c.shouldExecute = true
	return nil
}

func (c *cmdReplay) ConfigParser(parser *flags.Parser) {
	parser.AddCommand("replay", "replay pcap dump", "", c)
}

func (c *cmdReplay) ParsingFinished() (err error) {
	if !c.shouldExecute {
		return
	}
	conf := packet.ReplayConfig{
		IfaceName: c.OutputInterface,
		DumpName:  c.InputFileName,
		Limit:     c.Limit,
		Pps:       c.Pps,
		Loop:      c.Loop,
	}
	r := packet.NewReplay(&conf)
	errs.CheckE(r.Run())
	return
}

func init() {
	var c cmdReplay
	Registry.Register(&c)
}
<|endoftext|>


<commit_before>package service

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/kyleterry/tenyks/irc"
	. "github.com/kyleterry/tenyks/version"
)

type servicefn func(*Connection, *Message)

func (self *ServiceEngine) AddHandler(name string, fn servicefn) {
	handler := irc.NewHandler(func(p ...interface{}) {
		fn(p[0].(*Connection), p[1].(*Message))
	})
	self.CommandRg.AddHandler(name, handler)
}

func (self *ServiceEngine) addBaseHandlers() {
	self.AddHandler("PRIVMSG", (*Connection).PrivmsgServiceHandler)
	self.AddHandler("REGISTER", (*Connection).RegisterServiceHandler)
	self.AddHandler("BYE", (*Connection).ByeServiceHandler)
	self.AddHandler("PONG", (*Connection).PongServiceHandler)
}

func (self *Connection) PrivmsgIrcHandler(conn *irc.Connection, msg *irc.Message) {
	serviceMsg := Message{}
	serviceMsg.Target = msg.Params[0]
	serviceMsg.Command = msg.Command
	serviceMsg.Mask = msg.Host
	serviceMsg.Direct = irc.IsDirect(msg.Trail, conn.GetCurrentNick())
	serviceMsg.Nick = msg.Nick
	serviceMsg.Host = msg.Host
	serviceMsg.Full_message = msg.RawMsg
	serviceMsg.User = msg.Ident
	serviceMsg.From_channel = irc.IsChannel(msg.Params[0])
	serviceMsg.Connection = conn.Name
	serviceMsg.Meta = &Meta{"Tenyks", TenyksVersion, nil}
	if serviceMsg.Direct {
		serviceMsg.Payload = irc.StripNickOnDirect(msg.Trail, conn.GetCurrentNick())
	} else {
		serviceMsg.Payload = msg.Trail
	}

	jsonBytes, err := json.Marshal(serviceMsg)
	if err != nil {
		log.Fatal(err)
	}
	self.Out <- string(jsonBytes[:])
}

func (self *Connection) PrivmsgServiceHandler(msg *Message) {
	conn := self.getIrcConnByName(msg.Connection)
	if conn != nil {
		msgStr := fmt.Sprintf("%s %s :%s", msg.Command, msg.Target, msg.Payload)
		conn.Out <- msgStr
	} else {
		log.Debug("[service] No such connection `%s`. Ignoring.",
			msg.Connection)
	}
}

func (self *Connection) RegisterServiceHandler(msg *Message) {
	meta := msg.Meta
	if meta.UUID == nil {
		log.Error("[service] ERROR: UUID required to register with Tenyks")
		return
	}
	log.Debug("[service] %s (%s) wants to register", meta.UUID.String(), meta.Name)
	srv := &Service{}
	srv.Name = meta.Name
	srv.Version = meta.Version
	srv.Online = true
	srv.LastPing = time.Now()
	srv.UUID = meta.SID.UUID
	self.engine.ServiceRg.RegisterService(srv)
}

func (self *Connection) ByeServiceHandler(msg *Message) {
	meta := msg.Meta
	log.Debug("[service] %s is hanging up", meta.Name)
	srv := self.engine.ServiceRg.GetServiceByUUID(meta.Name)
	if srv != nil {
		srv.Online = false
	}
}

type ServiceListMessage struct {
	Services map[string]*Service `json:"services"`
	Command  string              `json:"command"`
	Meta     *Meta               `json:"meta"`
}

func (self *Connection) ListServiceHandler(msg *Message) {
	serviceList := &ServiceListMessage{}
	serviceList.Services = self.engine.ServiceRg.services
	serviceList.Command = "SERVICES"
	serviceList.Meta = &Meta{"Tenyks", TenyksVersion, nil}
	jsonBytes, err := json.Marshal(serviceList)
	if err != nil {
		log.Fatal(err)
	}
	self.Out <- string(jsonBytes[:])
}
<commit_msg>UUID -> SID<commit_after>package service

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/kyleterry/tenyks/irc"
	. "github.com/kyleterry/tenyks/version"
)

type servicefn func(*Connection, *Message)

func (self *ServiceEngine) AddHandler(name string, fn servicefn) {
	handler := irc.NewHandler(func(p ...interface{}) {
		fn(p[0].(*Connection), p[1].(*Message))
	})
	self.CommandRg.AddHandler(name, handler)
}

func (self *ServiceEngine) addBaseHandlers() {
	self.AddHandler("PRIVMSG", (*Connection).PrivmsgServiceHandler)
	self.AddHandler("REGISTER", (*Connection).RegisterServiceHandler)
	self.AddHandler("BYE", (*Connection).ByeServiceHandler)
	self.AddHandler("PONG", (*Connection).PongServiceHandler)
}

func (self *Connection) PrivmsgIrcHandler(conn *irc.Connection, msg *irc.Message) {
	serviceMsg := Message{}
	serviceMsg.Target = msg.Params[0]
	serviceMsg.Command = msg.Command
	serviceMsg.Mask = msg.Host
	serviceMsg.Direct = irc.IsDirect(msg.Trail, conn.GetCurrentNick())
	serviceMsg.Nick = msg.Nick
	serviceMsg.Host = msg.Host
	serviceMsg.Full_message = msg.RawMsg
	serviceMsg.User = msg.Ident
	serviceMsg.From_channel = irc.IsChannel(msg.Params[0])
	serviceMsg.Connection = conn.Name
	serviceMsg.Meta = &Meta{"Tenyks", TenyksVersion, nil}
	if serviceMsg.Direct {
		serviceMsg.Payload = irc.StripNickOnDirect(msg.Trail, conn.GetCurrentNick())
	} else {
		serviceMsg.Payload = msg.Trail
	}

	jsonBytes, err := json.Marshal(serviceMsg)
	if err != nil {
		log.Fatal(err)
	}
	self.Out <- string(jsonBytes[:])
}

func (self *Connection) PrivmsgServiceHandler(msg *Message) {
	conn := self.getIrcConnByName(msg.Connection)
	if conn != nil {
		msgStr := fmt.Sprintf("%s %s :%s", msg.Command, msg.Target, msg.Payload)
		conn.Out <- msgStr
	} else {
		log.Debug("[service] No such connection `%s`. Ignoring.",
			msg.Connection)
	}
}

func (self *Connection) RegisterServiceHandler(msg *Message) {
	meta := msg.Meta
	if meta.SID == nil {
		log.Error("[service] ERROR: UUID required to register with Tenyks")
		return
	}
	log.Debug("[service] %s (%s) wants to register", meta.SID.UUID.String(), meta.Name)
	srv := &Service{}
	srv.Name = meta.Name
	srv.Version = meta.Version
	srv.Online = true
	srv.LastPing = time.Now()
	srv.UUID = meta.SID.UUID
	self.engine.ServiceRg.RegisterService(srv)
}

func (self *Connection) ByeServiceHandler(msg *Message) {
	meta := msg.Meta
	log.Debug("[service] %s is hanging up", meta.Name)
	srv := self.engine.ServiceRg.GetServiceByUUID(meta.Name)
	if srv != nil {
		srv.Online = false
	}
}

type ServiceListMessage struct {
	Services map[string]*Service `json:"services"`
	Command  string              `json:"command"`
	Meta     *Meta               `json:"meta"`
}

func (self *Connection) ListServiceHandler(msg *Message) {
	serviceList := &ServiceListMessage{}
	serviceList.Services = self.engine.ServiceRg.services
	serviceList.Command = "SERVICES"
	serviceList.Meta = &Meta{"Tenyks", TenyksVersion, nil}
	jsonBytes, err := json.Marshal(serviceList)
	if err != nil {
		log.Fatal(err)
	}
	self.Out <- string(jsonBytes[:])
}
<|endoftext|>


<commit_before>/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/kubernetes/pkg/api/v1"
	"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
	"k8s.io/kubernetes/test/e2e/framework"
)

// verifyGCEDiskAttached performs a sanity check to verify the PD attached to the node
func verifyGCEDiskAttached(diskName string, nodeName types.NodeName) bool {
	gceCloud, err := framework.GetGCECloud()
	Expect(err).NotTo(HaveOccurred())
	isAttached, err := gceCloud.DiskIsAttached(diskName, nodeName)
	Expect(err).NotTo(HaveOccurred())
	return isAttached
}

// initializeGCETestSpec creates a PV, PVC, and ClientPod that will run until killed by test or clean up.
func initializeGCETestSpec(c clientset.Interface, ns string, pvConfig framework.PersistentVolumeConfig, pvcConfig framework.PersistentVolumeClaimConfig, isPrebound bool) (*v1.Pod, *v1.PersistentVolume, *v1.PersistentVolumeClaim) {
	By("Creating the PV and PVC")
	pv, pvc := framework.CreatePVPVC(c, pvConfig, pvcConfig, ns, isPrebound)
	framework.WaitOnPVandPVC(c, ns, pv, pvc)

	By("Creating the Client Pod")
	clientPod := framework.CreateClientPod(c, ns, pvc)
	return clientPod, pv, pvc
}

// Testing configurations of single a PV/PVC pair attached to a GCE PD
var _ = framework.KubeDescribe("PersistentVolumes:GCEPD [Volume]", func() {
	var (
		c         clientset.Interface
		diskName  string
		ns        string
		err       error
		pv        *v1.PersistentVolume
		pvc       *v1.PersistentVolumeClaim
		clientPod *v1.Pod
		pvConfig  framework.PersistentVolumeConfig
		pvcConfig framework.PersistentVolumeClaimConfig
		volLabel  labels.Set
		selector  *metav1.LabelSelector
		node      types.NodeName
	)

	f := framework.NewDefaultFramework("pv")
	BeforeEach(func() {
		c = f.ClientSet
		ns = f.Namespace.Name

		// Enforce binding only within test space via selector labels
		volLabel = labels.Set{framework.VolumeSelectorKey: ns}
		selector = metav1.SetAsLabelSelector(volLabel)

		framework.SkipUnlessProviderIs("gce", "gke")
		By("Initializing Test Spec")
		diskName, err = framework.CreatePDWithRetry()
		Expect(err).NotTo(HaveOccurred())
		pvConfig = framework.PersistentVolumeConfig{
			NamePrefix: "gce-",
			Labels:     volLabel,
			PVSource: v1.PersistentVolumeSource{
				GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
					PDName:   diskName,
					FSType:   "ext3",
					ReadOnly: false,
				},
			},
			Prebind: nil,
		}
		pvcConfig = framework.PersistentVolumeClaimConfig{
			Annotations: map[string]string{
				v1.BetaStorageClassAnnotation: "",
			},
			Selector: selector,
		}
		clientPod, pv, pvc = initializeGCETestSpec(c, ns, pvConfig, pvcConfig, false)
		node = types.NodeName(clientPod.Spec.NodeName)
	})

	AfterEach(func() {
		framework.Logf("AfterEach: Cleaning up test resources")
		if c != nil {
			framework.DeletePodWithWait(f, c, clientPod)
			framework.PVPVCCleanup(c, ns, pv, pvc)
			clientPod, pv, pvc, node = nil, nil, nil, ""
		}
	})

	AddCleanupAction(func() {
		if len(diskName) > 0 {
			framework.DeletePDWithRetry(diskName)
		}
	})

	// Attach a persistent disk to a pod using a PVC.
	// Delete the PVC and then the pod.  Expect the pod to succeed in unmounting and detaching PD on delete.
	It("should test that deleting a PVC before the pod does not cause pod deletion to fail on PD detach", func() {

		By("Deleting the Claim")
		framework.DeletePersistentVolumeClaim(c, pvc.Name, ns)
		verifyGCEDiskAttached(diskName, node)

		By("Deleting the Pod")
		framework.DeletePodWithWait(f, c, clientPod)

		By("Verifying Persistent Disk detach")
		err = waitForPDDetach(diskName, node)
		Expect(err).NotTo(HaveOccurred())
	})

	// Attach a persistent disk to a pod using a PVC.
	// Delete the PV and then the pod.  Expect the pod to succeed in unmounting and detaching PD on delete.
	It("should test that deleting the PV before the pod does not cause pod deletion to fail on PD detach", func() {

		By("Deleting the Persistent Volume")
		framework.DeletePersistentVolume(c, pv.Name)
		verifyGCEDiskAttached(diskName, node)

		By("Deleting the client pod")
		framework.DeletePodWithWait(f, c, clientPod)

		By("Verifying Persistent Disk detaches")
		err = waitForPDDetach(diskName, node)
		Expect(err).NotTo(HaveOccurred())
	})

	// Test that a Pod and PVC attached to a GCEPD successfully unmounts and detaches when the encompassing Namespace is deleted.
	It("should test that deleting the Namespace of a PVC and Pod causes the successful detach of Persistent Disk", func() {

		By("Deleting the Namespace")
		err := c.CoreV1().Namespaces().Delete(ns, nil)
		Expect(err).NotTo(HaveOccurred())

		err = framework.WaitForNamespacesDeleted(c, []string{ns}, 3*time.Minute)
		Expect(err).NotTo(HaveOccurred())

		By("Verifying Persistent Disk detaches")
		err = waitForPDDetach(diskName, node)
		Expect(err).NotTo(HaveOccurred())
	})
})
<commit_msg>move PD delete to AfterEach<commit_after>/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/apimachinery/pkg/types"
	"k8s.io/kubernetes/pkg/api/v1"
	"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
	"k8s.io/kubernetes/test/e2e/framework"
)

// verifyGCEDiskAttached performs a sanity check to verify the PD attached to the node
func verifyGCEDiskAttached(diskName string, nodeName types.NodeName) bool {
	gceCloud, err := framework.GetGCECloud()
	Expect(err).NotTo(HaveOccurred())
	isAttached, err := gceCloud.DiskIsAttached(diskName, nodeName)
	Expect(err).NotTo(HaveOccurred())
	return isAttached
}

// initializeGCETestSpec creates a PV, PVC, and ClientPod that will run until killed by test or clean up.
func initializeGCETestSpec(c clientset.Interface, ns string, pvConfig framework.PersistentVolumeConfig, pvcConfig framework.PersistentVolumeClaimConfig, isPrebound bool) (*v1.Pod, *v1.PersistentVolume, *v1.PersistentVolumeClaim) {
	By("Creating the PV and PVC")
	pv, pvc := framework.CreatePVPVC(c, pvConfig, pvcConfig, ns, isPrebound)
	framework.WaitOnPVandPVC(c, ns, pv, pvc)

	By("Creating the Client Pod")
	clientPod := framework.CreateClientPod(c, ns, pvc)
	return clientPod, pv, pvc
}

// Testing configurations of single a PV/PVC pair attached to a GCE PD
var _ = framework.KubeDescribe("PersistentVolumes:GCEPD [Volume]", func() {
	var (
		c         clientset.Interface
		diskName  string
		ns        string
		err       error
		pv        *v1.PersistentVolume
		pvc       *v1.PersistentVolumeClaim
		clientPod *v1.Pod
		pvConfig  framework.PersistentVolumeConfig
		pvcConfig framework.PersistentVolumeClaimConfig
		volLabel  labels.Set
		selector  *metav1.LabelSelector
		node      types.NodeName
	)

	f := framework.NewDefaultFramework("pv")
	BeforeEach(func() {
		c = f.ClientSet
		ns = f.Namespace.Name

		// Enforce binding only within test space via selector labels
		volLabel = labels.Set{framework.VolumeSelectorKey: ns}
		selector = metav1.SetAsLabelSelector(volLabel)

		framework.SkipUnlessProviderIs("gce", "gke")
		By("Initializing Test Spec")
		diskName, err = framework.CreatePDWithRetry()
		Expect(err).NotTo(HaveOccurred())
		pvConfig = framework.PersistentVolumeConfig{
			NamePrefix: "gce-",
			Labels:     volLabel,
			PVSource: v1.PersistentVolumeSource{
				GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
					PDName:   diskName,
					FSType:   "ext3",
					ReadOnly: false,
				},
			},
			Prebind: nil,
		}
		pvcConfig = framework.PersistentVolumeClaimConfig{
			Annotations: map[string]string{
				v1.BetaStorageClassAnnotation: "",
			},
			Selector: selector,
		}
		clientPod, pv, pvc = initializeGCETestSpec(c, ns, pvConfig, pvcConfig, false)
		node = types.NodeName(clientPod.Spec.NodeName)
	})

	AfterEach(func() {
		framework.Logf("AfterEach: Cleaning up test resources")
		if c != nil {
			framework.DeletePodWithWait(f, c, clientPod)
			framework.PVPVCCleanup(c, ns, pv, pvc)
			clientPod, pv, pvc, node = nil, nil, nil, ""
			if diskName != "" {
				framework.DeletePDWithRetry(diskName)
			}
		}
	})

	// Attach a persistent disk to a pod using a PVC.
	// Delete the PVC and then the pod.  Expect the pod to succeed in unmounting and detaching PD on delete.
	It("should test that deleting a PVC before the pod does not cause pod deletion to fail on PD detach", func() {

		By("Deleting the Claim")
		framework.DeletePersistentVolumeClaim(c, pvc.Name, ns)
		verifyGCEDiskAttached(diskName, node)

		By("Deleting the Pod")
		framework.DeletePodWithWait(f, c, clientPod)

		By("Verifying Persistent Disk detach")
		err = waitForPDDetach(diskName, node)
		Expect(err).NotTo(HaveOccurred())
	})

	// Attach a persistent disk to a pod using a PVC.
	// Delete the PV and then the pod.  Expect the pod to succeed in unmounting and detaching PD on delete.
	It("should test that deleting the PV before the pod does not cause pod deletion to fail on PD detach", func() {

		By("Deleting the Persistent Volume")
		framework.DeletePersistentVolume(c, pv.Name)
		verifyGCEDiskAttached(diskName, node)

		By("Deleting the client pod")
		framework.DeletePodWithWait(f, c, clientPod)

		By("Verifying Persistent Disk detaches")
		err = waitForPDDetach(diskName, node)
		Expect(err).NotTo(HaveOccurred())
	})

	// Test that a Pod and PVC attached to a GCEPD successfully unmounts and detaches when the encompassing Namespace is deleted.
	It("should test that deleting the Namespace of a PVC and Pod causes the successful detach of Persistent Disk", func() {

		By("Deleting the Namespace")
		err := c.CoreV1().Namespaces().Delete(ns, nil)
		Expect(err).NotTo(HaveOccurred())

		err = framework.WaitForNamespacesDeleted(c, []string{ns}, 3*time.Minute)
		Expect(err).NotTo(HaveOccurred())

		By("Verifying Persistent Disk detaches")
		err = waitForPDDetach(diskName, node)
		Expect(err).NotTo(HaveOccurred())
	})
})
<|endoftext|>


<commit_before>/*
Copyright 2016 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e_node

import (
	"fmt"

	v1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	kubeapi "k8s.io/kubernetes/pkg/apis/core"
	"k8s.io/kubernetes/pkg/apis/scheduling"
	kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
	kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
	"k8s.io/kubernetes/test/e2e/framework"
	imageutils "k8s.io/kubernetes/test/utils/image"

	"github.com/onsi/ginkgo"
	"github.com/onsi/gomega"
)

const (
	criticalPodName   = "critical-pod"
	guaranteedPodName = "guaranteed"
	burstablePodName  = "burstable"
	bestEffortPodName = "best-effort"
)

var _ = framework.KubeDescribe("CriticalPod [Serial] [Disruptive] [NodeFeature:CriticalPod]", func() {
	f := framework.NewDefaultFramework("critical-pod-test")

	ginkgo.Context("when we need to admit a critical pod", func() {
		tempSetCurrentKubeletConfig(f, func(initialConfig *kubeletconfig.KubeletConfiguration) {
			if initialConfig.FeatureGates == nil {
				initialConfig.FeatureGates = make(map[string]bool)
			}
		})

		ginkgo.It("should be able to create and delete a critical pod", func() {
			configEnabled, err := isKubeletConfigEnabled(f)
			framework.ExpectNoError(err)
			if !configEnabled {
				framework.Skipf("unable to run test without dynamic kubelet config enabled.")
			}

			// Define test pods
			nonCriticalGuaranteed := getTestPod(false, guaranteedPodName, v1.ResourceRequirements{
				Requests: v1.ResourceList{
					v1.ResourceCPU:    resource.MustParse("100m"),
					v1.ResourceMemory: resource.MustParse("100Mi"),
				},
				Limits: v1.ResourceList{
					v1.ResourceCPU:    resource.MustParse("100m"),
					v1.ResourceMemory: resource.MustParse("100Mi"),
				},
			})
			nonCriticalBurstable := getTestPod(false, burstablePodName, v1.ResourceRequirements{
				Requests: v1.ResourceList{
					v1.ResourceCPU:    resource.MustParse("100m"),
					v1.ResourceMemory: resource.MustParse("100Mi"),
				},
			})
			nonCriticalBestEffort := getTestPod(false, bestEffortPodName, v1.ResourceRequirements{})
			criticalPod := getTestPod(true, criticalPodName, v1.ResourceRequirements{
				// request the entire resource capacity of the node, so that
				// admitting this pod requires the other pod to be preempted
				Requests: getNodeCPUAndMemoryCapacity(f),
			})

			// Create pods, starting with non-critical so that the critical preempts the other pods.
			f.PodClient().CreateBatch([]*v1.Pod{nonCriticalBestEffort, nonCriticalBurstable, nonCriticalGuaranteed})
			f.PodClientNS(kubeapi.NamespaceSystem).CreateSyncInNamespace(criticalPod, kubeapi.NamespaceSystem)

			// Check that non-critical pods other than the besteffort have been evicted
			updatedPodList, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).List(metav1.ListOptions{})
			framework.ExpectNoError(err)
			for _, p := range updatedPodList.Items {
				if p.Name == nonCriticalBestEffort.Name {
					gomega.Expect(p.Status.Phase).NotTo(gomega.Equal(v1.PodFailed), fmt.Sprintf("pod: %v should be preempted", p.Name))
				} else {
					framework.ExpectEqual(p.Status.Phase, v1.PodFailed, fmt.Sprintf("pod: %v should not be preempted", p.Name))
				}
			}
		})
		ginkgo.AfterEach(func() {
			// Delete Pods
			f.PodClient().DeleteSync(guaranteedPodName, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
			f.PodClient().DeleteSync(burstablePodName, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
			f.PodClient().DeleteSync(bestEffortPodName, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
			f.PodClientNS(kubeapi.NamespaceSystem).DeleteSyncInNamespace(criticalPodName, kubeapi.NamespaceSystem, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
			// Log Events
			logPodEvents(f)
			logNodeEvents(f)

		})
	})
})

func getNodeCPUAndMemoryCapacity(f *framework.Framework) v1.ResourceList {
	nodeList, err := f.ClientSet.CoreV1().Nodes().List(metav1.ListOptions{})
	framework.ExpectNoError(err)
	// Assuming that there is only one node, because this is a node e2e test.
	framework.ExpectEqual(len(nodeList.Items), 1)
	capacity := nodeList.Items[0].Status.Allocatable
	return v1.ResourceList{
		v1.ResourceCPU:    capacity[v1.ResourceCPU],
		v1.ResourceMemory: capacity[v1.ResourceMemory],
	}
}

func getTestPod(critical bool, name string, resources v1.ResourceRequirements) *v1.Pod {
	pod := &v1.Pod{
		TypeMeta: metav1.TypeMeta{
			Kind:       "Pod",
			APIVersion: "v1",
		},
		ObjectMeta: metav1.ObjectMeta{Name: name},
		Spec: v1.PodSpec{
			Containers: []v1.Container{
				{
					Name:      "container",
					Image:     imageutils.GetPauseImageName(),
					Resources: resources,
				},
			},
		},
	}
	if critical {
		pod.ObjectMeta.Namespace = kubeapi.NamespaceSystem
		pod.ObjectMeta.Annotations = map[string]string{
			kubelettypes.ConfigSourceAnnotationKey: kubelettypes.FileSource,
		}
		podPriority := scheduling.SystemCriticalPriority
		pod.Spec.Priority = &podPriority

		gomega.Expect(kubelettypes.IsCriticalPod(pod)).To(gomega.BeTrue(), "pod should be a critical pod")
	} else {
		gomega.Expect(kubelettypes.IsCriticalPod(pod)).To(gomega.BeFalse(), "pod should not be a critical pod")
	}
	return pod
}
<commit_msg>using const instead<commit_after>/*
Copyright 2016 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e_node

import (
	"fmt"

	v1 "k8s.io/api/core/v1"
	schedulingv1 "k8s.io/api/scheduling/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/api/resource"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	kubeapi "k8s.io/kubernetes/pkg/apis/core"
	"k8s.io/kubernetes/pkg/apis/scheduling"
	kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
	kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
	"k8s.io/kubernetes/test/e2e/framework"
	imageutils "k8s.io/kubernetes/test/utils/image"

	"github.com/onsi/ginkgo"
	"github.com/onsi/gomega"
)

const (
	criticalPodName            = "critical-pod"
	guaranteedPodName          = "guaranteed"
	burstablePodName           = "burstable"
	bestEffortPodName          = "best-effort"
	systemCriticalPriorityName = "critical-pod-test-high-priority"
)

var _ = framework.KubeDescribe("CriticalPod [Serial] [Disruptive] [NodeFeature:CriticalPod]", func() {
	f := framework.NewDefaultFramework("critical-pod-test")
	ginkgo.Context("when we need to admit a critical pod", func() {
		tempSetCurrentKubeletConfig(f, func(initialConfig *kubeletconfig.KubeletConfiguration) {
			if initialConfig.FeatureGates == nil {
				initialConfig.FeatureGates = make(map[string]bool)
			}
		})

		ginkgo.It("should be able to create and delete a critical pod", func() {
			configEnabled, err := isKubeletConfigEnabled(f)
			framework.ExpectNoError(err)
			if !configEnabled {
				framework.Skipf("unable to run test without dynamic kubelet config enabled.")
			}
			// because adminssion Priority enable, If the priority class is not found, the Pod is rejected.
			systemCriticalPriority := &schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: systemCriticalPriorityName}, Value: scheduling.SystemCriticalPriority}

			// Define test pods
			nonCriticalGuaranteed := getTestPod(false, guaranteedPodName, v1.ResourceRequirements{
				Requests: v1.ResourceList{
					v1.ResourceCPU:    resource.MustParse("100m"),
					v1.ResourceMemory: resource.MustParse("100Mi"),
				},
				Limits: v1.ResourceList{
					v1.ResourceCPU:    resource.MustParse("100m"),
					v1.ResourceMemory: resource.MustParse("100Mi"),
				},
			})
			nonCriticalBurstable := getTestPod(false, burstablePodName, v1.ResourceRequirements{
				Requests: v1.ResourceList{
					v1.ResourceCPU:    resource.MustParse("100m"),
					v1.ResourceMemory: resource.MustParse("100Mi"),
				},
			})
			nonCriticalBestEffort := getTestPod(false, bestEffortPodName, v1.ResourceRequirements{})
			criticalPod := getTestPod(true, criticalPodName, v1.ResourceRequirements{
				// request the entire resource capacity of the node, so that
				// admitting this pod requires the other pod to be preempted
				Requests: getNodeCPUAndMemoryCapacity(f),
			})

			_, err = f.ClientSet.SchedulingV1().PriorityClasses().Create(systemCriticalPriority)
			gomega.Expect(err == nil || errors.IsAlreadyExists(err)).To(gomega.BeTrue(), "failed to create PriorityClasses with an error: %v", err)

			// Create pods, starting with non-critical so that the critical preempts the other pods.
			f.PodClient().CreateBatch([]*v1.Pod{nonCriticalBestEffort, nonCriticalBurstable, nonCriticalGuaranteed})
			f.PodClientNS(kubeapi.NamespaceSystem).CreateSyncInNamespace(criticalPod, kubeapi.NamespaceSystem)

			// Check that non-critical pods other than the besteffort have been evicted
			updatedPodList, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).List(metav1.ListOptions{})
			framework.ExpectNoError(err)
			for _, p := range updatedPodList.Items {
				if p.Name == nonCriticalBestEffort.Name {
					gomega.Expect(p.Status.Phase).NotTo(gomega.Equal(v1.PodFailed), fmt.Sprintf("pod: %v should be preempted", p.Name))
				} else {
					framework.ExpectEqual(p.Status.Phase, v1.PodFailed, fmt.Sprintf("pod: %v should not be preempted", p.Name))
				}
			}
		})
		ginkgo.AfterEach(func() {
			// Delete Pods
			f.PodClient().DeleteSync(guaranteedPodName, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
			f.PodClient().DeleteSync(burstablePodName, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
			f.PodClient().DeleteSync(bestEffortPodName, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
			f.PodClientNS(kubeapi.NamespaceSystem).DeleteSyncInNamespace(criticalPodName, kubeapi.NamespaceSystem, &metav1.DeleteOptions{}, framework.DefaultPodDeletionTimeout)
			err := f.ClientSet.SchedulingV1().PriorityClasses().Delete(systemCriticalPriorityName, &metav1.DeleteOptions{})
			framework.ExpectNoError(err)
			// Log Events
			logPodEvents(f)
			logNodeEvents(f)

		})
	})
})

func getNodeCPUAndMemoryCapacity(f *framework.Framework) v1.ResourceList {
	nodeList, err := f.ClientSet.CoreV1().Nodes().List(metav1.ListOptions{})
	framework.ExpectNoError(err)
	// Assuming that there is only one node, because this is a node e2e test.
	framework.ExpectEqual(len(nodeList.Items), 1)
	capacity := nodeList.Items[0].Status.Allocatable
	return v1.ResourceList{
		v1.ResourceCPU:    capacity[v1.ResourceCPU],
		v1.ResourceMemory: capacity[v1.ResourceMemory],
	}
}

func getTestPod(critical bool, name string, resources v1.ResourceRequirements) *v1.Pod {
	value := scheduling.SystemCriticalPriority
	pod := &v1.Pod{
		TypeMeta: metav1.TypeMeta{
			Kind:       "Pod",
			APIVersion: "v1",
		},
		ObjectMeta: metav1.ObjectMeta{Name: name},
		Spec: v1.PodSpec{
			Containers: []v1.Container{
				{
					Name:      "container",
					Image:     imageutils.GetPauseImageName(),
					Resources: resources,
				},
			},
		},
	}
	if critical {
		pod.ObjectMeta.Namespace = kubeapi.NamespaceSystem
		pod.ObjectMeta.Annotations = map[string]string{
			kubelettypes.ConfigSourceAnnotationKey: kubelettypes.FileSource,
		}
		pod.Spec.PriorityClassName = systemCriticalPriorityName
		pod.Spec.Priority = &value

		gomega.Expect(kubelettypes.IsCriticalPod(pod)).To(gomega.BeTrue(), "pod should be a critical pod")
	} else {
		gomega.Expect(kubelettypes.IsCriticalPod(pod)).To(gomega.BeFalse(), "pod should not be a critical pod")
	}
	return pod
}
<|endoftext|>


<commit_before>package saltboot

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"path/filepath"

	"github.com/hortonworks/salt-bootstrap/saltboot/cautils"
	"github.com/hortonworks/salt-bootstrap/saltboot/model"
)

type Credentials struct {
	Clients
	PublicIP *string `json:"PublicIP" yaml:"PublicIP"`
}

func ClientCredsHandler(w http.ResponseWriter, req *http.Request) {

	decoder := json.NewDecoder(req.Body)
	var credentials Credentials
	err := decoder.Decode(&credentials)
	if err != nil {
		log.Printf("[ClientCredsHandler] [ERROR] couldn't decode json: %s", err.Error())
		model.Response{Status: err.Error()}.WriteBadRequestHttp(w)
		return
	}

	// mkdir if needed
	log.Printf("[CAHandler] handleClientCreds executed")
	w.Header().Set("Content-Type", "application/json")
	pubIp := credentials.PublicIP

	if cautils.IsPathExisting(cautils.DetermineCrtDir(os.Getenv)) == false {
		if err := os.Mkdir(cautils.DetermineCrtDir(os.Getenv), 0755); err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}
	}
	caResp, _ := http.Get("http://" + credentials.Servers[0].Address + ":7070/saltboot/ca")
	caBytes, _ := ioutil.ReadAll(caResp.Body)
	caCrt, err := cautils.NewCertificateFromPEM(caBytes)
	if err != nil {
		log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
		model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
		return
	}
	err = caCrt.ToPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "ca.crt"))
	if cautils.IsPathExisting(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.key")) == false {
		key, err := cautils.NewKey()
		if err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}

		err = key.ToPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.key"))
		if err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}
	}
	if cautils.IsPathExisting(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.csr")) == false {
		key, err := cautils.NewKeyFromPrivateKeyPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.key"))
		if err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}

		csr, err := cautils.NewCertificateRequest(key, pubIp)
		if err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}
		err = csr.ToPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.csr"))
		if err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}
	}
	csr, err := cautils.NewCertificateRequestFromPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.csr"))
	if err != nil {
		log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
		model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
		return
	}
	pem, _ := csr.ToPEM()
	data := make(url.Values)
	data.Add("csr", string(pem))
	//resp, err := http.PostForm("http://" + host + "/certificates", data)
	resp, _ := http.PostForm("http://"+credentials.Servers[0].Address+":7070/saltboot/csr", data)
	crtBytes, _ := ioutil.ReadAll(resp.Body)
	crt, err := cautils.NewCertificateFromPEM(crtBytes)
	if err != nil {
		log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
		model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
		return
	}
	err = crt.ToPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.crt"))
	model.Response{Status: "OK"}.WriteHttp(w)
	return
}

func ClientCredsDistributeHandler(w http.ResponseWriter, req *http.Request) {
	log.Println("[ClientCredsDistributeHandler] execute distribute hostname request")

	decoder := json.NewDecoder(req.Body)
	var credentials Credentials
	err := decoder.Decode(&credentials)
	if err != nil {
		log.Printf("[ClientCredsDistributeHandler] [ERROR] couldn't decode json: %s", err)
		model.Response{Status: err.Error()}.WriteBadRequestHttp(w)
		return
	}

	user, pass := GetAuthUserPass(req)
	responses := credentials.DistributeClientCredentials(user, pass)
	cResp := model.Responses{Responses: responses}
	log.Printf("[ClientCredsDistributeHandler] distribute request executed: %s" + cResp.String())
	json.NewEncoder(w).Encode(cResp)
}

func (credentials *Credentials) DistributeClientCredentials(user string, pass string) []model.Response {
	log.Printf("[Clients.DistributeClientCredentials] Request: %v", credentials)
	credReq := Credentials{
		Clients: Clients{
			Servers: credentials.Servers,
		},
		PublicIP: credentials.PublicIP,
	}
	jsonBody, _ := json.Marshal(credReq)
	resp := distributeImpl(Distribute, []string{credentials.Servers[0].Address}, jsonBody, ClientCredsEP, user, pass)
	for _, r := range resp {
		if r.StatusCode != http.StatusOK {
			return resp
		}
	}

	credReq.PublicIP = nil
	jsonBody, _ = json.Marshal(credReq)

	return append(resp, distributeImpl(Distribute, credentials.Clients.Clients, jsonBody, ClientCredsEP, user, pass)...)
}
<commit_msg>pass AuthToken as an auth hader to CSR request of saltboot slave<commit_after>package saltboot

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"

	"github.com/hortonworks/salt-bootstrap/saltboot/cautils"
	"github.com/hortonworks/salt-bootstrap/saltboot/model"
)

type Credentials struct {
	Clients
	PublicIP  *string `json:"PublicIP" yaml:"PublicIP"`
	AuthToken *string `json:"AuthToken" yaml:"AuthToken"`
}

func ClientCredsHandler(w http.ResponseWriter, req *http.Request) {

	decoder := json.NewDecoder(req.Body)
	var credentials Credentials
	err := decoder.Decode(&credentials)
	if err != nil {
		log.Printf("[ClientCredsHandler] [ERROR] couldn't decode json: %s", err.Error())
		model.Response{Status: err.Error()}.WriteBadRequestHttp(w)
		return
	}

	// mkdir if needed
	log.Printf("[CAHandler] handleClientCreds executed")
	w.Header().Set("Content-Type", "application/json")
	pubIp := credentials.PublicIP
	authToken := credentials.AuthToken
	if cautils.IsPathExisting(cautils.DetermineCrtDir(os.Getenv)) == false {
		if err := os.Mkdir(cautils.DetermineCrtDir(os.Getenv), 0755); err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}
	}
	caResp, _ := http.Get("http://" + credentials.Servers[0].Address + ":7070/saltboot/ca")
	caBytes, _ := ioutil.ReadAll(caResp.Body)
	caCrt, err := cautils.NewCertificateFromPEM(caBytes)
	if err != nil {
		log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
		model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
		return
	}
	err = caCrt.ToPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "ca.crt"))
	if cautils.IsPathExisting(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.key")) == false {
		key, err := cautils.NewKey()
		if err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}

		err = key.ToPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.key"))
		if err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}
	}
	if cautils.IsPathExisting(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.csr")) == false {
		key, err := cautils.NewKeyFromPrivateKeyPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.key"))
		if err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}

		csr, err := cautils.NewCertificateRequest(key, pubIp)
		if err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}
		err = csr.ToPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.csr"))
		if err != nil {
			log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
			model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
			return
		}
	}
	csr, err := cautils.NewCertificateRequestFromPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.csr"))
	if err != nil {
		log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
		model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
		return
	}
	pem, _ := csr.ToPEM()
	data := make(url.Values)
	data.Add("csr", string(pem))
	//resp, err := http.PostForm("http://" + host + "/certificates", data)
	httpreq, err := http.NewRequest("POST", "http://"+credentials.Servers[0].Address+":7070/saltboot/csr/client", strings.NewReader(data.Encode()))
	httpreq.Header.Add("Authorization", "Token "+*authToken)
	httpreq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	resp, _ := http.DefaultClient.Do(httpreq)
	crtBytes, _ := ioutil.ReadAll(resp.Body)
	crt, err := cautils.NewCertificateFromPEM(crtBytes)
	if err != nil {
		log.Printf("[ClientCredsHandler] [ERROR]: %s", err.Error())
		model.Response{Status: err.Error()}.WriteInternalServerErrorHttp(w)
		return
	}
	err = crt.ToPEMFile(filepath.Join(cautils.DetermineCrtDir(os.Getenv), "client.crt"))
	model.Response{Status: "OK"}.WriteHttp(w)
	return
}

func ClientCredsDistributeHandler(w http.ResponseWriter, req *http.Request) {
	log.Println("[ClientCredsDistributeHandler] execute distribute hostname request")

	decoder := json.NewDecoder(req.Body)
	var credentials Credentials
	err := decoder.Decode(&credentials)
	if err != nil {
		log.Printf("[ClientCredsDistributeHandler] [ERROR] couldn't decode json: %s", err)
		model.Response{Status: err.Error()}.WriteBadRequestHttp(w)
		return
	}

	user, pass := GetAuthUserPass(req)
	responses := credentials.DistributeClientCredentials(user, pass)
	cResp := model.Responses{Responses: responses}
	log.Printf("[ClientCredsDistributeHandler] distribute request executed: %s" + cResp.String())
	json.NewEncoder(w).Encode(cResp)
}

func (credentials *Credentials) DistributeClientCredentials(user string, pass string) []model.Response {
	log.Printf("[Clients.DistributeClientCredentials] Request: %v", credentials)
	credReq := Credentials{
		Clients: Clients{
			Servers: credentials.Servers,
		},
		PublicIP: credentials.PublicIP,
	}
	jsonBody, _ := json.Marshal(credReq)
	resp := distributeImpl(Distribute, []string{credentials.Servers[0].Address}, jsonBody, ClientCredsEP, user, pass)
	for _, r := range resp {
		if r.StatusCode != http.StatusOK {
			return resp
		}
	}

	credReq.PublicIP = nil
	jsonBody, _ = json.Marshal(credReq)

	return append(resp, distributeImpl(Distribute, credentials.Clients.Clients, jsonBody, ClientCredsEP, user, pass)...)
}
<|endoftext|>


<commit_before>package render

import (
	"bytes"
	"strings"
	"testing"
)

func TestNew(t *testing.T) {
	tm := New()
	tm.AddFunc("upper", strings.ToUpper)

	tm.Push("wrapperA.html")
	tm.AddTemplate("contentA.html")
	tm.AddTemplate("contentB.html")

	var bufA bytes.Buffer
	var bufB bytes.Buffer
	var bufC bytes.Buffer
	var bufD bytes.Buffer
	var bufE bytes.Buffer

	t.Logf("Loaded %v templates:", len(tm.templates))
	for k := range tm.templates {
		t.Logf("Found template: %v", k)
	}

	err := tm.Execute(&bufA, "base", "contentA", "hello")
	if err != nil {
		t.Errorf("For base, contentA, hello: %v", err)
	}

	err = tm.Execute(&bufB, "base", "contentB", "hello")
	if err != nil {
		t.Errorf("For base2, contentB, hello: %v", err)
	}

	err = tm.Execute(&bufC, "base2", "contentA", "hello")
	if err != nil {
		t.Errorf("For base, contentA, hello: %v", err)
	}

	err = tm.Execute(&bufD, "base2", "contentB", "hello")
	if err != nil {
		t.Errorf("For base2, contentB, hello: %v", err)
	}

	err = tm.Execute(&bufE, "base2", "not here", "hello")
	if err == nil {
		t.Errorf("Expecting error, got nil")
	}

	expectedA := "<wrapper><content>hello</content></wrapper>"
	expectedB := "<wrapper><contentB>HELLO</contentB></wrapper>"
	expectedC := "<wrapper2><content>hello</content></wrapper2>"
	expectedD := "<wrapper2><contentB>HELLO</contentB></wrapper2>"

	if bufA.String() != expectedA {
		t.Errorf("Expecting `%s`, got: `%s`", expectedA, bufA.String())
	}

	if bufB.String() != expectedB {
		t.Errorf("Expecting `%s`, got: `%s`", expectedB, bufB.String())
	}

	if bufC.String() != expectedC {
		t.Errorf("Expecting `%s`, got: `%s`", expectedC, bufC.String())
	}

	if bufD.String() != expectedD {
		t.Errorf("Expecting `%s`, got: `%s`", expectedD, bufD.String())
	}
}
<commit_msg>Gofmt<commit_after>package render

import (
	"bytes"
	"strings"
	"testing"
)

func TestNew(t *testing.T) {
	tm := New()
	tm.AddFunc("upper", strings.ToUpper)

	tm.Push("wrapperA.html")
	tm.AddTemplate("contentA.html")
	tm.AddTemplate("contentB.html")

	var bufA bytes.Buffer
	var bufB bytes.Buffer
	var bufC bytes.Buffer
	var bufD bytes.Buffer
	var bufE bytes.Buffer

	t.Logf("Loaded %v templates:", len(tm.templates))
	for k := range tm.templates {
		t.Logf("Found template: %v", k)
	}

	err := tm.Execute(&bufA, "base", "contentA", "hello")
	if err != nil {
		t.Errorf("For base, contentA, hello: %v", err)
	}

	err = tm.Execute(&bufB, "base", "contentB", "hello")
	if err != nil {
		t.Errorf("For base2, contentB, hello: %v", err)
	}

	err = tm.Execute(&bufC, "base2", "contentA", "hello")
	if err != nil {
		t.Errorf("For base, contentA, hello: %v", err)
	}

	err = tm.Execute(&bufD, "base2", "contentB", "hello")
	if err != nil {
		t.Errorf("For base2, contentB, hello: %v", err)
	}

	err = tm.Execute(&bufE, "base2", "not here", "hello")
	if err == nil {
		t.Errorf("Expecting error, got nil")
	}

	expectedA := "<wrapper><content>hello</content></wrapper>"
	expectedB := "<wrapper><contentB>HELLO</contentB></wrapper>"
	expectedC := "<wrapper2><content>hello</content></wrapper2>"
	expectedD := "<wrapper2><contentB>HELLO</contentB></wrapper2>"

	if bufA.String() != expectedA {
		t.Errorf("Expecting `%s`, got: `%s`", expectedA, bufA.String())
	}

	if bufB.String() != expectedB {
		t.Errorf("Expecting `%s`, got: `%s`", expectedB, bufB.String())
	}

	if bufC.String() != expectedC {
		t.Errorf("Expecting `%s`, got: `%s`", expectedC, bufC.String())
	}

	if bufD.String() != expectedD {
		t.Errorf("Expecting `%s`, got: `%s`", expectedD, bufD.String())
	}
}
<|endoftext|>


<commit_before>package formats

import (
	"encoding/binary"
	"encoding/hex"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"strconv"
	"strings"
)

// Layout represents a parsed file structure layout as a flat list
type Layout struct {
	Offset int64
	Length byte
	Type   DataType
	Info   string
}

// DataType ...
type DataType int

func (dt DataType) String() string {

	m := map[DataType]string{
		Int8:     "int8",
		Uint8:    "uint8",
		Int16le:  "int16-le",
		Uint16le: "uint16-le",
		Int32le:  "int32-le",
		Uint32le: "uint32-le",
		ASCII:    "ASCII",
		ASCIIZ:   "ASCIIZ",
	}

	if val, ok := m[dt]; ok {
		return val
	}

	// NOTE should only be able to panic during dev (as in:
	// adding a new datatype and forgetting to add it to the map)
	panic(dt)
}

// ParsedLayout ...
type ParsedLayout struct {
	FormatName string
	FileSize   int64
	Layout     []Layout
}

// ...
const (
	Int8 DataType = 1 + iota
	Uint8
	Int16le
	Uint16le
	Int32le
	Uint32le
	ASCII
	ASCIIZ
)

func fileExt(file *os.File) string {

	ext := filepath.Ext(file.Name())
	if len(ext) > 0 {
		// strip leading dot
		ext = ext[1:]
	}
	return ext
}

// ParseLayout returns a ParsedLayout for the file
func ParseLayout(file *os.File) (*ParsedLayout, error) {

	parsed, err := parseFileByDescription(file, fileExt(file))
	if parsed == nil {
		fmt.Println(err)
		panic("XXX if find by extension fails, search all for magic id")
	}

	return parsed, err
}

func getFileSize(file *os.File) int64 {

	fi, err := file.Stat()
	if err != nil {
		log.Fatal(err)
	}
	return fi.Size()
}

func parseFileByDescription(
	file *os.File, formatName string) (*ParsedLayout, error) {

	format, err := ReadFormatDescription(formatName)
	if err != nil {
		return nil, err
	}

	res := ParsedLayout{
		FormatName: formatName,
		FileSize:   getFileSize(file),
	}

	for _, step := range format.Details {

		layout, err := res.intoLayout(file, step)
		if err != nil {
			fmt.Println("trouble parsing:", err)
		}

		res.Layout = append(res.Layout, *layout)
	}

	return &res, nil
}

func (l *Layout) parseByteN(reader io.Reader, expectedLen int64) ([]byte, error) {

	buf := make([]byte, expectedLen)

	readLen, err := reader.Read(buf)
	if err != nil {
		return nil, err
	}

	if int64(readLen) != expectedLen {
		return nil, fmt.Errorf("Expected %d bytes, got %d", expectedLen, readLen)
	}
	return buf, nil
}

// transforms a part of file into a Layout, according to `step`
func (pl *ParsedLayout) intoLayout(file *os.File, step string) (*Layout, error) {

	reader := io.Reader(file)

	// params: name | data type and size | type-dependant
	params := strings.Split(step, "|")

	layout := Layout{}

	layout.Offset, _ = file.Seek(0, os.SEEK_CUR)
	layout.Info = params[0]

	param1 := ""
	param2 := ""
	if len(params) > 1 {
		param1 = params[1]
	}
	if len(params) > 2 {
		param2 = params[2]
	}

	if expectedLen, err := parseExpectedBytes(&layout, reader, param1, param2); err == nil {
		layout.Length = byte(expectedLen)
		layout.Type = ASCII
	} else if _, err := parseExpectedByte(reader, param1, param2); err == nil {
		layout.Length = 1
		layout.Type = Uint8
	} else if _, err := parseExpectedUint16le(reader, param1, param2); err == nil {
		layout.Length = 2
		layout.Type = Uint16le
	} else {
		return nil, fmt.Errorf("dunno how to handle %s", param1)
	}

	return &layout, nil
}

func parseExpectedUint16le(reader io.Reader, param1 string, param2 string) (uint16, error) {

	if param1 != "uint16le" {
		return 0, fmt.Errorf("wrong type")
	}
	var b uint16
	err := binary.Read(reader, binary.LittleEndian, &b);
	return b, err
}

func parseExpectedByte(reader io.Reader, param1 string, param2 string) (byte, error) {

	if param1 != "uint8" && param1 != "byte" {
		return 0, fmt.Errorf("wrong type")
	}
	// XXX "byte", params[2] describes a bit field
	var b byte
	err := binary.Read(reader, binary.LittleEndian, &b);
	return b, err
}

func parseExpectedBytes(layout *Layout, reader io.Reader, param1 string, param2 string) (int64, error) {

	p1 := strings.Split(param1, ":")

	if p1[0] != "byte" || len(p1) != 2 {
		return 0, fmt.Errorf("wrong type")
	}

	expectedLen, err := parseExpectedLen(p1[1])
	if err != nil {
		return 0, err
	}

	// "byte:3", params[2] holds the bytes
	buf, err := layout.parseByteN(reader, expectedLen)
	if err != nil {
		return 0, err
	}

	// split expected forms on comma
	expectedForms := strings.Split(param2, ",")
	for _, expectedForm := range expectedForms {

		expectedBytes := []byte(expectedForm)
		if int64(len(expectedForm)) == 2*expectedLen {
			// hex string?
			bytes, err := hex.DecodeString(expectedForm)
			if err == nil && byteSliceEquals(buf, bytes) {
				return expectedLen, nil
			}
		}
		if string(buf) == string(expectedBytes) {
			return expectedLen, nil
		}
	}

	return 0, fmt.Errorf("didnt find expected bytes %s", param2)
}

func parseExpectedLen(s string) (int64, error) {
	expectedLen, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		return 0, err
	}
	if expectedLen > 255 {
		return 0, fmt.Errorf("len too big (max 255)")
	}
	if expectedLen <= 0 {
		return 0, fmt.Errorf("len too small (min 1)")
	}
	return expectedLen, nil
}

// PrettyHexView ...
func (pl *ParsedLayout) PrettyHexView(file *os.File) string {

	ofsFmt := "%08x"
	if pl.FileSize <= 0xffff {
		ofsFmt = "%04x"
	} else if pl.FileSize <= 0xffffff {
		ofsFmt = "%06x"
	}

	hex := ""

	base := HexView.StartingRow * int64(HexView.RowWidth)
	ceil := base + int64(HexView.VisibleRows*HexView.RowWidth)

	for i := base; i < ceil; i += int64(HexView.RowWidth) {

		ofs, err := file.Seek(i, os.SEEK_SET)
		if i != ofs {
			log.Fatalf("err: unexpected offset %04x, expected %04x\n", ofs, i)
		}
		line, err := pl.GetHex(file)

		ofsText := fmt.Sprintf(ofsFmt, i)

		hex += fmt.Sprintf("[[%s]](fg-yellow) %s\n", ofsText, line)
		if err != nil {
			fmt.Println("got err", err)
			break
		}
	}

	return hex
}

func (pl *ParsedLayout) isOffsetKnown(ofs int64) bool {

	for _, layout := range pl.Layout {
		if ofs >= layout.Offset && ofs < layout.Offset+int64(layout.Length) {
			return true
		}
	}
	return false
}

// GetHex dumps a row of hex from io.Reader
func (pl *ParsedLayout) GetHex(file *os.File) (string, error) {

	layout := pl.Layout[HexView.CurrentField]

	reader := io.Reader(file)

	symbols := []string{}

	base, err := file.Seek(0, os.SEEK_CUR)
	if err != nil {
		return "", err
	}

	for w := int64(0); w < 16; w++ {
		var b byte
		if err = binary.Read(reader, binary.LittleEndian, &b); err != nil {
			if err == io.EOF {
				return combineHexRow(symbols), nil
			}
			return "", err
		}

		ceil := base + w

		colorName := "fg-white"
		if !pl.isOffsetKnown(base + w) {
			colorName = "fg-red"
		}
		if ceil >= layout.Offset && ceil < layout.Offset+int64(layout.Length) {
			colorName = "fg-blue"
		}

		group := fmt.Sprintf("[%02x](%s)", b, colorName)
		symbols = append(symbols, group)
	}

	return combineHexRow(symbols), nil
}
<commit_msg>minor tweaks<commit_after>package formats

import (
	"encoding/binary"
	"encoding/hex"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"strconv"
	"strings"
)

// Layout represents a parsed file structure layout as a flat list
type Layout struct {
	Offset int64
	Length byte
	Type   DataType
	Info   string
}

// DataType ...
type DataType int

func (dt DataType) String() string {

	m := map[DataType]string{
		Int8:     "int8",
		Uint8:    "uint8",
		Int16le:  "int16-le",
		Uint16le: "uint16-le",
		Int32le:  "int32-le",
		Uint32le: "uint32-le",
		ASCII:    "ASCII",
		ASCIIZ:   "ASCIIZ",
	}

	if val, ok := m[dt]; ok {
		return val
	}

	// NOTE should only be able to panic during dev (as in:
	// adding a new datatype and forgetting to add it to the map)
	panic(dt)
}

// ParsedLayout ...
type ParsedLayout struct {
	FormatName string
	FileSize   int64
	Layout     []Layout
}

// ...
const (
	Int8 DataType = 1 + iota
	Uint8
	Int16le
	Uint16le
	Int32le
	Uint32le
	ASCII
	ASCIIZ
)

func fileExt(file *os.File) string {

	ext := filepath.Ext(file.Name())
	if len(ext) > 0 {
		// strip leading dot
		ext = ext[1:]
	}
	return ext
}

// ParseLayout returns a ParsedLayout for the file
func ParseLayout(file *os.File) (*ParsedLayout, error) {

	parsed, err := parseFileByDescription(file, fileExt(file))
	if parsed == nil {
		fmt.Println(err)
		panic("XXX if find by extension fails, search all for magic id")
	}

	return parsed, err
}

func getFileSize(file *os.File) int64 {

	fi, err := file.Stat()
	if err != nil {
		log.Fatal(err)
	}
	return fi.Size()
}

func parseFileByDescription(
	file *os.File, formatName string) (*ParsedLayout, error) {

	format, err := ReadFormatDescription(formatName)
	if err != nil {
		return nil, err
	}

	res := ParsedLayout{
		FormatName: formatName,
		FileSize:   getFileSize(file),
	}

	for _, step := range format.Details {

		layout, err := res.intoLayout(file, step)
		if err != nil {
			fmt.Println("trouble parsing:", err)
		}

		res.Layout = append(res.Layout, *layout)
	}

	return &res, nil
}

func (l *Layout) parseByteN(reader io.Reader, expectedLen int64) ([]byte, error) {

	buf := make([]byte, expectedLen)

	readLen, err := reader.Read(buf)
	if err != nil {
		return nil, err
	}

	if int64(readLen) != expectedLen {
		return nil, fmt.Errorf("Expected %d bytes, got %d", expectedLen, readLen)
	}
	return buf, nil
}

// transforms a part of file into a Layout, according to `step`
func (pl *ParsedLayout) intoLayout(file *os.File, step string) (*Layout, error) {

	reader := io.Reader(file)

	// params: name | data type and size | type-dependant
	params := strings.Split(step, "|")

	layout := Layout{}

	layout.Offset, _ = file.Seek(0, os.SEEK_CUR)
	layout.Info = params[0]

	param1 := ""
	param2 := ""
	if len(params) > 1 {
		param1 = params[1]
	}
	if len(params) > 2 {
		param2 = params[2]
	}

	if b, err := parseExpectedBytes(&layout, reader, param1, param2); err == nil {
		layout.Length = byte(len(b))
		layout.Type = ASCII
	} else if _, err := parseExpectedByte(reader, param1, param2); err == nil {
		layout.Length = 1
		layout.Type = Uint8
	} else if _, err := parseExpectedUint16le(reader, param1, param2); err == nil {
		layout.Length = 2
		layout.Type = Uint16le
	} else {
		return nil, fmt.Errorf("dunno how to handle %s", param1)
	}

	return &layout, nil
}

func parseExpectedUint16le(reader io.Reader, param1 string, param2 string) (uint16, error) {

	if param1 != "uint16le" {
		return 0, fmt.Errorf("wrong type")
	}
	var b uint16
	err := binary.Read(reader, binary.LittleEndian, &b);
	return b, err
}

func parseExpectedByte(reader io.Reader, param1 string, param2 string) (byte, error) {

	if param1 != "uint8" && param1 != "byte" {
		return 0, fmt.Errorf("wrong type")
	}
	// XXX "byte", params[2] describes a bit field
	var b byte
	err := binary.Read(reader, binary.LittleEndian, &b);
	return b, err
}

func parseExpectedBytes(layout *Layout, reader io.Reader, param1 string, param2 string) ([]byte, error) {

	p1 := strings.Split(param1, ":")

	if p1[0] != "byte" || len(p1) != 2 {
		return nil, fmt.Errorf("wrong type")
	}

	expectedLen, err := parseExpectedLen(p1[1])
	if err != nil {
		return nil, err
	}

	// "byte:3", params[2] holds the bytes
	buf, err := layout.parseByteN(reader, expectedLen)
	if err != nil {
		return nil, err
	}

	// split expected forms on comma
	expectedForms := strings.Split(param2, ",")
	for _, expectedForm := range expectedForms {

		expectedBytes := []byte(expectedForm)
		if int64(len(expectedForm)) == 2*expectedLen {
			// hex string?
			bytes, err := hex.DecodeString(expectedForm)
			if err == nil && byteSliceEquals(buf, bytes) {
				return expectedBytes, nil
			}
		}
		if string(buf) == string(expectedBytes) {
			return expectedBytes, nil
		}
	}

	return nil, fmt.Errorf("didnt find expected bytes %s", param2)
}

func parseExpectedLen(s string) (int64, error) {
	expectedLen, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		return 0, err
	}
	if expectedLen > 255 {
		return 0, fmt.Errorf("len too big (max 255)")
	}
	if expectedLen <= 0 {
		return 0, fmt.Errorf("len too small (min 1)")
	}
	return expectedLen, nil
}

// PrettyHexView ...
func (pl *ParsedLayout) PrettyHexView(file *os.File) string {

	ofsFmt := "%08x"
	if pl.FileSize <= 0xffff {
		ofsFmt = "%04x"
	} else if pl.FileSize <= 0xffffff {
		ofsFmt = "%06x"
	}

	hex := ""

	base := HexView.StartingRow * int64(HexView.RowWidth)
	ceil := base + int64(HexView.VisibleRows*HexView.RowWidth)

	for i := base; i < ceil; i += int64(HexView.RowWidth) {

		ofs, err := file.Seek(i, os.SEEK_SET)
		if i != ofs {
			log.Fatalf("err: unexpected offset %04x, expected %04x\n", ofs, i)
		}
		line, err := pl.GetHex(file)

		ofsText := fmt.Sprintf(ofsFmt, i)

		hex += fmt.Sprintf("[[%s]](fg-yellow) %s\n", ofsText, line)
		if err != nil {
			fmt.Println("got err", err)
			break
		}
	}

	return hex
}

func (pl *ParsedLayout) isOffsetKnown(ofs int64) bool {

	for _, layout := range pl.Layout {
		if ofs >= layout.Offset && ofs < layout.Offset+int64(layout.Length) {
			return true
		}
	}
	return false
}

// GetHex dumps a row of hex from io.Reader
func (pl *ParsedLayout) GetHex(file *os.File) (string, error) {

	layout := pl.Layout[HexView.CurrentField]

	reader := io.Reader(file)

	symbols := []string{}

	base, err := file.Seek(0, os.SEEK_CUR)
	if err != nil {
		return "", err
	}

	for w := int64(0); w < 16; w++ {
		var b byte
		if err = binary.Read(reader, binary.LittleEndian, &b); err != nil {
			if err == io.EOF {
				return combineHexRow(symbols), nil
			}
			return "", err
		}

		ceil := base + w

		colorName := "fg-white"
		if !pl.isOffsetKnown(base + w) {
			colorName = "fg-red"
		}
		if ceil >= layout.Offset && ceil < layout.Offset+int64(layout.Length) {
			colorName = "fg-blue"
		}

		group := fmt.Sprintf("[%02x](%s)", b, colorName)
		symbols = append(symbols, group)
	}

	return combineHexRow(symbols), nil
}
<|endoftext|>