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

package main

import (
	"context"
	"fmt"
	"net/http"
	"os"
	"regexp"
	"strings"
	"time"

	"github.com/prometheus-operator/prometheus-operator/pkg/versionutil"

	"github.com/go-kit/kit/log"
	"github.com/go-kit/kit/log/level"
	"github.com/oklog/run"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/prometheus/common/version"
	"github.com/thanos-io/thanos/pkg/reloader"
	"gopkg.in/alecthomas/kingpin.v2"
)

const (
	logFormatLogfmt = "logfmt"
	logFormatJson   = "json"

	logLevelDebug = "debug"
	logLevelInfo  = "info"
	logLevelWarn  = "warn"
	logLevelError = "error"
	logLevelNone  = "none"

	defaultWatchInterval = 3 * time.Minute // 3 minutes was the value previously hardcoded in github.com/thanos-io/thanos/pkg/reloader.
	defaultDelayInterval = 1 * time.Second // 1 second seems a reasonable amount of time for the kubelet to update the secrets/configmaps.
	defaultRetryInterval = 5 * time.Second // 5 seconds was the value previously hardcoded in github.com/thanos-io/thanos/pkg/reloader.

	statefulsetOrdinalEnvvar            = "STATEFULSET_ORDINAL_NUMBER"
	statefulsetOrdinalFromEnvvarDefault = "POD_NAME"
)

var (
	availableLogFormats = []string{
		logFormatLogfmt,
		logFormatJson,
	}
	availableLogLevels = []string{
		logLevelDebug,
		logLevelInfo,
		logLevelWarn,
		logLevelError,
		logLevelNone,
	}
)

func main() {
	app := kingpin.New("prometheus-config-reloader", "")
	cfgFile := app.Flag("config-file", "config file watched by the reloader").
		String()

	cfgSubstFile := app.Flag("config-envsubst-file", "output file for environment variable substituted config file").
		String()

	watchInterval := app.Flag("watch-interval", "how often the reloader re-reads the configuration file and directories").Default(defaultWatchInterval.String()).Duration()
	delayInterval := app.Flag("delay-interval", "how long the reloader waits before reloading after it has detected a change").Default(defaultDelayInterval.String()).Duration()
	retryInterval := app.Flag("retry-interval", "how long the reloader waits before retrying in case the endpoint returned an error").Default(defaultRetryInterval.String()).Duration()

	watchedDir := app.Flag("watched-dir", "directory to watch non-recursively").Strings()

	createStatefulsetOrdinalFrom := app.Flag(
		"statefulset-ordinal-from-envvar",
		fmt.Sprintf("parse this environment variable to create %s, containing the statefulset ordinal number", statefulsetOrdinalEnvvar)).
		Default(statefulsetOrdinalFromEnvvarDefault).String()

	listenAddress := app.Flag(
		"listen-address",
		"address on which to expose metrics (disabled when empty)").
		String()

	logFormat := app.Flag(
		"log-format",
		fmt.Sprintf("log format to use. Possible values: %s", strings.Join(availableLogFormats, ", "))).
		Default(logFormatLogfmt).String()

	logLevel := app.Flag(
		"log-level",
		fmt.Sprintf("log level to use. Possible values: %s", strings.Join(availableLogLevels, ", "))).
		Default(logLevelInfo).String()

	reloadURL := app.Flag("reload-url", "reload URL to trigger Prometheus reload on").
		Default("http://127.0.0.1:9090/-/reload").URL()

	versionutil.RegisterIntoKingpinFlags(app)

	if _, err := app.Parse(os.Args[1:]); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(2)
	}

	if versionutil.ShouldPrintVersion() {
		versionutil.Print(os.Stdout, "prometheus-config-reloader")
		os.Exit(0)
	}

	logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))

	if *logFormat == logFormatJson {
		logger = log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
	}

	switch *logLevel {
	case logLevelDebug:
		logger = level.NewFilter(logger, level.AllowDebug())
	case logLevelWarn:
		logger = level.NewFilter(logger, level.AllowWarn())
	case logLevelError:
		logger = level.NewFilter(logger, level.AllowError())
	case logLevelNone:
		logger = level.NewFilter(logger, level.AllowNone())
	default:
		logger = level.NewFilter(logger, level.AllowInfo())
	}

	logger = log.With(logger, "ts", log.DefaultTimestampUTC)
	logger = log.With(logger, "caller", log.DefaultCaller)

	if createStatefulsetOrdinalFrom != nil {
		if err := createOrdinalEnvvar(*createStatefulsetOrdinalFrom); err != nil {
			level.Warn(logger).Log("msg", fmt.Sprintf("Failed setting %s", statefulsetOrdinalEnvvar))
		}
	}

	level.Info(logger).Log("msg", "Starting prometheus-config-reloader", "version", version.Info())
	level.Info(logger).Log("build_context", version.BuildContext())

	r := prometheus.NewRegistry()
	r.MustRegister(
		prometheus.NewGoCollector(),
		prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
	)

	var g run.Group
	{
		ctx, cancel := context.WithCancel(context.Background())
		rel := reloader.New(
			logger,
			r,
			&reloader.Options{
				ReloadURL:     *reloadURL,
				CfgFile:       *cfgFile,
				CfgOutputFile: *cfgSubstFile,
				WatchedDirs:   *watchedDir,
				DelayInterval: *delayInterval,
				WatchInterval: *watchInterval,
				RetryInterval: *retryInterval,
			},
		)

		g.Add(func() error {
			return rel.Watch(ctx)
		}, func(error) {
			cancel()
		})
	}

	if *listenAddress != "" {
		g.Add(func() error {
			level.Info(logger).Log("msg", "Starting web server for metrics", "listen", *listenAddress)
			http.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{Registry: r}))
			return http.ListenAndServe(*listenAddress, nil)
		}, func(error) {
		})
	}

	if err := g.Run(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func createOrdinalEnvvar(fromName string) error {
	reg := regexp.MustCompile(`\d+$`)
	val := reg.FindString(os.Getenv(fromName))
	return os.Setenv(statefulsetOrdinalEnvvar, val)
}
<commit_msg>log error when reloader watching file failed<commit_after>// Copyright 2016 The prometheus-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"context"
	"fmt"
	"net/http"
	"os"
	"regexp"
	"strings"
	"time"

	"github.com/prometheus-operator/prometheus-operator/pkg/versionutil"

	"github.com/go-kit/kit/log"
	"github.com/go-kit/kit/log/level"
	"github.com/oklog/run"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/prometheus/common/version"
	"github.com/thanos-io/thanos/pkg/reloader"
	"gopkg.in/alecthomas/kingpin.v2"
)

const (
	logFormatLogfmt = "logfmt"
	logFormatJson   = "json"

	logLevelDebug = "debug"
	logLevelInfo  = "info"
	logLevelWarn  = "warn"
	logLevelError = "error"
	logLevelNone  = "none"

	defaultWatchInterval = 3 * time.Minute // 3 minutes was the value previously hardcoded in github.com/thanos-io/thanos/pkg/reloader.
	defaultDelayInterval = 1 * time.Second // 1 second seems a reasonable amount of time for the kubelet to update the secrets/configmaps.
	defaultRetryInterval = 5 * time.Second // 5 seconds was the value previously hardcoded in github.com/thanos-io/thanos/pkg/reloader.

	statefulsetOrdinalEnvvar            = "STATEFULSET_ORDINAL_NUMBER"
	statefulsetOrdinalFromEnvvarDefault = "POD_NAME"
)

var (
	availableLogFormats = []string{
		logFormatLogfmt,
		logFormatJson,
	}
	availableLogLevels = []string{
		logLevelDebug,
		logLevelInfo,
		logLevelWarn,
		logLevelError,
		logLevelNone,
	}
)

