<commit_before>package parser

import (
	"testing"
)

func TestParser_Parse(t *testing.T) {
	var tests = []struct {
		s   []string
		doc *Document
		err string
	}{
		{
			s: []string{
				`@Command Add_Device`,
				`@Is addDevice`,
				`@Parent root`,
				``,
				`Adds a Device to the link`,
				``,
				`This is a long description.`,
				`It really doesn't contain anything special.`,
				`But it is multiline`,
				``,
				`@Param deviceName string Name of the device to add. It will`,
				`appear as a node on the root of the link.`,
				`@Param username string The Username to access the device.`,
				``,
				`@Return value`,
				`@Column success bool Returns true on success. False otherwise.`,
			},
			doc: &Document{
				Type:       ActionDoc,
				Name:       "Add_Device",
				Is:         "addDevice",
				ParentName: "root",
				Short:      "Adds a Device to the link",
				Long:       "This is a long description. It really doesn't contain anything special. But it is multiline",
				Params: []*Parameter{
					{
						Name:        "deviceName",
						Type:        "string",
						Description: "Name of the device to add. It will appear as a node on the root of the link.",
					},
					{
						Name:        "username",
						Type:        "string",
						Description: "The Username to access the device.",
					},
				},
				Return: "value",
				Columns: []*Parameter{
					{
						Name:        "success",
						Type:        "bool",
						Description: "Returns true on success. False otherwise.",
					},
				},
			},
		},
		{
			s: []string{
				`@Node`,
				`@MetaType test`,
				`@Parent root`,
				``,
				`Short Test node`,
				``,
				`Also has a long description. But no value.`,
			},
			doc: &Document{
				Type:       NodeDoc,
				Name:       "test",
				ParentName: "root",
				Short:      "Short Test node",
				Long:       "Also has a long description. But no value.",
			},
		},
		{
			s: []string{
				`@Node version`,
				`@Parent root`,
				``,
				`Short version description`,
				``,
				`@Value string`,
			},
			doc: &Document{
				Type:       NodeDoc,
				Name:       "version",
				ParentName: "root",
				Short:      "Short version description",
				ValueType:  "string",
			},
		},
	}

	for i, tt := range tests {
		parser := NewParser()
		err := parser.Parse(tt.s)
		if err != nil {
			t.Errorf("%d. Unexpected error %q", i, err)
		}
		doc, err := parser.Build()
		if err != nil {
			t.Errorf("%d. Unexpected error %q", i, err)
		}
		d := doc.Children[0]

		var es string
		if err == nil {
			es = ""
		} else {
			es = err.Error()
		}

		if d.Type != tt.doc.Type {
			t.Errorf("%d. Doc Types do not match: exp=%q got=%q", i, tt.doc.Type, d.Type)
		}

		if d.Is != tt.doc.Is {
			t.Errorf("%d. Doc IsType does not match: exp=%q got=%q", i, tt.doc.Is, d.Is)
		}

		if d.ParentName != tt.doc.ParentName {
			t.Errorf("%d. Doc Parent does not match: exp=%q got=%q", i, tt.doc.ParentName, d.ParentName)
		}

		if d.Short != tt.doc.Short {
			t.Errorf("%d. Short Description does not match:\n  exp=%q\n  got=%q\n", i, tt.doc.Short, d.Short)
		}

		if d.Long != tt.doc.Long {
			t.Errorf("%d. Long Description does not match:\n  exp=%q\n  got=%q\n", i, tt.doc.Long, d.Long)
		}

		if len(tt.doc.Params) != 0 {
			if len(tt.doc.Params) != len(d.Params) {
				t.Errorf("%d. Unequal Parameter count: exp=%d got=%d", i, len(tt.doc.Params), len(d.Params))
			}
			for j, p := range tt.doc.Params {
				tp := d.Params[j]
				if p.Type != tp.Type {
					t.Errorf("%d. Param %d. Param type mismatch: exp=%q got=%q", i, j, p.Type, tp.Type)
				}
				if p.Name != tp.Name {
					t.Errorf("%d. Param %d. Param name mismatch: exp=%q got=%q", i, j, p.Name, tp.Name)
				}
				if p.Description != tp.Description {
					t.Errorf("%d. Param %d. Param description mismatch:\n  exp=%q\n  got=%q", i, j, p.Description, tp.Description)
				}
			}
		} else if len(d.Params) != 0 {
			t.Errorf("%d. Expected 0 parameters, found=%d", i, len(d.Params))
		}

		if d.Return != tt.doc.Return {
			t.Errorf("%d. Return type does not match: exp=%q got=%q", i, tt.doc.Return, d.Return)
		}

		if len(tt.doc.Columns) != 0 {
			if len(tt.doc.Columns) != len(d.Columns) {
				t.Errorf("%d. Unequal Columns count. exp=%d got=%d", i, len(tt.doc.Columns), len(d.Columns))
			}
			for j, p := range tt.doc.Columns {
				tp := d.Columns[i]
				if p.Type != tp.Type {
					t.Errorf("%d. Column %d. Column type mismatch: exp=%q got=%q", i, j, p.Type, tp.Type)
				}
				if p.Name != tp.Name {
					t.Errorf("%d. Column %d. Column name mismatch: exp=%q got=%q", i, j, p.Name, tp.Name)
				}
				if p.Description != tp.Description {
					t.Errorf("%d. Column %d. Column description mismatch:\n  exp=%q\n  got=%q", i, j, p.Description, tp.Description)
				}
			}
		} else if len(d.Columns) != 0 {
			t.Errorf("%d. Expect 0 columns, found=%d", i, len(d.Columns))
		}

		if d.ValueType != tt.doc.ValueType {
			t.Errorf("%d. Value type does not match: exp=%q got=%q", i, tt.doc.ValueType, d.ValueType)
		}

		if es != tt.err {
			t.Errorf("%d. Error mismatch:\n  exp=%q\n  got=%q\n", i, tt.err, es)
		}
	}
}
<commit_msg>Add more tests for Parser.Build<commit_after>package parser

import (
	"testing"
)

func TestParser_Parse(t *testing.T) {
	var tests = []struct {
		s   []string
		doc *Document
		err string
	}{
		{
			s: []string{
				`@Command Add_Device`,
				`@Is addDevice`,
				`@Parent root`,
				``,
				`Adds a Device to the link`,
				``,
				`This is a long description.`,
				`It really doesn't contain anything special.`,
				`But it is multiline`,
				``,
				`@Param deviceName string Name of the device to add. It will`,
				`appear as a node on the root of the link.`,
				`@Param username string The Username to access the device.`,
				``,
				`@Return value`,
				`@Column success bool Returns true on success. False otherwise.`,
			},
			doc: &Document{
				Type:       ActionDoc,
				Name:       "Add_Device",
				Is:         "addDevice",
				ParentName: "root",
				Short:      "Adds a Device to the link",
				Long:       "This is a long description. It really doesn't contain anything special. But it is multiline",
				Params: []*Parameter{
					{
						Name:        "deviceName",
						Type:        "string",
						Description: "Name of the device to add. It will appear as a node on the root of the link.",
					},
					{
						Name:        "username",
						Type:        "string",
						Description: "The Username to access the device.",
					},
				},
				Return: "value",
				Columns: []*Parameter{
					{
						Name:        "success",
						Type:        "bool",
						Description: "Returns true on success. False otherwise.",
					},
				},
			},
		},
		{
			s: []string{
				`@Node`,
				`@MetaType test`,
				`@Parent root`,
				``,
				`Short Test node`,
				``,
				`Also has a long description. But no value.`,
			},
			doc: &Document{
				Type:       NodeDoc,
				Name:       "test",
				ParentName: "root",
				Short:      "Short Test node",
				Long:       "Also has a long description. But no value.",
			},
		},
		{
			s: []string{
				`@Node version`,
				`@Parent root`,
				``,
				`Short version description`,
				``,
				`@Value string`,
			},
			doc: &Document{
				Type:       NodeDoc,
				Name:       "version",
				ParentName: "root",
				Short:      "Short version description",
				ValueType:  "string",
			},
		},
	}

	for i, tt := range tests {
		parser := NewParser()
		err := parser.Parse(tt.s)
		if err != nil {
			t.Errorf("%d. Unexpected error %q", i, err)
		}
		doc, err := parser.Build()
		if err != nil {
			t.Errorf("%d. Unexpected error %q", i, err)
		}
		d := doc.Children[0]

		var es string
		if err == nil {
			es = ""
		} else {
			es = err.Error()
		}

		if d.Type != tt.doc.Type {
			t.Errorf("%d. Doc Types do not match: exp=%q got=%q", i, tt.doc.Type, d.Type)
		}

		if d.Is != tt.doc.Is {
			t.Errorf("%d. Doc IsType does not match: exp=%q got=%q", i, tt.doc.Is, d.Is)
		}

		if d.ParentName != tt.doc.ParentName {
			t.Errorf("%d. Doc Parent does not match: exp=%q got=%q", i, tt.doc.ParentName, d.ParentName)
		}

		if d.Short != tt.doc.Short {
			t.Errorf("%d. Short Description does not match:\n  exp=%q\n  got=%q\n", i, tt.doc.Short, d.Short)
		}

		if d.Long != tt.doc.Long {
			t.Errorf("%d. Long Description does not match:\n  exp=%q\n  got=%q\n", i, tt.doc.Long, d.Long)
		}

		if len(tt.doc.Params) != 0 {
			if len(tt.doc.Params) != len(d.Params) {
				t.Errorf("%d. Unequal Parameter count: exp=%d got=%d", i, len(tt.doc.Params), len(d.Params))
			}
			for j, p := range tt.doc.Params {
				tp := d.Params[j]
				if p.Type != tp.Type {
					t.Errorf("%d. Param %d. Param type mismatch: exp=%q got=%q", i, j, p.Type, tp.Type)
				}
				if p.Name != tp.Name {
					t.Errorf("%d. Param %d. Param name mismatch: exp=%q got=%q", i, j, p.Name, tp.Name)
				}
				if p.Description != tp.Description {
					t.Errorf("%d. Param %d. Param description mismatch:\n  exp=%q\n  got=%q", i, j, p.Description, tp.Description)
				}
			}
		} else if len(d.Params) != 0 {
			t.Errorf("%d. Expected 0 parameters, found=%d", i, len(d.Params))
		}

		if d.Return != tt.doc.Return {
			t.Errorf("%d. Return type does not match: exp=%q got=%q", i, tt.doc.Return, d.Return)
		}

		if len(tt.doc.Columns) != 0 {
			if len(tt.doc.Columns) != len(d.Columns) {
				t.Errorf("%d. Unequal Columns count. exp=%d got=%d", i, len(tt.doc.Columns), len(d.Columns))
			}
			for j, p := range tt.doc.Columns {
				tp := d.Columns[i]
				if p.Type != tp.Type {
					t.Errorf("%d. Column %d. Column type mismatch: exp=%q got=%q", i, j, p.Type, tp.Type)
				}
				if p.Name != tp.Name {
					t.Errorf("%d. Column %d. Column name mismatch: exp=%q got=%q", i, j, p.Name, tp.Name)
				}
				if p.Description != tp.Description {
					t.Errorf("%d. Column %d. Column description mismatch:\n  exp=%q\n  got=%q", i, j, p.Description, tp.Description)
				}
			}
		} else if len(d.Columns) != 0 {
			t.Errorf("%d. Expect 0 columns, found=%d", i, len(d.Columns))
		}

		if d.ValueType != tt.doc.ValueType {
			t.Errorf("%d. Value type does not match: exp=%q got=%q", i, tt.doc.ValueType, d.ValueType)
		}

		if es != tt.err {
			t.Errorf("%d. Error mismatch:\n  exp=%q\n  got=%q\n", i, tt.err, es)
		}
	}
}

type testStruct struct {
	n string
	s []string
	d *Document
}

