<commit_before>/*
Copyright 2019 The Knative Authors

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

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

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

// spoof contains logic to make polling HTTP requests against an endpoint with optional host spoofing.

package spoof

import (
	"context"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"net/http"
	"strings"
	"time"

	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/client-go/kubernetes"
	"knative.dev/pkg/test/ingress"
	"knative.dev/pkg/test/logging"
	"knative.dev/pkg/test/zipkin"
	"knative.dev/pkg/tracing/propagation/tracecontextb3"

	"go.opencensus.io/plugin/ochttp"
	"go.opencensus.io/trace"
)

const (
	// Name of the temporary HTTP header that is added to http.Request to indicate that
	// it is a SpoofClient.Poll request. This header is removed before making call to backend.
	pollReqHeader = "X-Kn-Poll-Request-Do-Not-Trace"
)

// Response is a stripped down subset of http.Response. The is primarily useful
// for ResponseCheckers to inspect the response body without consuming it.
// Notably, Body is a byte slice instead of an io.ReadCloser.
type Response struct {
	Status     string
	StatusCode int
	Header     http.Header
	Body       []byte
}

func (r *Response) String() string {
	return fmt.Sprintf("status: %d, body: %s, headers: %v", r.StatusCode, string(r.Body), r.Header)
}

// Interface defines the actions that can be performed by the spoofing client.
type Interface interface {
	Do(*http.Request) (*Response, error)
	Poll(*http.Request, ResponseChecker, ...ErrorRetryChecker) (*Response, error)
}

// https://medium.com/stupid-gopher-tricks/ensuring-go-interface-satisfaction-at-compile-time-1ed158e8fa17
var (
	_           Interface = (*SpoofingClient)(nil)
	dialContext           = (&net.Dialer{}).DialContext
)

// ResponseChecker is used to determine when SpoofinClient.Poll is done polling.
// This allows you to predicate wait.PollImmediate on the request's http.Response.
//
// See the apimachinery wait package:
// https://github.com/kubernetes/apimachinery/blob/cf7ae2f57dabc02a3d215f15ca61ae1446f3be8f/pkg/util/wait/wait.go#L172
type ResponseChecker func(resp *Response) (done bool, err error)

// ErrorRetryChecker is used to determine if an error should be retried or not.
// If an error should be retried, it should return true and the wrapped error to explain why to retry.
type ErrorRetryChecker func(e error) (retry bool, err error)

// SpoofingClient is a minimal HTTP client wrapper that spoofs the domain of requests
// for non-resolvable domains.
type SpoofingClient struct {
	Client          *http.Client
	RequestInterval time.Duration
	RequestTimeout  time.Duration
	Logf            logging.FormatLogger
}

// TransportOption allows callers to customize the http.Transport used by a SpoofingClient
type TransportOption func(transport *http.Transport) *http.Transport

// New returns a SpoofingClient that rewrites requests if the target domain is not `resolvable`.
// It does this by looking up the ingress at construction time, so reusing a client will not
// follow the ingress if it moves (or if there are multiple ingresses).
//
// If that's a problem, see test/request.go#WaitForEndpointState for oneshot spoofing.
func New(
	kubeClientset *kubernetes.Clientset,
	logf logging.FormatLogger,
	domain string,
	resolvable bool,
	endpointOverride string,
	requestInterval time.Duration,
	requestTimeout time.Duration,
	opts ...TransportOption) (*SpoofingClient, error) {
	endpoint, err := ResolveEndpoint(kubeClientset, domain, resolvable, endpointOverride)
	if err != nil {
		return nil, fmt.Errorf("failed get the cluster endpoint: %w", err)
	}

	// Spoof the hostname at the resolver level
	logf("Spoofing %s -> %s", domain, endpoint)
	transport := &http.Transport{
		DialContext: func(ctx context.Context, network, addr string) (conn net.Conn, e error) {
			spoofed := addr
			if i := strings.LastIndex(addr, ":"); i != -1 && domain == addr[:i] {
				// The original hostname:port is spoofed by replacing the hostname by the value
				// returned by ResolveEndpoint.
				spoofed = endpoint + ":" + addr[i+1:]
			}
			return dialContext(ctx, network, spoofed)
		},
	}

	for _, opt := range opts {
		transport = opt(transport)
	}

	// Enable Zipkin tracing
	roundTripper := &ochttp.Transport{
		Base:        transport,
		Propagation: tracecontextb3.TraceContextEgress,
	}

	sc := SpoofingClient{
		Client:          &http.Client{Transport: roundTripper},
		RequestInterval: requestInterval,
		RequestTimeout:  requestTimeout,
		Logf:            logf,
	}
	return &sc, nil
}

// ResolveEndpoint resolves the endpoint address considering whether the domain is resolvable and taking into
// account whether the user overrode the endpoint address externally
func ResolveEndpoint(kubeClientset *kubernetes.Clientset, domain string, resolvable bool, endpointOverride string) (string, error) {
	// If the domain is resolvable, it can be used directly
	if resolvable {
		return domain, nil
	}
	// If an override is provided, use it
	if endpointOverride != "" {
		return endpointOverride, nil
	}
	// Otherwise, use the actual cluster endpoint
	return ingress.GetIngressEndpoint(kubeClientset)
}

// Do dispatches to the underlying http.Client.Do, spoofing domains as needed
// and transforming the http.Response into a spoof.Response.
// Each response is augmented with "ZipkinTraceID" header that identifies the zipkin trace corresponding to the request.
func (sc *SpoofingClient) Do(req *http.Request) (*Response, error) {
	// Starting span to capture zipkin trace.
	traceContext, span := trace.StartSpan(req.Context(), "SpoofingClient-Trace")
	defer span.End()

	// Check to see if the call to this method is coming from a Poll call.
	logZipkinTrace := true
	if req.Header.Get(pollReqHeader) != "" {
		req.Header.Del(pollReqHeader)
		logZipkinTrace = false
	}
	resp, err := sc.Client.Do(req.WithContext(traceContext))
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	resp.Header.Add(zipkin.ZipkinTraceIDHeader, span.SpanContext().TraceID.String())
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	spoofResp := &Response{
		Status:     resp.Status,
		StatusCode: resp.StatusCode,
		Header:     resp.Header,
		Body:       body,
	}

	if logZipkinTrace {
		sc.logZipkinTrace(spoofResp)
	}

	return spoofResp, nil
}

// Poll executes an http request until it satisfies the inState condition or encounters an error.
func (sc *SpoofingClient) Poll(req *http.Request, inState ResponseChecker, errorRetryCheckers ...ErrorRetryChecker) (*Response, error) {
	var (
		resp *Response
		err  error
	)

	err = wait.PollImmediate(sc.RequestInterval, sc.RequestTimeout, func() (bool, error) {
		// As we may do multiple Do calls as part of a single Poll we add this temporary header
		// to the request to indicate to Do method not to log Zipkin trace, instead it is
		// handled by this method itself.
		req.Header.Add(pollReqHeader, "True")
		resp, err = sc.Do(req)
		if err != nil {
			if len(errorRetryCheckers) == 0 {
				errorRetryCheckers = []ErrorRetryChecker{DefaultErrorRetryChecker}
			}
			for _, checker := range errorRetryCheckers {
				retry, newErr := checker(err)
				if retry {
					sc.Logf("Retrying %s: %v", req.URL.String(), newErr)
					return false, nil
				}
			}
			return true, err
		}

		return inState(resp)
	})

	if resp != nil {
		sc.logZipkinTrace(resp)
	}

	if err != nil {
		return resp, fmt.Errorf("response: %s did not pass checks: %w", resp, err)
	}
	return resp, nil
}

// DefaultErrorRetryChecker implements the defaults for retrying on error.
func DefaultErrorRetryChecker(err error) (bool, error) {
	if isTCPTimeout(err) {
		return true, fmt.Errorf("retrying for TCP timeout: %w", err)
	}
	// Retrying on DNS error, since we may be using xip.io or nip.io in tests.
	if isDNSError(err) {
		return true, fmt.Errorf("retrying for DNS error: %w", err)
	}
	// Repeat the poll on `connection refused` errors, which are usually transient Istio errors.
	if isConnectionRefused(err) {
		return true, fmt.Errorf("retrying for connection refused: %w", err)
	}
	if isConnectionReset(err) {
		return true, fmt.Errorf("retrying for connection reset: %w", err)
	}
	// Retry on connection/network errors.
	if errors.Is(err, io.EOF) {
		return true, fmt.Errorf("retrying for: %w", err)
	}
	return false, err
}

// logZipkinTrace provides support to log Zipkin Trace for param: spoofResponse
// We only log Zipkin trace for HTTP server errors i.e for HTTP status codes between 500 to 600
func (sc *SpoofingClient) logZipkinTrace(spoofResp *Response) {
	if !zipkin.ZipkinTracingEnabled || spoofResp.StatusCode < http.StatusInternalServerError || spoofResp.StatusCode >= 600 {
		return
	}

	traceID := spoofResp.Header.Get(zipkin.ZipkinTraceIDHeader)
	sc.Logf("Logging Zipkin Trace for: %s", traceID)

	json, err := zipkin.JSONTrace(traceID /* We don't know the expected number of spans */, -1, 5*time.Second)
	if err != nil {
		if _, ok := err.(*zipkin.TimeoutError); !ok {
			sc.Logf("Error getting zipkin trace: %v", err)
		}
	}

	sc.Logf("%s", json)
}
<commit_msg>Use TraceContextB3Egress for spoof client (#1447)<commit_after>/*
Copyright 2019 The Knative Authors

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

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

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

// spoof contains logic to make polling HTTP requests against an endpoint with optional host spoofing.

package spoof

import (
	"context"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net"
	"net/http"
	"strings"
	"time"

	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/client-go/kubernetes"
	"knative.dev/pkg/test/ingress"
	"knative.dev/pkg/test/logging"
	"knative.dev/pkg/test/zipkin"
	"knative.dev/pkg/tracing/propagation/tracecontextb3"

	"go.opencensus.io/plugin/ochttp"
	"go.opencensus.io/trace"
)

const (
	// Name of the temporary HTTP header that is added to http.Request to indicate that
	// it is a SpoofClient.Poll request. This header is removed before making call to backend.
	pollReqHeader = "X-Kn-Poll-Request-Do-Not-Trace"
)

// Response is a stripped down subset of http.Response. The is primarily useful
// for ResponseCheckers to inspect the response body without consuming it.
// Notably, Body is a byte slice instead of an io.ReadCloser.
type Response struct {
	Status     string
	StatusCode int
	Header     http.Header
	Body       []byte
}

func (r *Response) String() string {
	return fmt.Sprintf("status: %d, body: %s, headers: %v", r.StatusCode, string(r.Body), r.Header)
}

// Interface defines the actions that can be performed by the spoofing client.
type Interface interface {
	Do(*http.Request) (*Response, error)
	Poll(*http.Request, ResponseChecker, ...ErrorRetryChecker) (*Response, error)
}

// https://medium.com/stupid-gopher-tricks/ensuring-go-interface-satisfaction-at-compile-time-1ed158e8fa17
var (
	_           Interface = (*SpoofingClient)(nil)
	dialContext           = (&net.Dialer{}).DialContext
)

// ResponseChecker is used to determine when SpoofinClient.Poll is done polling.
// This allows you to predicate wait.PollImmediate on the request's http.Response.
//
// See the apimachinery wait package:
// https://github.com/kubernetes/apimachinery/blob/cf7ae2f57dabc02a3d215f15ca61ae1446f3be8f/pkg/util/wait/wait.go#L172
type ResponseChecker func(resp *Response) (done bool, err error)

// ErrorRetryChecker is used to determine if an error should be retried or not.
// If an error should be retried, it should return true and the wrapped error to explain why to retry.
type ErrorRetryChecker func(e error) (retry bool, err error)

// SpoofingClient is a minimal HTTP client wrapper that spoofs the domain of requests
// for non-resolvable domains.
type SpoofingClient struct {
	Client          *http.Client
	RequestInterval time.Duration
	RequestTimeout  time.Duration
	Logf            logging.FormatLogger
}

// TransportOption allows callers to customize the http.Transport used by a SpoofingClient
type TransportOption func(transport *http.Transport) *http.Transport

// New returns a SpoofingClient that rewrites requests if the target domain is not `resolvable`.
// It does this by looking up the ingress at construction time, so reusing a client will not
// follow the ingress if it moves (or if there are multiple ingresses).
//
// If that's a problem, see test/request.go#WaitForEndpointState for oneshot spoofing.
func New(
	kubeClientset *kubernetes.Clientset,
	logf logging.FormatLogger,
	domain string,
	resolvable bool,
	endpointOverride string,
	requestInterval time.Duration,
	requestTimeout time.Duration,
	opts ...TransportOption) (*SpoofingClient, error) {
	endpoint, err := ResolveEndpoint(kubeClientset, domain, resolvable, endpointOverride)
	if err != nil {
		return nil, fmt.Errorf("failed get the cluster endpoint: %w", err)
	}

	// Spoof the hostname at the resolver level
	logf("Spoofing %s -> %s", domain, endpoint)
	transport := &http.Transport{
		DialContext: func(ctx context.Context, network, addr string) (conn net.Conn, e error) {
			spoofed := addr
			if i := strings.LastIndex(addr, ":"); i != -1 && domain == addr[:i] {
				// The original hostname:port is spoofed by replacing the hostname by the value
				// returned by ResolveEndpoint.
				spoofed = endpoint + ":" + addr[i+1:]
			}
			return dialContext(ctx, network, spoofed)
		},
	}

	for _, opt := range opts {
		transport = opt(transport)
	}

	// Enable Zipkin tracing
	roundTripper := &ochttp.Transport{
		Base:        transport,
		Propagation: tracecontextb3.TraceContextB3Egress,
	}

	sc := SpoofingClient{
		Client:          &http.Client{Transport: roundTripper},
		RequestInterval: requestInterval,
		RequestTimeout:  requestTimeout,
		Logf:            logf,
	}
	return &sc, nil
}

// ResolveEndpoint resolves the endpoint address considering whether the domain is resolvable and taking into
// account whether the user overrode the endpoint address externally
func ResolveEndpoint(kubeClientset *kubernetes.Clientset, domain string, resolvable bool, endpointOverride string) (string, error) {
	// If the domain is resolvable, it can be used directly
	if resolvable {
		return domain, nil
	}
	// If an override is provided, use it
	if endpointOverride != "" {
		return endpointOverride, nil
	}
	// Otherwise, use the actual cluster endpoint
	return ingress.GetIngressEndpoint(kubeClientset)
}

// Do dispatches to the underlying http.Client.Do, spoofing domains as needed
// and transforming the http.Response into a spoof.Response.
// Each response is augmented with "ZipkinTraceID" header that identifies the zipkin trace corresponding to the request.
func (sc *SpoofingClient) Do(req *http.Request) (*Response, error) {
	// Starting span to capture zipkin trace.
	traceContext, span := trace.StartSpan(req.Context(), "SpoofingClient-Trace")
	defer span.End()

	// Check to see if the call to this method is coming from a Poll call.
	logZipkinTrace := true
	if req.Header.Get(pollReqHeader) != "" {
		req.Header.Del(pollReqHeader)
		logZipkinTrace = false
	}
	resp, err := sc.Client.Do(req.WithContext(traceContext))
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	resp.Header.Add(zipkin.ZipkinTraceIDHeader, span.SpanContext().TraceID.String())
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	spoofResp := &Response{
		Status:     resp.Status,
		StatusCode: resp.StatusCode,
		Header:     resp.Header,
		Body:       body,
	}

	if logZipkinTrace {
		sc.logZipkinTrace(spoofResp)
	}

	return spoofResp, nil
}

// Poll executes an http request until it satisfies the inState condition or encounters an error.
func (sc *SpoofingClient) Poll(req *http.Request, inState ResponseChecker, errorRetryCheckers ...ErrorRetryChecker) (*Response, error) {
	var (
		resp *Response
		err  error
	)

	err = wait.PollImmediate(sc.RequestInterval, sc.RequestTimeout, func() (bool, error) {
		// As we may do multiple Do calls as part of a single Poll we add this temporary header
		// to the request to indicate to Do method not to log Zipkin trace, instead it is
		// handled by this method itself.
		req.Header.Add(pollReqHeader, "True")
		resp, err = sc.Do(req)
		if err != nil {
			if len(errorRetryCheckers) == 0 {
				errorRetryCheckers = []ErrorRetryChecker{DefaultErrorRetryChecker}
			}
			for _, checker := range errorRetryCheckers {
				retry, newErr := checker(err)
				if retry {
					sc.Logf("Retrying %s: %v", req.URL.String(), newErr)
					return false, nil
				}
			}
			return true, err
		}

		return inState(resp)
	})

	if resp != nil {
		sc.logZipkinTrace(resp)
	}

	if err != nil {
		return resp, fmt.Errorf("response: %s did not pass checks: %w", resp, err)
	}
	return resp, nil
}

// DefaultErrorRetryChecker implements the defaults for retrying on error.
func DefaultErrorRetryChecker(err error) (bool, error) {
	if isTCPTimeout(err) {
		return true, fmt.Errorf("retrying for TCP timeout: %w", err)
	}
	// Retrying on DNS error, since we may be using xip.io or nip.io in tests.
	if isDNSError(err) {
		return true, fmt.Errorf("retrying for DNS error: %w", err)
	}
	// Repeat the poll on `connection refused` errors, which are usually transient Istio errors.
	if isConnectionRefused(err) {
		return true, fmt.Errorf("retrying for connection refused: %w", err)
	}
	if isConnectionReset(err) {
		return true, fmt.Errorf("retrying for connection reset: %w", err)
	}
	// Retry on connection/network errors.
	if errors.Is(err, io.EOF) {
		return true, fmt.Errorf("retrying for: %w", err)
	}
	return false, err
}

// logZipkinTrace provides support to log Zipkin Trace for param: spoofResponse
// We only log Zipkin trace for HTTP server errors i.e for HTTP status codes between 500 to 600
func (sc *SpoofingClient) logZipkinTrace(spoofResp *Response) {
	if !zipkin.ZipkinTracingEnabled || spoofResp.StatusCode < http.StatusInternalServerError || spoofResp.StatusCode >= 600 {
		return
	}

	traceID := spoofResp.Header.Get(zipkin.ZipkinTraceIDHeader)
	sc.Logf("Logging Zipkin Trace for: %s", traceID)

	json, err := zipkin.JSONTrace(traceID /* We don't know the expected number of spans */, -1, 5*time.Second)
	if err != nil {
		if _, ok := err.(*zipkin.TimeoutError); !ok {
			sc.Logf("Error getting zipkin trace: %v", err)
		}
	}

	sc.Logf("%s", json)
}
<|endoftext|>


<commit_before>package main

/*
 * An example of how to use lxd's golang /dev/lxd client. This is intended to
 * be run from inside a container.
 */

import (
	"bufio"
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"os"
	"time"

	"github.com/gorilla/websocket"
	"gopkg.in/yaml.v2"
)

type devLxdDialer struct {
	Path string
}

func (d devLxdDialer) devLxdDial(ctx context.Context, network, path string) (net.Conn, error) {
	addr, err := net.ResolveUnixAddr("unix", d.Path)
	if err != nil {
		return nil, err
	}

	conn, err := net.DialUnix("unix", nil, addr)
	if err != nil {
		return nil, err
	}

	return conn, err
}

var devLxdTransport = &http.Transport{
	DialContext: devLxdDialer{"/dev/lxd/sock"}.devLxdDial,
}

func devlxdMonitorStream() {
	client := http.Client{
		Transport: &http.Transport{
			DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
				return net.Dial("unix", "/dev/lxd/sock")
			},
		},
	}

	resp, err := client.Get("http://unix/1.0/events")
	if err != nil {
		panic(err)
	}

	scanner := bufio.NewScanner(resp.Body)

	for scanner.Scan() {
		message := make(map[string]any)
		err = json.Unmarshal(scanner.Bytes(), &message)
		if err != nil {
			return
		}

		message["timestamp"] = nil

		msg, err := yaml.Marshal(&message)
		if err != nil {
			return
		}

		fmt.Printf("%s\n", msg)
	}
}