func main() {
	app := kingpin.New("prometheus-config-reloader", "")
	cfgFile := app.Flag("config-file", "config file watched by the reloader").
		String()

	cfgSubstFile := app.Flag("config-envsubst-file", "output file for environment variable substituted config file").
		String()

	watchInterval := app.Flag("watch-interval", "how often the reloader re-reads the configuration file and directories").Default(defaultWatchInterval.String()).Duration()
	delayInterval := app.Flag("delay-interval", "how long the reloader waits before reloading after it has detected a change").Default(defaultDelayInterval.String()).Duration()
	retryInterval := app.Flag("retry-interval", "how long the reloader waits before retrying in case the endpoint returned an error").Default(defaultRetryInterval.String()).Duration()

	watchedDir := app.Flag("watched-dir", "directory to watch non-recursively").Strings()

	createStatefulsetOrdinalFrom := app.Flag(
		"statefulset-ordinal-from-envvar",
		fmt.Sprintf("parse this environment variable to create %s, containing the statefulset ordinal number", statefulsetOrdinalEnvvar)).
		Default(statefulsetOrdinalFromEnvvarDefault).String()

	listenAddress := app.Flag(
		"listen-address",
		"address on which to expose metrics (disabled when empty)").
		String()

	logFormat := app.Flag(
		"log-format",
		fmt.Sprintf("log format to use. Possible values: %s", strings.Join(availableLogFormats, ", "))).
		Default(logFormatLogfmt).String()

	logLevel := app.Flag(
		"log-level",
		fmt.Sprintf("log level to use. Possible values: %s", strings.Join(availableLogLevels, ", "))).
		Default(logLevelInfo).String()

	reloadURL := app.Flag("reload-url", "reload URL to trigger Prometheus reload on").
		Default("http://127.0.0.1:9090/-/reload").URL()

	versionutil.RegisterIntoKingpinFlags(app)

	if _, err := app.Parse(os.Args[1:]); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(2)
	}

	if versionutil.ShouldPrintVersion() {
		versionutil.Print(os.Stdout, "prometheus-config-reloader")
		os.Exit(0)
	}

	logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))

	if *logFormat == logFormatJson {
		logger = log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
	}

	switch *logLevel {
	case logLevelDebug:
		logger = level.NewFilter(logger, level.AllowDebug())
	case logLevelWarn:
		logger = level.NewFilter(logger, level.AllowWarn())
	case logLevelError:
		logger = level.NewFilter(logger, level.AllowError())
	case logLevelNone:
		logger = level.NewFilter(logger, level.AllowNone())
	default:
		logger = level.NewFilter(logger, level.AllowInfo())
	}

	logger = log.With(logger, "ts", log.DefaultTimestampUTC)
	logger = log.With(logger, "caller", log.DefaultCaller)

	if createStatefulsetOrdinalFrom != nil {
		if err := createOrdinalEnvvar(*createStatefulsetOrdinalFrom); err != nil {
			level.Warn(logger).Log("msg", fmt.Sprintf("Failed setting %s", statefulsetOrdinalEnvvar))
		}
	}

	level.Info(logger).Log("msg", "Starting prometheus-config-reloader", "version", version.Info())
	level.Info(logger).Log("build_context", version.BuildContext())

	r := prometheus.NewRegistry()
	r.MustRegister(
		prometheus.NewGoCollector(),
		prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}),
	)

	var g run.Group
	{
		ctx, cancel := context.WithCancel(context.Background())
		rel := reloader.New(
			logger,
			r,
			&reloader.Options{
				ReloadURL:     *reloadURL,
				CfgFile:       *cfgFile,
				CfgOutputFile: *cfgSubstFile,
				WatchedDirs:   *watchedDir,
				DelayInterval: *delayInterval,
				WatchInterval: *watchInterval,
				RetryInterval: *retryInterval,
			},
		)

		g.Add(func() error {
			return rel.Watch(ctx)
		}, func(error) {
			cancel()
		})
	}

	if *listenAddress != "" {
		g.Add(func() error {
			level.Info(logger).Log("msg", "Starting web server for metrics", "listen", *listenAddress)
			http.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{Registry: r}))
			return http.ListenAndServe(*listenAddress, nil)
		}, func(err error) {
			level.Error(logger).Log("Error", err)
		})
	}

	if err := g.Run(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func createOrdinalEnvvar(fromName string) error {
	reg := regexp.MustCompile(`\d+$`)
	val := reg.FindString(os.Getenv(fromName))
	return os.Setenv(statefulsetOrdinalEnvvar, val)
}
<|endoftext|>


<commit_before>// Copyright 2016 The Serviced 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.

// +build unit

package facade_test

import (
	"errors"
	"time"

	"github.com/control-center/serviced/domain/host"
	"github.com/control-center/serviced/domain/registry"
	"github.com/control-center/serviced/domain/service"
	"github.com/control-center/serviced/facade"
	"github.com/control-center/serviced/health"
	zkservice "github.com/control-center/serviced/zzk/service2"
	"github.com/stretchr/testify/mock"
	. "gopkg.in/check.v1"
)

var (
	ErrTestZK         = errors.New("mock zookeeper error")
	ErrTestHostStore  = errors.New("mock host store error")
	ErrTestImageStore = errors.New("mock image store error")
)

func (ft *FacadeUnitTest) TestGetServiceInstances_ServiceNotFound(c *C) {
	ft.serviceStore.On("Get", ft.ctx, "badservice").Return(nil, facade.ErrServiceDoesNotExist)
	inst, err := ft.Facade.GetServiceInstances(ft.ctx, "badservice")
	c.Assert(err, Equals, facade.ErrServiceDoesNotExist)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetServiceInstances_StatesError(c *C) {
	svc := &service.Service{
		ID:           "testservice",
		PoolID:       "default",
		Name:         "serviceA",
		ImageID:      "testtenant/image",
		DesiredState: int(service.SVCRun),
	}

	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(svc, nil)
	ft.zzk.On("GetServiceStates2", "default", "testservice").Return(nil, ErrTestZK)
	inst, err := ft.Facade.GetServiceInstances(ft.ctx, "testservice")
	c.Assert(err, Equals, ErrTestZK)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetServiceInstances_HostNotFound(c *C) {
	svc := &service.Service{
		ID:           "testservice",
		PoolID:       "default",
		Name:         "serviceA",
		ImageID:      "testtenant/image",
		DesiredState: int(service.SVCRun),
	}
	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(svc, nil)

	states := []zkservice.State{
		{
			HostID:     "testhost",
			ServiceID:  "testservice",
			InstanceID: 1,
			HostState2: zkservice.HostState2{
				DesiredState: service.SVCRun,
				Scheduled:    time.Now(),
			},
			ServiceState: zkservice.ServiceState{
				ImageID:     "someimageuuid",
				Started:     time.Now(),
				Paused:      false,
				ContainerID: "somecontainerid",
			},
		},
	}
	ft.zzk.On("GetServiceStates2", "default", "testservice").Return(states, nil)

	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(ErrTestHostStore)
	inst, err := ft.Facade.GetServiceInstances(ft.ctx, "testservice")
	c.Assert(err, Equals, ErrTestHostStore)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetServiceInstances_BadImage(c *C) {
	svc := &service.Service{
		ID:           "testservice",
		PoolID:       "default",
		Name:         "serviceA",
		ImageID:      "testtenant/image",
		DesiredState: int(service.SVCRun),
	}
	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(svc, nil)

	states := []zkservice.State{
		{
			HostID:     "testhost",
			ServiceID:  "testservice",
			InstanceID: 1,
			HostState2: zkservice.HostState2{
				DesiredState: service.SVCRun,
				Scheduled:    time.Now(),
			},
			ServiceState: zkservice.ServiceState{
				ImageID:     "someimageuuid",
				Started:     time.Now(),
				Paused:      false,
				ContainerID: "somecontainerid",
			},
		},
	}
	ft.zzk.On("GetServiceStates2", "default", "testservice").Return(states, nil)

	hst := &host.Host{
		ID:     "testhost",
		Name:   "sometest.host.org",
		PoolID: "default",
	}
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(nil).Run(func(args mock.Arguments) {
		arg := args.Get(2).(*host.Host)
		*arg = *hst
	})

	ft.registryStore.On("Get", ft.ctx, "testtenant/image:latest").Return(nil, ErrTestImageStore)
	inst, err := ft.Facade.GetServiceInstances(ft.ctx, "testservice")
	c.Assert(err, Equals, ErrTestImageStore)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetServiceInstances_Success(c *C) {
	svc := &service.Service{
		ID:           "testservice",
		PoolID:       "default",
		Name:         "serviceA",
		ImageID:      "testtenant/image",
		DesiredState: int(service.SVCRun),
	}
	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(svc, nil)

	states := []zkservice.State{
		{
			HostID:     "testhost",
			ServiceID:  "testservice",
			InstanceID: 1,
			HostState2: zkservice.HostState2{
				DesiredState: service.SVCRun,
				Scheduled:    time.Now(),
			},
			ServiceState: zkservice.ServiceState{
				ImageID:     "someimageuuid",
				Started:     time.Now(),
				Paused:      false,
				ContainerID: "somecontainerid",
			},
		},
	}
	ft.zzk.On("GetServiceStates2", "default", "testservice").Return(states, nil)

	hst := &host.Host{
		ID:     "testhost",
		Name:   "sometest.host.org",
		PoolID: "default",
	}
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(nil).Run(func(args mock.Arguments) {
		arg := args.Get(2).(*host.Host)
		*arg = *hst
	})

	img := &registry.Image{
		Library: "testtenant",
		Repo:    "image",
		Tag:     "latest",
		UUID:    "someimageuuid",
	}
	ft.registryStore.On("Get", ft.ctx, "testtenant/image:latest").Return(img, nil)

	expected := []service.Instance{
		{
			ID:           1,
			HostID:       "testhost",
			HostName:     "sometest.host.org",
			ServiceID:    "testservice",
			ServiceName:  "serviceA",
			ContainerID:  "somecontainerid",
			ImageSynced:  true,
			DesiredState: service.SVCRun,
			CurrentState: service.Running,
			HealthStatus: make(map[string]health.Status),
			Scheduled:    states[0].Scheduled,
			Started:      states[0].Started,
			Terminated:   states[0].Terminated,
		},
	}
	actual, err := ft.Facade.GetServiceInstances(ft.ctx, "testservice")
	c.Assert(err, IsNil)
	c.Assert(actual, DeepEquals, expected)
}

func (ft *FacadeUnitTest) TestGetHostInstances_HostNotFound(c *C) {
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(ErrTestHostStore)
	inst, err := ft.Facade.GetHostInstances(ft.ctx, "testhost")
	c.Assert(err, Equals, ErrTestHostStore)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetHostInstances_StatesError(c *C) {
	hst := &host.Host{
		ID:     "testhost",
		Name:   "sometest.host.org",
		PoolID: "default",
	}
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(nil).Run(func(args mock.Arguments) {
		arg := args.Get(2).(*host.Host)
		*arg = *hst
	})

	ft.zzk.On("GetHostStates", "default", "testhost").Return(nil, ErrTestZK)
	inst, err := ft.Facade.GetHostInstances(ft.ctx, "testhost")
	c.Assert(err, Equals, ErrTestZK)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetHostInstances_ServiceNotFound(c *C) {
	hst := &host.Host{
		ID:     "testhost",
		Name:   "sometest.host.org",
		PoolID: "default",
	}
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(nil).Run(func(args mock.Arguments) {
		arg := args.Get(2).(*host.Host)
		*arg = *hst
	})

	states := []zkservice.State{
		{
			HostID:     "testhost",
			ServiceID:  "testservice",
			InstanceID: 1,
			HostState2: zkservice.HostState2{
				DesiredState: service.SVCRun,
				Scheduled:    time.Now(),
			},
			ServiceState: zkservice.ServiceState{
				ImageID:     "someimageuuid",
				Started:     time.Now(),
				Paused:      false,
				ContainerID: "somecontainerid",
			},
		},
	}
	ft.zzk.On("GetHostStates", "default", "testhost").Return(states, nil)

	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(nil, facade.ErrServiceDoesNotExist)
	inst, err := ft.Facade.GetHostInstances(ft.ctx, "testhost")
	c.Assert(err, Equals, facade.ErrServiceDoesNotExist)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetHostInstances_Success(c *C) {
	hst := &host.Host{
		ID:     "testhost",
		Name:   "sometest.host.org",
		PoolID: "default",
	}
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(nil).Run(func(args mock.Arguments) {
		arg := args.Get(2).(*host.Host)
		*arg = *hst
	})

	states := []zkservice.State{
		{
			HostID:     "testhost",
			ServiceID:  "testservice",
			InstanceID: 1,
			HostState2: zkservice.HostState2{
				DesiredState: service.SVCRun,
				Scheduled:    time.Now(),
			},
			ServiceState: zkservice.ServiceState{
				ImageID:     "someimageuuid",
				Started:     time.Now(),
				Paused:      false,
				ContainerID: "somecontainerid",
			},
		},
	}
	ft.zzk.On("GetHostStates", "default", "testhost").Return(states, nil)

	svc := &service.Service{
		ID:           "testservice",
		PoolID:       "default",
		Name:         "serviceA",
		ImageID:      "testtenant/image",
		DesiredState: int(service.SVCRun),
	}
	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(svc, nil)

	img := &registry.Image{
		Library: "testtenant",
		Repo:    "image",
		Tag:     "latest",
		UUID:    "someimageuuid",
	}
	ft.registryStore.On("Get", ft.ctx, "testtenant/image:latest").Return(img, nil)

	expected := []service.Instance{
		{
			ID:           1,
			HostID:       "testhost",
			HostName:     "sometest.host.org",
			ServiceID:    "testservice",
			ServiceName:  "serviceA",
			ContainerID:  "somecontainerid",
			ImageSynced:  true,
			DesiredState: service.SVCRun,
			CurrentState: service.Running,
			HealthStatus: make(map[string]health.Status),
			Scheduled:    states[0].Scheduled,
			Started:      states[0].Started,
			Terminated:   states[0].Terminated,
		},
	}
	actual, err := ft.Facade.GetHostInstances(ft.ctx, "testhost")
	c.Assert(err, IsNil)
	c.Assert(actual, DeepEquals, expected)
}
<commit_msg>updated test to use correct object name<commit_after>// Copyright 2016 The Serviced 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.

// +build unit

package facade_test

import (
	"errors"
	"time"

	"github.com/control-center/serviced/domain/host"
	"github.com/control-center/serviced/domain/registry"
	"github.com/control-center/serviced/domain/service"
	"github.com/control-center/serviced/facade"
	"github.com/control-center/serviced/health"
	zkservice "github.com/control-center/serviced/zzk/service2"
	"github.com/stretchr/testify/mock"
	. "gopkg.in/check.v1"
)

var (
	ErrTestZK         = errors.New("mock zookeeper error")
	ErrTestHostStore  = errors.New("mock host store error")
	ErrTestImageStore = errors.New("mock image store error")
)

func (ft *FacadeUnitTest) TestGetServiceInstances_ServiceNotFound(c *C) {
	ft.serviceStore.On("Get", ft.ctx, "badservice").Return(nil, facade.ErrServiceDoesNotExist)
	inst, err := ft.Facade.GetServiceInstances(ft.ctx, "badservice")
	c.Assert(err, Equals, facade.ErrServiceDoesNotExist)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetServiceInstances_StatesError(c *C) {
	svc := &service.Service{
		ID:           "testservice",
		PoolID:       "default",
		Name:         "serviceA",
		ImageID:      "testtenant/image",
		DesiredState: int(service.SVCRun),
	}

	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(svc, nil)
	ft.zzk.On("GetServiceStates2", "default", "testservice").Return(nil, ErrTestZK)
	inst, err := ft.Facade.GetServiceInstances(ft.ctx, "testservice")
	c.Assert(err, Equals, ErrTestZK)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetServiceInstances_HostNotFound(c *C) {
	svc := &service.Service{
		ID:           "testservice",
		PoolID:       "default",
		Name:         "serviceA",
		ImageID:      "testtenant/image",
		DesiredState: int(service.SVCRun),
	}
	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(svc, nil)

	states := []zkservice.State{
		{
			HostID:     "testhost",
			ServiceID:  "testservice",
			InstanceID: 1,
			HostState: zkservice.HostState{
				DesiredState: service.SVCRun,
				Scheduled:    time.Now(),
			},
			ServiceState: zkservice.ServiceState{
				ImageID:     "someimageuuid",
				Started:     time.Now(),
				Paused:      false,
				ContainerID: "somecontainerid",
			},
		},
	}
	ft.zzk.On("GetServiceStates2", "default", "testservice").Return(states, nil)

	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(ErrTestHostStore)
	inst, err := ft.Facade.GetServiceInstances(ft.ctx, "testservice")
	c.Assert(err, Equals, ErrTestHostStore)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetServiceInstances_BadImage(c *C) {
	svc := &service.Service{
		ID:           "testservice",
		PoolID:       "default",
		Name:         "serviceA",
		ImageID:      "testtenant/image",
		DesiredState: int(service.SVCRun),
	}
	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(svc, nil)

	states := []zkservice.State{
		{
			HostID:     "testhost",
			ServiceID:  "testservice",
			InstanceID: 1,
			HostState: zkservice.HostState{
				DesiredState: service.SVCRun,
				Scheduled:    time.Now(),
			},
			ServiceState: zkservice.ServiceState{
				ImageID:     "someimageuuid",
				Started:     time.Now(),
				Paused:      false,
				ContainerID: "somecontainerid",
			},
		},
	}
	ft.zzk.On("GetServiceStates2", "default", "testservice").Return(states, nil)

	hst := &host.Host{
		ID:     "testhost",
		Name:   "sometest.host.org",
		PoolID: "default",
	}
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(nil).Run(func(args mock.Arguments) {
		arg := args.Get(2).(*host.Host)
		*arg = *hst
	})

	ft.registryStore.On("Get", ft.ctx, "testtenant/image:latest").Return(nil, ErrTestImageStore)
	inst, err := ft.Facade.GetServiceInstances(ft.ctx, "testservice")
	c.Assert(err, Equals, ErrTestImageStore)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetServiceInstances_Success(c *C) {
	svc := &service.Service{
		ID:           "testservice",
		PoolID:       "default",
		Name:         "serviceA",
		ImageID:      "testtenant/image",
		DesiredState: int(service.SVCRun),
	}
	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(svc, nil)

	states := []zkservice.State{
		{
			HostID:     "testhost",
			ServiceID:  "testservice",
			InstanceID: 1,
			HostState: zkservice.HostState{
				DesiredState: service.SVCRun,
				Scheduled:    time.Now(),
			},
			ServiceState: zkservice.ServiceState{
				ImageID:     "someimageuuid",
				Started:     time.Now(),
				Paused:      false,
				ContainerID: "somecontainerid",
			},
		},
	}
	ft.zzk.On("GetServiceStates2", "default", "testservice").Return(states, nil)

	hst := &host.Host{
		ID:     "testhost",
		Name:   "sometest.host.org",
		PoolID: "default",
	}
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(nil).Run(func(args mock.Arguments) {
		arg := args.Get(2).(*host.Host)
		*arg = *hst
	})

	img := &registry.Image{
		Library: "testtenant",
		Repo:    "image",
		Tag:     "latest",
		UUID:    "someimageuuid",
	}
	ft.registryStore.On("Get", ft.ctx, "testtenant/image:latest").Return(img, nil)

	expected := []service.Instance{
		{
			ID:           1,
			HostID:       "testhost",
			HostName:     "sometest.host.org",
			ServiceID:    "testservice",
			ServiceName:  "serviceA",
			ContainerID:  "somecontainerid",
			ImageSynced:  true,
			DesiredState: service.SVCRun,
			CurrentState: service.Running,
			HealthStatus: make(map[string]health.Status),
			Scheduled:    states[0].Scheduled,
			Started:      states[0].Started,
			Terminated:   states[0].Terminated,
		},
	}
	actual, err := ft.Facade.GetServiceInstances(ft.ctx, "testservice")
	c.Assert(err, IsNil)
	c.Assert(actual, DeepEquals, expected)
}

func (ft *FacadeUnitTest) TestGetHostInstances_HostNotFound(c *C) {
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(ErrTestHostStore)
	inst, err := ft.Facade.GetHostInstances(ft.ctx, "testhost")
	c.Assert(err, Equals, ErrTestHostStore)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetHostInstances_StatesError(c *C) {
	hst := &host.Host{
		ID:     "testhost",
		Name:   "sometest.host.org",
		PoolID: "default",
	}
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(nil).Run(func(args mock.Arguments) {
		arg := args.Get(2).(*host.Host)
		*arg = *hst
	})

	ft.zzk.On("GetHostStates", "default", "testhost").Return(nil, ErrTestZK)
	inst, err := ft.Facade.GetHostInstances(ft.ctx, "testhost")
	c.Assert(err, Equals, ErrTestZK)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetHostInstances_ServiceNotFound(c *C) {
	hst := &host.Host{
		ID:     "testhost",
		Name:   "sometest.host.org",
		PoolID: "default",
	}
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(nil).Run(func(args mock.Arguments) {
		arg := args.Get(2).(*host.Host)
		*arg = *hst
	})

	states := []zkservice.State{
		{
			HostID:     "testhost",
			ServiceID:  "testservice",
			InstanceID: 1,
			HostState: zkservice.HostState{
				DesiredState: service.SVCRun,
				Scheduled:    time.Now(),
			},
			ServiceState: zkservice.ServiceState{
				ImageID:     "someimageuuid",
				Started:     time.Now(),
				Paused:      false,
				ContainerID: "somecontainerid",
			},
		},
	}
	ft.zzk.On("GetHostStates", "default", "testhost").Return(states, nil)

	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(nil, facade.ErrServiceDoesNotExist)
	inst, err := ft.Facade.GetHostInstances(ft.ctx, "testhost")
	c.Assert(err, Equals, facade.ErrServiceDoesNotExist)
	c.Assert(inst, IsNil)
}

func (ft *FacadeUnitTest) TestGetHostInstances_Success(c *C) {
	hst := &host.Host{
		ID:     "testhost",
		Name:   "sometest.host.org",
		PoolID: "default",
	}
	ft.hostStore.On("Get", ft.ctx, host.HostKey("testhost"), mock.AnythingOfType("*host.Host")).Return(nil).Run(func(args mock.Arguments) {
		arg := args.Get(2).(*host.Host)
		*arg = *hst
	})

	states := []zkservice.State{
		{
			HostID:     "testhost",
			ServiceID:  "testservice",
			InstanceID: 1,
			HostState: zkservice.HostState{
				DesiredState: service.SVCRun,
				Scheduled:    time.Now(),
			},
			ServiceState: zkservice.ServiceState{
				ImageID:     "someimageuuid",
				Started:     time.Now(),
				Paused:      false,
				ContainerID: "somecontainerid",
			},
		},
	}
	ft.zzk.On("GetHostStates", "default", "testhost").Return(states, nil)

	svc := &service.Service{
		ID:           "testservice",
		PoolID:       "default",
		Name:         "serviceA",
		ImageID:      "testtenant/image",
		DesiredState: int(service.SVCRun),
	}
	ft.serviceStore.On("Get", ft.ctx, "testservice").Return(svc, nil)

	img := &registry.Image{
		Library: "testtenant",
		Repo:    "image",
		Tag:     "latest",
		UUID:    "someimageuuid",
	}
	ft.registryStore.On("Get", ft.ctx, "testtenant/image:latest").Return(img, nil)

	expected := []service.Instance{
		{
			ID:           1,
			HostID:       "testhost",
			HostName:     "sometest.host.org",
			ServiceID:    "testservice",
			ServiceName:  "serviceA",
			ContainerID:  "somecontainerid",
			ImageSynced:  true,
			DesiredState: service.SVCRun,
			CurrentState: service.Running,
			HealthStatus: make(map[string]health.Status),
			Scheduled:    states[0].Scheduled,
			Started:      states[0].Started,
			Terminated:   states[0].Terminated,
		},
	}
	actual, err := ft.Facade.GetHostInstances(ft.ctx, "testhost")
	c.Assert(err, IsNil)
	c.Assert(actual, DeepEquals, expected)
}
<|endoftext|>


<commit_before>package user

import "strings"

//NewAccount create new account.
func NewAccount() *Account {
	return &Account{}
}

//Account user account struct
type Account struct {
	//User accont keyword
	Keyword string
	//user account name
	Account string
}

//Equal check if an account is euqal to another.
func (a *Account) Equal(account *Account) bool {
	return a.Keyword == account.Keyword && a.Account == account.Account
}

//Accounts type account list
type Accounts []*Account

//NewAccounts creatre new accounts
func NewAccounts() *Accounts {
	return &Accounts{}
}

//Exists check if an account is in account list.
func (a *Accounts) Exists(account *Account) bool {
	for k := range *a {
		if (*a)[k].Equal(account) {
			return true
		}
	}
	return false
}

//Bind add account to accountlist.
//Return any error if raised.
//If account exists in account list,error ErrAccountBindingExists will be raised.
func (a *Accounts) Bind(account *Account) error {
	for k := range *a {
		if (*a)[k].Equal(account) {
			return ErrAccountBindingExists
		}
	}
	*a = append(*a, account)
	return nil
}

//Unbind remove account from accountlist.
//Return any error if raised.
//If account not exists in account list,error ErrAccountUnbindingNotExists will be raised.
func (a *Accounts) Unbind(account *Account) error {
	for k := range *a {
		if (*a)[k].Equal(account) {
			(*a) = append((*a)[:k], (*a)[k+1:]...)
			return nil
		}
	}
	return ErrAccountUnbindingNotExists
}

//AccountProvider account provider interface
type AccountProvider interface {
	//NewAccount create new account with keyword and account
	NewAccount(keyword string, account string) (*Account, error)
}

//PlainAccountProvider plain account provider.
type PlainAccountProvider struct {
	Prefix          string
	CaseInsensitive bool
}

//NewAccount create new account
//is CaseInsensitive is true,account name will be convert to lower
func (p *PlainAccountProvider) NewAccount(keyword string, account string) (*Account, error) {
	if p.CaseInsensitive {
		account = strings.ToLower(account)
	}
	a := NewAccount()
	a.Keyword = keyword
	a.Account = p.Prefix + account
	return a, nil
}

//CaseInsensitiveAcountProvider plain account provider which case insensitive
var CaseInsensitiveAcountProvider = &PlainAccountProvider{
	Prefix:          "",
	CaseInsensitive: true,
}

//CaseSensitiveAcountProvider plain account provider which case sensitive
var CaseSensitiveAcountProvider = &PlainAccountProvider{
	Prefix:          "",
	CaseInsensitive: false,
}
<commit_msg>update<commit_after>package user

import "strings"

//NewAccount create new account.
func NewAccount() *Account {
	return &Account{}
}

//Account user account struct
type Account struct {
	//User accont keyword
	Keyword string
	//user account name
	Account string
}

//Equal check if an account is euqal to another.
func (a *Account) Equal(account *Account) bool {
	return a.Keyword == account.Keyword && a.Account == account.Account
}

//Accounts type account list
type Accounts []*Account

//NewAccounts creatre new accounts
func NewAccounts() *Accounts {
	return &Accounts{}
}

//Exists check if an account is in account list.
func (a *Accounts) Exists(account *Account) bool {
	for k := range *a {
		if (*a)[k].Equal(account) {
			return true
		}
	}
	return false
}

//Data return account data
func (a *Accounts) Data() []*Account {
	return []*Account(*a)
}

//Bind add account to accountlist.
//Return any error if raised.
//If account exists in account list,error ErrAccountBindingExists will be raised.
func (a *Accounts) Bind(account *Account) error {
	for k := range *a {
		if (*a)[k].Equal(account) {
			return ErrAccountBindingExists
		}
	}
	*a = append(*a, account)
	return nil
}

//Unbind remove account from accountlist.
//Return any error if raised.
//If account not exists in account list,error ErrAccountUnbindingNotExists will be raised.
func (a *Accounts) Unbind(account *Account) error {
	for k := range *a {
		if (*a)[k].Equal(account) {
			(*a) = append((*a)[:k], (*a)[k+1:]...)
			return nil
		}
	}
	return ErrAccountUnbindingNotExists
}

//AccountProvider account provider interface
type AccountProvider interface {
	//NewAccount create new account with keyword and account
	NewAccount(keyword string, account string) (*Account, error)
}

//PlainAccountProvider plain account provider.
type PlainAccountProvider struct {
	Prefix          string
	CaseInsensitive bool
}

//NewAccount create new account
//is CaseInsensitive is true,account name will be convert to lower
func (p *PlainAccountProvider) NewAccount(keyword string, account string) (*Account, error) {
	if p.CaseInsensitive {
		account = strings.ToLower(account)
	}
	a := NewAccount()
	a.Keyword = keyword
	a.Account = p.Prefix + account
	return a, nil
}

//CaseInsensitiveAcountProvider plain account provider which case insensitive
var CaseInsensitiveAcountProvider = &PlainAccountProvider{
	Prefix:          "",
	CaseInsensitive: true,
}

//CaseSensitiveAcountProvider plain account provider which case sensitive
var CaseSensitiveAcountProvider = &PlainAccountProvider{
	Prefix:          "",
	CaseInsensitive: false,
}
<|endoftext|>


<commit_before>package flow

import (
	"reflect"
)

func (d *Dataset) GroupByKey() *Dataset {
	sorted_d := d.Partition(len(d.Shards)).LocalSort(nil)

	return sorted_d.LocalGroupByKey(nil)
}

func (d *Dataset) LocalGroupByKey(compareFunc interface{}) *Dataset {
	ret, step := add1ShardTo1Step(d, KeyValuesType)
	step.Name = "LocalGroupByKey"
	step.Function = func(task *Task) {
		outChan := task.Outputs[0].WriteChan

		leftChan := task.InputChans[0]

		// get first value
		leftKey, leftValue, leftHasValue := getKeyValue(leftChan)

		// get comparator
		if compareFunc == nil {
			if leftHasValue {
				compareFunc = getComparator(reflect.TypeOf(leftKey))
			}
		}
		fn := reflect.ValueOf(compareFunc)
		comparator := func(a, b interface{}) int64 {
			outs := fn.Call([]reflect.Value{
				reflect.ValueOf(a),
				reflect.ValueOf(b),
			})
			return outs[0].Int()
		}

		var valueType reflect.Type
		if leftHasValue {
			valueType = reflect.TypeOf(leftValue)
		}

		var leftValues []interface{}
		var leftNextKey, leftNextValue interface{}
		for leftHasValue {
			leftNextKey, leftNextValue, leftValues, leftHasValue = getSameKeyValues(leftChan, comparator, leftKey, leftValue, leftHasValue)
			sendKeyValues(outChan, leftKey, valueType, leftValues)
			leftKey, leftValue = leftNextKey, leftNextValue
		}

	}
	return ret
}

func (d *Dataset) CoGroup(other *Dataset) *Dataset {
	sorted_d := d.Partition(len(d.Shards)).LocalSort(nil)
	if d == other {
		// this should not happen, but just in case
		return sorted_d.LocalGroupByKey(nil)
	}
	sorted_other := other.Partition(len(d.Shards)).LocalSort(nil)
	return sorted_d.CoGroupPartitionedSorted(sorted_other, nil)
}

// CoGroupPartitionedSorted joins 2 datasets that are sharded
// by the same key, and locally sorted within the shard
func (this *Dataset) CoGroupPartitionedSorted(that *Dataset,
	compareFunc interface{}) (ret *Dataset) {
	ret = this.context.newNextDataset(len(this.Shards), KeyValuesValuesType)

	inputs := []*Dataset{this, that}
	step := this.context.MergeDatasets1ShardTo1Step(inputs, ret)
	step.Name = "CoGroupPartitionedSorted"
	step.Function = func(task *Task) {
		outChan := task.Outputs[0].WriteChan

		leftChan := task.InputChans[0]
		rightChan := task.InputChans[1]

		// get first value from both channels
		leftKey, leftValue, leftHasValue := getKeyValue(leftChan)
		rightKey, rightValue, rightHasValue := getKeyValue(rightChan)

		if compareFunc == nil {
			if leftHasValue {
				compareFunc = getComparator(reflect.TypeOf(leftKey))
			} else if rightHasValue {
				compareFunc = getComparator(reflect.TypeOf(rightKey))
			}
		}
		fn := reflect.ValueOf(compareFunc)
		comparator := func(a, b interface{}) int64 {
			outs := fn.Call([]reflect.Value{
				reflect.ValueOf(a),
				reflect.ValueOf(b),
			})
			return outs[0].Int()
		}

		var valueType reflect.Type
		if leftHasValue {
			valueType = reflect.TypeOf(leftValue)
		} else if rightHasValue {
			valueType = reflect.TypeOf(rightValue)
		}

		var leftValues, rightValues []interface{}
		var leftNextKey, leftNextValue, rightNextKey, rightNextValue interface{}
		for leftHasValue && rightHasValue {
			x := comparator(leftKey, rightKey)
			switch {
			case x == 0:
				leftNextKey, leftNextValue, leftValues, leftHasValue = getSameKeyValues(leftChan, comparator, leftKey, leftValue, leftHasValue)
				rightNextKey, rightNextValue, rightValues, rightHasValue = getSameKeyValues(rightChan, comparator, rightKey, rightValue, rightHasValue)
				sendKeyValuesValues(outChan, leftKey, valueType, leftValues, rightValues)
				leftKey, leftValue, rightKey, rightValue = leftNextKey, leftNextValue, rightNextKey, rightNextValue
			case x < 0:
				leftNextKey, leftNextValue, leftValues, leftHasValue = getSameKeyValues(leftChan, comparator, leftKey, leftValue, leftHasValue)
				sendKeyValuesValues(outChan, leftKey, valueType, leftValues, []interface{}{})
				leftKey, leftValue = leftNextKey, leftNextValue
			case x > 0:
				rightNextKey, rightNextValue, rightValues, rightHasValue = getSameKeyValues(rightChan, comparator, rightKey, rightValue, rightHasValue)
				sendKeyValuesValues(outChan, rightKey, valueType, []interface{}{}, rightValues)
				rightKey, rightValue = rightNextKey, rightNextValue
			}
		}
		for leftHasValue {
			leftNextKey, leftNextValue, leftValues, leftHasValue = getSameKeyValues(leftChan, comparator, leftKey, leftValue, leftHasValue)
			sendKeyValuesValues(outChan, leftKey, valueType, leftValues, []interface{}{})
			leftKey, leftValue = leftNextKey, leftNextValue
		}
		for rightHasValue {
			rightNextKey, rightNextValue, rightValues, rightHasValue = getSameKeyValues(rightChan, comparator, rightKey, rightValue, rightHasValue)
			sendKeyValuesValues(outChan, rightKey, valueType, []interface{}{}, rightValues)
			rightKey, rightValue = rightNextKey, rightNextValue
		}
	}
	return ret
}

func sendKeyValuesValues(outChan reflect.Value, key interface{}, valueType reflect.Type, values1, values2 []interface{}) {
	sliceType := reflect.SliceOf(valueType)

	slice1Len := len(values1)
	slice1Value := reflect.MakeSlice(sliceType, slice1Len, slice1Len)
	for i, value := range values1 {
		slice1Value.Index(i).Set(reflect.ValueOf(value))
	}

	slice2Len := len(values2)
	slice2Value := reflect.MakeSlice(sliceType, slice2Len, slice2Len)
	for i, value := range values2 {
		slice2Value.Index(i).Set(reflect.ValueOf(value))
	}

	outChan.Send(reflect.ValueOf(KeyValuesValues{key, slice1Value.Interface(), slice2Value.Interface()}))
}

func sendKeyValues(outChan reflect.Value, key interface{}, valueType reflect.Type, values []interface{}) {
	sliceElementType := reflect.SliceOf(valueType)

	sliceLen := len(values)
	sliceValue := reflect.MakeSlice(sliceElementType, sliceLen, sliceLen)
	for i, value := range values {
		sliceValue.Index(i).Set(reflect.ValueOf(value))
	}

	outChan.Send(reflect.ValueOf(KeyValues{key, sliceValue.Interface()}))
}
<commit_msg>cogroup supports different left and right types<commit_after>package flow

import (
	"reflect"
)

func (d *Dataset) GroupByKey() *Dataset {
	sorted_d := d.Partition(len(d.Shards)).LocalSort(nil)

	return sorted_d.LocalGroupByKey(nil)
}

func (d *Dataset) LocalGroupByKey(compareFunc interface{}) *Dataset {
	ret, step := add1ShardTo1Step(d, KeyValuesType)
	step.Name = "LocalGroupByKey"
	step.Function = func(task *Task) {
		outChan := task.Outputs[0].WriteChan

		leftChan := task.InputChans[0]

		// get first value
		leftKey, leftValue, leftHasValue := getKeyValue(leftChan)

		// get comparator
		if compareFunc == nil {
			if leftHasValue {
				compareFunc = getComparator(reflect.TypeOf(leftKey))
			}
		}
		fn := reflect.ValueOf(compareFunc)
		comparator := func(a, b interface{}) int64 {
			outs := fn.Call([]reflect.Value{
				reflect.ValueOf(a),
				reflect.ValueOf(b),
			})
			return outs[0].Int()
		}

		var valueType reflect.Type
		if leftHasValue {
			valueType = reflect.TypeOf(leftValue)
		}

		var leftValues []interface{}
		var leftNextKey, leftNextValue interface{}
		for leftHasValue {
			leftNextKey, leftNextValue, leftValues, leftHasValue = getSameKeyValues(leftChan, comparator, leftKey, leftValue, leftHasValue)
			sendKeyValues(outChan, leftKey, valueType, leftValues)
			leftKey, leftValue = leftNextKey, leftNextValue
		}

	}
	return ret
}

func (d *Dataset) CoGroup(other *Dataset) *Dataset {
	sorted_d := d.Partition(len(d.Shards)).LocalSort(nil)
	if d == other {
		// this should not happen, but just in case
		return sorted_d.LocalGroupByKey(nil)
	}
	sorted_other := other.Partition(len(d.Shards)).LocalSort(nil)
	return sorted_d.CoGroupPartitionedSorted(sorted_other, nil)
}

// CoGroupPartitionedSorted joins 2 datasets that are sharded
// by the same key, and locally sorted within the shard
func (this *Dataset) CoGroupPartitionedSorted(that *Dataset,
	compareFunc interface{}) (ret *Dataset) {
	ret = this.context.newNextDataset(len(this.Shards), KeyValuesValuesType)

	inputs := []*Dataset{this, that}
	step := this.context.MergeDatasets1ShardTo1Step(inputs, ret)
	step.Name = "CoGroupPartitionedSorted"
	step.Function = func(task *Task) {
		outChan := task.Outputs[0].WriteChan

		leftChan := task.InputChans[0]
		rightChan := task.InputChans[1]

		// get first value from both channels
		leftKey, leftValue, leftHasValue := getKeyValue(leftChan)
		rightKey, rightValue, rightHasValue := getKeyValue(rightChan)

		if compareFunc == nil {
			if leftHasValue {
				compareFunc = getComparator(reflect.TypeOf(leftKey))
			} else if rightHasValue {
				compareFunc = getComparator(reflect.TypeOf(rightKey))
			}
		}
		fn := reflect.ValueOf(compareFunc)
		comparator := func(a, b interface{}) int64 {
			outs := fn.Call([]reflect.Value{
				reflect.ValueOf(a),
				reflect.ValueOf(b),
			})
			return outs[0].Int()
		}

		var leftType, rightType reflect.Type
		if leftHasValue {
			leftType = reflect.TypeOf(leftValue)
		}
		if rightHasValue {
			rightType = reflect.TypeOf(rightValue)
		}

		var leftValues, rightValues []interface{}
		var leftNextKey, leftNextValue, rightNextKey, rightNextValue interface{}
		for leftHasValue && rightHasValue {
			x := comparator(leftKey, rightKey)
			switch {
			case x == 0:
				leftNextKey, leftNextValue, leftValues, leftHasValue = getSameKeyValues(leftChan, comparator, leftKey, leftValue, leftHasValue)
				rightNextKey, rightNextValue, rightValues, rightHasValue = getSameKeyValues(rightChan, comparator, rightKey, rightValue, rightHasValue)
				sendKeyValuesValues(outChan, leftKey, leftType, leftValues, rightType, rightValues)
				leftKey, leftValue, rightKey, rightValue = leftNextKey, leftNextValue, rightNextKey, rightNextValue
			case x < 0:
				leftNextKey, leftNextValue, leftValues, leftHasValue = getSameKeyValues(leftChan, comparator, leftKey, leftValue, leftHasValue)
				sendKeyValuesValues(outChan, leftKey, leftType, leftValues, rightType, []interface{}{})
				leftKey, leftValue = leftNextKey, leftNextValue
			case x > 0:
				rightNextKey, rightNextValue, rightValues, rightHasValue = getSameKeyValues(rightChan, comparator, rightKey, rightValue, rightHasValue)
				sendKeyValuesValues(outChan, rightKey, leftType, []interface{}{}, rightType, rightValues)
				rightKey, rightValue = rightNextKey, rightNextValue
			}
		}
		for leftHasValue {
			leftNextKey, leftNextValue, leftValues, leftHasValue = getSameKeyValues(leftChan, comparator, leftKey, leftValue, leftHasValue)
			sendKeyValuesValues(outChan, leftKey, leftType, leftValues, rightType, []interface{}{})
			leftKey, leftValue = leftNextKey, leftNextValue
		}
		for rightHasValue {
			rightNextKey, rightNextValue, rightValues, rightHasValue = getSameKeyValues(rightChan, comparator, rightKey, rightValue, rightHasValue)
			sendKeyValuesValues(outChan, rightKey, leftType, []interface{}{}, rightType, rightValues)
			rightKey, rightValue = rightNextKey, rightNextValue
		}
	}
	return ret
}

func sendKeyValuesValues(outChan reflect.Value, key interface{},
	leftType reflect.Type, leftValues []interface{}, rightType reflect.Type, rightValues []interface{}) {

	slice1Len := len(leftValues)
	slice1Value := reflect.MakeSlice(reflect.SliceOf(leftType), slice1Len, slice1Len)
	for i, value := range leftValues {
		slice1Value.Index(i).Set(reflect.ValueOf(value))
	}

	slice2Len := len(rightValues)
	slice2Value := reflect.MakeSlice(reflect.SliceOf(rightType), slice2Len, slice2Len)
	for i, value := range rightValues {
		slice2Value.Index(i).Set(reflect.ValueOf(value))
	}

	outChan.Send(reflect.ValueOf(KeyValuesValues{key, slice1Value.Interface(), slice2Value.Interface()}))
}

func sendKeyValues(outChan reflect.Value, key interface{}, valueType reflect.Type, values []interface{}) {
	sliceElementType := reflect.SliceOf(valueType)

	sliceLen := len(values)
	sliceValue := reflect.MakeSlice(sliceElementType, sliceLen, sliceLen)
	for i, value := range values {
		sliceValue.Index(i).Set(reflect.ValueOf(value))
	}

	outChan.Send(reflect.ValueOf(KeyValues{key, sliceValue.Interface()}))
}
<|endoftext|>


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

import "golang.org/x/mobile/sprite"

// Standard tween functions.
//
// Easing means a slowing near the timing boundary, as defined by
// a cubic bezier curve. Exact parameters match the CSS properties.
var (
	EaseIn    = CubicBezier(0.42, 0, 1, 1)
	EaseOut   = CubicBezier(0, 0, 0.58, 1)
	EaseInOut = CubicBezier(0.42, 0, 0.58, 1)
)

func Linear(t0, t1, t sprite.Time) float32 {
	if t0 == t1 {
		return 1
	}
	return float32(t-t0) / float32(t1-t0)
}

// CubicBezier generates a tween function determined by a Cubic Bézier curve.
//
// The parameters are cubic control parameters. The curve starts at (0,0)
// going toward (x0,y0), and arrives at (1,1) coming from (x1,y1).
func CubicBezier(x0, y0, x1, y1 float32) func(t0, t1, t sprite.Time) float32 {
	return func(start, end, now sprite.Time) float32 {
		// A Cubic-Bezier curve restricted to starting at (0,0) and
		// ending at (1,1) is defined as
		//
		// 	B(t) = 3*(1-t)^2*t*P0 + 3*(1-t)*t^2*P1 + t^3
		//
		// with derivative
		//
		//	B'(t) = 3*(1-t)^2*P0 + 6*(1-t)*t*(P1-P0) + 3*t^2*(1-P1)
		//
		// Given a value x ∈ [0,1], we solve for t using Newton's
		// method and solve for y using t.

		x := Linear(start, end, now)

		// Solve for t using x.
		t := x
		for i := 0; i < 5; i++ {
			t2 := t * t
			t3 := t2 * t
			d := 1 - t
			d2 := d * d

			nx := 3*d2*t*x0 + 3*d*t2*x1 + t3
			dxdt := 3*d2*x0 + 6*d*t*(x1-x0) + 3*t2*(1-x1)
			if dxdt == 0 {
				break
			}

			t -= (nx - x) / dxdt
			if t <= 0 || t >= 1 {
				break
			}
		}
		if t < 0 {
			t = 0
		}
		if t > 1 {
			t = 1
		}

		// Solve for y using t.
		t2 := t * t
		t3 := t2 * t
		d := 1 - t
		d2 := d * d
		y := 3*d2*t*y0 + 3*d*t2*y1 + t3

		return y
	}
}
<commit_msg>remove accidental file<commit_after><|endoftext|>


<commit_before>package gotility

import (
	"fmt"
	"reflect"
	"testing"
)

func squareInt(arg int) int {
	return arg * arg
}

func squareFloat64(arg float64) float64 {
	return arg * arg
}

func squareString(arg string) string {
	return arg + arg
}

func TestIsEmail(t *testing.T) {
	isEmail := IsEmail("xyz@gmail.com")
	if isEmail != true {
		t.Error("Expected true, got ", isEmail)
	}
	isEmail = IsEmail("mail")
	if isEmail != false {
		t.Error("Expected false, got ", isEmail)
	}
}

func TestIsUrl(t *testing.T) {
	isUrl := IsUrl("google.com")
	if isUrl != true {
		t.Error("Expected true, got ", isUrl)
	}
	isUrl = IsUrl("123.com1")
	if isUrl != false {
		t.Error("Expected false, got ", isUrl)
	}
}

func TestIsNumber(t *testing.T) {
	isNumber := IsNumber("12134.0009")
	if isNumber != true {
		t.Error("Expected true, got ", isNumber)
	}
	isNumber = IsNumber("a12134.0009")
	if isNumber != false {
		t.Error("Expected false, got ", isNumber)
	}
}

func TestIsTitleCase(t *testing.T) {
	isTitleCase := IsTitleCase("Go Is VEry Popular")
	if isTitleCase != true {
		t.Error("Expected true, got ", isTitleCase)
	}
	isTitleCase = IsTitleCase("gO IS NOT POPULAR")
	if isTitleCase != false {
		t.Error("Expected false, got ", isTitleCase)
	}
}

func TestFlattenFloat64(t *testing.T) {
	row := FlattenFloat64([][]float64{{1.1, 2.2}, {3.3, 4.4}})
	if !reflect.DeepEqual(row, []float64{1.1, 2.2, 3.3, 4.4}) {
		t.Error("Expected [1.1 2.2 3.3 4.4], got ", row)
	}
}

func TestFlattenInt(t *testing.T) {
	row := FlattenInt([][]int{{1, 2}, {3, 4}})
	if !reflect.DeepEqual(row, []int{1, 2, 3, 4}) {
		t.Error("Expected [1 2 3 4], got ", row)
	}
}

func TestFlattenString(t *testing.T) {
	row := FlattenString([][]string{{"A", "B"}, {"C", "D"}})
	if !reflect.DeepEqual(row, []string{"A", "B", "C", "D"}) {
		t.Error("Expected [A B C D], got ", row)
	}
}

func TestSumInt(t *testing.T) {
	sum := SumInt([]int{1, 2, 3})
	if sum != 6 {
		t.Error("Expected 6, got ", sum)
	}
}

func TestSumFloat64(t *testing.T) {
	sum := SumFloat64([]float64{1.1, 2.2, 3.3})
	if sum != 6.6 {
		t.Error("Expected 6.6 got ", sum)
	}
}

func TestMapInt(t *testing.T) {
	square := MapInt([]int{1, 2, 3}, squareInt)
	if !reflect.DeepEqual(square, []int{1, 4, 9}) {
		t.Error("Expected [1 4 9], got ", square)
	}
}

func TestMapFloat64(t *testing.T) {
	square := MapFloat64([]float64{1.1, 2.2, 3.3}, squareFloat64)
	if !reflect.DeepEqual(square, []float64{1.2100000000000002, 4.840000000000001, 10.889999999999999}) {
		t.Error("Expected 1.2100000000000002 4.840000000000001 10.889999999999999], got ", square)
	}
}

func TestMapString(t *testing.T) {
	square := MapString([]string{"A", "B", "C"}, squareString)
	if !reflect.DeepEqual(square, []string{"AA", "BB", "CC"}) {
		t.Error("Expected [AA BB CC], got ", square)
	}
}

func TestToMatrixInt(t *testing.T) {
	_, err := ToMatrixInt([]int{1, 2, 3, 4}, 0)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 0), ", got ", err)
	}
	_, err = ToMatrixInt([]int{1, 2, 3, 4}, 3)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 3), ", got ", err)
	}
	matrix, _ := ToMatrixInt([]int{1, 2, 3, 4}, 2)
	if !reflect.DeepEqual(matrix, [][]int{{1, 2}, {3, 4}}) {
		t.Error("Expected [[1, 2] [3, 4]], got ", matrix)
	}
}

func TestToMatrixFloat64(t *testing.T) {
	_, err := ToMatrixFloat64([]float64{1.1, 2.2, 3.3, 4.4}, 0)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 0), ", got ", err)
	}
	_, err = ToMatrixFloat64([]float64{1.1, 2.2, 3.3, 4.4}, 3)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 3), ", got ", err)
	}
	matrix, _ := ToMatrixFloat64([]float64{1.1, 2.2, 3.3, 4.4}, 2)
	if !reflect.DeepEqual(matrix, [][]float64{{1.1, 2.2}, {3.3, 4.4}}) {
		t.Error("Expected [[1.1, 2.2] [3.3, 4.4]], got ", matrix)
	}
}

func TestToMatrixString(t *testing.T) {
	_, err := ToMatrixString([]string{"A", "B", "C", "D"}, 0)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 0), ", got ", err)
	}
	_, err = ToMatrixString([]string{"A", "B", "C", "D"}, 3)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 3), ", got ", err)
	}
	matrix, _ := ToMatrixString([]string{"A", "B", "C", "D"}, 2)
	if !reflect.DeepEqual(matrix, [][]string{{"A", "B"}, {"C", "D"}}) {
		t.Error("Expected [[A, B] [C, D]], got ", matrix)
	}
}