func TestParser_Build(t *testing.T) {
	var tests = []testStruct{
		{
			n: "Test",
			s: []string{
				`@Node Test`,
				`@Is testNode`,
				`@Parent root`,
				``,
				`Short test`,
			},
			d: &Document{
				Name:       "Test",
				Is:         "testNode",
				ParentName: "root",
				Short:      "Short test",
			},
		},
		{
			n: "Test2",
			s: []string{
				`@Node Test2`,
				`@Is test2Node`,
				`@Parent root`,
				``,
				`Short test 2nd`,
			},
			d: &Document{
				Name:       "Test2",
				Is:         "test2Node",
				ParentName: "root",
				Short:      "Short test 2nd",
			},
		},
	}

	p := NewParser()
	for i, tt := range tests {
		err := p.Parse(tt.s)
		if err != nil {
			t.Errorf("%d. Unexpected error parsing: %q", i, err)
		}
	}

	docs, err := p.Build()
	if err != nil {
		t.Errorf("Unexpected build error %q", err)
	}

	i := 0
	d := docs.Children[i]
	for d != nil {
		tt := getTest(tests, d.Name)
		if tt.Name != d.Name {
			t.Errorf("Name %q does not match %q", tt.Name, d.Name)
		}
		if tt.ParentName != d.Parent.Name {
			t.Errorf("Parent Name %q does not match %q", tt.ParentName, d.Parent.Name)
		}
		i++
		if i >= len(docs.Children) {
			d = nil
		} else {
			d = docs.Children[i]
		}
	}
}

func getTest(tests []testStruct, name string) *Document {
	for _, tt := range tests {
		if tt.n == name {
			return tt.d
		}
	}
	return nil
}
<|endoftext|>


<commit_before>package handlers

import (
	"github.com/go-openapi/runtime/middleware"
	coreclusters "github.com/radanalyticsio/oshinko-core/clusters"
	osa "github.com/radanalyticsio/oshinko-rest/helpers/authentication"
	oe "github.com/radanalyticsio/oshinko-rest/helpers/errors"
	"github.com/radanalyticsio/oshinko-rest/helpers/info"
	"github.com/radanalyticsio/oshinko-rest/models"
	apiclusters "github.com/radanalyticsio/oshinko-rest/restapi/operations/clusters"
	"strings"
)

const nameSpaceMsg = "cannot determine target openshift namespace"
const clientMsg = "unable to create an openshift client"

var codes map[int]int32 = map[int]int32{
	coreclusters.NoCodeAvailable: 500,
	coreclusters.ClusterConfigCode: 409,
	coreclusters.ClientOperationCode: 500,
	coreclusters.ClusterIncompleteCode: 409,
	coreclusters.NoSuchClusterCode: 404,
	coreclusters.ComponentExistsCode: 409,
}

func generalErr(err error, title, msg string, code int32) *models.ErrorResponse {
	if err != nil {
		if msg != "" {
			msg += ", reason: "
		}
		msg += err.Error()
	}
	return oe.NewSingleErrorResponse(code, title, msg)
}

func tostrptr(val string) *string {
	v := val
	return &v
}

func getErrorCode(err error) int32 {

	code := coreclusters.ErrorCode(err)
	if httpcode, ok := codes[code]; ok {
		return httpcode
	}
	return 500

}

func singleClusterResponse(sc coreclusters.SparkCluster) *models.SingleCluster {

	addpod := func(p coreclusters.SparkPod) *models.ClusterModelPodsItems0 {
		pod := new(models.ClusterModelPodsItems0)
		pod.IP = tostrptr(p.IP)
		pod.Status = tostrptr(p.Status)
		pod.Type = tostrptr(p.Type)
		return pod
	}

	// Build the response
	cluster := &models.SingleCluster{&models.ClusterModel{}}
	cluster.Cluster.Name = tostrptr(sc.Name)
	cluster.Cluster.MasterURL = tostrptr(sc.MasterURL)
	cluster.Cluster.MasterWebURL = tostrptr(sc.MasterWebURL)

	cluster.Cluster.Status = tostrptr(sc.Status)

	cluster.Cluster.Pods = []*models.ClusterModelPodsItems0{}
	for i := range sc.Pods {
		cluster.Cluster.Pods = append(cluster.Cluster.Pods, addpod(sc.Pods[i]))
	}

	cluster.Cluster.Config = &models.NewClusterConfig{
		SparkMasterConfig: sc.Config.SparkMasterConfig,
		SparkWorkerConfig: sc.Config.SparkWorkerConfig,
		MasterCount: int64(sc.Config.MasterCount),
		WorkerCount: int64(sc.Config.WorkerCount),
		Name: sc.Config.Name,
	}
	return cluster
}

func assignConfig(config *models.NewClusterConfig) *coreclusters.ClusterConfig {
	if config == nil {
		return nil
	}
	result := &coreclusters.ClusterConfig{
		Name: config.Name,
		MasterCount: int(config.MasterCount),
		WorkerCount: int(config.WorkerCount),
		SparkMasterConfig: config.SparkMasterConfig,
		SparkWorkerConfig: config.SparkWorkerConfig,
	}
	return result
}

// CreateClusterResponse create a cluster and return the representation
func CreateClusterResponse(params apiclusters.CreateClusterParams) middleware.Responder {

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.CreateClusterDefault {
		return apiclusters.NewCreateClusterDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for create failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cannot create cluster", msg, code)
	}

	const imageMsg = "cannot determine name of spark image"

	clustername := *params.Cluster.Name

	namespace, err := info.GetNamespace()
	if namespace == "" || err != nil {
		return reterr(fail(err, nameSpaceMsg, 500))
	}

	image, err := info.GetSparkImage()
	if image == "" || err != nil {
		return reterr(fail(err, imageMsg, 500))
	}

	client, err := osa.GetKubeClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	osclient, err := osa.GetOpenShiftClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	config := assignConfig(params.Cluster.Config)
	sc, err := coreclusters.CreateCluster(clustername, namespace, image, config, osclient, client)
	if err != nil {
		return reterr(fail(err, "", getErrorCode(err)))
	}
	return apiclusters.NewCreateClusterCreated().WithLocation(sc.Href).WithPayload(singleClusterResponse(sc))
}

// DeleteClusterResponse delete a cluster
func DeleteClusterResponse(params apiclusters.DeleteSingleClusterParams) middleware.Responder {

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.DeleteSingleClusterDefault {
		return apiclusters.NewDeleteSingleClusterDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for delete failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cluster deletion failed", msg, code)
	}

	namespace, err := info.GetNamespace()
	if namespace == "" || err != nil {
		return reterr(fail(err, nameSpaceMsg, 500))
	}

	osclient, err := osa.GetOpenShiftClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	client, err := osa.GetKubeClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	info, err := coreclusters.DeleteCluster(params.Name, namespace, osclient, client)
	if err != nil {
		return reterr(fail(err, "", getErrorCode(err)))
	}
	if info != "" {
		return reterr(fail(nil, "deletion may be incomplete: " + info, 500))
	}
	return apiclusters.NewDeleteSingleClusterNoContent()
}

// FindClustersResponse find a cluster and return its representation
func FindClustersResponse(params apiclusters.FindClustersParams) middleware.Responder {

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.FindClustersDefault {
		return apiclusters.NewFindClustersDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for list failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cannot list clusters", msg, code)
	}

	namespace, err := info.GetNamespace()
	if namespace == "" || err != nil {
		return reterr(fail(err, nameSpaceMsg, 500))
	}

	client, err := osa.GetKubeClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}
	scs, err := coreclusters.FindClusters(namespace, client)
	if err != nil {
		return reterr(fail(err, "", getErrorCode(err)))
	}

	// Create the payload that we're going to write into for the response
	payload := apiclusters.FindClustersOKBodyBody{}
	payload.Clusters = []*apiclusters.ClustersItems0{}
	for idx := range(scs) {
		clt := new(apiclusters.ClustersItems0)
		clt.Href = &scs[idx].Href
		clt.MasterURL = &scs[idx].MasterURL
		clt.MasterWebURL = &scs[idx].MasterWebURL
		clt.Name = &scs[idx].Name
		clt.Status = &scs[idx].Status
		wc := int64(scs[idx].WorkerCount)
		clt.WorkerCount = &wc
		payload.Clusters = append(payload.Clusters, clt)
	}

	return apiclusters.NewFindClustersOK().WithPayload(payload)
}

// FindSingleClusterResponse find a cluster and return its representation
func FindSingleClusterResponse(params apiclusters.FindSingleClusterParams) middleware.Responder {

	clustername := params.Name

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.FindSingleClusterDefault {
		return apiclusters.NewFindSingleClusterDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for get failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cannot get cluster", msg, code)
	}

	namespace, err := info.GetNamespace()
	if namespace == "" || err != nil {
		return reterr(fail(err, nameSpaceMsg, 500))
	}

	osclient, err := osa.GetOpenShiftClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	client, err := osa.GetKubeClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	sc, err := coreclusters.FindSingleCluster(clustername, namespace, osclient, client)
	if err != nil {
		return reterr(fail(err, "", getErrorCode(err)))
	}

	return apiclusters.NewFindSingleClusterOK().WithPayload(singleClusterResponse(sc))
}

// UpdateSingleClusterResponse update a cluster and return the new representation
func UpdateSingleClusterResponse(params apiclusters.UpdateSingleClusterParams) middleware.Responder {

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.UpdateSingleClusterDefault {
		return apiclusters.NewUpdateSingleClusterDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for update failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cannot update cluster", msg, code)
	}

	const clusterNameMsg = "changing the cluster name is not supported"

	clustername := params.Name

	// Before we do further checks, make sure that we have deploymentconfigs
	// If either the master or the worker deploymentconfig are missing, we
	// assume that the cluster is missing. These are the base objects that
	// we use to create a cluster
	namespace, err := info.GetNamespace()
	if namespace == "" || err != nil {
		return reterr(fail(err, nameSpaceMsg, 500))
	}

	osclient, err := osa.GetOpenShiftClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	client, err := osa.GetKubeClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	// Simple things first. At this time we do not support cluster name change and
	// we do not suppport scaling the master count (likely need HA setup for that to make sense)
	if clustername != *params.Cluster.Name {
		return reterr(fail(nil, clusterNameMsg, 409))
	}

	config := assignConfig(params.Cluster.Config)
	sc, err := coreclusters.UpdateCluster(clustername, namespace, config, osclient, client)
	if err != nil {
		return reterr(fail(err, "", getErrorCode(err)))
	}
	return apiclusters.NewUpdateSingleClusterAccepted().WithPayload(singleClusterResponse(sc))
}
<commit_msg>Remove unused import<commit_after>package handlers

import (
	"github.com/go-openapi/runtime/middleware"
	coreclusters "github.com/radanalyticsio/oshinko-core/clusters"
	osa "github.com/radanalyticsio/oshinko-rest/helpers/authentication"
	oe "github.com/radanalyticsio/oshinko-rest/helpers/errors"
	"github.com/radanalyticsio/oshinko-rest/helpers/info"
	"github.com/radanalyticsio/oshinko-rest/models"
	apiclusters "github.com/radanalyticsio/oshinko-rest/restapi/operations/clusters"
)

const nameSpaceMsg = "cannot determine target openshift namespace"
const clientMsg = "unable to create an openshift client"

var codes map[int]int32 = map[int]int32{
	coreclusters.NoCodeAvailable: 500,
	coreclusters.ClusterConfigCode: 409,
	coreclusters.ClientOperationCode: 500,
	coreclusters.ClusterIncompleteCode: 409,
	coreclusters.NoSuchClusterCode: 404,
	coreclusters.ComponentExistsCode: 409,
}

func generalErr(err error, title, msg string, code int32) *models.ErrorResponse {
	if err != nil {
		if msg != "" {
			msg += ", reason: "
		}
		msg += err.Error()
	}
	return oe.NewSingleErrorResponse(code, title, msg)
}

func tostrptr(val string) *string {
	v := val
	return &v
}

func getErrorCode(err error) int32 {

	code := coreclusters.ErrorCode(err)
	if httpcode, ok := codes[code]; ok {
		return httpcode
	}
	return 500

}