func devlxdMonitorWebsocket(c http.Client) {
	dialer := websocket.Dialer{
		NetDialContext:   devLxdTransport.DialContext,
		HandshakeTimeout: time.Second * 5,
	}

	conn, _, err := dialer.Dial("ws://unix.socket/1.0/events", nil)
	if err != nil {
		return
	}

	for {
		_, data, err := conn.ReadMessage()
		if err != nil {
			return
		}

		message := make(map[string]any)
		err = json.Unmarshal(data, &message)
		if err != nil {
			return
		}

		message["timestamp"] = nil

		msg, err := yaml.Marshal(&message)
		if err != nil {
			return
		}

		fmt.Printf("%s\n", msg)
	}
}

func main() {
	c := http.Client{Transport: devLxdTransport}
	raw, err := c.Get("http://meshuggah-rocks/")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if raw.StatusCode != http.StatusOK {
		fmt.Println("http error", raw.StatusCode)
		result, err := ioutil.ReadAll(raw.Body)
		if err != nil {
			os.Exit(1)
		}

		fmt.Println(string(result))
	}

	result := []string{}
	err = json.NewDecoder(raw.Body).Decode(&result)
	if err != nil {
		fmt.Println("err decoding response", err)
		os.Exit(1)
	}

	if result[0] != "/1.0" {
		fmt.Println("unknown response", result)
		os.Exit(1)
	}

	if len(os.Args) > 1 {
		if os.Args[1] == "monitor-websocket" {
			devlxdMonitorWebsocket(c)
			os.Exit(0)
		}

		if os.Args[1] == "monitor-stream" {
			devlxdMonitorStream()
			os.Exit(0)
		}

		raw, err := c.Get(fmt.Sprintf("http://meshuggah-rocks/1.0/config/%s", os.Args[1]))
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		value, err := ioutil.ReadAll(raw.Body)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		fmt.Println(string(value))
	} else {
		fmt.Println("/dev/lxd ok")
	}
}
<commit_msg>test/deps: Add ready-state subcommand<commit_after>package main

/*
 * An example of how to use lxd's golang /dev/lxd client. This is intended to
 * be run from inside a container.
 */

import (
	"bufio"
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"os"
	"strconv"
	"time"

	"github.com/gorilla/websocket"
	"gopkg.in/yaml.v2"

	"github.com/lxc/lxd/shared/api"
)

type devLxdDialer struct {
	Path string
}

func (d devLxdDialer) devLxdDial(ctx context.Context, network, path string) (net.Conn, error) {
	addr, err := net.ResolveUnixAddr("unix", d.Path)
	if err != nil {
		return nil, err
	}

	conn, err := net.DialUnix("unix", nil, addr)
	if err != nil {
		return nil, err
	}

	return conn, err
}

var devLxdTransport = &http.Transport{
	DialContext: devLxdDialer{"/dev/lxd/sock"}.devLxdDial,
}

func devlxdMonitorStream() {
	client := http.Client{
		Transport: &http.Transport{
			DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
				return net.Dial("unix", "/dev/lxd/sock")
			},
		},
	}

	resp, err := client.Get("http://unix/1.0/events")
	if err != nil {
		panic(err)
	}

	scanner := bufio.NewScanner(resp.Body)

	for scanner.Scan() {
		message := make(map[string]any)
		err = json.Unmarshal(scanner.Bytes(), &message)
		if err != nil {
			return
		}

		message["timestamp"] = nil

		msg, err := yaml.Marshal(&message)
		if err != nil {
			return
		}

		fmt.Printf("%s\n", msg)
	}
}

func devlxdMonitorWebsocket(c http.Client) {
	dialer := websocket.Dialer{
		NetDialContext:   devLxdTransport.DialContext,
		HandshakeTimeout: time.Second * 5,
	}

	conn, _, err := dialer.Dial("ws://unix.socket/1.0/events", nil)
	if err != nil {
		return
	}

	for {
		_, data, err := conn.ReadMessage()
		if err != nil {
			return
		}

		message := make(map[string]any)
		err = json.Unmarshal(data, &message)
		if err != nil {
			return
		}

		message["timestamp"] = nil

		msg, err := yaml.Marshal(&message)
		if err != nil {
			return
		}

		fmt.Printf("%s\n", msg)
	}
}