func TestSum(t *testing.T) {
	sum, _ := Sum([]int{1, 2, 3})
	if sum != int64(6) {
		t.Error("Expected 6 got ", sum)
	}
	sum, _ = Sum([]float64{1, 2, 3})
	if sum != float64(6) {
		t.Error("Expected 6 got ", sum)
	}
	sum, _ = Sum([]float64{0})
	if sum != float64(0) {
		t.Error("Expected 0 got ", sum)
	}
	_, err := Sum([]bool{true})
	if err == nil {
		t.Error("Expected ", fmt.Errorf("cannot sum the given slice"), " got <nil>")
	}
	_, err = Sum(2)
	if err == nil {
		t.Error("Expected ", "Expected slice, got: int ", " got <nil>")
	}

}

func TestFindIndex(t *testing.T) {
	_, err := FindIndex(6, 2)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("expected slice, got: int"), ", got ", err)
	}
	_, err = FindIndex([]int{}, 2)
	if err != nil {
		t.Error("Expected nil got ", err)
	}
	_, err = FindIndex([]int{1, 2, 3}, "india")
	if err == nil {
		t.Error("Expected ", fmt.Errorf("Expected element to be int, got: string "), " got nil")
	}
	index, _ := FindIndex([]int{1, 2, 3}, 1)
	if index != 0 {
		t.Error("Expected index to be 0 got ", index)
	}
	index, _ = FindIndex([]float64{1, 2, 3}, 2.0)
	if index != 1 {
		t.Error("Expected index to be 1 got ", index)
	}
	index, _ = FindIndex([]string{"india", "is", "great"}, "great")
	if index != 2 {
		t.Error("Expected index to be 2 got ", index)
	}
	index, _ = FindIndex([]string{"india", "is", "great"}, "are")
	if index != -1 {
		t.Error("Expected index to be -1 got ", index)
	}
}