func singleClusterResponse(sc coreclusters.SparkCluster) *models.SingleCluster {

	addpod := func(p coreclusters.SparkPod) *models.ClusterModelPodsItems0 {
		pod := new(models.ClusterModelPodsItems0)
		pod.IP = tostrptr(p.IP)
		pod.Status = tostrptr(p.Status)
		pod.Type = tostrptr(p.Type)
		return pod
	}

	// Build the response
	cluster := &models.SingleCluster{&models.ClusterModel{}}
	cluster.Cluster.Name = tostrptr(sc.Name)
	cluster.Cluster.MasterURL = tostrptr(sc.MasterURL)
	cluster.Cluster.MasterWebURL = tostrptr(sc.MasterWebURL)

	cluster.Cluster.Status = tostrptr(sc.Status)

	cluster.Cluster.Pods = []*models.ClusterModelPodsItems0{}
	for i := range sc.Pods {
		cluster.Cluster.Pods = append(cluster.Cluster.Pods, addpod(sc.Pods[i]))
	}

	cluster.Cluster.Config = &models.NewClusterConfig{
		SparkMasterConfig: sc.Config.SparkMasterConfig,
		SparkWorkerConfig: sc.Config.SparkWorkerConfig,
		MasterCount: int64(sc.Config.MasterCount),
		WorkerCount: int64(sc.Config.WorkerCount),
		Name: sc.Config.Name,
	}
	return cluster
}

func assignConfig(config *models.NewClusterConfig) *coreclusters.ClusterConfig {
	if config == nil {
		return nil
	}
	result := &coreclusters.ClusterConfig{
		Name: config.Name,
		MasterCount: int(config.MasterCount),
		WorkerCount: int(config.WorkerCount),
		SparkMasterConfig: config.SparkMasterConfig,
		SparkWorkerConfig: config.SparkWorkerConfig,
	}
	return result
}

// CreateClusterResponse create a cluster and return the representation
func CreateClusterResponse(params apiclusters.CreateClusterParams) middleware.Responder {

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.CreateClusterDefault {
		return apiclusters.NewCreateClusterDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for create failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cannot create cluster", msg, code)
	}

	const imageMsg = "cannot determine name of spark image"

	clustername := *params.Cluster.Name

	namespace, err := info.GetNamespace()
	if namespace == "" || err != nil {
		return reterr(fail(err, nameSpaceMsg, 500))
	}

	image, err := info.GetSparkImage()
	if image == "" || err != nil {
		return reterr(fail(err, imageMsg, 500))
	}

	client, err := osa.GetKubeClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	osclient, err := osa.GetOpenShiftClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	config := assignConfig(params.Cluster.Config)
	sc, err := coreclusters.CreateCluster(clustername, namespace, image, config, osclient, client)
	if err != nil {
		return reterr(fail(err, "", getErrorCode(err)))
	}
	return apiclusters.NewCreateClusterCreated().WithLocation(sc.Href).WithPayload(singleClusterResponse(sc))
}

// DeleteClusterResponse delete a cluster
func DeleteClusterResponse(params apiclusters.DeleteSingleClusterParams) middleware.Responder {

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.DeleteSingleClusterDefault {
		return apiclusters.NewDeleteSingleClusterDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for delete failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cluster deletion failed", msg, code)
	}

	namespace, err := info.GetNamespace()
	if namespace == "" || err != nil {
		return reterr(fail(err, nameSpaceMsg, 500))
	}

	osclient, err := osa.GetOpenShiftClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	client, err := osa.GetKubeClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	info, err := coreclusters.DeleteCluster(params.Name, namespace, osclient, client)
	if err != nil {
		return reterr(fail(err, "", getErrorCode(err)))
	}
	if info != "" {
		return reterr(fail(nil, "deletion may be incomplete: " + info, 500))
	}
	return apiclusters.NewDeleteSingleClusterNoContent()
}

// FindClustersResponse find a cluster and return its representation
func FindClustersResponse(params apiclusters.FindClustersParams) middleware.Responder {

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.FindClustersDefault {
		return apiclusters.NewFindClustersDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for list failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cannot list clusters", msg, code)
	}

	namespace, err := info.GetNamespace()
	if namespace == "" || err != nil {
		return reterr(fail(err, nameSpaceMsg, 500))
	}

	client, err := osa.GetKubeClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}
	scs, err := coreclusters.FindClusters(namespace, client)
	if err != nil {
		return reterr(fail(err, "", getErrorCode(err)))
	}

	// Create the payload that we're going to write into for the response
	payload := apiclusters.FindClustersOKBodyBody{}
	payload.Clusters = []*apiclusters.ClustersItems0{}
	for idx := range(scs) {
		clt := new(apiclusters.ClustersItems0)
		clt.Href = &scs[idx].Href
		clt.MasterURL = &scs[idx].MasterURL
		clt.MasterWebURL = &scs[idx].MasterWebURL
		clt.Name = &scs[idx].Name
		clt.Status = &scs[idx].Status
		wc := int64(scs[idx].WorkerCount)
		clt.WorkerCount = &wc
		payload.Clusters = append(payload.Clusters, clt)
	}

	return apiclusters.NewFindClustersOK().WithPayload(payload)
}

// FindSingleClusterResponse find a cluster and return its representation
func FindSingleClusterResponse(params apiclusters.FindSingleClusterParams) middleware.Responder {

	clustername := params.Name

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.FindSingleClusterDefault {
		return apiclusters.NewFindSingleClusterDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for get failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cannot get cluster", msg, code)
	}

	namespace, err := info.GetNamespace()
	if namespace == "" || err != nil {
		return reterr(fail(err, nameSpaceMsg, 500))
	}

	osclient, err := osa.GetOpenShiftClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	client, err := osa.GetKubeClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	sc, err := coreclusters.FindSingleCluster(clustername, namespace, osclient, client)
	if err != nil {
		return reterr(fail(err, "", getErrorCode(err)))
	}

	return apiclusters.NewFindSingleClusterOK().WithPayload(singleClusterResponse(sc))
}

// UpdateSingleClusterResponse update a cluster and return the new representation
func UpdateSingleClusterResponse(params apiclusters.UpdateSingleClusterParams) middleware.Responder {

	// Do this so that we only have to specify the error code when we build ErrorResponse
	reterr := func(err *models.ErrorResponse) *apiclusters.UpdateSingleClusterDefault {
		return apiclusters.NewUpdateSingleClusterDefault(int(*err.Errors[0].Status)).WithPayload(err)
	}

	// Convenience wrapper for update failure
	fail := func(err error, msg string, code int32) *models.ErrorResponse {
		return generalErr(err, "cannot update cluster", msg, code)
	}

	const clusterNameMsg = "changing the cluster name is not supported"

	clustername := params.Name

	// Before we do further checks, make sure that we have deploymentconfigs
	// If either the master or the worker deploymentconfig are missing, we
	// assume that the cluster is missing. These are the base objects that
	// we use to create a cluster
	namespace, err := info.GetNamespace()
	if namespace == "" || err != nil {
		return reterr(fail(err, nameSpaceMsg, 500))
	}

	osclient, err := osa.GetOpenShiftClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	client, err := osa.GetKubeClient()
	if err != nil {
		return reterr(fail(err, clientMsg, 500))
	}

	// Simple things first. At this time we do not support cluster name change and
	// we do not suppport scaling the master count (likely need HA setup for that to make sense)
	if clustername != *params.Cluster.Name {
		return reterr(fail(nil, clusterNameMsg, 409))
	}

	config := assignConfig(params.Cluster.Config)
	sc, err := coreclusters.UpdateCluster(clustername, namespace, config, osclient, client)
	if err != nil {
		return reterr(fail(err, "", getErrorCode(err)))
	}
	return apiclusters.NewUpdateSingleClusterAccepted().WithPayload(singleClusterResponse(sc))
}
<|endoftext|>


<commit_before>package handlers

import (
	"strconv"

	"gopkg.in/alexcesaro/statsd.v2"
	"gopkg.in/gin-contrib/cors.v1"
	"gopkg.in/gin-gonic/gin.v1"

	"github.com/ghmeier/bloodlines/gateways"
	coi "github.com/ghmeier/coinage/gateways"
	t "github.com/jakelong95/TownCenter/gateways"
	w "github.com/lcollin/warehouse/gateways"
	cov "github.com/yuderekyu/covenant/gateways"
)

/*BaseHandler contains wrapper methods that all handlers need and should use
  for consistency across services*/
type BaseHandler struct {
	Stats *statsd.Client
}

/*GatewayContext contains references to each type of gateway used for simple
  use in handler construction*/
type GatewayContext struct {
	Sql        gateways.SQL
	Sendgrid   gateways.SendgridI
	TownCenter t.TownCenterI
	Covenant   cov.Covenant
	Warehouse  w.Warehouse
	Bloodlines gateways.Bloodlines
	Coinage    coi.Coinage
	Rabbit     gateways.RabbitI
	Stats      *statsd.Client
	Stripe     coi.Stripe
}

/*NewBaseHandler returns a new BaseHandler instance from a given stats*/
func NewBaseHandler(stats *statsd.Client) *BaseHandler {
	return &BaseHandler{Stats: stats}
}

/*GetPaging returns the offset and limit parameters from a gin request context
defaults to offset=0 and limit=20*/
func (b *BaseHandler) GetPaging(ctx *gin.Context) (int, int) {
	offset, _ := strconv.Atoi(ctx.DefaultQuery("offset", "0"))
	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "20"))
	return offset, limit
}

/*UserError sends a 400 response with the given message string and error object*/
func (b *BaseHandler) UserError(ctx *gin.Context, msg string, obj interface{}) {
	if b.Stats != nil {
		b.Stats.Increment("400")
	}
	b.send(ctx, 400, &gin.H{"success": false, "message": msg, "data": obj})
}

/*NotFoundError sends a 404 response and false success when a resource is not present*/
func (b *BaseHandler) NotFoundError(ctx *gin.Context, msg string) {
	if b.Stats != nil {
		b.Stats.Increment("404")
	}
	b.send(ctx, 404, &gin.H{"success": false, "message": msg})
}

/*Unauthorized sends a 401 response along with a message*/
func (b *BaseHandler) Unauthorized(ctx *gin.Context, msg string) {
	if b.Stats != nil {
		b.Stats.Increment("401")
	}
	b.send(ctx, 401, &gin.H{"success": false, "message": msg})
}

/*ServerError sends a 500 response with the given error and object*/
func (b *BaseHandler) ServerError(ctx *gin.Context, err error, obj interface{}) {
	if b.Stats != nil {
		b.Stats.Increment("500")
	}
	b.send(ctx, 500, &gin.H{"success": false, "message": err.Error(), "data": obj})
}

/*Success sends a 200 response with the given object*/
func (b *BaseHandler) Success(ctx *gin.Context, obj interface{}) {
	if b.Stats != nil {
		b.Stats.Increment("200")
	}
	b.send(ctx, 200, &gin.H{"success": true, "data": obj})
}

func (b *BaseHandler) send(ctx *gin.Context, status int, json *gin.H) {
	ctx.JSON(status, json)
}

/*Time sets up gin middleware for sending timing stats*/
func (b *BaseHandler) Time() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer b.Stats.NewTiming().Send(c.Request.Method)
		c.Next()
	}
}

/*GetCors returns a gin handlerFunc for CORS reuquests in expresso services */
func GetCors() gin.HandlerFunc {
	config := cors.DefaultConfig()
	config.AddAllowMethods("DELETE")
	config.AllowAllOrigins = true
	return cors.New(config)
}
<commit_msg>Check for nil stats<commit_after>package handlers

import (
	"strconv"

	"gopkg.in/alexcesaro/statsd.v2"
	"gopkg.in/gin-contrib/cors.v1"
	"gopkg.in/gin-gonic/gin.v1"

	"github.com/ghmeier/bloodlines/gateways"
	coi "github.com/ghmeier/coinage/gateways"
	t "github.com/jakelong95/TownCenter/gateways"
	w "github.com/lcollin/warehouse/gateways"
	cov "github.com/yuderekyu/covenant/gateways"
)

/*BaseHandler contains wrapper methods that all handlers need and should use
  for consistency across services*/
type BaseHandler struct {
	Stats *statsd.Client
}