func devlxdState(ready bool) {
	client := http.Client{
		Transport: &http.Transport{
			DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
				return net.Dial("unix", "/dev/lxd/sock")
			},
		},
	}

	var body bytes.Buffer
	payload := struct {
		State string `json:"state"`
	}{}

	if ready {
		payload.State = api.Ready.String()
	} else {
		payload.State = api.Started.String()
	}

	err := json.NewEncoder(&body).Encode(&payload)
	if err != nil {
		return
	}

	req, err := http.NewRequest("PATCH", "http://unix/1.0", &body)
	if err != nil {
		return
	}

	req.Header.Set("Content-Type", "application/json")

	_, err = client.Do(req)
	if err != nil {
		return
	}
}

func main() {
	c := http.Client{Transport: devLxdTransport}
	raw, err := c.Get("http://meshuggah-rocks/")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if raw.StatusCode != http.StatusOK {
		fmt.Println("http error", raw.StatusCode)
		result, err := ioutil.ReadAll(raw.Body)
		if err != nil {
			os.Exit(1)
		}

		fmt.Println(string(result))
	}

	result := []string{}
	err = json.NewDecoder(raw.Body).Decode(&result)
	if err != nil {
		fmt.Println("err decoding response", err)
		os.Exit(1)
	}

	if result[0] != "/1.0" {
		fmt.Println("unknown response", result)
		os.Exit(1)
	}

	if len(os.Args) > 1 {
		if os.Args[1] == "monitor-websocket" {
			devlxdMonitorWebsocket(c)
			os.Exit(0)
		}

		if os.Args[1] == "monitor-stream" {
			devlxdMonitorStream()
			os.Exit(0)
		}

		if os.Args[1] == "ready-state" {
			ready, err := strconv.ParseBool(os.Args[2])
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}

			devlxdState(ready)
			os.Exit(0)
		}

		raw, err := c.Get(fmt.Sprintf("http://meshuggah-rocks/1.0/config/%s", os.Args[1]))
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		value, err := ioutil.ReadAll(raw.Body)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		fmt.Println(string(value))
	} else {
		fmt.Println("/dev/lxd ok")
	}
}
<|endoftext|>


<commit_before>package main

import (
	"time"

	. "github.com/onsi/gomega"

	. "github.com/docker/api/tests/framework"
)

func main() {
	SetupTest()

	It("ensures context command includes azure-login and aci-create", func() {
		output := NewDockerCommand("context", "create", "--help").ExecOrDie()
		Expect(output).To(ContainSubstring("docker context create CONTEXT BACKEND [OPTIONS] [flags]"))
		Expect(output).To(ContainSubstring("--aci-location"))
		Expect(output).To(ContainSubstring("--aci-subscription-id"))
		Expect(output).To(ContainSubstring("--aci-resource-group"))
	})

	It("should be initialized with default context", func() {
		NewCommand("docker", "context", "use", "default").ExecOrDie()
		output := NewCommand("docker", "context", "ls").ExecOrDie()
		Expect(output).To(Not(ContainSubstring("test-example")))
		Expect(output).To(ContainSubstring("default *"))
	})

	It("should list all legacy commands", func() {
		output := NewDockerCommand("--help").ExecOrDie()
		Expect(output).To(ContainSubstring("swarm"))
	})

	It("should execute legacy commands", func() {
		output, _ := NewDockerCommand("swarm", "join").Exec()
		Expect(output).To(ContainSubstring("\"docker swarm join\" requires exactly 1 argument."))
	})

	It("should run local container in less than 2 secs", func() {
		NewDockerCommand("pull", "hello-world").ExecOrDie()
		output := NewDockerCommand("run", "hello-world").WithTimeout(time.NewTimer(2 * time.Second).C).ExecOrDie()
		Expect(output).To(ContainSubstring("Hello from Docker!"))
	})

	It("should list local container", func() {
		output := NewDockerCommand("ps", "-a").ExecOrDie()
		Expect(output).To(ContainSubstring("hello-world"))
	})

	It("creates a new test context to hardcoded example backend", func() {
		NewDockerCommand("context", "create", "test-example", "example").ExecOrDie()
		//Expect(output).To(ContainSubstring("test-example context acitest created"))
	})
	defer NewDockerCommand("context", "rm", "test-example").ExecOrDie()

	It("uses the test context", func() {
		currentContext := NewCommand("docker", "context", "use", "test-example").ExecOrDie()
		Expect(currentContext).To(ContainSubstring("test-example"))
		output := NewCommand("docker", "context", "ls").ExecOrDie()
		Expect(output).To(ContainSubstring("test-example *"))
	})

	It("can run ps command", func() {
		output := NewDockerCommand("ps").ExecOrDie()
		lines := Lines(output)
		Expect(len(lines)).To(Equal(3))
		Expect(lines[2]).To(ContainSubstring("1234  alpine"))
	})

	It("can run quiet ps command", func() {
		output := NewDockerCommand("ps", "-q").ExecOrDie()
		lines := Lines(output)
		Expect(len(lines)).To(Equal(2))
		Expect(lines[0]).To(Equal("id"))
		Expect(lines[1]).To(Equal("1234"))
	})

	It("can run 'run' command", func() {
		output := NewDockerCommand("run", "nginx", "-p", "80:80").ExecOrDie()
		Expect(output).To(ContainSubstring("Running container \"nginx\" with name"))
	})
}
<commit_msg>e2e: Use new context use command<commit_after>package main

import (
	"time"

	. "github.com/onsi/gomega"

	. "github.com/docker/api/tests/framework"
)

func main() {
	SetupTest()

	It("ensures context command includes azure-login and aci-create", func() {
		output := NewDockerCommand("context", "create", "--help").ExecOrDie()
		Expect(output).To(ContainSubstring("docker context create CONTEXT BACKEND [OPTIONS] [flags]"))
		Expect(output).To(ContainSubstring("--aci-location"))
		Expect(output).To(ContainSubstring("--aci-subscription-id"))
		Expect(output).To(ContainSubstring("--aci-resource-group"))
	})

	It("should be initialized with default context", func() {
		NewDockerCommand("context", "use", "default").ExecOrDie()
		output := NewCommand("docker", "context", "ls").ExecOrDie()
		Expect(output).To(Not(ContainSubstring("test-example")))
		Expect(output).To(ContainSubstring("default *"))
	})

	It("should list all legacy commands", func() {
		output := NewDockerCommand("--help").ExecOrDie()
		Expect(output).To(ContainSubstring("swarm"))
	})

	It("should execute legacy commands", func() {
		output, _ := NewDockerCommand("swarm", "join").Exec()
		Expect(output).To(ContainSubstring("\"docker swarm join\" requires exactly 1 argument."))
	})

	It("should run local container in less than 2 secs", func() {
		NewDockerCommand("pull", "hello-world").ExecOrDie()
		output := NewDockerCommand("run", "hello-world").WithTimeout(time.NewTimer(2 * time.Second).C).ExecOrDie()
		Expect(output).To(ContainSubstring("Hello from Docker!"))
	})

	It("should list local container", func() {
		output := NewDockerCommand("ps", "-a").ExecOrDie()
		Expect(output).To(ContainSubstring("hello-world"))
	})

	It("creates a new test context to hardcoded example backend", func() {
		NewDockerCommand("context", "create", "test-example", "example").ExecOrDie()
		//Expect(output).To(ContainSubstring("test-example context acitest created"))
	})
	defer NewDockerCommand("context", "rm", "test-example").ExecOrDie()
	defer NewDockerCommand("context", "use", "default").ExecOrDie()

	It("uses the test context", func() {
		currentContext := NewDockerCommand("context", "use", "test-example").ExecOrDie()
		Expect(currentContext).To(ContainSubstring("test-example"))
		output := NewCommand("docker", "context", "ls").ExecOrDie()
		Expect(output).To(ContainSubstring("test-example *"))
	})

	It("can run ps command", func() {
		output := NewDockerCommand("ps").ExecOrDie()
		lines := Lines(output)
		Expect(len(lines)).To(Equal(3))
		Expect(lines[2]).To(ContainSubstring("1234  alpine"))
	})

	It("can run quiet ps command", func() {
		output := NewDockerCommand("ps", "-q").ExecOrDie()
		lines := Lines(output)
		Expect(len(lines)).To(Equal(2))
		Expect(lines[0]).To(Equal("id"))
		Expect(lines[1]).To(Equal("1234"))
	})

	It("can run 'run' command", func() {
		output := NewDockerCommand("run", "nginx", "-p", "80:80").ExecOrDie()
		Expect(output).To(ContainSubstring("Running container \"nginx\" with name"))
	})
}
<|endoftext|>


<commit_before>// +build !amd64,!386 appengine

package roaring

import (
	"encoding/binary"
	"io"
)

func (b *arrayContainer) writeTo(stream io.Writer) (int, error) {
	buf := make([]byte, 2*len(b.content))
	for i, v := range b.content {
		base := i * 2
		buf[base] = byte(v)
		buf[base+1] = byte(v >> 8)
	}
	return stream.Write(buf)
}

func (b *arrayContainer) readFrom(stream io.Reader) (int, error) {
	err := binary.Read(stream, binary.LittleEndian, b.content)
	if err != nil {
		return 0, err
	}
	return 2 * len(b.content), nil
}

func (b *bitmapContainer) writeTo(stream io.Writer) (int, error) {
	// Write set
	buf := make([]byte, 8*len(b.bitmap))
	for i, v := range b.bitmap {
		base := i * 8
		buf[base] = byte(v)
		buf[base+1] = byte(v >> 8)
		buf[base+2] = byte(v >> 16)
		buf[base+3] = byte(v >> 24)
		buf[base+4] = byte(v >> 32)
		buf[base+5] = byte(v >> 40)
		buf[base+6] = byte(v >> 48)
		buf[base+7] = byte(v >> 56)
	}
	return stream.Write(buf)
}

func (b *bitmapContainer) readFrom(stream io.Reader) (int, error) {
	err := binary.Read(stream, binary.LittleEndian, b.bitmap)
	if err != nil {
		return 0, err
	}
	return 8 * len(b.bitmap), nil
}

func (bc *bitmapContainer) asLittleEndianByteSlice() []byte {
	by := make([]byte, len(bc.bitmap)*8)
	for i := range bc.bitmap {
		binary.LittleEndian.PutUint64(by[i*8:], bc.bitmap[i])
	}
	return by
}

func byteSliceAsUint16Slice(slice []byte) []uint16 {
	if len(slice)%2 != 0 {
		panic("Slice size should be divisible by 2")
	}

	b := make([]uint16, len(slice), len(slice))

	for i := range b {
		b[i] = binary.LittleEndian.Uint16(slice[2*i:])
	}

	return b
}

func byteSliceAsUint64Slice(slice []byte) []uint64 {
	if len(slice)%8 != 0 {
		panic("Slice size should be divisible by 8")
	}

	b := make([]uint64, len(slice), len(slice))

	for i := range b {
		b[i] = binary.LittleEndian.Uint64(slice[8*i:])
	}

	return b
}
<commit_msg>Generic bitmapContainer.readFrom() function should compute cardinality<commit_after>// +build !amd64,!386 appengine

package roaring

import (
	"encoding/binary"
	"io"
)

func (b *arrayContainer) writeTo(stream io.Writer) (int, error) {
	buf := make([]byte, 2*len(b.content))
	for i, v := range b.content {
		base := i * 2
		buf[base] = byte(v)
		buf[base+1] = byte(v >> 8)
	}
	return stream.Write(buf)
}

func (b *arrayContainer) readFrom(stream io.Reader) (int, error) {
	err := binary.Read(stream, binary.LittleEndian, b.content)
	if err != nil {
		return 0, err
	}
	return 2 * len(b.content), nil
}