func TestFindLastIndex(t *testing.T) {
	_, err := FindLastIndex(6, 2)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("expected slice, got: int"), ", got ", err)
	}
	_, err = FindLastIndex([]int{}, 2)
	if err != nil {
		t.Error("Expected nil got ", err)
	}
	_, err = FindLastIndex([]int{1, 2, 3}, "india")
	if err == nil {
		t.Error("Expected ", fmt.Errorf("Expected element to be int, got: string "), " got nil")
	}
	index, _ := FindLastIndex([]int{1, 2, 3, 1}, 1)
	if index != 3 {
		t.Error("Expected index to be 3 got ", index)
	}
	index, _ = FindLastIndex([]float64{1, 2, 3, 2, 2}, 2.0)
	if index != 4 {
		t.Error("Expected index to be 4 got ", index)
	}
	index, _ = FindLastIndex([]string{"great", "india", "is", "great"}, "great")
	if index != 3 {
		t.Error("Expected index to be 3 got ", index)
	}
	index, _ = FindLastIndex([]string{"india", "is", "great"}, "are")
	if index != -1 {
		t.Error("Expected index to be -1 got ", index)
	}
}

func TestGetKeys(t *testing.T) {
	var Map = make(map[string]string)
	Map["key1"] = "Value1"
	Map["key2"] = "Value2"
	Map["key3"] = "Value3"
	Keys, _ := GetKeys(Map)
	expectedKeys := []string{"key1", "key2", "key3"}
	if reflect.DeepEqual(Keys, expectedKeys) {
		t.Error("Expected ",expectedKeys, " got ", Keys)
	}
	_, err := GetKeys(1)
	if err == nil {
		t.Error("Expected Error got nil")
	}
}