/*GatewayContext contains references to each type of gateway used for simple
  use in handler construction*/
type GatewayContext struct {
	Sql        gateways.SQL
	Sendgrid   gateways.SendgridI
	TownCenter t.TownCenterI
	Covenant   cov.Covenant
	Warehouse  w.Warehouse
	Bloodlines gateways.Bloodlines
	Coinage    coi.Coinage
	Rabbit     gateways.RabbitI
	Stats      *statsd.Client
	Stripe     coi.Stripe
}

/*NewBaseHandler returns a new BaseHandler instance from a given stats*/
func NewBaseHandler(stats *statsd.Client) *BaseHandler {
	return &BaseHandler{Stats: stats}
}

/*GetPaging returns the offset and limit parameters from a gin request context
defaults to offset=0 and limit=20*/
func (b *BaseHandler) GetPaging(ctx *gin.Context) (int, int) {
	offset, _ := strconv.Atoi(ctx.DefaultQuery("offset", "0"))
	limit, _ := strconv.Atoi(ctx.DefaultQuery("limit", "20"))
	return offset, limit
}

/*UserError sends a 400 response with the given message string and error object*/
func (b *BaseHandler) UserError(ctx *gin.Context, msg string, obj interface{}) {
	if b.Stats != nil {
		b.Stats.Increment("400")
	}
	b.send(ctx, 400, &gin.H{"success": false, "message": msg, "data": obj})
}

/*NotFoundError sends a 404 response and false success when a resource is not present*/
func (b *BaseHandler) NotFoundError(ctx *gin.Context, msg string) {
	if b.Stats != nil {
		b.Stats.Increment("404")
	}
	b.send(ctx, 404, &gin.H{"success": false, "message": msg})
}

/*Unauthorized sends a 401 response along with a message*/
func (b *BaseHandler) Unauthorized(ctx *gin.Context, msg string) {
	if b.Stats != nil {
		b.Stats.Increment("401")
	}
	b.send(ctx, 401, &gin.H{"success": false, "message": msg})
}

/*ServerError sends a 500 response with the given error and object*/
func (b *BaseHandler) ServerError(ctx *gin.Context, err error, obj interface{}) {
	if b.Stats != nil {
		b.Stats.Increment("500")
	}
	b.send(ctx, 500, &gin.H{"success": false, "message": err.Error(), "data": obj})
}

/*Success sends a 200 response with the given object*/
func (b *BaseHandler) Success(ctx *gin.Context, obj interface{}) {
	if b.Stats != nil {
		b.Stats.Increment("200")
	}
	b.send(ctx, 200, &gin.H{"success": true, "data": obj})
}

func (b *BaseHandler) send(ctx *gin.Context, status int, json *gin.H) {
	ctx.JSON(status, json)
}

/*Time sets up gin middleware for sending timing stats*/
func (b *BaseHandler) Time() gin.HandlerFunc {
	return func(c *gin.Context) {
		if b.Stats != nil {
			defer b.Stats.NewTiming().Send(c.Request.Method)
		}
		c.Next()
	}
}

/*GetCors returns a gin handlerFunc for CORS reuquests in expresso services */
func GetCors() gin.HandlerFunc {
	config := cors.DefaultConfig()
	config.AddAllowMethods("DELETE")
	config.AllowAllOrigins = true
	return cors.New(config)
}
<|endoftext|>


<commit_before>package handlers

import (
	"net/http"
)

import (
	"github.com/ajm188/gwiz/db"
)

type ConnectionHandlerFunc func(db.Connection, http.ResponseWriter, *http.Request)

func WithConnectionFunc(handlerFunc ConnectionHandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		conn, err := db.NewConnection(nil)
		if err != nil {
			http.NotFoundHandler().ServeHTTP(w, r)
			return
		}
		defer conn.Close()
		handlerFunc(conn, w, r)
	}
}
<commit_msg>Adds two more wrappers for our new request type<commit_after>package handlers

import (
	"net/http"
)

import (
	"github.com/ajm188/gwiz/db"
)

type RequestFunc func(*Request)

func WithRequest(f RequestFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		req := Request{
			w,
			r,
			nil,
		}
		f(&req)
	}
}

func WithConnection(f RequestFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		req := Request{
			w,
			r,
			nil,
		}
		conn, err := db.NewConnection(nil)
		if err != nil {
			req.Error(500, err)
			return
		}
		defer conn.Close()
		req.Connection = conn
		f(&req)
	}
}

type ConnectionHandlerFunc func(db.Connection, http.ResponseWriter, *http.Request)

func WithConnectionFunc(handlerFunc ConnectionHandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		conn, err := db.NewConnection(nil)
		if err != nil {
			http.NotFoundHandler().ServeHTTP(w, r)
			return
		}
		defer conn.Close()
		handlerFunc(conn, w, r)
	}
}
<|endoftext|>


<commit_before>package hystrix

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
	"time"

	. "github.com/smartystreets/goconvey/convey"
)

type eventStreamTestServer struct {
	*httptest.Server
	eventStreamer
}

type eventStreamer interface {
	Stop()
}

func (s *eventStreamTestServer) stopTestServer() error {
	s.Close()
	s.Stop()
	Flush()

	return nil
}

func startTestServer() *eventStreamTestServer {
	hystrixStreamHandler := NewStreamHandler()
	hystrixStreamHandler.Start()
	return &eventStreamTestServer{
		httptest.NewServer(hystrixStreamHandler),
		hystrixStreamHandler,
	}
}

func sleepingCommand(t *testing.T, name string, duration time.Duration) {
	done := make(chan bool)
	errChan := Go(name, func() error {
		time.Sleep(duration)
		done <- true
		return nil
	}, nil)

	select {
	case _ = <-done:
		// do nothing
	case err := <-errChan:
		t.Fatal(err)
	}
}

func failingCommand(t *testing.T, name string, duration time.Duration) {
	done := make(chan bool)
	errChan := Go(name, func() error {
		time.Sleep(duration)
		return fmt.Errorf("fail")
	}, nil)

	select {
	case _ = <-done:
		t.Fatal("should not have succeeded")
	case _ = <-errChan:
		// do nothing
	}
}

// grabFirstFromStream reads on the http request until we see the first
// full result printed
func grabFirstCommandFromStream(t *testing.T, url string) streamCmdMetric {
	var event streamCmdMetric

	metrics, done := streamMetrics(t, url)
	for m := range metrics {
		if strings.Contains(m, "HystrixCommand") {
			done <- true
			close(done)

			err := json.Unmarshal([]byte(m), &event)
			if err != nil {
				t.Fatal(err)
			}

			break
		}
	}

	return event
}

func grabFirstThreadPoolFromStream(t *testing.T, url string) streamThreadPoolMetric {
	var event streamThreadPoolMetric

	metrics, done := streamMetrics(t, url)
	for m := range metrics {
		if strings.Contains(m, "HystrixThreadPool") {
			done <- true
			close(done)

			err := json.Unmarshal([]byte(m), &event)
			if err != nil {
				t.Fatal(err)
			}
			break
		}
	}

	return event
}

func streamMetrics(t *testing.T, url string) (chan string, chan bool) {
	metrics := make(chan string, 1)
	done := make(chan bool, 1)

	go func() {
		res, err := http.Get(url)
		if err != nil {
			t.Fatal(err)
		}
		defer res.Body.Close()

		buf := []byte{0}
		data := ""
		for {
			_, err := res.Body.Read(buf)
			if err != nil {
				t.Fatal(err)
			}

			data += string(buf)
			if strings.Contains(data, "\n\n") {
				data = strings.Replace(data, "data:{", "{", 1)
				metrics <- data
				data = ""
			}

			select {
			case _ = <-done:
				close(metrics)
				return
			default:
			}
		}
	}()

	return metrics, done
}

func TestEventStream(t *testing.T) {
	Convey("given a running event stream", t, func() {
		server := startTestServer()
		defer server.stopTestServer()

		Convey("after 2 successful commands", func() {
			sleepingCommand(t, "eventstream", 1*time.Millisecond)
			sleepingCommand(t, "eventstream", 1*time.Millisecond)

			Convey("request count should be 2", func() {
				event := grabFirstCommandFromStream(t, server.URL)

				So(event.Name, ShouldEqual, "eventstream")
				So(int(event.RequestCount), ShouldEqual, 2)
			})
		})

		Convey("after 1 successful command and 2 unsuccessful commands", func() {
			sleepingCommand(t, "errorpercent", 1*time.Millisecond)
			failingCommand(t, "errorpercent", 1*time.Millisecond)
			failingCommand(t, "errorpercent", 1*time.Millisecond)

			Convey("the error precentage should be 67", func() {
				metric := grabFirstCommandFromStream(t, server.URL)

				So(metric.ErrorPct, ShouldEqual, 67)
			})
		})
	})
}

func TestThreadPoolStream(t *testing.T) {
	Convey("given a running event stream", t, func() {
		server := startTestServer()
		defer server.stopTestServer()

		Convey("after a successful command", func() {
			sleepingCommand(t, "threadpool", 1*time.Millisecond)
			metric := grabFirstThreadPoolFromStream(t, server.URL)

			Convey("the rolling count of executions should increment", func() {
				So(metric.RollingCountThreadsExecuted, ShouldEqual, 1)
			})

			Convey("the pool size should be 10", func() {
				So(metric.CurrentPoolSize, ShouldEqual, 10)
			})
		})
	})
}
<commit_msg>Added unit test to confirm that disconnected clients are detected quickly<commit_after>package hystrix

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"net/http/httptest"
	"strings"
	"sync"
	"testing"
	"time"

	. "github.com/smartystreets/goconvey/convey"
)

type eventStreamTestServer struct {
	*httptest.Server
	*StreamHandler
}

func (s *eventStreamTestServer) stopTestServer() error {
	s.Close()
	s.Stop()
	Flush()

	return nil
}

func startTestServer() *eventStreamTestServer {
	hystrixStreamHandler := NewStreamHandler()
	hystrixStreamHandler.Start()
	return &eventStreamTestServer{
		httptest.NewServer(hystrixStreamHandler),
		hystrixStreamHandler,
	}
}

func sleepingCommand(t *testing.T, name string, duration time.Duration) {
	done := make(chan bool)
	errChan := Go(name, func() error {
		time.Sleep(duration)
		done <- true
		return nil
	}, nil)

	select {
	case _ = <-done:
		// do nothing
	case err := <-errChan:
		t.Fatal(err)
	}
}

func failingCommand(t *testing.T, name string, duration time.Duration) {
	done := make(chan bool)
	errChan := Go(name, func() error {
		time.Sleep(duration)
		return fmt.Errorf("fail")
	}, nil)

	select {
	case _ = <-done:
		t.Fatal("should not have succeeded")
	case _ = <-errChan:
		// do nothing
	}
}

// grabFirstFromStream reads on the http request until we see the first
// full result printed
func grabFirstCommandFromStream(t *testing.T, url string) streamCmdMetric {
	var event streamCmdMetric

	metrics, done := streamMetrics(t, url)
	for m := range metrics {
		if strings.Contains(m, "HystrixCommand") {
			done <- true
			close(done)

			err := json.Unmarshal([]byte(m), &event)
			if err != nil {
				t.Fatal(err)
			}

			break
		}
	}

	return event
}

func grabFirstThreadPoolFromStream(t *testing.T, url string) streamThreadPoolMetric {
	var event streamThreadPoolMetric

	metrics, done := streamMetrics(t, url)
	for m := range metrics {
		if strings.Contains(m, "HystrixThreadPool") {
			done <- true
			close(done)

			err := json.Unmarshal([]byte(m), &event)
			if err != nil {
				t.Fatal(err)
			}
			break
		}
	}

	return event
}

func streamMetrics(t *testing.T, url string) (chan string, chan bool) {
	metrics := make(chan string, 1)
	done := make(chan bool, 1)

	go func() {
		res, err := http.Get(url)
		if err != nil {
			t.Fatal(err)
		}
		defer res.Body.Close()

		buf := []byte{0}
		data := ""
		for {
			_, err := res.Body.Read(buf)
			if err != nil {
				t.Fatal(err)
			}

			data += string(buf)
			if strings.Contains(data, "\n\n") {
				data = strings.Replace(data, "data:{", "{", 1)
				metrics <- data
				data = ""
			}

			select {
			case _ = <-done:
				close(metrics)
				return
			default:
			}
		}
	}()

	return metrics, done
}