func (b *bitmapContainer) writeTo(stream io.Writer) (int, error) {
	// Write set
	buf := make([]byte, 8*len(b.bitmap))
	for i, v := range b.bitmap {
		base := i * 8
		buf[base] = byte(v)
		buf[base+1] = byte(v >> 8)
		buf[base+2] = byte(v >> 16)
		buf[base+3] = byte(v >> 24)
		buf[base+4] = byte(v >> 32)
		buf[base+5] = byte(v >> 40)
		buf[base+6] = byte(v >> 48)
		buf[base+7] = byte(v >> 56)
	}
	return stream.Write(buf)
}

func (b *bitmapContainer) readFrom(stream io.Reader) (int, error) {
	err := binary.Read(stream, binary.LittleEndian, b.bitmap)
	if err != nil {
		return 0, err
	}
	b.computeCardinality()
	return 8 * len(b.bitmap), nil
}

func (bc *bitmapContainer) asLittleEndianByteSlice() []byte {
	by := make([]byte, len(bc.bitmap)*8)
	for i := range bc.bitmap {
		binary.LittleEndian.PutUint64(by[i*8:], bc.bitmap[i])
	}
	return by
}

func byteSliceAsUint16Slice(slice []byte) []uint16 {
	if len(slice)%2 != 0 {
		panic("Slice size should be divisible by 2")
	}

	b := make([]uint16, len(slice), len(slice))

	for i := range b {
		b[i] = binary.LittleEndian.Uint16(slice[2*i:])
	}

	return b
}

func byteSliceAsUint64Slice(slice []byte) []uint64 {
	if len(slice)%8 != 0 {
		panic("Slice size should be divisible by 8")
	}

	b := make([]uint64, len(slice), len(slice))

	for i := range b {
		b[i] = binary.LittleEndian.Uint64(slice[8*i:])
	}

	return b
}
<|endoftext|>


<commit_before>package utils

import (
	"bytes"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/jwt"
	storage "google.golang.org/api/storage/v1"
	"io/ioutil"

	"github.com/techjanitor/pram-post/config"
)

var gckey []byte

func init() {
	var err error

	gckey, err = ioutil.ReadFile(config.Settings.Google.Key)
	if err != nil {
		panic(err)
	}

}

func getGCS() (service *storage.Service, err error) {

	authconf := &jwt.Config{
		Email:      config.Settings.Google.Auth,
		PrivateKey: gckey,
		Scopes:     []string{storage.DevstorageRead_writeScope},
		TokenURL:   "https://accounts.google.com/o/oauth2/token",
	}

	client := authconf.Client(oauth2.NoContext)

	service, err := storage.New(client)
	if err != nil {
		return errors.New("problem saving file to gcs")
	}

	return

}

func (i *ImageType) UploadGCS() (err error) {
	buffer := bytes.NewReader(i.image)

	object := &storage.Object{Name: i.Filename}

	service, err := getGCS()
	if err != nil {
		return
	}

	_, err = service.Objects.Insert(config.Settings.Google.Bucket, object).Media(buffer).Do()
	if err != nil {
		return errors.New("problem saving file to gcs")
	}

}

func (i *ImageType) DeletGCS() (err error) {

	service, err := getGCS()
	if err != nil {
		return
	}

	err := service.Objects.Delete(config.Settings.Google.Bucket, objectName).Do()
	if err != nil {
		return errors.New("problem deleting gcs file")
	}

}
<commit_msg>add google storage<commit_after>package utils

import (
	"bytes"
	"errors"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/jwt"
	storage "google.golang.org/api/storage/v1"
	"io/ioutil"

	"github.com/techjanitor/pram-post/config"
)

var gckey []byte

func init() {
	var err error

	gckey, err = ioutil.ReadFile(config.Settings.Google.Key)
	if err != nil {
		panic(err)
	}

}

func getGCS() (service *storage.Service, err error) {

	authconf := &jwt.Config{
		Email:      config.Settings.Google.Auth,
		PrivateKey: gckey,
		Scopes:     []string{storage.DevstorageRead_writeScope},
		TokenURL:   "https://accounts.google.com/o/oauth2/token",
	}

	client := authconf.Client(oauth2.NoContext)

	service, err = storage.New(client)
	if err != nil {
		return errors.New("problem saving file to gcs")
	}

	return

}

func (i *ImageType) UploadGCS() (err error) {
	buffer := bytes.NewReader(i.image)

	object := &storage.Object{Name: i.Filename}

	service, err := getGCS()
	if err != nil {
		return
	}

	_, err = service.Objects.Insert(config.Settings.Google.Bucket, object).Media(buffer).Do()
	if err != nil {
		return errors.New("problem saving file to gcs")
	}

	return

}

func DeletGCS(object string) (err error) {

	service, err := getGCS()
	if err != nil {
		return
	}

	err := service.Objects.Delete(config.Settings.Google.Bucket, object).Do()
	if err != nil {
		return errors.New("problem deleting gcs file")
	}

	return

}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io/ioutil"
	"time"
	"strconv"
	"text/template"
	"os"
	"bufio"
	"regexp"
	"flag"
	"encoding/json"
)

var outputPath string = os.Getenv("HOME") + "/public_html/"
const entriesPath = "./entries/"
const templatesPath = "./templates/"
const defaultTemplateFile = "log"
const defaultOutFile = "~log"

type Entry struct {
	Date string
	Body []byte
}

func loadEntry(rawDate string) (*Entry, error) {
	filename := entriesPath + rawDate
	body, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	// Get raw date parts for formatting
	year := rawDate[:4]
	month, moErr := strconv.Atoi(rawDate[4:6])
	if moErr != nil {
		return nil, moErr
	}
	date, dateErr := strconv.Atoi(rawDate[6:8])
	if dateErr != nil {
		return nil, dateErr
	}

	formattedDate := fmt.Sprintf("%d %s %s", date, time.Month(month).String(), year)

	return &Entry{Date: formattedDate, Body: body}, nil
}

func main() {
	fmt.Println()
	fmt.Println("    ~log generator v1.1")
	fmt.Println()

	// Get any arguments
	templateFilePtr := flag.String("t", defaultTemplateFile, "Squigglelog template file (defined name).")
	outFilePtr := flag.String("o", defaultOutFile, "Squigglelog template file (defined name).")
	flag.Parse()

	c := configuration()
	if c = nil {
		c := configure()
	}

	if c != nil {
		templateFilePtr = c.TemplateFile
	}

	entryFiles := getEntries()
	entries := make([]Entry, len(*entryFiles))
	i := 0
	for _, file := range *entryFiles {
		entry, err := loadEntry(file)
		if err != nil {
			fmt.Printf("Error, skipping entry %s: %s\n", file, err)
			continue
		}
		fmt.Printf("Adding entry %s...\n", file)
		entries[i] = *entry
		i++
	}

	fmt.Printf("Using template %s...\n", *templateFilePtr)

	generateLog(entries, *templateFilePtr, *outFilePtr)

	fmt.Printf("Finished! Saved to %s%s.html\n", outputPath, *outFilePtr)
}

type Config struct {
	EntriesPath string
	TemplateFile string
}

func configuration() *Config {
	file, err := os.Open(os.Getenv("HOME") + "/.tildelog")
	if err != nil {
		return nil
	}
	d := json.NewDecoder(file)
	c := Config{}
	err = d.Decode(&c)
	if err != nil {
		fmt.Printf("Couldn't decode configuration: %s\n", err)
	}
	return &c
}

var validFileFormat = regexp.MustCompile("^[0-9]{8}$")

func getEntries() *[]string {
	files, _ := ioutil.ReadDir(entriesPath)
	fileList := make([]string, len(files))
	fileCount := 0
	// Traverse file list in reverse, i.e. newest to oldest
	for i := len(files)-1; i >= 0; i-- {
		file := files[i]
		if validFileFormat.Match([]byte(file.Name())) {
			fileList[fileCount] = file.Name()
			fileCount++
		}
	}
	fileList = fileList[:fileCount]
	return &fileList
}

func generateLog(entries []Entry, templateFile string, outFile string) {
	file, err := os.Create(outputPath + outFile + ".html")
	if err != nil {
		panic(err)
	}

	defer file.Close()

	writer := bufio.NewWriter(file)
	template, err := template.ParseGlob(templatesPath + "*.html")
	if err != nil {
		panic(err)
	}
	template.ExecuteTemplate(writer, templateFile, entries)
	writer.Flush()
}

<commit_msg>Remove unfinished configuration initialization<commit_after>package main

import (
	"fmt"
	"io/ioutil"
	"time"
	"strconv"
	"text/template"
	"os"
	"bufio"
	"regexp"
	"flag"
	"encoding/json"
)

var outputPath string = os.Getenv("HOME") + "/public_html/"
const entriesPath = "./entries/"
const templatesPath = "./templates/"
const defaultTemplateFile = "log"
const defaultOutFile = "~log"

type Entry struct {
	Date string
	Body []byte
}

func loadEntry(rawDate string) (*Entry, error) {
	filename := entriesPath + rawDate
	body, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	// Get raw date parts for formatting
	year := rawDate[:4]
	month, moErr := strconv.Atoi(rawDate[4:6])
	if moErr != nil {
		return nil, moErr
	}
	date, dateErr := strconv.Atoi(rawDate[6:8])
	if dateErr != nil {
		return nil, dateErr
	}

	formattedDate := fmt.Sprintf("%d %s %s", date, time.Month(month).String(), year)

	return &Entry{Date: formattedDate, Body: body}, nil
}

func main() {
	fmt.Println()
	fmt.Println("    ~log generator v1.1")
	fmt.Println()

	// Get any arguments
	templateFilePtr := flag.String("t", defaultTemplateFile, "Squigglelog template file (defined name).")
	outFilePtr := flag.String("o", defaultOutFile, "Squigglelog template file (defined name).")
	flag.Parse()

	entryFiles := getEntries()
	entries := make([]Entry, len(*entryFiles))
	i := 0
	for _, file := range *entryFiles {
		entry, err := loadEntry(file)
		if err != nil {
			fmt.Printf("Error, skipping entry %s: %s\n", file, err)
			continue
		}
		fmt.Printf("Adding entry %s...\n", file)
		entries[i] = *entry
		i++
	}

	fmt.Printf("Using template %s...\n", *templateFilePtr)

	generateLog(entries, *templateFilePtr, *outFilePtr)

	fmt.Printf("Finished! Saved to %s%s.html\n", outputPath, *outFilePtr)
}

type Config struct {
	EntriesPath string
	TemplateFile string
}

func configuration() *Config {
	file, err := os.Open(os.Getenv("HOME") + "/.tildelog")
	if err != nil {
		return nil
	}
	d := json.NewDecoder(file)
	c := Config{}
	err = d.Decode(&c)
	if err != nil {
		fmt.Printf("Couldn't decode configuration: %s\n", err)
	}
	return &c
}

var validFileFormat = regexp.MustCompile("^[0-9]{8}$")

func getEntries() *[]string {
	files, _ := ioutil.ReadDir(entriesPath)
	fileList := make([]string, len(files))
	fileCount := 0
	// Traverse file list in reverse, i.e. newest to oldest
	for i := len(files)-1; i >= 0; i-- {
		file := files[i]
		if validFileFormat.Match([]byte(file.Name())) {
			fileList[fileCount] = file.Name()
			fileCount++
		}
	}
	fileList = fileList[:fileCount]
	return &fileList
}

func generateLog(entries []Entry, templateFile string, outFile string) {
	file, err := os.Create(outputPath + outFile + ".html")
	if err != nil {
		panic(err)
	}

	defer file.Close()

	writer := bufio.NewWriter(file)
	template, err := template.ParseGlob(templatesPath + "*.html")
	if err != nil {
		panic(err)
	}
	template.ExecuteTemplate(writer, templateFile, entries)
	writer.Flush()
}

<|endoftext|>


<commit_before>//  Copyright (c) 2013 Couchbase, Inc.
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
//  except in compliance with the License. You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//  Unless required by applicable law or agreed to in writing, software distributed under the
//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
//  either express or implied. See the License for the specific language governing permissions
//  and limitations under the License.

package couchbase