func TestGetValues(t *testing.T) {
	var Map = make(map[string]string)
	Map["key1"] = "Value1"
	Map["key2"] = "Value2"
	Map["key3"] = "Value3"
	Values, _ := GetValues(Map)
	expectedValues := []string{"Value1", "Value2", "Value3"}
	if reflect.DeepEqual(Values, expectedValues) {
		t.Error("Expected ",expectedValues, " got ", Values)
	}
	_, err := GetValues(1)
	if err == nil {
		t.Error("Expected Error got nil")
	}
}<commit_msg>GoFmt run<commit_after>package gotility

import (
	"fmt"
	"reflect"
	"testing"
)

func squareInt(arg int) int {
	return arg * arg
}

func squareFloat64(arg float64) float64 {
	return arg * arg
}

func squareString(arg string) string {
	return arg + arg
}

func TestIsEmail(t *testing.T) {
	isEmail := IsEmail("xyz@gmail.com")
	if isEmail != true {
		t.Error("Expected true, got ", isEmail)
	}
	isEmail = IsEmail("mail")
	if isEmail != false {
		t.Error("Expected false, got ", isEmail)
	}
}

func TestIsUrl(t *testing.T) {
	isUrl := IsUrl("google.com")
	if isUrl != true {
		t.Error("Expected true, got ", isUrl)
	}
	isUrl = IsUrl("123.com1")
	if isUrl != false {
		t.Error("Expected false, got ", isUrl)
	}
}

func TestIsNumber(t *testing.T) {
	isNumber := IsNumber("12134.0009")
	if isNumber != true {
		t.Error("Expected true, got ", isNumber)
	}
	isNumber = IsNumber("a12134.0009")
	if isNumber != false {
		t.Error("Expected false, got ", isNumber)
	}
}

func TestIsTitleCase(t *testing.T) {
	isTitleCase := IsTitleCase("Go Is VEry Popular")
	if isTitleCase != true {
		t.Error("Expected true, got ", isTitleCase)
	}
	isTitleCase = IsTitleCase("gO IS NOT POPULAR")
	if isTitleCase != false {
		t.Error("Expected false, got ", isTitleCase)
	}
}

func TestFlattenFloat64(t *testing.T) {
	row := FlattenFloat64([][]float64{{1.1, 2.2}, {3.3, 4.4}})
	if !reflect.DeepEqual(row, []float64{1.1, 2.2, 3.3, 4.4}) {
		t.Error("Expected [1.1 2.2 3.3 4.4], got ", row)
	}
}

func TestFlattenInt(t *testing.T) {
	row := FlattenInt([][]int{{1, 2}, {3, 4}})
	if !reflect.DeepEqual(row, []int{1, 2, 3, 4}) {
		t.Error("Expected [1 2 3 4], got ", row)
	}
}

func TestFlattenString(t *testing.T) {
	row := FlattenString([][]string{{"A", "B"}, {"C", "D"}})
	if !reflect.DeepEqual(row, []string{"A", "B", "C", "D"}) {
		t.Error("Expected [A B C D], got ", row)
	}
}

func TestSumInt(t *testing.T) {
	sum := SumInt([]int{1, 2, 3})
	if sum != 6 {
		t.Error("Expected 6, got ", sum)
	}
}

func TestSumFloat64(t *testing.T) {
	sum := SumFloat64([]float64{1.1, 2.2, 3.3})
	if sum != 6.6 {
		t.Error("Expected 6.6 got ", sum)
	}
}

func TestMapInt(t *testing.T) {
	square := MapInt([]int{1, 2, 3}, squareInt)
	if !reflect.DeepEqual(square, []int{1, 4, 9}) {
		t.Error("Expected [1 4 9], got ", square)
	}
}

func TestMapFloat64(t *testing.T) {
	square := MapFloat64([]float64{1.1, 2.2, 3.3}, squareFloat64)
	if !reflect.DeepEqual(square, []float64{1.2100000000000002, 4.840000000000001, 10.889999999999999}) {
		t.Error("Expected 1.2100000000000002 4.840000000000001 10.889999999999999], got ", square)
	}
}

func TestMapString(t *testing.T) {
	square := MapString([]string{"A", "B", "C"}, squareString)
	if !reflect.DeepEqual(square, []string{"AA", "BB", "CC"}) {
		t.Error("Expected [AA BB CC], got ", square)
	}
}