func TestEventStream(t *testing.T) {
	Convey("given a running event stream", t, func() {
		server := startTestServer()
		defer server.stopTestServer()

		Convey("after 2 successful commands", func() {
			sleepingCommand(t, "eventstream", 1*time.Millisecond)
			sleepingCommand(t, "eventstream", 1*time.Millisecond)

			Convey("request count should be 2", func() {
				event := grabFirstCommandFromStream(t, server.URL)

				So(event.Name, ShouldEqual, "eventstream")
				So(int(event.RequestCount), ShouldEqual, 2)
			})
		})

		Convey("after 1 successful command and 2 unsuccessful commands", func() {
			sleepingCommand(t, "errorpercent", 1*time.Millisecond)
			failingCommand(t, "errorpercent", 1*time.Millisecond)
			failingCommand(t, "errorpercent", 1*time.Millisecond)

			Convey("the error precentage should be 67", func() {
				metric := grabFirstCommandFromStream(t, server.URL)

				So(metric.ErrorPct, ShouldEqual, 67)
			})
		})
	})
}

func TestClientCancelEventStream(t *testing.T) {
	Convey("given a running event stream", t, func() {
		server := startTestServer()
		defer server.stopTestServer()

		sleepingCommand(t, "eventstream", 1*time.Millisecond)

		Convey("after a client connects", func() {
			req, err := http.NewRequest("GET", server.URL, nil)
			if err != nil {
				t.Fatal(err)
			}
			// use a transport so we can cancel the stream when we're done - in 1.5 this is much easier
			tr := &http.Transport{}
			client := &http.Client{Transport: tr}
			wait := make(chan struct{})
			afterFirstRead := &sync.WaitGroup{}
			afterFirstRead.Add(1)

			go func() {
				afr := afterFirstRead
				buf := []byte{0}
				res, err := client.Do(req)
				if err != nil {
					log.Fatal(err)
					t.Fatal(err)
				}
				defer res.Body.Close()

				for {
					select {
					case <-wait:
						//wait for master goroutine to break us out
						tr.CancelRequest(req)
						return
					default:
						//read something
						_, err = res.Body.Read(buf)
						if err != nil {
							log.Fatal(err)
							t.Fatal(err)
						}
						if afr != nil {
							afr.Done()
							afr = nil
						}
					}
				}
			}()
			// need to make sure our request has round-tripped to the server
			afterFirstRead.Wait()

			Convey("it should be registered", func() {
				So(len(server.StreamHandler.requests), ShouldEqual, 1)

				Convey("after client disconnects", func() {
					// let the request be cancelled and the body closed
					close(wait)
					// wait for the server to clean up
					time.Sleep(2000 * time.Millisecond)
					Convey("it should be detected as disconnected and de-registered", func() {
						//confirm we have 0 clients
						So(len(server.StreamHandler.requests), ShouldEqual, 0)
					})
				})
			})
		})
	})
}

func TestThreadPoolStream(t *testing.T) {
	Convey("given a running event stream", t, func() {
		server := startTestServer()
		defer server.stopTestServer()

		Convey("after a successful command", func() {
			sleepingCommand(t, "threadpool", 1*time.Millisecond)
			metric := grabFirstThreadPoolFromStream(t, server.URL)

			Convey("the rolling count of executions should increment", func() {
				So(metric.RollingCountThreadsExecuted, ShouldEqual, 1)
			})

			Convey("the pool size should be 10", func() {
				So(metric.CurrentPoolSize, ShouldEqual, 10)
			})
		})
	})
}
<|endoftext|>


<commit_before>package cluster

import (
	"sync"

	"github.com/hashicorp/nomad/api"
	"github.com/jippi/hashi-ui/backend/structs"
)

const (
	EvaluateAllJobs = "NOMAD_EVALUATE_ALL_JOBS"
)

type evaluateAllJobs struct {
	action structs.Action
	client *api.Client
}

func NewEvaluateAllJobs(action structs.Action, client *api.Client) *evaluateAllJobs {
	return &evaluateAllJobs{
		action: action,
		client: client,
	}
}

func (w *evaluateAllJobs) Do() (*structs.Response, error) {
	jobs, _, err := w.client.Jobs().List(nil)
	if err != nil {
		return structs.NewErrorResponse(err)
	}

	var wg sync.WaitGroup
	for _, job := range jobs {
		wg.Add(1)

		go func(job *api.JobListStub) {
			w.client.Jobs().ForceEvaluate(job.ID, nil)
			wg.Done()
		}(job)
	}

	wg.Wait()

	return structs.NewSuccessResponse("Successfully force-evaluated all jobs")
}

func (w *evaluateAllJobs) Key() string {
	return "/system/evaluate_all_jobs"
}

func (w *evaluateAllJobs) IsMutable() bool {
	return true
}

func (w *evaluateAllJobs) BackendType() string {
	return "nomad"
}
<commit_msg>don't force-evaluate batch jobs<commit_after>package cluster

import (
	"sync"

	"github.com/hashicorp/nomad/api"
	"github.com/jippi/hashi-ui/backend/structs"
)

const (
	EvaluateAllJobs = "NOMAD_EVALUATE_ALL_JOBS"
)

type evaluateAllJobs struct {
	action structs.Action
	client *api.Client
}

func NewEvaluateAllJobs(action structs.Action, client *api.Client) *evaluateAllJobs {
	return &evaluateAllJobs{
		action: action,
		client: client,
	}
}

func (w *evaluateAllJobs) Do() (*structs.Response, error) {
	jobs, _, err := w.client.Jobs().List(nil)
	if err != nil {
		return structs.NewErrorResponse(err)
	}

	var wg sync.WaitGroup
	for _, job := range jobs {
		// don't trigger batch jobs
		if job.Type == api.JobTypeBatch {
			continue
		}

		wg.Add(1)

		go func(job *api.JobListStub) {
			w.client.Jobs().ForceEvaluate(job.ID, nil)
			wg.Done()
		}(job)
	}

	wg.Wait()

	return structs.NewSuccessResponse("Successfully force-evaluated all jobs")
}

func (w *evaluateAllJobs) Key() string {
	return "/system/evaluate_all_jobs"
}

func (w *evaluateAllJobs) IsMutable() bool {
	return true
}

func (w *evaluateAllJobs) BackendType() string {
	return "nomad"
}
<|endoftext|>


<commit_before>// (C) 2017 Julian Andres Klode <jak@jak-linux.org>
// Licensed under the 2-Clause BSD license, see LICENSE for more information.
package parser

import (
	"reflect"
	"testing"

	"github.com/julian-klode/lingolang/permission"
)

// testCases contains tests for the permission parser.
var testCases = map[string]permission.Permission{
	"123":     nil,
	"!":       nil,
	"a !":     nil,
	"a error": nil,
	"":        nil,
	"oe":      nil,
	"or":      permission.Owned | permission.Read,
	"ow":      permission.Owned | permission.Write,
	"orwR":    permission.Owned | permission.Read | permission.Write | permission.ExclRead,
	"orR":     permission.Owned | permission.Read | permission.ExclRead,
	"owW":     permission.Owned | permission.Write | permission.ExclWrite,
	"om":      permission.Owned | permission.Mutable,
	"ov":      permission.Owned | permission.Value,
	"a":       permission.Any,
	"on":      permission.Owned,
	"n":       permission.None,
	"m [":     nil,
	"m [1":    nil,
	"m []":    nil,
	"m [1]":   nil,
	"m [] a": &permission.ArraySlicePermission{
		BasePermission:    permission.Mutable,
		ElementPermission: permission.Any,
	},
	"m [1] a": &permission.ArraySlicePermission{
		BasePermission:    permission.Mutable,
		ElementPermission: permission.Any,
	},
	"m map[v]l": &permission.MapPermission{
		BasePermission:  permission.Mutable,
		KeyPermission:   permission.Value,
		ValuePermission: permission.LinearValue,
	},
	"n map":         nil,
	"n map [":       nil,
	"n map [error]": nil,
	"n map [n":      nil,
	"n map [n]":     nil,
	"m chan l": &permission.ChanPermission{
		BasePermission:    permission.Mutable,
		ElementPermission: permission.LinearValue,
	},
	"m chan":       nil,
	"m chan error": nil,
	"m * l": &permission.PointerPermission{
		BasePermission: permission.Mutable,
		Target:         permission.LinearValue,
	},
	"error":     nil,
	"m * error": nil,
	"m func (v) a": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      nil,
		Params:         []permission.Permission{permission.Value},
		Results:        []permission.Permission{permission.Any},
	},
	"m (m) func (v) a": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      []permission.Permission{permission.Mutable},
		Params:         []permission.Permission{permission.Value},
		Results:        []permission.Permission{permission.Any},
	},
	"m (m) func (v, l) a": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      []permission.Permission{permission.Mutable},
		Params:         []permission.Permission{permission.Value, permission.LinearValue},
		Results:        []permission.Permission{permission.Any},
	},
	"m (m) func (v, l) (a)": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      []permission.Permission{permission.Mutable},
		Params:         []permission.Permission{permission.Value, permission.LinearValue},
		Results:        []permission.Permission{permission.Any},
	},
	"m (m) func (v, l) (a, n)": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      []permission.Permission{permission.Mutable},
		Params:         []permission.Permission{permission.Value, permission.LinearValue},
		Results:        []permission.Permission{permission.Any, permission.None},
	},
	"m (m) func (v, l)": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      []permission.Permission{permission.Mutable},
		Params:         []permission.Permission{permission.Value, permission.LinearValue},
		Results:        nil,
	},
	"m () func (v, l)":       nil,
	"m (m":                   nil,
	"m (m)":                  nil,
	"m (m) func":             nil,
	"m (m) func (":           nil,
	"m (m) func (v":          nil,
	"m (m) func (v,)":        nil,
	"m (m) func ()":          nil,
	"m (m) func (v) error":   nil,
	"m (m) func (v) (error)": nil,
	"m (m) func (v) (v,)":    nil,
	"m (m) func (v) (v !)":   nil,
	"m (m) func (v) (v":      nil,
	"m (m) func (v) hello":   nil,
	// Interface
	"m interface": &permission.InterfacePermission{
		BasePermission: permission.Mutable,
	},
	"l interface": &permission.InterfacePermission{
		BasePermission: permission.LinearValue,
	},
	"error interface": nil,
	"interface error": nil,
}

func helper() (perm permission.Permission, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()
	perm = NewParser("error").parseBasePermission()
	return perm, nil
}

func TestParser(t *testing.T) {
	for input, expected := range testCases {
		perm, err := NewParser(input).Parse()
		if !reflect.DeepEqual(perm, expected) {
			t.Errorf("Input %s: Unexpected permission %v, expected %v - error: %v", input, perm, expected, err)
		}
	}

	perm, err := helper()
	if err == nil {
		t.Errorf("Input 'error' parsed to valid base permission %v", perm)
	}
}

func BenchmarkParser(b *testing.B) {
	for i := 0; i < b.N; i++ {
		NewParser("m (m) func (v, l) (a, n)").Parse()
	}
}
<commit_msg>Benchmark all valid test case inputs<commit_after>// (C) 2017 Julian Andres Klode <jak@jak-linux.org>
// Licensed under the 2-Clause BSD license, see LICENSE for more information.
package parser

import (
	"reflect"
	"sort"
	"testing"

	"github.com/julian-klode/lingolang/permission"
)