import (
	"fmt"

	cb "github.com/couchbaselabs/go-couchbase"
	"github.com/couchbaselabs/tuqtng/catalog"
	"github.com/couchbaselabs/tuqtng/query"
)

type site struct {
	client cb.Client
}

func (s *site) URL() string {
	return s.client.BaseURL.String()
}

func (s *site) PoolNames() ([]string, query.Error) {
	// FIXME discover couchbase pools
	return []string{"default"}, nil
}

func (s *site) Pool(name string) (p catalog.Pool, e query.Error) {
	return newPool(s, name)
}

// NewSite creates a new file-based site for the given filepath.
func NewSite(url string) (catalog.Site, query.Error) {

	client, err := cb.Connect(url)

	if err != nil {
		return nil, query.NewError(err, "")
	}

	return &site{client: client}, nil
}

type pool struct {
	site   *site
	name   string
	cbpool cb.Pool
}

func (p *pool) Name() string {
	return p.name
}

func (p *pool) BucketNames() ([]string, query.Error) {
	rv := make([]string, 0, len(p.cbpool.BucketMap))
	for name, _ := range p.cbpool.BucketMap {
		rv = append(rv, name)
	}
	return rv, nil
}

func (p *pool) Bucket(name string) (catalog.Bucket, query.Error) {
	return newBucket(p, name)
}

func newPool(s *site, name string) (*pool, query.Error) {
	cbpool, err := s.client.GetPool(name)
	if err != nil {
		return nil, query.NewError(nil, fmt.Sprintf("Pool %v not found.", name))
	}
	return &pool{
		site:   s,
		name:   name,
		cbpool: cbpool,
	}, nil
}

type bucket struct {
	pool     *pool
	name     string
	scanners map[string]catalog.Scanner
	cbbucket *cb.Bucket
}

func (b *bucket) Name() string {
	return b.name
}

func (b *bucket) Count() (int64, query.Error) {
	return 0, query.NewError(nil, fmt.Sprintf("Count not supported on Couchbase at this time"))
}

func (b *bucket) ScannerNames() ([]string, query.Error) {
	rv := make([]string, 0, len(b.scanners))
	for name, _ := range b.scanners {
		rv = append(rv, name)
	}
	return rv, nil
}

func (b *bucket) Scanners() ([]catalog.Scanner, query.Error) {
	rv := make([]catalog.Scanner, 0, len(b.scanners))
	for _, scanner := range b.scanners {
		rv = append(rv, scanner)
	}
	return rv, nil
}

func (b *bucket) Scanner(name string) (catalog.Scanner, query.Error) {
	scanner, ok := b.scanners[name]
	if !ok {
		return nil, query.NewError(nil, fmt.Sprintf("Scanner %v not found.", name))
	}
	return scanner, nil
}

func (b *bucket) Fetch(id string) (query.Item, query.Error) {
	var doc map[string]query.Value
	err := b.cbbucket.Get(id, &doc)

	if err != nil {
		return nil, query.NewError(err, "")
	}

	meta := map[string]query.Value{
		"id": id,
	}

	item := query.NewMapItem(doc, meta)

	return item, nil
}

func newBucket(p *pool, name string) (*bucket, query.Error) {
	cbbucket, err := p.cbpool.GetBucket(name)
	if err != nil {
		query.NewError(nil, fmt.Sprintf("Bucket %v not found.", name))
	}

	rv := &bucket{
		pool:     p,
		name:     name,
		cbbucket: cbbucket,
	}

	rv.scanners = make(map[string]catalog.Scanner, 1)
	// build scanner
	allDocsScanner, err := newViewScanner(rv, "", "_all_docs")
	if err != nil {
		return nil, query.NewError(err, "")
	}
	rv.scanners[allDocsScanner.Name()] = allDocsScanner

	return rv, nil
}

type viewScanner struct {
	ddoc   string
	view   string
	bucket *bucket
}

func (vs *viewScanner) Name() string {
	return fmt.Sprintf("_design/%s/_view/%s", vs.ddoc, vs.view)
}

func (vs *viewScanner) ScanAll(ch query.ItemChannel, warnch, errch query.ErrorChannel) {
	go vs.scanAll(ch, warnch, errch)
}

func (vs *viewScanner) scanAll(ch query.ItemChannel, warnch, errch query.ErrorChannel) {
	defer close(ch)
	defer close(warnch)
	defer close(errch)

	viewRowChannel := make(chan cb.ViewRow)
	go WalkViewInBatches(viewRowChannel, vs.bucket.cbbucket, vs.ddoc, vs.view, map[string]interface{}{}, 1000)
	for viewRow := range viewRowChannel {
		doc := map[string]query.Value{}
		meta := map[string]query.Value{"id": viewRow.ID}
		ch <- query.NewMapItem(doc, meta)
	}
}

func newViewScanner(b *bucket, ddoc string, view string) (*viewScanner, query.Error) {
	return &viewScanner{
		bucket: b,
		view:   view,
		ddoc:   ddoc,
	}, nil
}
<commit_msg>fix crash querying non-existant bucket<commit_after>//  Copyright (c) 2013 Couchbase, Inc.
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
//  except in compliance with the License. You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//  Unless required by applicable law or agreed to in writing, software distributed under the
//  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
//  either express or implied. See the License for the specific language governing permissions
//  and limitations under the License.

package couchbase

import (
	"fmt"

	cb "github.com/couchbaselabs/go-couchbase"
	"github.com/couchbaselabs/tuqtng/catalog"
	"github.com/couchbaselabs/tuqtng/query"
)

type site struct {
	client cb.Client
}

func (s *site) URL() string {
	return s.client.BaseURL.String()
}

func (s *site) PoolNames() ([]string, query.Error) {
	// FIXME discover couchbase pools
	return []string{"default"}, nil
}

func (s *site) Pool(name string) (p catalog.Pool, e query.Error) {
	return newPool(s, name)
}

// NewSite creates a new file-based site for the given filepath.
func NewSite(url string) (catalog.Site, query.Error) {

	client, err := cb.Connect(url)

	if err != nil {
		return nil, query.NewError(err, "")
	}

	return &site{client: client}, nil
}

type pool struct {
	site   *site
	name   string
	cbpool cb.Pool
}

func (p *pool) Name() string {
	return p.name
}

func (p *pool) BucketNames() ([]string, query.Error) {
	rv := make([]string, 0, len(p.cbpool.BucketMap))
	for name, _ := range p.cbpool.BucketMap {
		rv = append(rv, name)
	}
	return rv, nil
}

func (p *pool) Bucket(name string) (catalog.Bucket, query.Error) {
	return newBucket(p, name)
}

func newPool(s *site, name string) (*pool, query.Error) {
	cbpool, err := s.client.GetPool(name)
	if err != nil {
		return nil, query.NewError(nil, fmt.Sprintf("Pool %v not found.", name))
	}
	return &pool{
		site:   s,
		name:   name,
		cbpool: cbpool,
	}, nil
}

type bucket struct {
	pool     *pool
	name     string
	scanners map[string]catalog.Scanner
	cbbucket *cb.Bucket
}

func (b *bucket) Name() string {
	return b.name
}

func (b *bucket) Count() (int64, query.Error) {
	return 0, query.NewError(nil, fmt.Sprintf("Count not supported on Couchbase at this time"))
}

func (b *bucket) ScannerNames() ([]string, query.Error) {
	rv := make([]string, 0, len(b.scanners))
	for name, _ := range b.scanners {
		rv = append(rv, name)
	}
	return rv, nil
}

func (b *bucket) Scanners() ([]catalog.Scanner, query.Error) {
	rv := make([]catalog.Scanner, 0, len(b.scanners))
	for _, scanner := range b.scanners {
		rv = append(rv, scanner)
	}
	return rv, nil
}

func (b *bucket) Scanner(name string) (catalog.Scanner, query.Error) {
	scanner, ok := b.scanners[name]
	if !ok {
		return nil, query.NewError(nil, fmt.Sprintf("Scanner %v not found.", name))
	}
	return scanner, nil
}

func (b *bucket) Fetch(id string) (query.Item, query.Error) {
	var doc map[string]query.Value
	err := b.cbbucket.Get(id, &doc)

	if err != nil {
		return nil, query.NewError(err, "")
	}

	meta := map[string]query.Value{
		"id": id,
	}

	item := query.NewMapItem(doc, meta)

	return item, nil
}

func newBucket(p *pool, name string) (*bucket, query.Error) {
	cbbucket, err := p.cbpool.GetBucket(name)
	if err != nil {
		return nil, query.NewError(nil, fmt.Sprintf("Bucket %v not found.", name))
	}

	rv := &bucket{
		pool:     p,
		name:     name,
		cbbucket: cbbucket,
	}

	rv.scanners = make(map[string]catalog.Scanner, 1)
	// build scanner
	allDocsScanner, err := newViewScanner(rv, "", "_all_docs")
	if err != nil {
		return nil, query.NewError(err, "")
	}
	rv.scanners[allDocsScanner.Name()] = allDocsScanner

	return rv, nil
}

type viewScanner struct {
	ddoc   string
	view   string
	bucket *bucket
}

func (vs *viewScanner) Name() string {
	return fmt.Sprintf("_design/%s/_view/%s", vs.ddoc, vs.view)
}

func (vs *viewScanner) ScanAll(ch query.ItemChannel, warnch, errch query.ErrorChannel) {
	go vs.scanAll(ch, warnch, errch)
}

func (vs *viewScanner) scanAll(ch query.ItemChannel, warnch, errch query.ErrorChannel) {
	defer close(ch)
	defer close(warnch)
	defer close(errch)

	viewRowChannel := make(chan cb.ViewRow)
	go WalkViewInBatches(viewRowChannel, vs.bucket.cbbucket, vs.ddoc, vs.view, map[string]interface{}{}, 1000)
	for viewRow := range viewRowChannel {
		doc := map[string]query.Value{}
		meta := map[string]query.Value{"id": viewRow.ID}
		ch <- query.NewMapItem(doc, meta)
	}
}

func newViewScanner(b *bucket, ddoc string, view string) (*viewScanner, query.Error) {
	return &viewScanner{
		bucket: b,
		view:   view,
		ddoc:   ddoc,
	}, nil
}
<|endoftext|>


<commit_before>package cli

import (
	"fmt"
	"time"

	g "github.com/onsi/ginkgo"
	o "github.com/onsi/gomega"

	"k8s.io/apimachinery/pkg/util/wait"

	exutil "github.com/openshift/origin/test/extended/util"
)

var (
	buildTimeout = 5 * time.Minute
)