func TestToMatrixInt(t *testing.T) {
	_, err := ToMatrixInt([]int{1, 2, 3, 4}, 0)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 0), ", got ", err)
	}
	_, err = ToMatrixInt([]int{1, 2, 3, 4}, 3)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 3), ", got ", err)
	}
	matrix, _ := ToMatrixInt([]int{1, 2, 3, 4}, 2)
	if !reflect.DeepEqual(matrix, [][]int{{1, 2}, {3, 4}}) {
		t.Error("Expected [[1, 2] [3, 4]], got ", matrix)
	}
}

func TestToMatrixFloat64(t *testing.T) {
	_, err := ToMatrixFloat64([]float64{1.1, 2.2, 3.3, 4.4}, 0)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 0), ", got ", err)
	}
	_, err = ToMatrixFloat64([]float64{1.1, 2.2, 3.3, 4.4}, 3)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 3), ", got ", err)
	}
	matrix, _ := ToMatrixFloat64([]float64{1.1, 2.2, 3.3, 4.4}, 2)
	if !reflect.DeepEqual(matrix, [][]float64{{1.1, 2.2}, {3.3, 4.4}}) {
		t.Error("Expected [[1.1, 2.2] [3.3, 4.4]], got ", matrix)
	}
}

func TestToMatrixString(t *testing.T) {
	_, err := ToMatrixString([]string{"A", "B", "C", "D"}, 0)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 0), ", got ", err)
	}
	_, err = ToMatrixString([]string{"A", "B", "C", "D"}, 3)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("division of row to matrix not possible. Invalid numRows : %d ", 3), ", got ", err)
	}
	matrix, _ := ToMatrixString([]string{"A", "B", "C", "D"}, 2)
	if !reflect.DeepEqual(matrix, [][]string{{"A", "B"}, {"C", "D"}}) {
		t.Error("Expected [[A, B] [C, D]], got ", matrix)
	}
}

func TestSum(t *testing.T) {
	sum, _ := Sum([]int{1, 2, 3})
	if sum != int64(6) {
		t.Error("Expected 6 got ", sum)
	}
	sum, _ = Sum([]float64{1, 2, 3})
	if sum != float64(6) {
		t.Error("Expected 6 got ", sum)
	}
	sum, _ = Sum([]float64{0})
	if sum != float64(0) {
		t.Error("Expected 0 got ", sum)
	}
	_, err := Sum([]bool{true})
	if err == nil {
		t.Error("Expected ", fmt.Errorf("cannot sum the given slice"), " got <nil>")
	}
	_, err = Sum(2)
	if err == nil {
		t.Error("Expected ", "Expected slice, got: int ", " got <nil>")
	}

}

func TestFindIndex(t *testing.T) {
	_, err := FindIndex(6, 2)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("expected slice, got: int"), ", got ", err)
	}
	_, err = FindIndex([]int{}, 2)
	if err != nil {
		t.Error("Expected nil got ", err)
	}
	_, err = FindIndex([]int{1, 2, 3}, "india")
	if err == nil {
		t.Error("Expected ", fmt.Errorf("Expected element to be int, got: string "), " got nil")
	}
	index, _ := FindIndex([]int{1, 2, 3}, 1)
	if index != 0 {
		t.Error("Expected index to be 0 got ", index)
	}
	index, _ = FindIndex([]float64{1, 2, 3}, 2.0)
	if index != 1 {
		t.Error("Expected index to be 1 got ", index)
	}
	index, _ = FindIndex([]string{"india", "is", "great"}, "great")
	if index != 2 {
		t.Error("Expected index to be 2 got ", index)
	}
	index, _ = FindIndex([]string{"india", "is", "great"}, "are")
	if index != -1 {
		t.Error("Expected index to be -1 got ", index)
	}
}

func TestFindLastIndex(t *testing.T) {
	_, err := FindLastIndex(6, 2)
	if err == nil {
		t.Error("Expected ", fmt.Errorf("expected slice, got: int"), ", got ", err)
	}
	_, err = FindLastIndex([]int{}, 2)
	if err != nil {
		t.Error("Expected nil got ", err)
	}
	_, err = FindLastIndex([]int{1, 2, 3}, "india")
	if err == nil {
		t.Error("Expected ", fmt.Errorf("Expected element to be int, got: string "), " got nil")
	}
	index, _ := FindLastIndex([]int{1, 2, 3, 1}, 1)
	if index != 3 {
		t.Error("Expected index to be 3 got ", index)
	}
	index, _ = FindLastIndex([]float64{1, 2, 3, 2, 2}, 2.0)
	if index != 4 {
		t.Error("Expected index to be 4 got ", index)
	}
	index, _ = FindLastIndex([]string{"great", "india", "is", "great"}, "great")
	if index != 3 {
		t.Error("Expected index to be 3 got ", index)
	}
	index, _ = FindLastIndex([]string{"india", "is", "great"}, "are")
	if index != -1 {
		t.Error("Expected index to be -1 got ", index)
	}
}

func TestGetKeys(t *testing.T) {
	var Map = make(map[string]string)
	Map["key1"] = "Value1"
	Map["key2"] = "Value2"
	Map["key3"] = "Value3"
	Keys, _ := GetKeys(Map)
	expectedKeys := []string{"key1", "key2", "key3"}
	if reflect.DeepEqual(Keys, expectedKeys) {
		t.Error("Expected ", expectedKeys, " got ", Keys)
	}
	_, err := GetKeys(1)
	if err == nil {
		t.Error("Expected Error got nil")
	}
}

func TestGetValues(t *testing.T) {
	var Map = make(map[string]string)
	Map["key1"] = "Value1"
	Map["key2"] = "Value2"
	Map["key3"] = "Value3"
	Values, _ := GetValues(Map)
	expectedValues := []string{"Value1", "Value2", "Value3"}
	if reflect.DeepEqual(Values, expectedValues) {
		t.Error("Expected ", expectedValues, " got ", Values)
	}
	_, err := GetValues(1)
	if err == nil {
		t.Error("Expected Error got nil")
	}
}
<|endoftext|>


<commit_before>// Package admin desrcibes the admin view containing references to
// various managers and editors
package admin

import (
	"bytes"
	"html/template"

	"github.com/nilslice/cms/content"
)

const adminHTML = `<!doctype html>

        <title>CMS</title>

CMS

            {{ range $t, $f := .Types }}

{{ $t }}

            {{ end }}

        {{ if .Subview}}

            {{ .Subview }}

        {{ end }}

`

type admin struct {
	Types   map[string]func() interface{}
	Subview template.HTML
}

// Admin ...
func Admin(manager []byte) []byte {
	a := admin{
		Types:   content.Types,
		Subview: template.HTML(manager),
	}

	buf := &bytes.Buffer{}
	tmpl := template.Must(template.New("admin").Parse(adminHTML))
	tmpl.Execute(buf, a)

	return buf.Bytes()
}
<commit_msg>css changes and additions for new input elements<commit_after>// Package admin desrcibes the admin view containing references to
// various managers and editors
package admin

import (
	"bytes"
	"html/template"

	"github.com/nilslice/cms/content"
)

const adminHTML = `<!doctype html>

        <title>CMS</title>

CMS

            {{ range $t, $f := .Types }}

{{ $t }}

            {{ end }}

        {{ if .Subview}}

            {{ .Subview }}

        {{ end }}

`

type admin struct {
	Types   map[string]func() interface{}
	Subview template.HTML
}

// Admin ...
func Admin(manager []byte) []byte {
	a := admin{
		Types:   content.Types,
		Subview: template.HTML(manager),
	}

	buf := &bytes.Buffer{}
	tmpl := template.Must(template.New("admin").Parse(adminHTML))
	tmpl.Execute(buf, a)

	return buf.Bytes()
}
<|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 implements the symbol table.

package golisp

import (
	"errors"
	"fmt"
)

type SymbolTableFrame struct {
	Parent      *SymbolTableFrame
	Frame       *FrameMap
	Bindings    map[string]*Binding
	CurrentCode string
}

var Global *SymbolTableFrame

func (self *SymbolTableFrame) InternalDump(frameNumber int) {
	fmt.Printf("Frame %d: %s\n", frameNumber, self.CurrentCode)
	for _, b := range self.Bindings {
		if b.Val == nil || TypeOf(b.Val) != PrimitiveType {
			b.Dump()
		}
	}
	fmt.Printf("\n")
	if self.Parent != nil {
		self.Parent.InternalDump(frameNumber + 1)
	}
}

func (self *SymbolTableFrame) Dump() {
	println()
	self.InternalDump(0)
}

func (self *SymbolTableFrame) DumpSingleFrame(frameNumber int) {
	if frameNumber == 0 {
		fmt.Printf("%s\n", self.CurrentCode)
		for _, b := range self.Bindings {
			if b.Val == nil || TypeOf(b.Val) != PrimitiveType {
				b.Dump()
			}
		}
		fmt.Printf("\n")
	} else if self.Parent != nil {
		self.Parent.DumpSingleFrame(frameNumber - 1)
	} else {
		fmt.Printf("Invalid frame selected.\n")
	}
}

func (self *SymbolTableFrame) InternalDumpHeaders(frameNumber int) {
	fmt.Printf("Frame %d: %s\n", frameNumber, self.CurrentCode)
	if self.Parent != nil {
		self.Parent.InternalDumpHeaders(frameNumber + 1)
	}
}

func (self *SymbolTableFrame) DumpHeaders() {
	println()
	self.InternalDumpHeaders(0)
}

func (self *SymbolTableFrame) DumpHeader() {
	fmt.Printf("%s\n", self.CurrentCode)
}

func NewSymbolTableFrameBelow(p *SymbolTableFrame) *SymbolTableFrame {
	var f *FrameMap = nil
	if p != nil {
		f = p.Frame
	}
	return &SymbolTableFrame{Parent: p, Bindings: make(map[string]*Binding), Frame: f}
}

func NewSymbolTableFrameBelowWithFrame(p *SymbolTableFrame, f *FrameMap) *SymbolTableFrame {
	if f == nil {
		f = p.Frame
	}
	return &SymbolTableFrame{Parent: p, Bindings: make(map[string]*Binding, 10), Frame: f}
}

func (self *SymbolTableFrame) HasFrame() bool {
	return self.Frame != nil
}

func (self *SymbolTableFrame) BindingNamed(name string) (b *Binding, present bool) {
	b, present = self.Bindings[name]
	return
}

func (self *SymbolTableFrame) SetBindingAt(name string, b *Binding) {
	self.Bindings[name] = b
}

func (self *SymbolTableFrame) findSymbol(name string) (symbol *Data, found bool) {
	binding, found := self.BindingNamed(name)
	if found {
		return binding.Sym, true
	} else if self.Parent != nil {
		return self.Parent.findSymbol(name)
	} else {
		return nil, false
	}
}

func (self *SymbolTableFrame) findBindingFor(symbol *Data) (binding *Binding, found bool) {
	name := StringValue(symbol)
	binding, found = self.BindingNamed(name)
	if found {
		return
	} else if self.Parent != nil {
		return self.Parent.findBindingFor(symbol)
	} else {
		return nil, false
	}
}

func (self *SymbolTableFrame) Intern(name string) (sym *Data) {
	sym, found := self.findSymbol(name)
	if !found {
		sym = SymbolWithName(name)
		self.BindTo(sym, nil)
		return
	} else {
		return nil
	}
}

func (self *SymbolTableFrame) BindTo(symbol *Data, value *Data) *Data {
	binding, found := self.findBindingFor(symbol)
	if found {
		binding.Val = value
	} else {
		binding := BindingWithSymbolAndValue(symbol, value)
		self.SetBindingAt(StringValue(symbol), binding)
	}
	return value
}

func (self *SymbolTableFrame) SetTo(symbol *Data, value *Data) (result *Data, err error) {
	localBinding, found := self.findBindingInLocalFrameFor(symbol)
	if found {
		localBinding.Val = value
		return value, nil
	}

	naked := StringValue(NakedSymbolFrom(symbol))
	if self.HasFrame() && self.Frame.HasSlot(naked) {
		self.Frame.Set(naked, value)
		return value, nil
	}

	binding, found := self.findBindingFor(symbol)
	if found {
		binding.Val = value
		return value, nil
	}

	return nil, errors.New(fmt.Sprintf("%s is undefined", StringValue(symbol)))
}

func (self *SymbolTableFrame) findBindingInLocalFrameFor(symbol *Data) (b *Binding, found bool) {
	return self.BindingNamed(StringValue(symbol))
}

func (self *SymbolTableFrame) BindLocallyTo(symbol *Data, value *Data) *Data {
	binding, found := self.findBindingInLocalFrameFor(symbol)
	if found {
		binding.Val = value
	} else {
		binding := BindingWithSymbolAndValue(symbol, value)
		self.SetBindingAt(StringValue(symbol), binding)
	}
	return value
}

func (self *SymbolTableFrame) ValueOf(symbol *Data) *Data {
	localBinding, found := self.findBindingInLocalFrameFor(symbol)
	if found {
		return localBinding.Val
	}

	naked := StringValue(NakedSymbolFrom(symbol))
	if self.HasFrame() && self.Frame.HasSlot(naked) {
		return self.Frame.Get(naked)
	}

	binding, found := self.findBindingFor(symbol)
	if found {
		return binding.Val
	} else {
		return nil
	}
}
<commit_msg>Added a depth function.<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 implements the symbol table.