// testCases contains tests for the permission parser.
var testCases = map[string]permission.Permission{
	"123":     nil,
	"!":       nil,
	"a !":     nil,
	"a error": nil,
	"":        nil,
	"oe":      nil,
	"or":      permission.Owned | permission.Read,
	"ow":      permission.Owned | permission.Write,
	"orwR":    permission.Owned | permission.Read | permission.Write | permission.ExclRead,
	"orR":     permission.Owned | permission.Read | permission.ExclRead,
	"owW":     permission.Owned | permission.Write | permission.ExclWrite,
	"om":      permission.Owned | permission.Mutable,
	"ov":      permission.Owned | permission.Value,
	"a":       permission.Any,
	"on":      permission.Owned,
	"n":       permission.None,
	"m [":     nil,
	"m [1":    nil,
	"m []":    nil,
	"m [1]":   nil,
	"m [] a": &permission.ArraySlicePermission{
		BasePermission:    permission.Mutable,
		ElementPermission: permission.Any,
	},
	"m [1] a": &permission.ArraySlicePermission{
		BasePermission:    permission.Mutable,
		ElementPermission: permission.Any,
	},
	"m map[v]l": &permission.MapPermission{
		BasePermission:  permission.Mutable,
		KeyPermission:   permission.Value,
		ValuePermission: permission.LinearValue,
	},
	"n map":         nil,
	"n map [":       nil,
	"n map [error]": nil,
	"n map [n":      nil,
	"n map [n]":     nil,
	"m chan l": &permission.ChanPermission{
		BasePermission:    permission.Mutable,
		ElementPermission: permission.LinearValue,
	},
	"m chan":       nil,
	"m chan error": nil,
	"m * l": &permission.PointerPermission{
		BasePermission: permission.Mutable,
		Target:         permission.LinearValue,
	},
	"error":     nil,
	"m * error": nil,
	"m func (v) a": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      nil,
		Params:         []permission.Permission{permission.Value},
		Results:        []permission.Permission{permission.Any},
	},
	"m (m) func (v) a": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      []permission.Permission{permission.Mutable},
		Params:         []permission.Permission{permission.Value},
		Results:        []permission.Permission{permission.Any},
	},
	"m (m) func (v, l) a": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      []permission.Permission{permission.Mutable},
		Params:         []permission.Permission{permission.Value, permission.LinearValue},
		Results:        []permission.Permission{permission.Any},
	},
	"m (m) func (v, l) (a)": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      []permission.Permission{permission.Mutable},
		Params:         []permission.Permission{permission.Value, permission.LinearValue},
		Results:        []permission.Permission{permission.Any},
	},
	"m (m) func (v, l) (a, n)": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      []permission.Permission{permission.Mutable},
		Params:         []permission.Permission{permission.Value, permission.LinearValue},
		Results:        []permission.Permission{permission.Any, permission.None},
	},
	"m (m) func (v, l)": &permission.FuncPermission{
		BasePermission: permission.Mutable,
		Receivers:      []permission.Permission{permission.Mutable},
		Params:         []permission.Permission{permission.Value, permission.LinearValue},
		Results:        nil,
	},
	"m () func (v, l)":       nil,
	"m (m":                   nil,
	"m (m)":                  nil,
	"m (m) func":             nil,
	"m (m) func (":           nil,
	"m (m) func (v":          nil,
	"m (m) func (v,)":        nil,
	"m (m) func ()":          nil,
	"m (m) func (v) error":   nil,
	"m (m) func (v) (error)": nil,
	"m (m) func (v) (v,)":    nil,
	"m (m) func (v) (v !)":   nil,
	"m (m) func (v) (v":      nil,
	"m (m) func (v) hello":   nil,
	// Interface
	"m interface": &permission.InterfacePermission{
		BasePermission: permission.Mutable,
	},
	"l interface": &permission.InterfacePermission{
		BasePermission: permission.LinearValue,
	},
	"error interface": nil,
	"interface error": nil,
}

func helper() (perm permission.Permission, err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()
	perm = NewParser("error").parseBasePermission()
	return perm, nil
}

func TestParser(t *testing.T) {
	for input, expected := range testCases {
		perm, err := NewParser(input).Parse()
		if !reflect.DeepEqual(perm, expected) {
			t.Errorf("Input %s: Unexpected permission %v, expected %v - error: %v", input, perm, expected, err)
		}
	}

	perm, err := helper()
	if err == nil {
		t.Errorf("Input 'error' parsed to valid base permission %v", perm)
	}
}

func BenchmarkParser(b *testing.B) {
	keys := make([]string, 0, len(testCases))
	for input, _ := range testCases {
		keys = append(keys, input)
	}
	sort.Strings(keys)

	for _, input := range keys {
		expected := testCases[input]
		if expected == nil {
			continue
		}
		input := input
		b.Run(input, func(b *testing.B) {
			for i := 0; i < b.N; i++ {
				NewParser(input).Parse()
			}
		})
	}
}
<|endoftext|>


<commit_before>package test

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"os/exec"
	"path"
	"strings"
	"time"

	"gopkg.in/op/go-logging.v1"

	"build"
	"core"
)

var log = logging.MustGetLogger("test")

const dummyOutput = "=== RUN DummyTest\n--- PASS: DummyTest (0.00s)\nPASS\n"
const dummyCoverage = "<?xml version=\"1.0\" ?><coverage></coverage>"

func Test(tid int, state *core.BuildState, label core.BuildLabel) {
	state.LogBuildResult(tid, label, core.TargetTesting, "Testing...")
	startTime := time.Now()
	target := state.Graph.TargetOrDie(label)
	hash, err := build.RuntimeHash(state, target)
	if err != nil {
		state.LogBuildError(tid, label, core.TargetTestFailed, err, "Failed to calculate target hash")
		return
	}
	// Check the cached output files if the target wasn't rebuilt.
	hashStr := base64.RawURLEncoding.EncodeToString(core.CollapseHash(hash))
	resultsFileName := fmt.Sprintf(".test_results_%s_%s", label.Name, hashStr)
	coverageFileName := fmt.Sprintf(".test_coverage_%s_%s", label.Name, hashStr)
	outputFile := path.Join(target.TestDir(), "test.results")
	coverageFile := path.Join(target.TestDir(), "test.coverage")
	cachedOutputFile := path.Join(target.OutDir(), resultsFileName)
	cachedCoverageFile := path.Join(target.OutDir(), coverageFileName)
	needCoverage := state.NeedCoverage && !target.NoTestOutput

	cachedTest := func() {
		log.Debug("Not re-running test %s; got cached results.", label)
		coverage := parseCoverageFile(target, cachedCoverageFile)
		results, err := parseTestResults(target, cachedOutputFile, true)
		target.Results.Duration = time.Since(startTime).Seconds()
		target.Results.Cached = true
		if err != nil {
			state.LogBuildError(tid, label, core.TargetTestFailed, err, "Failed to parse cached test file %s", cachedOutputFile)
		} else if results.Failed > 0 {
			panic("Test results with failures shouldn't be cached.")
		} else {
			logTestSuccess(state, tid, label, results, coverage)
		}
	}

	moveAndCacheOutputFiles := func(results core.TestResults, coverage core.TestCoverage) bool {
		// Never cache test results when given arguments; the results may be incomplete.
		if len(state.TestArgs) > 0 {
			log.Debug("Not caching results for %s, we passed it arguments", label)
			return true
		}
		if err := moveAndCacheOutputFile(state, target, hash, outputFile, cachedOutputFile, resultsFileName, dummyOutput); err != nil {
			state.LogTestResult(tid, label, core.TargetTestFailed, results, coverage, err, "Failed to move test output file")
			return false
		}
		if needCoverage || core.PathExists(coverageFile) {
			if err := moveAndCacheOutputFile(state, target, hash, coverageFile, cachedCoverageFile, coverageFileName, dummyCoverage); err != nil {
				state.LogTestResult(tid, label, core.TargetTestFailed, results, coverage, err, "Failed to move test coverage file")
				return false
			}
		}
		for _, output := range target.TestOutputs {
			tmpFile := path.Join(target.TestDir(), output)
			outFile := path.Join(target.OutDir(), output)
			if err := moveAndCacheOutputFile(state, target, hash, tmpFile, outFile, output, ""); err != nil {
				state.LogTestResult(tid, label, core.TargetTestFailed, results, coverage, err, "Failed to move test output file")
				return false
			}
		}
		return true
	}

	needToRun := func() bool {
		if target.State() == core.Unchanged && core.PathExists(cachedOutputFile) {
			// Output file exists already and appears to be valid. We might still need to rerun though
			// if the coverage files aren't available.
			if needCoverage && !core.PathExists(cachedCoverageFile) {
				return true
			}
			return false
		}
		// Check the cache for these artifacts.
		if state.Cache == nil {
			return true
		}
		cache := *state.Cache
		if !cache.RetrieveExtra(target, hash, resultsFileName) {
			return true
		}
		if needCoverage && !cache.RetrieveExtra(target, hash, coverageFileName) {
			return true
		}
		for _, output := range target.TestOutputs {
			if !cache.RetrieveExtra(target, hash, output) {
				return true
			}
		}
		return false
	}

	// Don't cache when doing multiple runs, presumably the user explicitly wants to check it.
	if state.NumTestRuns <= 1 && !needToRun() {
		cachedTest()
		return
	}
	// Remove any cached test result file.
	if err := RemoveCachedTestFiles(target); err != nil {
		state.LogBuildError(tid, label, core.TargetTestFailed, err, "Failed to remove cached test files")
		return
	}
	numSucceeded := 0
	numFlakes := 0
	numRuns, successesRequired := calcNumRuns(state.NumTestRuns, target.Flakiness)
	var resultErr error
	resultMsg := ""
	var coverage core.TestCoverage
	for i := 0; i < numRuns && numSucceeded < successesRequired; i++ {
		if numRuns > 1 {
			state.LogBuildResult(tid, label, core.TargetTesting, fmt.Sprintf("Testing (%d of %d)...", i+1, numRuns))
		}
		out, err := prepareAndRunTest(tid, state, target)
		duration := time.Since(startTime).Seconds()
		startTime = time.Now() // reset this for next time

		// This is all pretty involved; there are lots of different possibilities of what could happen.
		// The contract is that the test must return zero on success or non-zero on failure (Unix FTW).
		// If it's successful, it must produce a parseable file named "test.results" in its temp folder.
		// (alternatively, this can be a directory containing parseable files).
		// Tests can opt out of the file requirement individually, in which case they're judged only
		// by their return value.
		// But of course, we still have to consider all the alternatives here and handle them nicely.
		target.Results.Output = string(out)
		if err != nil && target.Results.Output == "" {
			target.Results.Output = err.Error()
		}
		if err != nil {
			_, target.Results.TimedOut = err.(core.TimeoutError)
		}
		coverage = parseCoverageFile(target, coverageFile)
		target.Results.Duration += duration
		if !core.PathExists(outputFile) {
			if err == nil && target.NoTestOutput {
				target.Results.NumTests += 1
				target.Results.Passed += 1
				numSucceeded++
			} else if err == nil {
				target.Results.NumTests++
				target.Results.Failed++
				resultErr = fmt.Errorf("Test failed to produce output results file")
				resultMsg = fmt.Sprintf("Test apparently succeeded but failed to produce %s. Output: %s", outputFile, string(out))
				numFlakes++
			} else {
				target.Results.NumTests++
				target.Results.Failed++
				numFlakes++
				resultErr = err
				resultMsg = fmt.Sprintf("Test failed with no results. Output: %s", string(out))
			}
		} else {
			results, err2 := parseTestResults(target, outputFile, false)
			if err2 != nil {
				resultErr = err2
				resultMsg = fmt.Sprintf("Couldn't parse test output file: %s. Stdout: %s", err2, string(out))
				numFlakes++
			} else if err != nil && results.Failed == 0 {
				// Add a failure result to the test so it shows up in the final aggregation.
				results.Failed = 1
				results.Failures = append(results.Failures, core.TestFailure{
					Name:   "Return value",
					Type:   fmt.Sprintf("%s", err),
					Stdout: string(out),
				})
				numFlakes++
				resultErr = err
				resultMsg = fmt.Sprintf("Test returned nonzero but reported no errors: %s. Output: %s", err, string(out))
			} else if err == nil && results.Failed != 0 {
				resultErr = fmt.Errorf("Test returned 0 but still reported failures")
				resultMsg = fmt.Sprintf("Test returned 0 but still reported failures. Stdout: %s", string(out))
				numFlakes++
			} else if results.Failed != 0 {
				resultErr = fmt.Errorf("Tests failed")
				resultMsg = fmt.Sprintf("Tests failed. Stdout: %s", string(out))
				numFlakes++
			} else {
				numSucceeded++
			}
		}
	}
	if numSucceeded >= successesRequired {
		if numSucceeded > 0 && numFlakes > 0 {
			target.Results.Flakes = numFlakes
		}
		// Success, clean things up
		if moveAndCacheOutputFiles(target.Results, coverage) {
			logTestSuccess(state, tid, label, target.Results, coverage)
		}
		// Clean up the test directory.
		if state.CleanWorkdirs {
			if err := os.RemoveAll(target.TestDir()); err != nil {
				log.Warning("Failed to remove test directory for %s: %s", target.Label, err)
			}
		}
	} else {
		state.LogTestResult(tid, label, core.TargetTestFailed, target.Results, coverage, resultErr, resultMsg)
	}
}