var _ = g.Describe("[sig-cli] oc debug", func() {
	defer g.GinkgoRecover()

	oc := exutil.NewCLI("oc-debug")
	testCLIDebug := exutil.FixturePath("testdata", "test-cli-debug.yaml")
	testDeploymentConfig := exutil.FixturePath("testdata", "test-deployment-config.yaml")
	testReplicationController := exutil.FixturePath("testdata", "test-replication-controller.yaml")
	helloPod := exutil.FixturePath("..", "..", "examples", "hello-openshift", "hello-pod.json")
	imageStreamsCentos := exutil.FixturePath("..", "..", "examples", "image-streams", "image-streams-centos7.json")

	g.It("deployment configs from a build", func() {
		err := oc.Run("create").Args("-f", testCLIDebug).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())
		// wait for image stream to be present which means the build has completed
		err = wait.Poll(cliInterval, buildTimeout, func() (bool, error) {
			err := oc.Run("get").Args("imagestreamtags", "local-busybox:latest").Execute()
			return err == nil, nil
		})
		o.Expect(err).NotTo(o.HaveOccurred())
		// and for replication controller which means we can kick of debug session
		err = wait.Poll(cliInterval, buildTimeout, func() (bool, error) {
			err := oc.Run("get").Args("replicationcontrollers", "local-busybox1-1").Execute()
			return err == nil, nil
		})
		o.Expect(err).NotTo(o.HaveOccurred())

		g.By("should print the imagestream-based container entrypoint/command")
		var out string
		out, err = oc.Run("debug").Args("dc/local-busybox1").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("Starting pod/local-busybox1-debug, command was: /usr/bin/bash\n"))

		g.By("should print the overridden imagestream-based container entrypoint/command")
		out, err = oc.Run("debug").Args("dc/local-busybox2").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("Starting pod/local-busybox2-debug, command was: foo bar baz qux\n"))

		g.By("should print the container image-based container entrypoint/command")
		out, err = oc.Run("debug").Args("dc/busybox1").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("Starting pod/busybox1-debug ...\n"))

		g.By("should print the overridden container image-based container entrypoint/command")
		out, err = oc.Run("debug").Args("dc/busybox2").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("Starting pod/busybox2-debug, command was: foo bar baz qux\n"))
	})

	g.It("dissect deployment config debug", func() {
		err := oc.Run("create").Args("-f", testDeploymentConfig).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		var out string
		out, err = oc.Run("debug").Args("dc/test-deployment-config", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("- /bin/sh"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--keep-annotations", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("annotations:"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--as-root", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("runAsUser: 0"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--as-root=false", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("runAsNonRoot: true"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--as-user=1", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("runAsUser: 1"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "-t", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("stdinOnce"))
		o.Expect(out).To(o.ContainSubstring("tty"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--tty=false", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).NotTo(o.ContainSubstring("tty"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "-oyaml", "--", "/bin/env").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("- /bin/env"))
		o.Expect(out).NotTo(o.ContainSubstring("stdin"))
		o.Expect(out).NotTo(o.ContainSubstring("tty"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--node-name=invalid", "--", "/bin/env").Output()
		o.Expect(err).To(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring(`on node "invalid"`))
	})

	g.It("does not require a real resource on the server", func() {
		out, err := oc.Run("debug").Args("-T", "-f", helloPod, "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).NotTo(o.ContainSubstring("tty"))

		err = oc.Run("debug").Args("-f", helloPod, "--keep-liveness", "--keep-readiness", "-oyaml").Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		out, err = oc.Run("debug").Args("-f", helloPod, "-oyaml", "--", "/bin/env").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("- /bin/env"))
		o.Expect(out).NotTo(o.ContainSubstring("stdin"))
		o.Expect(out).NotTo(o.ContainSubstring("tty"))
	})

	// TODO: write a test that emulates a TTY to verify the correct defaulting of what the pod is created

	g.It("ensure debug does not depend on a container actually existing for the selected resource", func() {
		err := oc.Run("create").Args("-f", testReplicationController).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())
		err = oc.Run("create").Args("-f", testDeploymentConfig).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		// The command should not hang waiting for an attachable pod. Timeout each cmd after 10s.
		err = oc.Run("scale").Args("--replicas=0", "rc/test-replication-controller").Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		var out string
		out, err = oc.Run("debug").Args("--request-timeout=10s", "-c", "ruby-helloworld", "--one-container", "rc/test-replication-controller", "-o", "jsonpath='{.metadata.name}").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("test-replication-controller-debug"))

		err = oc.Run("scale").Args("--replicas=0", "dc/test-deployment-config").Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		out, err = oc.Run("debug").Args("--request-timeout=10s", "-c", "ruby-helloworld", "--one-container", "dc/test-deployment-config", "-o", "jsonpath='{.metadata.name}").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("test-deployment-config"))

		err = oc.Run("create").Args("-f", "-").InputString(`
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-deployment
  labels:
    deployment: test-deployment
spec:
  replicas: 0
  selector:
    matchLabels:
      deployment: test-deployment
  template:
    metadata:
      labels:
        deployment: test-deployment
      name: test-deployment
    spec:
      containers:
      - name: ruby-helloworld
        image: openshift/origin-pod
        imagePullPolicy: IfNotPresent
`).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		out, err = oc.Run("debug").Args("--request-timeout=10s", "-c", "ruby-helloworld", "--one-container", "deploy/test-deployment", "-o", "jsonpath='{.metadata.name}").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("test-deployment-debug"))
	})

	g.It("ensure it works with image streams", func() {
		err := oc.Run("create").Args("-f", imageStreamsCentos).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())
		err = wait.Poll(cliInterval, cliTimeout, func() (bool, error) {
			err := oc.Run("get").Args("imagestreamtags", "wildfly:latest").Execute()
			return err == nil, nil
		})
		o.Expect(err).NotTo(o.HaveOccurred())

		var out string
		out, err = oc.Run("debug").Args("istag/wildfly:latest", "-o", "yaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.MatchRegexp("image:.*oc-debug-.*/wildfly@sha256"))

		var sha string
		sha, err = oc.Run("get").Args("istag/wildfly:latest", "--template", "{{ .image.metadata.name }}").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		out, err = oc.Run("debug").Args(fmt.Sprintf("isimage/wildfly@%s", sha), "-o", "yaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("image: docker.io/openshift/wildfly-150-centos7"))
	})
})
<commit_msg>Bug 1896188: bump build timeout to 10m and deploy to 2m in oc debug<commit_after>package cli

import (
	"fmt"
	"time"

	g "github.com/onsi/ginkgo"
	o "github.com/onsi/gomega"

	"k8s.io/apimachinery/pkg/util/wait"

	exutil "github.com/openshift/origin/test/extended/util"
)

var (
	buildTimeout  = 10 * time.Minute
	deployTimeout = 2 * time.Minute
)

var _ = g.Describe("[sig-cli] oc debug", func() {
	defer g.GinkgoRecover()

	oc := exutil.NewCLI("oc-debug")
	testCLIDebug := exutil.FixturePath("testdata", "test-cli-debug.yaml")
	testDeploymentConfig := exutil.FixturePath("testdata", "test-deployment-config.yaml")
	testReplicationController := exutil.FixturePath("testdata", "test-replication-controller.yaml")
	helloPod := exutil.FixturePath("..", "..", "examples", "hello-openshift", "hello-pod.json")
	imageStreamsCentos := exutil.FixturePath("..", "..", "examples", "image-streams", "image-streams-centos7.json")

	g.It("deployment configs from a build", func() {
		err := oc.Run("create").Args("-f", testCLIDebug).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())
		// wait for image stream to be present which means the build has completed
		err = wait.Poll(cliInterval, buildTimeout, func() (bool, error) {
			err := oc.Run("get").Args("imagestreamtags", "local-busybox:latest").Execute()
			return err == nil, nil
		})
		o.Expect(err).NotTo(o.HaveOccurred())
		// and for replication controller which means we can kick of debug session
		err = wait.Poll(cliInterval, deployTimeout, func() (bool, error) {
			err := oc.Run("get").Args("replicationcontrollers", "local-busybox1-1").Execute()
			return err == nil, nil
		})
		o.Expect(err).NotTo(o.HaveOccurred())

		g.By("should print the imagestream-based container entrypoint/command")
		var out string
		out, err = oc.Run("debug").Args("dc/local-busybox1").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("Starting pod/local-busybox1-debug, command was: /usr/bin/bash\n"))

		g.By("should print the overridden imagestream-based container entrypoint/command")
		out, err = oc.Run("debug").Args("dc/local-busybox2").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("Starting pod/local-busybox2-debug, command was: foo bar baz qux\n"))

		g.By("should print the container image-based container entrypoint/command")
		out, err = oc.Run("debug").Args("dc/busybox1").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("Starting pod/busybox1-debug ...\n"))

		g.By("should print the overridden container image-based container entrypoint/command")
		out, err = oc.Run("debug").Args("dc/busybox2").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("Starting pod/busybox2-debug, command was: foo bar baz qux\n"))
	})

	g.It("dissect deployment config debug", func() {
		err := oc.Run("create").Args("-f", testDeploymentConfig).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		var out string
		out, err = oc.Run("debug").Args("dc/test-deployment-config", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("- /bin/sh"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--keep-annotations", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("annotations:"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--as-root", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("runAsUser: 0"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--as-root=false", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("runAsNonRoot: true"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--as-user=1", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("runAsUser: 1"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "-t", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("stdinOnce"))
		o.Expect(out).To(o.ContainSubstring("tty"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--tty=false", "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).NotTo(o.ContainSubstring("tty"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "-oyaml", "--", "/bin/env").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("- /bin/env"))
		o.Expect(out).NotTo(o.ContainSubstring("stdin"))
		o.Expect(out).NotTo(o.ContainSubstring("tty"))

		out, err = oc.Run("debug").Args("dc/test-deployment-config", "--node-name=invalid", "--", "/bin/env").Output()
		o.Expect(err).To(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring(`on node "invalid"`))
	})

	g.It("does not require a real resource on the server", func() {
		out, err := oc.Run("debug").Args("-T", "-f", helloPod, "-oyaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).NotTo(o.ContainSubstring("tty"))

		err = oc.Run("debug").Args("-f", helloPod, "--keep-liveness", "--keep-readiness", "-oyaml").Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		out, err = oc.Run("debug").Args("-f", helloPod, "-oyaml", "--", "/bin/env").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("- /bin/env"))
		o.Expect(out).NotTo(o.ContainSubstring("stdin"))
		o.Expect(out).NotTo(o.ContainSubstring("tty"))
	})

	// TODO: write a test that emulates a TTY to verify the correct defaulting of what the pod is created

	g.It("ensure debug does not depend on a container actually existing for the selected resource", func() {
		err := oc.Run("create").Args("-f", testReplicationController).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())
		err = oc.Run("create").Args("-f", testDeploymentConfig).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		// The command should not hang waiting for an attachable pod. Timeout each cmd after 10s.
		err = oc.Run("scale").Args("--replicas=0", "rc/test-replication-controller").Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		var out string
		out, err = oc.Run("debug").Args("--request-timeout=10s", "-c", "ruby-helloworld", "--one-container", "rc/test-replication-controller", "-o", "jsonpath='{.metadata.name}").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("test-replication-controller-debug"))

		err = oc.Run("scale").Args("--replicas=0", "dc/test-deployment-config").Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		out, err = oc.Run("debug").Args("--request-timeout=10s", "-c", "ruby-helloworld", "--one-container", "dc/test-deployment-config", "-o", "jsonpath='{.metadata.name}").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("test-deployment-config"))

		err = oc.Run("create").Args("-f", "-").InputString(`
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-deployment
  labels:
    deployment: test-deployment
spec:
  replicas: 0
  selector:
    matchLabels:
      deployment: test-deployment
  template:
    metadata:
      labels:
        deployment: test-deployment
      name: test-deployment
    spec:
      containers:
      - name: ruby-helloworld
        image: openshift/origin-pod
        imagePullPolicy: IfNotPresent
`).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())

		out, err = oc.Run("debug").Args("--request-timeout=10s", "-c", "ruby-helloworld", "--one-container", "deploy/test-deployment", "-o", "jsonpath='{.metadata.name}").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("test-deployment-debug"))
	})

	g.It("ensure it works with image streams", func() {
		err := oc.Run("create").Args("-f", imageStreamsCentos).Execute()
		o.Expect(err).NotTo(o.HaveOccurred())
		err = wait.Poll(cliInterval, cliTimeout, func() (bool, error) {
			err := oc.Run("get").Args("imagestreamtags", "wildfly:latest").Execute()
			return err == nil, nil
		})
		o.Expect(err).NotTo(o.HaveOccurred())

		var out string
		out, err = oc.Run("debug").Args("istag/wildfly:latest", "-o", "yaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.MatchRegexp("image:.*oc-debug-.*/wildfly@sha256"))

		var sha string
		sha, err = oc.Run("get").Args("istag/wildfly:latest", "--template", "{{ .image.metadata.name }}").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		out, err = oc.Run("debug").Args(fmt.Sprintf("isimage/wildfly@%s", sha), "-o", "yaml").Output()
		o.Expect(err).NotTo(o.HaveOccurred())
		o.Expect(out).To(o.ContainSubstring("image: docker.io/openshift/wildfly-150-centos7"))
	})
})
<|endoftext|>


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

// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file contains the system primitive functions.

package golisp

import (
	"fmt"
	"os"
	"strings"
	"time"
)

var symbolCounts map[string]int = make(map[string]int)