package golisp

import (
	"errors"
	"fmt"
)

type SymbolTableFrame struct {
	Parent      *SymbolTableFrame
	Frame       *FrameMap
	Bindings    map[string]*Binding
	CurrentCode string
}

var Global *SymbolTableFrame

func (self *SymbolTableFrame) Depth() int {
	if self.Parent == nil {
		return 1
	} else {
		return 1 + self.Parent.Depth()
	}
}

func (self *SymbolTableFrame) InternalDump(frameNumber int) {
	fmt.Printf("Frame %d: %s\n", frameNumber, self.CurrentCode)
	for _, b := range self.Bindings {
		if b.Val == nil || TypeOf(b.Val) != PrimitiveType {
			b.Dump()
		}
	}
	fmt.Printf("\n")
	if self.Parent != nil {
		self.Parent.InternalDump(frameNumber + 1)
	}
}

func (self *SymbolTableFrame) Dump() {
	println()
	self.InternalDump(0)
}

func (self *SymbolTableFrame) DumpSingleFrame(frameNumber int) {
	if frameNumber == 0 {
		fmt.Printf("%s\n", self.CurrentCode)
		for _, b := range self.Bindings {
			if b.Val == nil || TypeOf(b.Val) != PrimitiveType {
				b.Dump()
			}
		}
		fmt.Printf("\n")
	} else if self.Parent != nil {
		self.Parent.DumpSingleFrame(frameNumber - 1)
	} else {
		fmt.Printf("Invalid frame selected.\n")
	}
}

func (self *SymbolTableFrame) InternalDumpHeaders(frameNumber int) {
	fmt.Printf("Frame %d: %s\n", frameNumber, self.CurrentCode)
	if self.Parent != nil {
		self.Parent.InternalDumpHeaders(frameNumber + 1)
	}
}

func (self *SymbolTableFrame) DumpHeaders() {
	println()
	self.InternalDumpHeaders(0)
}

func (self *SymbolTableFrame) DumpHeader() {
	fmt.Printf("%s\n", self.CurrentCode)
}

func NewSymbolTableFrameBelow(p *SymbolTableFrame) *SymbolTableFrame {
	var f *FrameMap = nil
	if p != nil {
		f = p.Frame
	}
	return &SymbolTableFrame{Parent: p, Bindings: make(map[string]*Binding), Frame: f}
}

func NewSymbolTableFrameBelowWithFrame(p *SymbolTableFrame, f *FrameMap) *SymbolTableFrame {
	if f == nil {
		f = p.Frame
	}
	return &SymbolTableFrame{Parent: p, Bindings: make(map[string]*Binding, 10), Frame: f}
}

func (self *SymbolTableFrame) HasFrame() bool {
	return self.Frame != nil
}

func (self *SymbolTableFrame) BindingNamed(name string) (b *Binding, present bool) {
	b, present = self.Bindings[name]
	return
}

func (self *SymbolTableFrame) SetBindingAt(name string, b *Binding) {
	self.Bindings[name] = b
}

func (self *SymbolTableFrame) findSymbol(name string) (symbol *Data, found bool) {
	binding, found := self.BindingNamed(name)
	if found {
		return binding.Sym, true
	} else if self.Parent != nil {
		return self.Parent.findSymbol(name)
	} else {
		return nil, false
	}
}

func (self *SymbolTableFrame) findBindingFor(symbol *Data) (binding *Binding, found bool) {
	name := StringValue(symbol)
	binding, found = self.BindingNamed(name)
	if found {
		return
	} else if self.Parent != nil {
		return self.Parent.findBindingFor(symbol)
	} else {
		return nil, false
	}
}

func (self *SymbolTableFrame) Intern(name string) (sym *Data) {
	sym, found := self.findSymbol(name)
	if !found {
		sym = SymbolWithName(name)
		self.BindTo(sym, nil)
		return
	} else {
		return nil
	}
}

func (self *SymbolTableFrame) BindTo(symbol *Data, value *Data) *Data {
	binding, found := self.findBindingFor(symbol)
	if found {
		binding.Val = value
	} else {
		binding := BindingWithSymbolAndValue(symbol, value)
		self.SetBindingAt(StringValue(symbol), binding)
	}
	return value
}

func (self *SymbolTableFrame) SetTo(symbol *Data, value *Data) (result *Data, err error) {
	localBinding, found := self.findBindingInLocalFrameFor(symbol)
	if found {
		localBinding.Val = value
		return value, nil
	}

	naked := StringValue(NakedSymbolFrom(symbol))
	if self.HasFrame() && self.Frame.HasSlot(naked) {
		self.Frame.Set(naked, value)
		return value, nil
	}

	binding, found := self.findBindingFor(symbol)
	if found {
		binding.Val = value
		return value, nil
	}

	return nil, errors.New(fmt.Sprintf("%s is undefined", StringValue(symbol)))
}

func (self *SymbolTableFrame) findBindingInLocalFrameFor(symbol *Data) (b *Binding, found bool) {
	return self.BindingNamed(StringValue(symbol))
}

func (self *SymbolTableFrame) BindLocallyTo(symbol *Data, value *Data) *Data {
	binding, found := self.findBindingInLocalFrameFor(symbol)
	if found {
		binding.Val = value
	} else {
		binding := BindingWithSymbolAndValue(symbol, value)
		self.SetBindingAt(StringValue(symbol), binding)
	}
	return value
}

func (self *SymbolTableFrame) ValueOf(symbol *Data) *Data {
	localBinding, found := self.findBindingInLocalFrameFor(symbol)
	if found {
		return localBinding.Val
	}

	naked := StringValue(NakedSymbolFrom(symbol))
	if self.HasFrame() && self.Frame.HasSlot(naked) {
		return self.Frame.Get(naked)
	}

	binding, found := self.findBindingFor(symbol)
	if found {
		return binding.Val
	} else {
		return nil
	}
}
<|endoftext|>


<commit_before>package plugins

import (
    "strings"

    "github.com/bwmarrin/discordgo"
    "git.lukas.moe/sn0w/Karen/helpers"
)

// Announcement such as updates, downtimes...
type Announcement struct {}

// Commands that are availble to trigger an announcement
func (a *Announcement) Commands() []string {
    return []string {
        "announce",
    }
}

// Init func
func (a *Announcement) Init(s *discordgo.Session) {}

// Action of the announcement
func (a *Announcement) Action(command string, content string, msg *discordgo.Message, session *discordgo.Session) {
    if !helpers.IsBotAdmin(msg) {
        return
    }
    title := ""
    contentSplit := strings.Fields(content)
    subcommand := contentSplit[0]
    text := content[len(subcommand):]
    switch subcommand {
    case "update":
        title = ":loudspeaker: **UPDATE**"
    case "downtime":
        title = ":warning: **DOWNTIME**"
    case "maintenance":
        title = ":clock5: **MAINTENANCE**"
    }
    // Iterate through all joined guilds
    for _, guild := range session.State.Guilds {
        // Check if we have an announcement channel set for this guild
        if helpers.GuildSettingsGetCached(guild.ID).AnnouncementsEnabled {
            // Get the announcement channel id
            channelID := helpers.GuildSettingsGetCached(guild.ID).AnnouncementsChannel
            // Send the announce to the channel
            session.ChannelMessageSendEmbed(channelID, &discordgo.MessageEmbed{
                Title: title,
                Description: text,
                Color: 0x0FADED,
            })
        }
    }
}<commit_msg>[plugin|announcements] Fix syntax error<commit_after>package plugins

import (
    "strings"

    "github.com/bwmarrin/discordgo"
    "git.lukas.moe/sn0w/Karen/helpers"
)

// Announcement such as updates, downtimes...
type Announcement struct {}

// Commands that are availble to trigger an announcement
func (a *Announcement) Commands() []string {
    return []string {
        "announce",
    }
}

// Init func
func (a *Announcement) Init(s *discordgo.Session) {}

// Action of the announcement
func (a *Announcement) Action(command string, content string, msg *discordgo.Message, session *discordgo.Session) {
    if !helpers.IsBotAdmin(msg.Author.ID) {
        return
    }

    title := ""
    contentSplit := strings.Fields(content)
    subcommand := contentSplit[0]
    text := content[len(subcommand):]

    switch subcommand {
    case "update":
        title = ":loudspeaker: **UPDATE**"
    case "downtime":
        title = ":warning: **DOWNTIME**"
    case "maintenance":
        title = ":clock5: **MAINTENANCE**"
    }
    // Iterate through all joined guilds
    for _, guild := range session.State.Guilds {
        // Check if we have an announcement channel set for this guild
        if helpers.GuildSettingsGetCached(guild.ID).AnnouncementsEnabled {
            // Get the announcement channel id
            channelID := helpers.GuildSettingsGetCached(guild.ID).AnnouncementsChannel
            // Send the announce to the channel
            session.ChannelMessageSendEmbed(channelID, &discordgo.MessageEmbed{
                Title: title,
                Description: text,
                Color: 0x0FADED,
            })
        }
    }
}
<|endoftext|>


<commit_before>package types

import "github.com/docker/docker/pkg/version"

// ContainerCreateResponse contains the information returned to a client on the
// creation of a new container.
type ContainerCreateResponse struct {
	// ID is the ID of the created container.
	ID string `json:"Id"`

	// Warnings are any warnings encountered during the creation of the container.
	Warnings []string `json:"Warnings"`
}

// POST /containers/{name:.*}/exec
type ContainerExecCreateResponse struct {
	// ID is the exec ID.
	ID string `json:"Id"`

	// Warnings are any warnings encountered during the execution of the command.
	Warnings []string `json:"Warnings"`
}

// POST /auth
type AuthResponse struct {
	// Status is the authentication status
	Status string `json:"Status"`
}

// POST "/containers/"+containerID+"/wait"
type ContainerWaitResponse struct {
	// StatusCode is the status code of the wait job
	StatusCode int `json:"StatusCode"`
}

// POST "/commit?container="+containerID
type ContainerCommitResponse struct {
	ID string `json:"Id"`
}

// GET "/containers/{name:.*}/changes"
type ContainerChange struct {
	Kind int
	Path string
}

// GET "/images/{name:.*}/history"
type ImageHistory struct {
	ID        string `json:"Id"`
	Created   int64
	CreatedBy string
	Tags      []string
	Size      int64
	Comment   string
}

// DELETE "/images/{name:.*}"
type ImageDelete struct {
	Untagged string `json:",omitempty"`
	Deleted  string `json:",omitempty"`
}

// GET "/images/json"
type Image struct {
	ID          string `json:"Id"`
	ParentId    string
	RepoTags    []string
	RepoDigests []string
	Created     int
	Size        int
	VirtualSize int
	Labels      map[string]string
}

type LegacyImage struct {
	ID          string `json:"Id"`
	Repository  string
	Tag         string
	Created     int
	Size        int
	VirtualSize int
}

// GET  "/containers/json"
type Port struct {
	IP          string
	PrivatePort int
	PublicPort  int
	Type        string
}

type Container struct {
	ID         string            `json:"Id"`
	Names      []string          `json:",omitempty"`
	Image      string            `json:",omitempty"`
	Command    string            `json:",omitempty"`
	Created    int               `json:",omitempty"`
	Ports      []Port            `json:",omitempty"`
	SizeRw     int               `json:",omitempty"`
	SizeRootFs int               `json:",omitempty"`
	Labels     map[string]string `json:",omitempty"`
	Status     string            `json:",omitempty"`
}

// POST "/containers/"+containerID+"/copy"
type CopyConfig struct {
	Resource string
}

// GET "/containers/{name:.*}/top"
type ContainerProcessList struct {
	Processes [][]string
	Titles    []string
}

type Version struct {
	Version       string
	ApiVersion    version.Version
	GitCommit     string
	GoVersion     string
	Os            string
	Arch          string
	KernelVersion string `json:",omitempty"`
}

// GET "/info"
type Info struct {
	ID                 string
	Containers         int
	Images             int
	Driver             string
	DriverStatus       [][2]string
	MemoryLimit        bool
	SwapLimit          bool
	IPv4Forwarding     bool
	Debug              bool
	NFd                int
	NGoroutines        int
	SystemTime         string
	ExecutionDriver    string
	LoggingDriver      string
	NEventsListener    int
	KernelVersion      string
	OperatingSystem    string
	IndexServerAddress string
	RegistryConfig     interface{}
	InitSha1           string
	InitPath           string
	NCPU               int
	MemTotal           int64
	DockerRootDir      string
	HttpProxy          string
	HttpsProxy         string
	NoProxy            string
	Name               string
	Labels             []string
}
<commit_msg>Add support cpu cfs quota<commit_after>package types

import "github.com/docker/docker/pkg/version"

// ContainerCreateResponse contains the information returned to a client on the
// creation of a new container.
type ContainerCreateResponse struct {
	// ID is the ID of the created container.
	ID string `json:"Id"`

	// Warnings are any warnings encountered during the creation of the container.
	Warnings []string `json:"Warnings"`
}

// POST /containers/{name:.*}/exec
type ContainerExecCreateResponse struct {
	// ID is the exec ID.
	ID string `json:"Id"`

	// Warnings are any warnings encountered during the execution of the command.
	Warnings []string `json:"Warnings"`
}

// POST /auth
type AuthResponse struct {
	// Status is the authentication status
	Status string `json:"Status"`
}