func logTestSuccess(state *core.BuildState, tid int, label core.BuildLabel, results core.TestResults, coverage core.TestCoverage) {
	var description string
	tests := pluralise("test", results.NumTests)
	if results.Skipped != 0 || results.ExpectedFailures != 0 {
		failures := pluralise("failure", results.ExpectedFailures)
		description = fmt.Sprintf("%d %s passed. %d skipped, %d expected %s",
			results.NumTests, tests, results.Skipped, results.ExpectedFailures, failures)
	} else {
		description = fmt.Sprintf("%d %s passed.", results.NumTests, tests)
	}
	state.LogTestResult(tid, label, core.TargetTested, results, coverage, nil, description)
}

func pluralise(word string, quantity int) string {
	if quantity == 1 {
		return word
	}
	return word + "s"
}

func prepareTestDir(graph *core.BuildGraph, target *core.BuildTarget) error {
	if err := os.RemoveAll(target.TestDir()); err != nil {
		return err
	}
	if err := os.MkdirAll(target.TestDir(), core.DirPermissions); err != nil {
		return err
	}
	for out := range core.IterRuntimeFiles(graph, target, true) {
		if err := core.PrepareSourcePair(out); err != nil {
			return err
		}
	}
	return nil
}

func runTest(state *core.BuildState, target *core.BuildTarget, timeout int) ([]byte, error) {
	replacedCmd := build.ReplaceTestSequences(target, target.TestCommand)
	env := core.BuildEnvironment(state, target, true)
	if len(state.TestArgs) > 0 {
		args := strings.Join(state.TestArgs, " ")
		replacedCmd += " " + args
		env = append(env, "TESTS="+args)
	}
	cmd := exec.Command("bash", "-c", replacedCmd)
	cmd.Dir = target.TestDir()
	cmd.Env = env
	log.Debug("Running test %s\nENVIRONMENT:\n%s\n%s", target.Label, strings.Join(cmd.Env, "\n"), replacedCmd)
	if state.PrintCommands {
		log.Notice("Running test %s: %s", target.Label, replacedCmd)
	}
	return core.ExecWithTimeout(cmd, target.TestTimeout, timeout)
}

// prepareAndRunTest sets up a test directory and runs the test.
func prepareAndRunTest(tid int, state *core.BuildState, target *core.BuildTarget) (out []byte, err error) {
	if err = prepareTestDir(state.Graph, target); err != nil {
		state.LogBuildError(tid, target.Label, core.TargetTestFailed, err, "Failed to prepare test directory for %s: %s", target.Label, err)
		return []byte{}, err
	}
	return runPossiblyContainerisedTest(state, target)
}

// Parses the coverage output for a single target.
func parseCoverageFile(target *core.BuildTarget, coverageFile string) core.TestCoverage {
	coverage, err := parseTestCoverage(target, coverageFile)
	if err != nil {
		log.Errorf("Failed to parse coverage file for %s: %s", target.Label, err)
	}
	return coverage
}

// RemoveCachedTestFiles removes any cached test or coverage result files for a target.
func RemoveCachedTestFiles(target *core.BuildTarget) error {
	if err := removeAnyFilesWithPrefix(target.OutDir(), ".test_results_"+target.Label.Name); err != nil {
		return err
	}
	if err := removeAnyFilesWithPrefix(target.OutDir(), ".test_coverage_"+target.Label.Name); err != nil {
		return err
	}
	for _, output := range target.TestOutputs {
		if err := os.RemoveAll(path.Join(target.OutDir(), output)); err != nil {
			return err
		}
	}
	return nil
}

// removeAnyFilesWithPrefix deletes any files in a directory matching a given prefix.
func removeAnyFilesWithPrefix(dir, prefix string) error {
	infos, err := ioutil.ReadDir(dir)
	if err != nil {
		return err
	}
	for _, info := range infos {
		if strings.HasPrefix(info.Name(), prefix) {
			if err := os.RemoveAll(path.Join(dir, info.Name())); err != nil {
				return err
			}
		}
	}
	return nil
}

// Attempt to write a dummy coverage file to record that it's been done for a test.
func moveAndCacheOutputFile(state *core.BuildState, target *core.BuildTarget, hash []byte, from, to, filename, dummy string) error {
	if !core.PathExists(from) {
		if dummy == "" {
			return nil
		}
		if err := ioutil.WriteFile(to, []byte(dummy), 0644); err != nil {
			return err
		}
	} else if err := os.Rename(from, to); err != nil {
		return err
	}
	if state.Cache != nil {
		(*state.Cache).StoreExtra(target, hash, filename)
	}
	return nil
}

// calcNumRuns works out how many total runs we should have for a test, and how many successes
// are required for it to count as success.
func calcNumRuns(numRuns, flakiness int) (int, int) {
	if numRuns > 0 && flakiness > 0 { // If flag is passed we run exactly that many times with proportionate flakiness.
		return numRuns, int(math.Ceil(float64(numRuns) / float64(flakiness)))
	} else if numRuns > 0 {
		return numRuns, numRuns
	} else if flakiness > 0 { // Test is flaky, run that many times
		return flakiness, 1
	}
	return 1, 1
}
<commit_msg>Add explicit test failures on timeout etc<commit_after>package test

import (
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"os/exec"
	"path"
	"strings"
	"time"

	"gopkg.in/op/go-logging.v1"

	"build"
	"core"
)

var log = logging.MustGetLogger("test")

const dummyOutput = "=== RUN DummyTest\n--- PASS: DummyTest (0.00s)\nPASS\n"
const dummyCoverage = "<?xml version=\"1.0\" ?><coverage></coverage>"

func Test(tid int, state *core.BuildState, label core.BuildLabel) {
	state.LogBuildResult(tid, label, core.TargetTesting, "Testing...")
	startTime := time.Now()
	target := state.Graph.TargetOrDie(label)
	hash, err := build.RuntimeHash(state, target)
	if err != nil {
		state.LogBuildError(tid, label, core.TargetTestFailed, err, "Failed to calculate target hash")
		return
	}
	// Check the cached output files if the target wasn't rebuilt.
	hashStr := base64.RawURLEncoding.EncodeToString(core.CollapseHash(hash))
	resultsFileName := fmt.Sprintf(".test_results_%s_%s", label.Name, hashStr)
	coverageFileName := fmt.Sprintf(".test_coverage_%s_%s", label.Name, hashStr)
	outputFile := path.Join(target.TestDir(), "test.results")
	coverageFile := path.Join(target.TestDir(), "test.coverage")
	cachedOutputFile := path.Join(target.OutDir(), resultsFileName)
	cachedCoverageFile := path.Join(target.OutDir(), coverageFileName)
	needCoverage := state.NeedCoverage && !target.NoTestOutput

	cachedTest := func() {
		log.Debug("Not re-running test %s; got cached results.", label)
		coverage := parseCoverageFile(target, cachedCoverageFile)
		results, err := parseTestResults(target, cachedOutputFile, true)
		target.Results.Duration = time.Since(startTime).Seconds()
		target.Results.Cached = true
		if err != nil {
			state.LogBuildError(tid, label, core.TargetTestFailed, err, "Failed to parse cached test file %s", cachedOutputFile)
		} else if results.Failed > 0 {
			panic("Test results with failures shouldn't be cached.")
		} else {
			logTestSuccess(state, tid, label, results, coverage)
		}
	}

	moveAndCacheOutputFiles := func(results core.TestResults, coverage core.TestCoverage) bool {
		// Never cache test results when given arguments; the results may be incomplete.
		if len(state.TestArgs) > 0 {
			log.Debug("Not caching results for %s, we passed it arguments", label)
			return true
		}
		if err := moveAndCacheOutputFile(state, target, hash, outputFile, cachedOutputFile, resultsFileName, dummyOutput); err != nil {
			state.LogTestResult(tid, label, core.TargetTestFailed, results, coverage, err, "Failed to move test output file")
			return false
		}
		if needCoverage || core.PathExists(coverageFile) {
			if err := moveAndCacheOutputFile(state, target, hash, coverageFile, cachedCoverageFile, coverageFileName, dummyCoverage); err != nil {
				state.LogTestResult(tid, label, core.TargetTestFailed, results, coverage, err, "Failed to move test coverage file")
				return false
			}
		}
		for _, output := range target.TestOutputs {
			tmpFile := path.Join(target.TestDir(), output)
			outFile := path.Join(target.OutDir(), output)
			if err := moveAndCacheOutputFile(state, target, hash, tmpFile, outFile, output, ""); err != nil {
				state.LogTestResult(tid, label, core.TargetTestFailed, results, coverage, err, "Failed to move test output file")
				return false
			}
		}
		return true
	}

	needToRun := func() bool {
		if target.State() == core.Unchanged && core.PathExists(cachedOutputFile) {
			// Output file exists already and appears to be valid. We might still need to rerun though
			// if the coverage files aren't available.
			if needCoverage && !core.PathExists(cachedCoverageFile) {
				return true
			}
			return false
		}
		// Check the cache for these artifacts.
		if state.Cache == nil {
			return true
		}
		cache := *state.Cache
		if !cache.RetrieveExtra(target, hash, resultsFileName) {
			return true
		}
		if needCoverage && !cache.RetrieveExtra(target, hash, coverageFileName) {
			return true
		}
		for _, output := range target.TestOutputs {
			if !cache.RetrieveExtra(target, hash, output) {
				return true
			}
		}
		return false
	}

	// Don't cache when doing multiple runs, presumably the user explicitly wants to check it.
	if state.NumTestRuns <= 1 && !needToRun() {
		cachedTest()
		return
	}
	// Remove any cached test result file.
	if err := RemoveCachedTestFiles(target); err != nil {
		state.LogBuildError(tid, label, core.TargetTestFailed, err, "Failed to remove cached test files")
		return
	}
	numSucceeded := 0
	numFlakes := 0
	numRuns, successesRequired := calcNumRuns(state.NumTestRuns, target.Flakiness)
	var resultErr error
	resultMsg := ""
	var coverage core.TestCoverage
	for i := 0; i < numRuns && numSucceeded < successesRequired; i++ {
		if numRuns > 1 {
			state.LogBuildResult(tid, label, core.TargetTesting, fmt.Sprintf("Testing (%d of %d)...", i+1, numRuns))
		}
		out, err := prepareAndRunTest(tid, state, target)
		duration := time.Since(startTime).Seconds()
		startTime = time.Now() // reset this for next time

		// This is all pretty involved; there are lots of different possibilities of what could happen.
		// The contract is that the test must return zero on success or non-zero on failure (Unix FTW).
		// If it's successful, it must produce a parseable file named "test.results" in its temp folder.
		// (alternatively, this can be a directory containing parseable files).
		// Tests can opt out of the file requirement individually, in which case they're judged only
		// by their return value.
		// But of course, we still have to consider all the alternatives here and handle them nicely.
		target.Results.Output = string(out)
		if err != nil && target.Results.Output == "" {
			target.Results.Output = err.Error()
		}
		if err != nil {
			_, target.Results.TimedOut = err.(core.TimeoutError)
		}
		coverage = parseCoverageFile(target, coverageFile)
		target.Results.Duration += duration
		if !core.PathExists(outputFile) {
			if err == nil && target.NoTestOutput {
				target.Results.NumTests += 1
				target.Results.Passed += 1
				numSucceeded++
			} else if err == nil {
				target.Results.NumTests++
				target.Results.Failed++
				target.Results.Failures = append(target.Results.Failures, core.TestFailure{
					Name:   "Missing results",
					Stdout: string(out),
				})
				resultErr = fmt.Errorf("Test failed to produce output results file")
				resultMsg = fmt.Sprintf("Test apparently succeeded but failed to produce %s. Output: %s", outputFile, string(out))
				numFlakes++
			} else {
				target.Results.NumTests++
				target.Results.Failed++
				target.Results.Failures = append(target.Results.Failures, core.TestFailure{
					Name:   "Test failed with no results",
					Stdout: string(out),
				})
				numFlakes++
				resultErr = err
				resultMsg = fmt.Sprintf("Test failed with no results. Output: %s", string(out))
			}
		} else {
			results, err2 := parseTestResults(target, outputFile, false)
			if err2 != nil {
				resultErr = err2
				resultMsg = fmt.Sprintf("Couldn't parse test output file: %s. Stdout: %s", err2, string(out))
				numFlakes++
			} else if err != nil && results.Failed == 0 {
				// Add a failure result to the test so it shows up in the final aggregation.
				results.Failed = 1
				results.Failures = append(results.Failures, core.TestFailure{
					Name:   "Return value",
					Type:   fmt.Sprintf("%s", err),
					Stdout: string(out),
				})
				numFlakes++
				resultErr = err
				resultMsg = fmt.Sprintf("Test returned nonzero but reported no errors: %s. Output: %s", err, string(out))
			} else if err == nil && results.Failed != 0 {
				resultErr = fmt.Errorf("Test returned 0 but still reported failures")
				resultMsg = fmt.Sprintf("Test returned 0 but still reported failures. Stdout: %s", string(out))
				numFlakes++
			} else if results.Failed != 0 {
				resultErr = fmt.Errorf("Tests failed")
				resultMsg = fmt.Sprintf("Tests failed. Stdout: %s", string(out))
				numFlakes++
			} else {
				numSucceeded++
			}
		}
	}
	if numSucceeded >= successesRequired {
		if numSucceeded > 0 && numFlakes > 0 {
			target.Results.Flakes = numFlakes
		}
		// Success, clean things up
		if moveAndCacheOutputFiles(target.Results, coverage) {
			logTestSuccess(state, tid, label, target.Results, coverage)
		}
		// Clean up the test directory.
		if state.CleanWorkdirs {
			if err := os.RemoveAll(target.TestDir()); err != nil {
				log.Warning("Failed to remove test directory for %s: %s", target.Label, err)
			}
		}
	} else {
		state.LogTestResult(tid, label, core.TargetTestFailed, target.Results, coverage, resultErr, resultMsg)
	}
}