func RegisterSystemPrimitives() {
	MakePrimitiveFunction("load", "1", LoadFileImpl)
	MakePrimitiveFunction("sleep", "1", SleepImpl)
	MakePrimitiveFunction("millis", "0", MillisImpl)
	MakePrimitiveFunction("newline", "0", NewlineImpl)
	MakePrimitiveFunction("write", "1", WriteImpl)
	MakePrimitiveFunction("write-line", "1", WriteLineImpl)
	MakePrimitiveFunction("str", "*", MakeStringImpl)
	MakePrimitiveFunction("intern", "1", InternImpl)
	MakePrimitiveFunction("quit", "0", QuitImpl)
	MakePrimitiveFunction("gensym", "0|1", GensymImpl)
	MakePrimitiveFunction("eval", "1|2", EvalImpl)
	MakePrimitiveFunction("global-eval", "1", GlobalEvalImpl)
	MakeSpecialForm("time", "1", TimeImpl)
}

func LoadFileImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	filename := Car(args)
	if !StringP(filename) {
		err = ProcessError("Filename must be a string", env)
		return
	}

	return ProcessFile(StringValue(filename))
}

func QuitImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	WriteHistoryToFile(".golisp_history")
	fmt.Printf("\n\nGoodbye.\n\n")
	os.Exit(0)
	return
}

func SleepImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	n := Car(args)
	if !IntegerP(n) {
		err = ProcessError(fmt.Sprintf("Number expected, received %s", String(n)), env)
		return
	}
	millis := IntegerValue(n)
	time.Sleep(time.Duration(millis) * time.Millisecond)
	return
}

func MillisImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	result = IntegerWithValue(int64(time.Now().UnixNano() / 1e6))
	return
}

func NewlineImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	fmt.Printf("\n")
	return
}

func WriteImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	fmt.Printf(PrintString(Car(args)))
	return
}

func WriteLineImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	fmt.Printf("%s\n", PrintString(Car(args)))
	return
}

func MakeStringImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	if NilP(args) || Length(args) == 0 {
		return StringWithValue("()"), nil
	}

	pieces := make([]string, 2)
	for cell := args; NotNilP(cell); cell = Cdr(cell) {
		pieces = append(pieces, PrintString(Car(cell)))
	}
	return StringWithValue(strings.Join(pieces, "")), nil
}

func TimeImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	fmt.Printf("Starting timer.\n")
	startTime := time.Now()

	for cell := args; NotNilP(cell); cell = Cdr(cell) {
		sexpr := Car(cell)
		result, err = Eval(sexpr, env)
		if err != nil {
			break
		}
	}

	d := time.Since(startTime)
	fmt.Printf("Stopped timer.\nTook %v to run.\n", d)
	result = IntegerWithValue(int64(d.Nanoseconds() / 1000000))
	return
}

func InternImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	sym := Car(args)
	if !StringP(sym) {
		err = ProcessError(fmt.Sprintf("intern expects a string, but received %s.", String(sym)), env)
		return
	}

	return Intern(StringValue(sym)), nil
}

func GensymImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	var prefix string
	if Length(args) > 1 {
		err = ProcessError(fmt.Sprintf("gensym expects 0 or 1 argument, but received %d.", Length(args)), env)
		return
	}

	if Length(args) == 0 {
		prefix = "GENSYM"
	} else {
		arg := Car(args)
		if !StringP(arg) && !SymbolP(arg) {
			err = ProcessError(fmt.Sprintf("gensym expects a string or symbol, but recieved %s.", String(arg)), env)
			return
		}
		prefix = StringValue(arg)
	}

	count := symbolCounts[prefix]
	if count == 0 {
		count = 1
	} else {
		count += 1
	}
	symbolCounts[prefix] = count
	result = Intern(fmt.Sprintf("%s-%d", prefix, count))
	return
}

func EvalImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	var evalEnv *SymbolTableFrame
	sexpr := Car(args)
	if Length(args) == 2 {
		if !EnvironmentP(Cadr(args)) {
			err = ProcessError(fmt.Sprintf("eval expects an environment as it's second argument, but recieved %s.", String(Cadr(args))), env)
			return
		}
		evalEnv = EnvironmentValue(Cadr(args))
	} else {
		evalEnv = env
	}
	return Eval(sexpr, evalEnv)
}

func GlobalEvalImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	return Eval(Car(args), Global)
}
<commit_msg>Have write and write-line concat the string forms of their args, so as not to require the use of str.<commit_after>// Copyright 2014 SteelSeries ApS.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This package implements a basic LISP interpretor for embedding in a go program for scripting.
// This file contains the system primitive functions.

package golisp

import (
	"fmt"
	"os"
	"strings"
	"time"
)

var symbolCounts map[string]int = make(map[string]int)

func RegisterSystemPrimitives() {
	MakePrimitiveFunction("load", "1", LoadFileImpl)
	MakePrimitiveFunction("sleep", "1", SleepImpl)
	MakePrimitiveFunction("millis", "0", MillisImpl)
	MakePrimitiveFunction("newline", "0", NewlineImpl)
	MakePrimitiveFunction("write", "*", WriteImpl)
	MakePrimitiveFunction("write-line", "*", WriteLineImpl)
	MakePrimitiveFunction("str", "*", MakeStringImpl)
	MakePrimitiveFunction("intern", "1", InternImpl)
	MakePrimitiveFunction("quit", "0", QuitImpl)
	MakePrimitiveFunction("gensym", "0|1", GensymImpl)
	MakePrimitiveFunction("eval", "1|2", EvalImpl)
	MakePrimitiveFunction("global-eval", "1", GlobalEvalImpl)
	MakeSpecialForm("time", "1", TimeImpl)
}

func LoadFileImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	filename := Car(args)
	if !StringP(filename) {
		err = ProcessError("Filename must be a string", env)
		return
	}

	return ProcessFile(StringValue(filename))
}

func QuitImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	WriteHistoryToFile(".golisp_history")
	fmt.Printf("\n\nGoodbye.\n\n")
	os.Exit(0)
	return
}

func SleepImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	n := Car(args)
	if !IntegerP(n) {
		err = ProcessError(fmt.Sprintf("Number expected, received %s", String(n)), env)
		return
	}
	millis := IntegerValue(n)
	time.Sleep(time.Duration(millis) * time.Millisecond)
	return
}

func MillisImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	result = IntegerWithValue(int64(time.Now().UnixNano() / 1e6))
	return
}

func NewlineImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	println("")
	return
}

func concatStringForms(args *Data) (str string) {
	if NilP(args) || Length(args) == 0 {
		return "()"
	}
	pieces := make([]string, 2)
	for cell := args; NotNilP(cell); cell = Cdr(cell) {
		pieces = append(pieces, PrintString(Car(cell)))
	}
	return strings.Join(pieces, "")
}

func WriteImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	print(concatStringForms(args))
	return
}

func WriteLineImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	println(concatStringForms(args))
	return
}

func MakeStringImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	return StringWithValue(concatStringForms(args)), nil
}

func TimeImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	fmt.Printf("Starting timer.\n")
	startTime := time.Now()

	for cell := args; NotNilP(cell); cell = Cdr(cell) {
		sexpr := Car(cell)
		result, err = Eval(sexpr, env)
		if err != nil {
			break
		}
	}

	d := time.Since(startTime)
	fmt.Printf("Stopped timer.\nTook %v to run.\n", d)
	result = IntegerWithValue(int64(d.Nanoseconds() / 1000000))
	return
}

func InternImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	sym := Car(args)
	if !StringP(sym) {
		err = ProcessError(fmt.Sprintf("intern expects a string, but received %s.", String(sym)), env)
		return
	}

	return Intern(StringValue(sym)), nil
}

func GensymImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	var prefix string
	if Length(args) > 1 {
		err = ProcessError(fmt.Sprintf("gensym expects 0 or 1 argument, but received %d.", Length(args)), env)
		return
	}

	if Length(args) == 0 {
		prefix = "GENSYM"
	} else {
		arg := Car(args)
		if !StringP(arg) && !SymbolP(arg) {
			err = ProcessError(fmt.Sprintf("gensym expects a string or symbol, but recieved %s.", String(arg)), env)
			return
		}
		prefix = StringValue(arg)
	}

	count := symbolCounts[prefix]
	if count == 0 {
		count = 1
	} else {
		count += 1
	}
	symbolCounts[prefix] = count
	result = Intern(fmt.Sprintf("%s-%d", prefix, count))
	return
}

func EvalImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	var evalEnv *SymbolTableFrame
	sexpr := Car(args)
	if Length(args) == 2 {
		if !EnvironmentP(Cadr(args)) {
			err = ProcessError(fmt.Sprintf("eval expects an environment as it's second argument, but recieved %s.", String(Cadr(args))), env)
			return
		}
		evalEnv = EnvironmentValue(Cadr(args))
	} else {
		evalEnv = env
	}
	return Eval(sexpr, evalEnv)
}

func GlobalEvalImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
	return Eval(Car(args), Global)
}
<|endoftext|>


<commit_before>package probe

import (
	"encoding/json"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/gophergala/goffee/data"
	"github.com/gophergala/goffee/queue"
	"github.com/gophergala/goffee/tor"
)

const ipReflector = "https://goffee.herokuapp.com/ip"
const ipRefreshInterval = 5 * time.Minute

type IPResponse struct {
	IP      string
	Country string
}

var (
	exit           = make(chan bool)
	lastIPChange   time.Time
	currentIP      string
	currentCountry string
)

func Run() {
	go run()
}

func run() {
	var wg sync.WaitGroup
	newip()

	for { // ever
		batch := queue.FetchBatch()
		for _, item := range batch {
			wg.Add(1)
			go check(item, &wg)
		}

		wg.Wait()

		if time.Since(lastIPChange) > ipRefreshInterval {
			newip()
		}
	}
}

func newip() {
	tor.NewIP()
	body, err := tor.TorGet(ipReflector)
	if err != nil {
		return
	}
	var response IPResponse
	err = json.Unmarshal([]byte(body), &response)
	if err != nil {
		return
	}
	currentIP = response.IP
	currentCountry = response.Country
	lastIPChange = time.Now()
}

func check(address string, wg *sync.WaitGroup) {
	defer wg.Done()

	status, err := tor.TorGetStatus(address)

	var statusCode int
	if err != nil {
		statusCode = -1
	} else {
		statusCode, err = strconv.Atoi(strings.Split(status, " ")[0])
		if err != nil {
			statusCode = -2
		}
	}

	result := &data.Result{
		CreatedAt: time.Now(),
		Status:    statusCode,
		Success:   statusCode >= 200 && statusCode < 300,
		URL:       address,
		IP:        currentIP,
		Country:   currentCountry,
	}

	data, err := json.Marshal(result)
	if err != nil {
		return
	}

	queue.WriteResult(string(data))
}

func Wait() {
	<-exit
}
<commit_msg>Probe locally if Tor fails<commit_after>package probe

import (
	"encoding/json"
	"net/http"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/gophergala/goffee/data"
	"github.com/gophergala/goffee/queue"
	"github.com/gophergala/goffee/tor"
)

const ipReflector = "https://goffee.herokuapp.com/ip"
const ipRefreshInterval = 5 * time.Minute

type IPResponse struct {
	IP      string
	Country string
}

var (
	exit           = make(chan bool)
	lastIPChange   time.Time
	currentIP      string
	currentCountry string
)

func Run() {
	go run()
}

func run() {
	var wg sync.WaitGroup
	newip()

	for { // ever
		batch := queue.FetchBatch()
		for _, item := range batch {
			wg.Add(1)
			go check(item, &wg)
		}

		wg.Wait()

		if time.Since(lastIPChange) > ipRefreshInterval {
			newip()
		}
	}
}

func newip() {
	tor.NewIP()
	body, err := tor.TorGet(ipReflector)
	if err != nil {
		return
	}
	var response IPResponse
	err = json.Unmarshal([]byte(body), &response)
	if err != nil {
		return
	}
	currentIP = response.IP
	currentCountry = response.Country
	lastIPChange = time.Now()
}