// POST "/containers/"+containerID+"/wait"
type ContainerWaitResponse struct {
	// StatusCode is the status code of the wait job
	StatusCode int `json:"StatusCode"`
}

// POST "/commit?container="+containerID
type ContainerCommitResponse struct {
	ID string `json:"Id"`
}

// GET "/containers/{name:.*}/changes"
type ContainerChange struct {
	Kind int
	Path string
}

// GET "/images/{name:.*}/history"
type ImageHistory struct {
	ID        string `json:"Id"`
	Created   int64
	CreatedBy string
	Tags      []string
	Size      int64
	Comment   string
}

// DELETE "/images/{name:.*}"
type ImageDelete struct {
	Untagged string `json:",omitempty"`
	Deleted  string `json:",omitempty"`
}

// GET "/images/json"
type Image struct {
	ID          string `json:"Id"`
	ParentId    string
	RepoTags    []string
	RepoDigests []string
	Created     int
	Size        int
	VirtualSize int
	Labels      map[string]string
}

type LegacyImage struct {
	ID          string `json:"Id"`
	Repository  string
	Tag         string
	Created     int
	Size        int
	VirtualSize int
}

// GET  "/containers/json"
type Port struct {
	IP          string
	PrivatePort int
	PublicPort  int
	Type        string
}

type Container struct {
	ID         string            `json:"Id"`
	Names      []string          `json:",omitempty"`
	Image      string            `json:",omitempty"`
	Command    string            `json:",omitempty"`
	Created    int               `json:",omitempty"`
	Ports      []Port            `json:",omitempty"`
	SizeRw     int               `json:",omitempty"`
	SizeRootFs int               `json:",omitempty"`
	Labels     map[string]string `json:",omitempty"`
	Status     string            `json:",omitempty"`
}

// POST "/containers/"+containerID+"/copy"
type CopyConfig struct {
	Resource string
}

// GET "/containers/{name:.*}/top"
type ContainerProcessList struct {
	Processes [][]string
	Titles    []string
}

type Version struct {
	Version       string
	ApiVersion    version.Version
	GitCommit     string
	GoVersion     string
	Os            string
	Arch          string
	KernelVersion string `json:",omitempty"`
}

// GET "/info"
type Info struct {
	ID                 string
	Containers         int
	Images             int
	Driver             string
	DriverStatus       [][2]string
	MemoryLimit        bool
	SwapLimit          bool
	CpuCfsQuota        bool
	IPv4Forwarding     bool
	Debug              bool
	NFd                int
	NGoroutines        int
	SystemTime         string
	ExecutionDriver    string
	LoggingDriver      string
	NEventsListener    int
	KernelVersion      string
	OperatingSystem    string
	IndexServerAddress string
	RegistryConfig     interface{}
	InitSha1           string
	InitPath           string
	NCPU               int
	MemTotal           int64
	DockerRootDir      string
	HttpProxy          string
	HttpsProxy         string
	NoProxy            string
	Name               string
	Labels             []string
}
<|endoftext|>


<commit_before>package main

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

	"github.com/animenotifier/arn"
	"github.com/animenotifier/twist"
	"github.com/fatih/color"
)

var rateLimiter = time.NewTicker(500 * time.Millisecond)

func main() {
	// Replace this with ID list from twist.moe later
	twistAnime, err := twist.GetAnimeIndex()
	arn.PanicOnError(err)
	idList := twistAnime.KitsuIDs()

	color.Yellow("Refreshing twist.moe links for %d anime", len(idList))

	for count, animeID := range idList {
		// Wait for rate limiter
		<-rateLimiter.C

		anime, animeErr := arn.GetAnime(animeID)

		if animeErr != nil {
			color.Red("Error fetching anime from the database with ID %s: %v", animeID, animeErr)
			continue
		}

		// Log
		fmt.Fprintf(os.Stdout, "[%d / %d] ", count+1, len(idList))

		// Get twist.moe feed
		feed, err := twist.GetFeedByKitsuID(animeID)

		if err != nil {
			color.Red("Error querying ID %s: %v", animeID, err)
			continue
		}

		episodes := feed.Episodes

		// // Sort by episode number
		// sort.Slice(episodes, func(a, b int) bool {
		// 	return episodes[a].Number < episodes[b].Number
		// })

		for _, episode := range episodes {
			arnEpisode := anime.EpisodeByNumber(episode.Number)

			if arnEpisode == nil {
				color.Red("Anime %s Episode %d not found", anime.ID, episode.Number)
				continue
			}

			if arnEpisode.Links == nil {
				arnEpisode.Links = map[string]string{}
			}

			arnEpisode.Links["twist.moe"] = strings.Replace(episode.Link, "https://test.twist.moe/", "https://twist.moe/", 1)
		}

		arn.PanicOnError(anime.Episodes().Save())
		color.Green("Found %d episodes for anime %s", len(episodes), animeID)
	}
}
<commit_msg>Improved twist.moe updater<commit_after>package main

import (
	"fmt"
	"os"
	"time"

	"github.com/animenotifier/arn"
	"github.com/animenotifier/twist"
	"github.com/fatih/color"
)

var rateLimiter = time.NewTicker(500 * time.Millisecond)

func main() {
	// Replace this with ID list from twist.moe later
	twistAnime, err := twist.GetAnimeIndex()
	arn.PanicOnError(err)
	idList := twistAnime.KitsuIDs()

	color.Yellow("Refreshing twist.moe links for %d anime", len(idList))

	for count, animeID := range idList {
		anime, animeErr := arn.GetAnime(animeID)

		if animeErr != nil {
			color.Red("Error fetching anime from the database with ID %s: %v", animeID, animeErr)
			continue
		}

		// Log
		fmt.Fprintf(os.Stdout, "[%d / %d] ", count+1, len(idList))

		// Refresh
		anime.RefreshEpisodes()

		// Ok
		color.Green("Found %d episodes for anime %s", len(anime.Episodes().Items), animeID)

		// Wait for rate limiter
		<-rateLimiter.C
	}
}
<|endoftext|>


<commit_before>package heroku

import (
	"encoding/json"
	"errors"
	"fmt"
	"log"
	"net/http"
	"net/http/httputil"
	"os"
	"strings"

	"code.google.com/p/go-uuid/uuid"
)

var DefaultTransport = &Transport{}

var DefaultClient = &http.Client{
	Transport: DefaultTransport,
}

type Transport struct {
	// Username is the HTTP basic auth username for API calls made by this Client.
	Username string

	// Password is the HTTP basic auth password for API calls made by this Client.
	Password string

	// UserAgent to be provided in API requests. Set to DefaultUserAgent if not
	// specified.
	UserAgent string

	// Debug mode can be used to dump the full request and response to stdout.
	Debug bool

	// AdditionalHeaders are extra headers to add to each HTTP request sent by
	// this Client.
	AdditionalHeaders http.Header

	// Transport is the HTTP transport to use when making requests.
	// It will default to http.DefaultTransport if nil.
	Transport http.RoundTripper
}

// Forward CancelRequest to underlying Transport
func (t *Transport) CancelRequest(req *http.Request) {
	type canceler interface {
		CancelRequest(*http.Request)
	}
	tr, ok := t.Transport.(canceler)
	if !ok {
		log.Printf("heroku: Client Transport of type %T doesn't support CancelRequest; Timeout not supported\n", t.Transport)
		return
	}
	tr.CancelRequest(req)
}

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
	if t.Transport == nil {
		t.Transport = http.DefaultTransport
	}

	// Making a copy of the Request so that
	// we don't modify the Request we were given.
	req = cloneRequest(req)

	if t.UserAgent != "" {
		req.Header.Set("User-Agent", t.UserAgent)
	}

	req.Header.Set("Accept", "application/vnd.heroku+json; version=3")
	req.Header.Set("Request-Id", uuid.New())
	req.SetBasicAuth(t.Username, t.Password)
	for k, v := range t.AdditionalHeaders {
		req.Header[k] = v
	}

	if t.Debug {
		dump, err := httputil.DumpRequestOut(req, true)
		if err != nil {
			log.Println(err)
		} else {
			os.Stderr.Write(dump)
			os.Stderr.Write([]byte{'\n', '\n'})
		}
	}

	resp, err := t.Transport.RoundTrip(req)
	if err != nil {
		if resp != nil {
			resp.Body.Close()
		}
		return nil, err
	}

	if t.Debug {
		dump, err := httputil.DumpResponse(resp, true)
		if err != nil {
			log.Println(err)
		} else {
			os.Stderr.Write(dump)
			os.Stderr.Write([]byte{'\n'})
		}
	}

	if err = checkResponse(resp); err != nil {
		if resp != nil {
			resp.Body.Close()
		}
		return nil, err
	}

	return resp, nil
}

type Error struct {
	error
	ID  string
	URL string
}

func checkResponse(resp *http.Response) error {
	if resp.StatusCode/100 != 2 { // 200, 201, 202, etc
		var e struct {
			Message string
			ID      string
			URL     string `json:"url"`
		}
		err := json.NewDecoder(resp.Body).Decode(&e)
		if err != nil {
			return fmt.Errorf("encountered an error : %s", resp.Status)
		}
		return Error{error: errors.New(e.Message), ID: e.ID, URL: e.URL}
	}
	if msg := resp.Header.Get("X-Heroku-Warning"); msg != "" {
		log.Println(strings.TrimSpace(msg))
	}
	return nil
}

// cloneRequest returns a clone of the provided *http.Request.
func cloneRequest(req *http.Request) *http.Request {
	// shallow copy of the struct
	clone := new(http.Request)
	*clone = *req
	// deep copy of the Header
	clone.Header = make(http.Header)
	for k, s := range req.Header {
		clone.Header[k] = s
	}
	return clone
}
<commit_msg>uuid lib has moved from code.google.com to github<commit_after>package heroku

import (
	"encoding/json"
	"errors"
	"fmt"
	"log"
	"net/http"
	"net/http/httputil"
	"os"
	"strings"

	"github.com/pborman/uuid"
)

var DefaultTransport = &Transport{}

var DefaultClient = &http.Client{
	Transport: DefaultTransport,
}

type Transport struct {
	// Username is the HTTP basic auth username for API calls made by this Client.
	Username string

	// Password is the HTTP basic auth password for API calls made by this Client.
	Password string

	// UserAgent to be provided in API requests. Set to DefaultUserAgent if not
	// specified.
	UserAgent string

	// Debug mode can be used to dump the full request and response to stdout.
	Debug bool

	// AdditionalHeaders are extra headers to add to each HTTP request sent by
	// this Client.
	AdditionalHeaders http.Header

	// Transport is the HTTP transport to use when making requests.
	// It will default to http.DefaultTransport if nil.
	Transport http.RoundTripper
}

// Forward CancelRequest to underlying Transport
func (t *Transport) CancelRequest(req *http.Request) {
	type canceler interface {
		CancelRequest(*http.Request)
	}
	tr, ok := t.Transport.(canceler)
	if !ok {
		log.Printf("heroku: Client Transport of type %T doesn't support CancelRequest; Timeout not supported\n", t.Transport)
		return
	}
	tr.CancelRequest(req)
}

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
	if t.Transport == nil {
		t.Transport = http.DefaultTransport
	}

	// Making a copy of the Request so that
	// we don't modify the Request we were given.
	req = cloneRequest(req)

	if t.UserAgent != "" {
		req.Header.Set("User-Agent", t.UserAgent)
	}

	req.Header.Set("Accept", "application/vnd.heroku+json; version=3")
	req.Header.Set("Request-Id", uuid.New())
	req.SetBasicAuth(t.Username, t.Password)
	for k, v := range t.AdditionalHeaders {
		req.Header[k] = v
	}

	if t.Debug {
		dump, err := httputil.DumpRequestOut(req, true)
		if err != nil {
			log.Println(err)
		} else {
			os.Stderr.Write(dump)
			os.Stderr.Write([]byte{'\n', '\n'})
		}
	}

	resp, err := t.Transport.RoundTrip(req)
	if err != nil {
		if resp != nil {
			resp.Body.Close()
		}
		return nil, err
	}

	if t.Debug {
		dump, err := httputil.DumpResponse(resp, true)
		if err != nil {
			log.Println(err)
		} else {
			os.Stderr.Write(dump)
			os.Stderr.Write([]byte{'\n'})
		}
	}

	if err = checkResponse(resp); err != nil {
		if resp != nil {
			resp.Body.Close()
		}
		return nil, err
	}

	return resp, nil
}

type Error struct {
	error
	ID  string
	URL string
}

func checkResponse(resp *http.Response) error {
	if resp.StatusCode/100 != 2 { // 200, 201, 202, etc
		var e struct {
			Message string
			ID      string
			URL     string `json:"url"`
		}
		err := json.NewDecoder(resp.Body).Decode(&e)
		if err != nil {
			return fmt.Errorf("encountered an error : %s", resp.Status)
		}
		return Error{error: errors.New(e.Message), ID: e.ID, URL: e.URL}
	}
	if msg := resp.Header.Get("X-Heroku-Warning"); msg != "" {
		log.Println(strings.TrimSpace(msg))
	}
	return nil
}

// cloneRequest returns a clone of the provided *http.Request.
func cloneRequest(req *http.Request) *http.Request {
	// shallow copy of the struct
	clone := new(http.Request)
	*clone = *req
	// deep copy of the Header
	clone.Header = make(http.Header)
	for k, s := range req.Header {
		clone.Header[k] = s
	}
	return clone
}
<|endoftext|>