func logTestSuccess(state *core.BuildState, tid int, label core.BuildLabel, results core.TestResults, coverage core.TestCoverage) {
	var description string
	tests := pluralise("test", results.NumTests)
	if results.Skipped != 0 || results.ExpectedFailures != 0 {
		failures := pluralise("failure", results.ExpectedFailures)
		description = fmt.Sprintf("%d %s passed. %d skipped, %d expected %s",
			results.NumTests, tests, results.Skipped, results.ExpectedFailures, failures)
	} else {
		description = fmt.Sprintf("%d %s passed.", results.NumTests, tests)
	}
	state.LogTestResult(tid, label, core.TargetTested, results, coverage, nil, description)
}

func pluralise(word string, quantity int) string {
	if quantity == 1 {
		return word
	}
	return word + "s"
}

func prepareTestDir(graph *core.BuildGraph, target *core.BuildTarget) error {
	if err := os.RemoveAll(target.TestDir()); err != nil {
		return err
	}
	if err := os.MkdirAll(target.TestDir(), core.DirPermissions); err != nil {
		return err
	}
	for out := range core.IterRuntimeFiles(graph, target, true) {
		if err := core.PrepareSourcePair(out); err != nil {
			return err
		}
	}
	return nil
}

func runTest(state *core.BuildState, target *core.BuildTarget, timeout int) ([]byte, error) {
	replacedCmd := build.ReplaceTestSequences(target, target.TestCommand)
	env := core.BuildEnvironment(state, target, true)
	if len(state.TestArgs) > 0 {
		args := strings.Join(state.TestArgs, " ")
		replacedCmd += " " + args
		env = append(env, "TESTS="+args)
	}
	cmd := exec.Command("bash", "-c", replacedCmd)
	cmd.Dir = target.TestDir()
	cmd.Env = env
	log.Debug("Running test %s\nENVIRONMENT:\n%s\n%s", target.Label, strings.Join(cmd.Env, "\n"), replacedCmd)
	if state.PrintCommands {
		log.Notice("Running test %s: %s", target.Label, replacedCmd)
	}
	return core.ExecWithTimeout(cmd, target.TestTimeout, timeout)
}

// prepareAndRunTest sets up a test directory and runs the test.
func prepareAndRunTest(tid int, state *core.BuildState, target *core.BuildTarget) (out []byte, err error) {
	if err = prepareTestDir(state.Graph, target); err != nil {
		state.LogBuildError(tid, target.Label, core.TargetTestFailed, err, "Failed to prepare test directory for %s: %s", target.Label, err)
		return []byte{}, err
	}
	return runPossiblyContainerisedTest(state, target)
}

// Parses the coverage output for a single target.
func parseCoverageFile(target *core.BuildTarget, coverageFile string) core.TestCoverage {
	coverage, err := parseTestCoverage(target, coverageFile)
	if err != nil {
		log.Errorf("Failed to parse coverage file for %s: %s", target.Label, err)
	}
	return coverage
}

// RemoveCachedTestFiles removes any cached test or coverage result files for a target.
func RemoveCachedTestFiles(target *core.BuildTarget) error {
	if err := removeAnyFilesWithPrefix(target.OutDir(), ".test_results_"+target.Label.Name); err != nil {
		return err
	}
	if err := removeAnyFilesWithPrefix(target.OutDir(), ".test_coverage_"+target.Label.Name); err != nil {
		return err
	}
	for _, output := range target.TestOutputs {
		if err := os.RemoveAll(path.Join(target.OutDir(), output)); err != nil {
			return err
		}
	}
	return nil
}

// removeAnyFilesWithPrefix deletes any files in a directory matching a given prefix.
func removeAnyFilesWithPrefix(dir, prefix string) error {
	infos, err := ioutil.ReadDir(dir)
	if err != nil {
		return err
	}
	for _, info := range infos {
		if strings.HasPrefix(info.Name(), prefix) {
			if err := os.RemoveAll(path.Join(dir, info.Name())); err != nil {
				return err
			}
		}
	}
	return nil
}

// Attempt to write a dummy coverage file to record that it's been done for a test.
func moveAndCacheOutputFile(state *core.BuildState, target *core.BuildTarget, hash []byte, from, to, filename, dummy string) error {
	if !core.PathExists(from) {
		if dummy == "" {
			return nil
		}
		if err := ioutil.WriteFile(to, []byte(dummy), 0644); err != nil {
			return err
		}
	} else if err := os.Rename(from, to); err != nil {
		return err
	}
	if state.Cache != nil {
		(*state.Cache).StoreExtra(target, hash, filename)
	}
	return nil
}

// calcNumRuns works out how many total runs we should have for a test, and how many successes
// are required for it to count as success.
func calcNumRuns(numRuns, flakiness int) (int, int) {
	if numRuns > 0 && flakiness > 0 { // If flag is passed we run exactly that many times with proportionate flakiness.
		return numRuns, int(math.Ceil(float64(numRuns) / float64(flakiness)))
	} else if numRuns > 0 {
		return numRuns, numRuns
	} else if flakiness > 0 { // Test is flaky, run that many times
		return flakiness, 1
	}
	return 1, 1
}
<|endoftext|>


<commit_before>package cameradar

import (
	"strings"

	"github.com/Ullaakut/nmap"
)

// Scan scans the target networks and tries to find RTSP streams within them.
//
// targets can be:
//
//    - a subnet (e.g.: 172.16.100.0/24)
//    - an IP (e.g.: 172.16.100.10)
//    - a hostname (e.g.: localhost)
//    - a range of IPs (e.g.: 172.16.100.10-20)
//
// ports can be:
//
//    - one or multiple ports and port ranges separated by commas (e.g.: 554,8554-8560,18554-28554)
func (s *Scanner) Scan() ([]Stream, error) {
	s.term.StartStep("Scanning the network")

	// Run nmap command to discover open ports on the specified targets & ports.
	nmapScanner, err := nmap.NewScanner(
		nmap.WithTargets(s.targets...),
		nmap.WithPorts(s.ports...),
		nmap.WithServiceInfo(),
		nmap.WithTimingTemplate(nmap.Timing(s.scanSpeed)),
	)
	if err != nil {
		return nil, s.term.FailStepf("unable to create network scanner: %v", err)
	}

	return s.scan(nmapScanner)
}

func (s *Scanner) scan(nmapScanner nmap.ScanRunner) ([]Stream, error) {
	results, warnings, err := nmapScanner.Run()
	if err != nil {
		return nil, s.term.FailStepf("error while scanning network: %v", err)
	}

	for _, warning := range warnings {
		s.term.Infoln("[Nmap Warning]", warning)
	}

	// Get streams from nmap results.
	var streams []Stream
	for _, host := range results.Hosts {
		for _, port := range host.Ports {
			if port.Status() != "open" {
				continue
			}

			if !strings.Contains(port.Service.Name, "rtsp") {
				continue
			}

			for _, address := range host.Addresses {
				streams = append(streams, Stream{
					Device:  port.Service.Product,
					Address: address.Addr,
					Port:    port.ID,
				})
			}
		}
	}

	s.term.Debugf("Found %d RTSP streams\n", len(streams))

	s.term.EndStep()

	return streams, nil
}
<commit_msg>Display nmap warnings correctly before exiting when a fatal error occurs (#285)<commit_after>package cameradar

import (
	"strings"

	"github.com/Ullaakut/nmap"
)

// Scan scans the target networks and tries to find RTSP streams within them.
//
// targets can be:
//
//    - a subnet (e.g.: 172.16.100.0/24)
//    - an IP (e.g.: 172.16.100.10)
//    - a hostname (e.g.: localhost)
//    - a range of IPs (e.g.: 172.16.100.10-20)
//
// ports can be:
//
//    - one or multiple ports and port ranges separated by commas (e.g.: 554,8554-8560,18554-28554)
func (s *Scanner) Scan() ([]Stream, error) {
	s.term.StartStep("Scanning the network")

	// Run nmap command to discover open ports on the specified targets & ports.
	nmapScanner, err := nmap.NewScanner(
		nmap.WithTargets(s.targets...),
		nmap.WithPorts(s.ports...),
		nmap.WithServiceInfo(),
		nmap.WithTimingTemplate(nmap.Timing(s.scanSpeed)),
	)
	if err != nil {
		return nil, s.term.FailStepf("unable to create network scanner: %v", err)
	}

	return s.scan(nmapScanner)
}

func (s *Scanner) scan(nmapScanner nmap.ScanRunner) ([]Stream, error) {
	results, warnings, err := nmapScanner.Run()
	for _, warning := range warnings {
		s.term.Infoln("[Nmap Warning]", warning)
	}
	if err != nil {
		return nil, s.term.FailStepf("error while scanning network: %v", err)
	}

	// Get streams from nmap results.
	var streams []Stream
	for _, host := range results.Hosts {
		for _, port := range host.Ports {
			if port.Status() != "open" {
				continue
			}

			if !strings.Contains(port.Service.Name, "rtsp") {
				continue
			}

			for _, address := range host.Addresses {
				streams = append(streams, Stream{
					Device:  port.Service.Product,
					Address: address.Addr,
					Port:    port.ID,
				})
			}
		}
	}

	s.term.Debugf("Found %d RTSP streams\n", len(streams))

	s.term.EndStep()

	return streams, nil
}
<|endoftext|>