func check(address string, wg *sync.WaitGroup) {
	defer wg.Done()

	status, err := tor.TorGetStatus(address)

	var statusCode int
	if err != nil {
		statusCode = -1
	} else {
		statusCode, err = strconv.Atoi(strings.Split(status, " ")[0])
		if err != nil {
			statusCode = -2
		}
	}

	// If Tor fails, try locally.
	if statusCode == -1 {
		resp, err := http.Get(address)
		if err == nil {
			defer resp.Body.Close()
			statusCode = resp.StatusCode
		}
	}

	result := &data.Result{
		CreatedAt: time.Now(),
		Status:    statusCode,
		Success:   statusCode >= 200 && statusCode < 300,
		URL:       address,
		IP:        currentIP,
		Country:   currentCountry,
	}

	data, err := json.Marshal(result)
	if err != nil {
		return
	}

	queue.WriteResult(string(data))
}

func Wait() {
	<-exit
}
<|endoftext|>


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

package ssh

import (
	"bytes"
	"crypto"
	"crypto/aes"
	"crypto/rand"
	"testing"
)

func TestDefaultCiphersExist(t *testing.T) {
	for _, cipherAlgo := range supportedCiphers {
		if _, ok := cipherModes[cipherAlgo]; !ok {
			t.Errorf("supported cipher %q is unknown", cipherAlgo)
		}
	}
	for _, cipherAlgo := range preferredCiphers {
		if _, ok := cipherModes[cipherAlgo]; !ok {
			t.Errorf("preferred cipher %q is unknown", cipherAlgo)
		}
	}
}

func TestPacketCiphers(t *testing.T) {
	defaultMac := "hmac-sha2-256"
	defaultCipher := "aes128-ctr"
	for cipher := range cipherModes {
		t.Run("cipher="+cipher,
			func(t *testing.T) { testPacketCipher(t, cipher, defaultMac) })
	}
	for mac := range macModes {
		t.Run("mac="+mac,
			func(t *testing.T) { testPacketCipher(t, defaultCipher, mac) })
	}
}

func testPacketCipher(t *testing.T, cipher, mac string) {
	kr := &kexResult{Hash: crypto.SHA1}
	algs := directionAlgorithms{
		Cipher:      cipher,
		MAC:         mac,
		Compression: "none",
	}
	client, err := newPacketCipher(clientKeys, algs, kr)
	if err != nil {
		t.Fatalf("newPacketCipher(client, %q, %q): %v", cipher, mac, err)
	}
	server, err := newPacketCipher(clientKeys, algs, kr)
	if err != nil {
		t.Fatalf("newPacketCipher(client, %q, %q): %v", cipher, mac, err)
	}

	want := "bla bla"
	input := []byte(want)
	buf := &bytes.Buffer{}
	if err := client.writePacket(0, buf, rand.Reader, input); err != nil {
		t.Fatalf("writePacket(%q, %q): %v", cipher, mac, err)
	}

	packet, err := server.readPacket(0, buf)
	if err != nil {
		t.Fatalf("readPacket(%q, %q): %v", cipher, mac, err)
	}

	if string(packet) != want {
		t.Errorf("roundtrip(%q, %q): got %q, want %q", cipher, mac, packet, want)
	}
}

func TestCBCOracleCounterMeasure(t *testing.T) {
	cipherModes[aes128cbcID] = &streamCipherMode{16, aes.BlockSize, 0, nil}
	defer delete(cipherModes, aes128cbcID)

	kr := &kexResult{Hash: crypto.SHA1}
	algs := directionAlgorithms{
		Cipher:      aes128cbcID,
		MAC:         "hmac-sha1",
		Compression: "none",
	}
	client, err := newPacketCipher(clientKeys, algs, kr)
	if err != nil {
		t.Fatalf("newPacketCipher(client): %v", err)
	}

	want := "bla bla"
	input := []byte(want)
	buf := &bytes.Buffer{}
	if err := client.writePacket(0, buf, rand.Reader, input); err != nil {
		t.Errorf("writePacket: %v", err)
	}

	packetSize := buf.Len()
	buf.Write(make([]byte, 2*maxPacket))

	// We corrupt each byte, but this usually will only test the
	// 'packet too large' or 'MAC failure' cases.
	lastRead := -1
	for i := 0; i < packetSize; i++ {
		server, err := newPacketCipher(clientKeys, algs, kr)
		if err != nil {
			t.Fatalf("newPacketCipher(client): %v", err)
		}

		fresh := &bytes.Buffer{}
		fresh.Write(buf.Bytes())
		fresh.Bytes()[i] ^= 0x01

		before := fresh.Len()
		_, err = server.readPacket(0, fresh)
		if err == nil {
			t.Errorf("corrupt byte %d: readPacket succeeded ", i)
			continue
		}
		if _, ok := err.(cbcError); !ok {
			t.Errorf("corrupt byte %d: got %v (%T), want cbcError", i, err, err)
			continue
		}

		after := fresh.Len()
		bytesRead := before - after
		if bytesRead < maxPacket {
			t.Errorf("corrupt byte %d: read %d bytes, want more than %d", i, bytesRead, maxPacket)
			continue
		}

		if i > 0 && bytesRead != lastRead {
			t.Errorf("corrupt byte %d: read %d bytes, want %d bytes read", i, bytesRead, lastRead)
		}
		lastRead = bytesRead
	}
}
<commit_msg>ssh: remove redundant code<commit_after>// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package ssh

import (
	"bytes"
	"crypto"
	"crypto/rand"
	"testing"
)

func TestDefaultCiphersExist(t *testing.T) {
	for _, cipherAlgo := range supportedCiphers {
		if _, ok := cipherModes[cipherAlgo]; !ok {
			t.Errorf("supported cipher %q is unknown", cipherAlgo)
		}
	}
	for _, cipherAlgo := range preferredCiphers {
		if _, ok := cipherModes[cipherAlgo]; !ok {
			t.Errorf("preferred cipher %q is unknown", cipherAlgo)
		}
	}
}

func TestPacketCiphers(t *testing.T) {
	defaultMac := "hmac-sha2-256"
	defaultCipher := "aes128-ctr"
	for cipher := range cipherModes {
		t.Run("cipher="+cipher,
			func(t *testing.T) { testPacketCipher(t, cipher, defaultMac) })
	}
	for mac := range macModes {
		t.Run("mac="+mac,
			func(t *testing.T) { testPacketCipher(t, defaultCipher, mac) })
	}
}

func testPacketCipher(t *testing.T, cipher, mac string) {
	kr := &kexResult{Hash: crypto.SHA1}
	algs := directionAlgorithms{
		Cipher:      cipher,
		MAC:         mac,
		Compression: "none",
	}
	client, err := newPacketCipher(clientKeys, algs, kr)
	if err != nil {
		t.Fatalf("newPacketCipher(client, %q, %q): %v", cipher, mac, err)
	}
	server, err := newPacketCipher(clientKeys, algs, kr)
	if err != nil {
		t.Fatalf("newPacketCipher(client, %q, %q): %v", cipher, mac, err)
	}

	want := "bla bla"
	input := []byte(want)
	buf := &bytes.Buffer{}
	if err := client.writePacket(0, buf, rand.Reader, input); err != nil {
		t.Fatalf("writePacket(%q, %q): %v", cipher, mac, err)
	}

	packet, err := server.readPacket(0, buf)
	if err != nil {
		t.Fatalf("readPacket(%q, %q): %v", cipher, mac, err)
	}

	if string(packet) != want {
		t.Errorf("roundtrip(%q, %q): got %q, want %q", cipher, mac, packet, want)
	}
}

func TestCBCOracleCounterMeasure(t *testing.T) {
	kr := &kexResult{Hash: crypto.SHA1}
	algs := directionAlgorithms{
		Cipher:      aes128cbcID,
		MAC:         "hmac-sha1",
		Compression: "none",
	}
	client, err := newPacketCipher(clientKeys, algs, kr)
	if err != nil {
		t.Fatalf("newPacketCipher(client): %v", err)
	}

	want := "bla bla"
	input := []byte(want)
	buf := &bytes.Buffer{}
	if err := client.writePacket(0, buf, rand.Reader, input); err != nil {
		t.Errorf("writePacket: %v", err)
	}

	packetSize := buf.Len()
	buf.Write(make([]byte, 2*maxPacket))

	// We corrupt each byte, but this usually will only test the
	// 'packet too large' or 'MAC failure' cases.
	lastRead := -1
	for i := 0; i < packetSize; i++ {
		server, err := newPacketCipher(clientKeys, algs, kr)
		if err != nil {
			t.Fatalf("newPacketCipher(client): %v", err)
		}

		fresh := &bytes.Buffer{}
		fresh.Write(buf.Bytes())
		fresh.Bytes()[i] ^= 0x01

		before := fresh.Len()
		_, err = server.readPacket(0, fresh)
		if err == nil {
			t.Errorf("corrupt byte %d: readPacket succeeded ", i)
			continue
		}
		if _, ok := err.(cbcError); !ok {
			t.Errorf("corrupt byte %d: got %v (%T), want cbcError", i, err, err)
			continue
		}

		after := fresh.Len()
		bytesRead := before - after
		if bytesRead < maxPacket {
			t.Errorf("corrupt byte %d: read %d bytes, want more than %d", i, bytesRead, maxPacket)
			continue
		}

		if i > 0 && bytesRead != lastRead {
			t.Errorf("corrupt byte %d: read %d bytes, want %d bytes read", i, bytesRead, lastRead)
		}
		lastRead = bytesRead
	}
}
<|endoftext|>


<commit_before>package tasks

import (
	"fmt"

	"github.com/DVI-GI-2017/Jira__backend/db"
	"github.com/DVI-GI-2017/Jira__backend/models"
	"gopkg.in/mgo.v2/bson"
)

// Checks if label already set on this task.
func CheckLabelAlreadySet(source db.DataSource, id bson.ObjectId, label models.Label) (bool, error) {
	task, err := FindTaskById(source, id)

	if err != nil {
		return false, err
	}

	return task.HasLabel(label), nil
}

func AllLabels(source db.DataSource, id bson.ObjectId) (models.LabelsList, error) {
	task, err := FindTaskById(source, id)
	if err != nil {
		return models.LabelsList{}, err
	}

	return task.Labels, nil
}

// Adds label to task and returns new list of labels on this task.
func AddLabelToTask(source db.DataSource, task_id bson.ObjectId, label models.Label) (models.LabelsList, error) {
	task, err := FindTaskById(source, task_id)
	if err != nil {
		return models.LabelsList{},
			fmt.Errorf("can not find task with id '%s': %v", task_id, err)
	}

	labels := append(task.Labels, label)

	err = source.C(cTasks).Update(task, bson.M{"labels": labels})
	if err != nil {
		return models.LabelsList{}, fmt.Errorf("can not update labels '%v' on task '%v': %v", labels, task, err)
	}

	return labels, nil
}
<commit_msg>Add missing comment line.<commit_after>package tasks

import (
	"fmt"

	"github.com/DVI-GI-2017/Jira__backend/db"
	"github.com/DVI-GI-2017/Jira__backend/models"
	"gopkg.in/mgo.v2/bson"
)

// Checks if label already set on this task.
func CheckLabelAlreadySet(source db.DataSource, id bson.ObjectId, label models.Label) (bool, error) {
	task, err := FindTaskById(source, id)

	if err != nil {
		return false, err
	}

	return task.HasLabel(label), nil
}

// Returns all labels from given task.
func AllLabels(source db.DataSource, id bson.ObjectId) (models.LabelsList, error) {
	task, err := FindTaskById(source, id)
	if err != nil {
		return models.LabelsList{}, err
	}

	return task.Labels, nil
}

// Adds label to task and returns new list of labels on this task.
func AddLabelToTask(source db.DataSource, task_id bson.ObjectId, label models.Label) (models.LabelsList, error) {
	task, err := FindTaskById(source, task_id)
	if err != nil {
		return models.LabelsList{},
			fmt.Errorf("can not find task with id '%s': %v", task_id, err)
	}

	labels := append(task.Labels, label)

	err = source.C(cTasks).Update(task, bson.M{"labels": labels})
	if err != nil {
		return models.LabelsList{}, fmt.Errorf("can not update labels '%v' on task '%v': %v", labels, task, err)
	}

	return labels, nil
}
<|endoftext|>