<commit_before>/*
Copyright 2021 The Vitess 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.
*/

/*

ABOUT THIS TEST
===============

This test plays part in testing an upgrade path from a previous version/tag. It takes a GitHub workflow file to complete the functionality.
What's in this file is the setting up of a cluster, sharded and unsharded keyspace, creating and populating some tables, then testing retrieval of data.
The twist here is that you can run this test over pre-existing vtdataroot, which means this test can reuse existing etcd, existing tables, existing mysql,
in which case it will not attempt to create keyspaces/schemas/tables, nor will it populate table data. Instead, it will only check for retrieval of data.

The game is to setup the cluster with a stable version (say `v8.0.0`), take it down (and preserve data), then setup a new cluster with a new version (namely the branch/PR head) and attempt to read the data.

Both executions must force some settings so that both reuse same directories, ports, etc. An invocation will look like:
go test ./go/test/endtoend/versionupgrade80/upgrade_test.go --keep-data -force-vtdataroot /tmp/vtdataroot/vtroot_10901 --force-port-start 11900 --force-base-tablet-uid 1190

*/

package versionupgrade

import (
	"flag"
	"fmt"
	"os"
	"path"
	"testing"

	"vitess.io/vitess/go/mysql"

	"vitess.io/vitess/go/test/endtoend/cluster"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var (
	clusterInstance       *cluster.LocalProcessCluster
	vtParams              mysql.ConnParams
	hostname              = "localhost"
	keyspaceName          = "ks"
	cell                  = "zone1"
	schemaChangeDirectory = ""
	totalTableCount       = 4
	createTable           = `
		CREATE TABLE %s (
			id bigint(20) NOT NULL,
			msg varchar(64),
			PRIMARY KEY (id)
		) ENGINE=InnoDB;
		`
	insertIntoTable = `
		INSERT INTO %s (id, msg) VALUES (17, 'abc');
		`
	selectFromTable = `
		SELECT id, msg FROM %s LIMIT 1;
		`
)

// TestMain is the main entry point
func TestMain(m *testing.M) {
	defer cluster.PanicHandler(nil)
	flag.Parse()

	exitcode, err := func() (int, error) {
		clusterInstance = cluster.NewCluster(cell, hostname)
		schemaChangeDirectory = path.Join("/tmp", fmt.Sprintf("schema_change_dir_%d", clusterInstance.GetAndReserveTabletUID()))
		defer os.RemoveAll(schemaChangeDirectory)
		defer clusterInstance.Teardown()

		if _, err := os.Stat(schemaChangeDirectory); os.IsNotExist(err) {
			_ = os.Mkdir(schemaChangeDirectory, 0700)
		}

		clusterInstance.VtctldExtraArgs = []string{
			"-schema_change_dir", schemaChangeDirectory,
			"-schema_change_controller", "local",
			"-schema_change_check_interval", "1"}

		if err := clusterInstance.StartTopo(); err != nil {
			return 1, err
		}

		// Start keyspace
		keyspace := &cluster.Keyspace{
			Name: keyspaceName,
		}

		if err := clusterInstance.StartUnshardedKeyspaceLegacy(*keyspace, 2, true); err != nil {
			return 1, err
		}
		if err := clusterInstance.StartKeyspaceLegacy(*keyspace, []string{"1"}, 1, false); err != nil {
			return 1, err
		}

		// TODO: remove this once we upgrade to v12
		// setting the planner version to 0, so the vtgate binary's default is used
		clusterInstance.VtGatePlannerVersion = 0
		vtgateInstance := clusterInstance.NewVtgateInstance()
		// set the gateway we want to use
		vtgateInstance.GatewayImplementation = "tabletgateway"
		// Start vtgate
		if err := vtgateInstance.Setup(); err != nil {
			return 1, err
		}
		// ensure it is torn down during cluster TearDown
		clusterInstance.VtgateProcess = *vtgateInstance
		vtParams = mysql.ConnParams{
			Host: clusterInstance.Hostname,
			Port: clusterInstance.VtgateMySQLPort,
		}

		return m.Run(), nil
	}()
	if err != nil {
		fmt.Printf("%v\n", err)
		os.Exit(1)
	} else {
		os.Exit(exitcode)
	}

}

func TestShards(t *testing.T) {
	defer cluster.PanicHandler(t)
	assert.Equal(t, 2, len(clusterInstance.Keyspaces[0].Shards))
}

func TestDeploySchema(t *testing.T) {
	defer cluster.PanicHandler(t)

	if clusterInstance.ReusingVTDATAROOT {
		// we assume data is already deployed
		return
	}
	// Create n tables, populate
	for i := 0; i < totalTableCount; i++ {
		tableName := fmt.Sprintf("vt_upgrade_test_%02d", i)

		{
			sqlQuery := fmt.Sprintf(createTable, tableName)
			result, err := clusterInstance.VtctlclientProcess.ApplySchemaWithOutput(keyspaceName, sqlQuery, cluster.VtctlClientParams{DDLStrategy: ""})
			require.Nil(t, err, result)
		}
		for i := range clusterInstance.Keyspaces[0].Shards {
			sqlQuery := fmt.Sprintf(insertIntoTable, tableName)
			tablet := clusterInstance.Keyspaces[0].Shards[i].Vttablets[0]
			_, err := tablet.VttabletProcess.QueryTablet(sqlQuery, keyspaceName, true)
			require.Nil(t, err)
		}
	}

	checkTables(t, "", totalTableCount)
}

func TestTablesExist(t *testing.T) {
	defer cluster.PanicHandler(t)

	checkTables(t, "", totalTableCount)
}

// checkTables checks the number of tables in the first two shards.
func checkTables(t *testing.T, showTableName string, expectCount int) {
	for i := range clusterInstance.Keyspaces[0].Shards {
		checkTablesCount(t, clusterInstance.Keyspaces[0].Shards[i].Vttablets[0], showTableName, expectCount)
	}
}

// checkTablesCount checks the number of tables in the given tablet
func checkTablesCount(t *testing.T, tablet *cluster.Vttablet, showTableName string, expectCount int) {
	query := fmt.Sprintf(`show tables like '%%%s%%';`, showTableName)
	queryResult, err := tablet.VttabletProcess.QueryTablet(query, keyspaceName, true)
	require.Nil(t, err)
	assert.Equal(t, expectCount, len(queryResult.Rows))
}

// TestTablesData checks the data in tables
func TestTablesData(t *testing.T) {
	// Create n tables, populate
	for i := 0; i < totalTableCount; i++ {
		tableName := fmt.Sprintf("vt_upgrade_test_%02d", i)

		for i := range clusterInstance.Keyspaces[0].Shards {
			sqlQuery := fmt.Sprintf(selectFromTable, tableName)
			tablet := clusterInstance.Keyspaces[0].Shards[i].Vttablets[0]
			queryResult, err := tablet.VttabletProcess.QueryTablet(sqlQuery, keyspaceName, true)
			require.Nil(t, err)
			require.NotNil(t, queryResult)
			row := queryResult.Named().Row()
			require.NotNil(t, row)
			require.Equal(t, int64(17), row.AsInt64("id", 0))
			require.Equal(t, "abc", row.AsString("msg", ""))
		}
	}
}
<commit_msg>[endtoend/versionupgrade] fix flags in tests (#9917)<commit_after>/*
Copyright 2021 The Vitess 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.
*/

/*

ABOUT THIS TEST
===============

This test plays part in testing an upgrade path from a previous version/tag. It takes a GitHub workflow file to complete the functionality.
What's in this file is the setting up of a cluster, sharded and unsharded keyspace, creating and populating some tables, then testing retrieval of data.
The twist here is that you can run this test over pre-existing vtdataroot, which means this test can reuse existing etcd, existing tables, existing mysql,
in which case it will not attempt to create keyspaces/schemas/tables, nor will it populate table data. Instead, it will only check for retrieval of data.

The game is to setup the cluster with a stable version (say `v8.0.0`), take it down (and preserve data), then setup a new cluster with a new version (namely the branch/PR head) and attempt to read the data.

Both executions must force some settings so that both reuse same directories, ports, etc. An invocation will look like:
go test ./go/test/endtoend/versionupgrade80/upgrade_test.go --keep-data --force-vtdataroot /tmp/vtdataroot/vtroot_10901 --force-port-start 11900 --force-base-tablet-uid 1190

*/

package versionupgrade

import (
	"flag"
	"fmt"
	"os"
	"path"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"vitess.io/vitess/go/mysql"
	"vitess.io/vitess/go/test/endtoend/cluster"
)

var (
	clusterInstance       *cluster.LocalProcessCluster
	vtParams              mysql.ConnParams
	hostname              = "localhost"
	keyspaceName          = "ks"
	cell                  = "zone1"
	schemaChangeDirectory = ""
	totalTableCount       = 4
	createTable           = `
		CREATE TABLE %s (
			id bigint(20) NOT NULL,
			msg varchar(64),
			PRIMARY KEY (id)
		) ENGINE=InnoDB;
		`
	insertIntoTable = `
		INSERT INTO %s (id, msg) VALUES (17, 'abc');
		`
	selectFromTable = `
		SELECT id, msg FROM %s LIMIT 1;
		`
)

// TestMain is the main entry point
func TestMain(m *testing.M) {
	defer cluster.PanicHandler(nil)
	flag.Parse()

	exitcode, err := func() (int, error) {
		clusterInstance = cluster.NewCluster(cell, hostname)
		schemaChangeDirectory = path.Join("/tmp", fmt.Sprintf("schema_change_dir_%d", clusterInstance.GetAndReserveTabletUID()))
		defer os.RemoveAll(schemaChangeDirectory)
		defer clusterInstance.Teardown()

		if _, err := os.Stat(schemaChangeDirectory); os.IsNotExist(err) {
			_ = os.Mkdir(schemaChangeDirectory, 0700)
		}

		clusterInstance.VtctldExtraArgs = []string{
			"--schema_change_dir", schemaChangeDirectory,
			"--schema_change_controller", "local",
			"--schema_change_check_interval", "1"}

		if err := clusterInstance.StartTopo(); err != nil {
			return 1, err
		}

		// Start keyspace
		keyspace := &cluster.Keyspace{
			Name: keyspaceName,
		}

		if err := clusterInstance.StartUnshardedKeyspaceLegacy(*keyspace, 2, true); err != nil {
			return 1, err
		}
		if err := clusterInstance.StartKeyspaceLegacy(*keyspace, []string{"1"}, 1, false); err != nil {
			return 1, err
		}

		// TODO: remove this once we upgrade to v12
		// setting the planner version to 0, so the vtgate binary's default is used
		clusterInstance.VtGatePlannerVersion = 0
		vtgateInstance := clusterInstance.NewVtgateInstance()
		// set the gateway we want to use
		vtgateInstance.GatewayImplementation = "tabletgateway"
		// Start vtgate
		if err := vtgateInstance.Setup(); err != nil {
			return 1, err
		}
		// ensure it is torn down during cluster TearDown
		clusterInstance.VtgateProcess = *vtgateInstance
		vtParams = mysql.ConnParams{
			Host: clusterInstance.Hostname,
			Port: clusterInstance.VtgateMySQLPort,
		}

		return m.Run(), nil
	}()
	if err != nil {
		fmt.Printf("%v\n", err)
		os.Exit(1)
	} else {
		os.Exit(exitcode)
	}

}

func TestShards(t *testing.T) {
	defer cluster.PanicHandler(t)
	assert.Equal(t, 2, len(clusterInstance.Keyspaces[0].Shards))
}

func TestDeploySchema(t *testing.T) {
	defer cluster.PanicHandler(t)

	if clusterInstance.ReusingVTDATAROOT {
		// we assume data is already deployed
		return
	}
	// Create n tables, populate
	for i := 0; i < totalTableCount; i++ {
		tableName := fmt.Sprintf("vt_upgrade_test_%02d", i)

		{
			sqlQuery := fmt.Sprintf(createTable, tableName)
			result, err := clusterInstance.VtctlclientProcess.ApplySchemaWithOutput(keyspaceName, sqlQuery, cluster.VtctlClientParams{DDLStrategy: ""})
			require.Nil(t, err, result)
		}
		for i := range clusterInstance.Keyspaces[0].Shards {
			sqlQuery := fmt.Sprintf(insertIntoTable, tableName)
			tablet := clusterInstance.Keyspaces[0].Shards[i].Vttablets[0]
			_, err := tablet.VttabletProcess.QueryTablet(sqlQuery, keyspaceName, true)
			require.Nil(t, err)
		}
	}

	checkTables(t, "", totalTableCount)
}

func TestTablesExist(t *testing.T) {
	defer cluster.PanicHandler(t)

	checkTables(t, "", totalTableCount)
}

// checkTables checks the number of tables in the first two shards.
func checkTables(t *testing.T, showTableName string, expectCount int) {
	for i := range clusterInstance.Keyspaces[0].Shards {
		checkTablesCount(t, clusterInstance.Keyspaces[0].Shards[i].Vttablets[0], showTableName, expectCount)
	}
}

// checkTablesCount checks the number of tables in the given tablet
func checkTablesCount(t *testing.T, tablet *cluster.Vttablet, showTableName string, expectCount int) {
	query := fmt.Sprintf(`show tables like '%%%s%%';`, showTableName)
	queryResult, err := tablet.VttabletProcess.QueryTablet(query, keyspaceName, true)
	require.Nil(t, err)
	assert.Equal(t, expectCount, len(queryResult.Rows))
}

// TestTablesData checks the data in tables
func TestTablesData(t *testing.T) {
	// Create n tables, populate
	for i := 0; i < totalTableCount; i++ {
		tableName := fmt.Sprintf("vt_upgrade_test_%02d", i)

		for i := range clusterInstance.Keyspaces[0].Shards {
			sqlQuery := fmt.Sprintf(selectFromTable, tableName)
			tablet := clusterInstance.Keyspaces[0].Shards[i].Vttablets[0]
			queryResult, err := tablet.VttabletProcess.QueryTablet(sqlQuery, keyspaceName, true)
			require.Nil(t, err)
			require.NotNil(t, queryResult)
			row := queryResult.Named().Row()
			require.NotNil(t, row)
			require.Equal(t, int64(17), row.AsInt64("id", 0))
			require.Equal(t, "abc", row.AsString("msg", ""))
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"errors"
	"fmt"
	"math/rand"
	"time"
)

const backoffAttempts = 5

type retryable func() error

func main() {

	a, b := 0, 0

	res := 0

	f := func() error {
		var err error
		res, err = myFunction(a, b)
		if err != nil {
			return err
		}
		return nil
	}

	err := withSimpleRetry(f)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("output is ", res)
}

func withSimpleRetry(r retryable) error {
	i := 0
	var err error

	bkoff := BackoffConfig{}
	setDefaults(&bkoff)

	for i = 1; i <= backoffAttempts; i++ {
		if err = r(); err != nil {
			fmt.Println("received error. retrying... attempt", i)
			delay := bkoff.backoff(i)
			if delay < 0 {
				return errors.New("max retry duration exceeded, still failing. latest error: " + err.Error())
			}
			continue
		}
		return nil
	}
	return fmt.Errorf("failed after %v errors. last error is %v", i, err)
}

func myFunction(a, b int) (int, error) {
	//return 5, nil // enable to test the positive case
	return 0, fmt.Errorf("simulated error") // enable this to test the negative case
}

///////////////////////////////////
// below code is copied from https://github.com/grpc/grpc-go/blob/master/backoff.go

// DefaultBackoffConfig uses values specified for backoff
var DefaultBackoffConfig = BackoffConfig{
	MaxDelay:  120 * time.Second,
	baseDelay: 1.0 * time.Second,
	factor:    1.6,
	jitter:    0.2,
}

// backoffStrategy defines the methodology for backing off after a grpc
// connection failure.
type backoffStrategy interface {
	// backoff returns the amount of time to wait before the next retry given
	// the number of consecutive failures.
	backoff(retries int) time.Duration
}

// BackoffConfig defines the parameters for the default gRPC backoff strategy.
type BackoffConfig struct {
	// MaxDelay is the upper bound of backoff delay.
	MaxDelay time.Duration

	// baseDelay is the amount of time to wait before retrying after the first
	// failure.
	baseDelay time.Duration

	// factor is applied to the backoff after each retry.
	factor float64

	// jitter provides a range to randomize backoff delays.
	jitter float64
}

func setDefaults(bc *BackoffConfig) {
	md := bc.MaxDelay
	*bc = DefaultBackoffConfig

	if md > 0 {
		bc.MaxDelay = md
	}
}

func (bc BackoffConfig) backoff(retries int) time.Duration {
	if retries == 0 {
		return bc.baseDelay
	}
	backoff, max := float64(bc.baseDelay), float64(bc.MaxDelay)
	for backoff < max && retries > 0 {
		backoff *= bc.factor
		retries--
	}
	if backoff > max {
		return -1
	}
	// Randomize backoff delays so that if a cluster of requests start at
	// the same time, they won't operate in lockstep.
	backoff *= 1 + bc.jitter*(rand.Float64()*2-1)
	if backoff < 0 {
		return 0
	}
	return time.Duration(backoff)
}
<commit_msg>integration with grpc's backoff logic<commit_after>package main

import (
	"errors"
	"fmt"
	"math/rand"
	"time"
)

const backoffAttempts = 5

type retryable func() error

func main() {

	a, b := 0, 0

	res := 0

	f := func() error {
		var err error
		res, err = myFunction(a, b)
		if err != nil {
			return err
		}
		return nil
	}

	err := withSimpleRetry(f)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("output is ", res)
}

func withSimpleRetry(r retryable) error {
	i := 0
	var err error

	bkoff := BackoffConfig{}
	setDefaults(&bkoff)

	for i = 1; i <= backoffAttempts; i++ {
		if err = r(); err != nil {
			fmt.Println("received error. retrying... attempt", i)
			delay := bkoff.backoff(i)
			if delay < 0 {
				return errors.New("max retry duration exceeded, still failing. latest error: " + err.Error())
			}
			time.Sleep(delay)
			continue
		}
		return nil
	}
	return fmt.Errorf("failed after %v errors. last error is %v", i, err)
}

func myFunction(a, b int) (int, error) {
	//return 5, nil // enable to test the positive case
	return 0, fmt.Errorf("simulated error") // enable this to test the negative case
}

///////////////////////////////////
// below code is copied from https://github.com/grpc/grpc-go/blob/master/backoff.go

// DefaultBackoffConfig uses values specified for backoff
var DefaultBackoffConfig = BackoffConfig{
	MaxDelay:  120 * time.Second,
	baseDelay: 1.0 * time.Second,
	factor:    1.6,
	jitter:    0.2,
}

// backoffStrategy defines the methodology for backing off after a grpc
// connection failure.
type backoffStrategy interface {
	// backoff returns the amount of time to wait before the next retry given
	// the number of consecutive failures.
	backoff(retries int) time.Duration
}

// BackoffConfig defines the parameters for the default gRPC backoff strategy.
type BackoffConfig struct {
	// MaxDelay is the upper bound of backoff delay.
	MaxDelay time.Duration

	// baseDelay is the amount of time to wait before retrying after the first
	// failure.
	baseDelay time.Duration

	// factor is applied to the backoff after each retry.
	factor float64

	// jitter provides a range to randomize backoff delays.
	jitter float64
}

func setDefaults(bc *BackoffConfig) {
	md := bc.MaxDelay
	*bc = DefaultBackoffConfig

	if md > 0 {
		bc.MaxDelay = md
	}
}

func (bc BackoffConfig) backoff(retries int) time.Duration {
	if retries == 0 {
		return bc.baseDelay
	}
	backoff, max := float64(bc.baseDelay), float64(bc.MaxDelay)
	for backoff < max && retries > 0 {
		backoff *= bc.factor
		retries--
	}
	if backoff > max {
		return -1 // modified this to -1 to exit after reaching max backoff time.
	}
	// Randomize backoff delays so that if a cluster of requests start at
	// the same time, they won't operate in lockstep.
	backoff *= 1 + bc.jitter*(rand.Float64()*2-1)
	if backoff < 0 {
		return 0
	}
	return time.Duration(backoff)
}
<|endoftext|>


<commit_before>package google

import (
	"fmt"
	"reflect"
	"sort"
	"testing"

	"github.com/hashicorp/terraform/helper/acctest"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccPubsubSubscriptionIamBinding(t *testing.T) {
	t.Parallel()

	topic := "test-topic-iam-" + acctest.RandString(10)
	subscription := "test-subscription-iam-" + acctest.RandString(10)
	account := "test-iam-" + acctest.RandString(10)

	resource.Test(t, resource.TestCase{
		PreCheck:  func() { testAccPreCheck(t) },
		Providers: testAccProviders,
		Steps: []resource.TestStep{
			{
				// Test IAM Binding creation
				Config: testAccPubsubSubscriptionIamBinding_basic(subscription, topic, account),
				Check: testAccCheckPubsubSubscriptionIam(subscription, "roles/pubsub.subscriber", []string{
					fmt.Sprintf("serviceAccount:%s-1@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
				}),
			},
			{
				// Test IAM Binding update
				Config: testAccPubsubSubscriptionIamBinding_update(subscription, topic, account),
				Check: testAccCheckPubsubSubscriptionIam(subscription, "roles/pubsub.subscriber", []string{
					fmt.Sprintf("serviceAccount:%s-1@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
					fmt.Sprintf("serviceAccount:%s-2@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
				}),
			},
			{
				ResourceName:      "google_pubsub_subscription_iam_binding.foo",
				ImportStateId:     fmt.Sprintf("%s roles/pubsub.subscriber", getComputedSubscriptionName(getTestProjectFromEnv(), subscription)),
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccPubsubSubscriptionIamMember(t *testing.T) {
	t.Parallel()

	topic := "test-topic-iam-" + acctest.RandString(10)
	subscription := "test-subscription-iam-" + acctest.RandString(10)
	account := "test-iam-" + acctest.RandString(10)
	accountEmail := fmt.Sprintf("%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv())

	resource.Test(t, resource.TestCase{
		PreCheck:  func() { testAccPreCheck(t) },
		Providers: testAccProviders,
		Steps: []resource.TestStep{
			{
				// Test Iam Member creation (no update for member, no need to test)
				Config: testAccPubsubSubscriptionIamMember_basic(subscription, topic, account),
				Check: testAccCheckPubsubSubscriptionIam(subscription, "roles/pubsub.subscriber", []string{
					fmt.Sprintf("serviceAccount:%s", accountEmail),
				}),
			},
			{
				ResourceName:      "google_pubsub_subscription_iam_member.foo",
				ImportStateId:     fmt.Sprintf("%s roles/pubsub.subscriber serviceAccount:%s", getComputedSubscriptionName(getTestProjectFromEnv(), subscription), accountEmail),
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccPubsubSubscriptionIamPolicy(t *testing.T) {
	t.Parallel()

	topic := "test-topic-iam-" + acctest.RandString(10)
	subscription := "test-subscription-iam-" + acctest.RandString(10)
	account := "test-iam-" + acctest.RandString(10)

	resource.Test(t, resource.TestCase{
		PreCheck:  func() { testAccPreCheck(t) },
		Providers: testAccProviders,
		Steps: []resource.TestStep{
			{
				Config: testAccPubsubSubscriptionIamPolicy_basic(subscription, topic, account, "roles/pubsub.subscriber"),
				Check: testAccCheckPubsubSubscriptionIam(subscription, "roles/pubsub.subscriber", []string{
					fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
				}),
			},
			{
				Config: testAccPubsubSubscriptionIamPolicy_basic(subscription, topic, account, "roles/pubsub.viewer"),
				Check: testAccCheckPubsubSubscriptionIam(subscription, "roles/pubsub.viewer", []string{
					fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
				}),
			},
			{
				ResourceName:      "google_pubsub_subscription_iam_policy.foo",
				ImportStateId:     getComputedSubscriptionName(getTestProjectFromEnv(), subscription),
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func testAccCheckPubsubSubscriptionIam(subscription, role string, members []string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		config := testAccProvider.Meta().(*Config)
		p, err := config.clientPubsub.Projects.Subscriptions.GetIamPolicy(getComputedSubscriptionName(getTestProjectFromEnv(), subscription)).Do()
		if err != nil {
			return err
		}

		for _, binding := range p.Bindings {
			if binding.Role == role {
				sort.Strings(members)
				sort.Strings(binding.Members)

				if reflect.DeepEqual(members, binding.Members) {
					return nil
				}

				return fmt.Errorf("Binding found but expected members is %v, got %v", members, binding.Members)
			}
		}

		return fmt.Errorf("No binding for role %q", role)
	}
}

func testAccPubsubSubscriptionIamBinding_basic(subscription, topic, account string) string {
	return fmt.Sprintf(`
resource "google_pubsub_topic" "topic" {
  name = "%s"
}

resource "google_pubsub_subscription" "subscription" {
  name  = "%s"
  topic = "${google_pubsub_topic.topic.id}"
}

resource "google_service_account" "test-account-1" {
  account_id   = "%s-1"
  display_name = "Pubsub Subscription Iam Testing Account"
}

resource "google_pubsub_subscription_iam_binding" "foo" {
  subscription = "${google_pubsub_subscription.subscription.id}"
  role         = "roles/pubsub.subscriber"
  members      = [
    "serviceAccount:${google_service_account.test-account-1.email}",
  ]
}
`, topic, subscription, account)
}

func testAccPubsubSubscriptionIamBinding_update(subscription, topic, account string) string {
	return fmt.Sprintf(`
resource "google_pubsub_topic" "topic" {
  name = "%s"
}

resource "google_pubsub_subscription" "subscription" {
  name  = "%s"
  topic = "${google_pubsub_topic.topic.id}"
}

resource "google_service_account" "test-account-1" {
  account_id   = "%s-1"
  display_name = ""Pubsub Subscription Iam Testing Account"
}

resource "google_service_account" "test-account-2" {
  account_id   = "%s-2"
  display_name = ""Pubsub Subscription Iam Testing Account"
}

resource "google_pubsub_subscription_iam_binding" "foo" {
  subscription = "${google_pubsub_subscription.subscription.id}"
  role         = "roles/pubsub.subscriber"
  members      = [
    "serviceAccount:${google_service_account.test-account-1.email}",
    "serviceAccount:${google_service_account.test-account-2.email}",
  ]
}
`, topic, subscription, account, account)
}

func testAccPubsubSubscriptionIamMember_basic(subscription, topic, account string) string {
	return fmt.Sprintf(`
resource "google_pubsub_topic" "topic" {
  name = "%s"
}

resource "google_pubsub_subscription" "subscription" {
  name  = "%s"
  topic = "${google_pubsub_topic.topic.id}"
}

resource "google_service_account" "test-account" {
  account_id   = "%s"
  display_name = ""Pubsub Subscription Iam Testing Account"
}

resource "google_pubsub_subscription_iam_member" "foo" {
  subscription = "${google_pubsub_subscription.subscription.id}"
  role         = "roles/pubsub.subscriber"
  member       = "serviceAccount:${google_service_account.test-account.email}"
}
`, topic, subscription, account)
}

func testAccPubsubSubscriptionIamPolicy_basic(subscription, topic, account, role string) string {
	return fmt.Sprintf(`
resource "google_pubsub_topic" "topic" {
  name = "%s"
}

resource "google_pubsub_subscription" "subscription" {
  name  = "%s"
  topic = "${google_pubsub_topic.topic.id}"
}

resource "google_service_account" "test-account" {
  account_id   = "%s"
  display_name = ""Pubsub Subscription Iam Testing Account"
}

data "google_iam_policy" "foo" {
	binding {
		role    = "%s"
		members = ["serviceAccount:${google_service_account.test-account.email}"]
	}
}

resource "google_pubsub_subscription_iam_policy" "foo" {
  subscription = "${google_pubsub_subscription.subscription.id}"
  policy_data  = "${data.google_iam_policy.foo.policy_data}"
}
`, topic, subscription, account, role)
}
<commit_msg>Remove double quotes from pubsub subscription iam test<commit_after>package google

import (
	"fmt"
	"reflect"
	"sort"
	"testing"

	"github.com/hashicorp/terraform/helper/acctest"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccPubsubSubscriptionIamBinding(t *testing.T) {
	t.Parallel()

	topic := "test-topic-iam-" + acctest.RandString(10)
	subscription := "test-subscription-iam-" + acctest.RandString(10)
	account := "test-iam-" + acctest.RandString(10)

	resource.Test(t, resource.TestCase{
		PreCheck:  func() { testAccPreCheck(t) },
		Providers: testAccProviders,
		Steps: []resource.TestStep{
			{
				// Test IAM Binding creation
				Config: testAccPubsubSubscriptionIamBinding_basic(subscription, topic, account),
				Check: testAccCheckPubsubSubscriptionIam(subscription, "roles/pubsub.subscriber", []string{
					fmt.Sprintf("serviceAccount:%s-1@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
				}),
			},
			{
				// Test IAM Binding update
				Config: testAccPubsubSubscriptionIamBinding_update(subscription, topic, account),
				Check: testAccCheckPubsubSubscriptionIam(subscription, "roles/pubsub.subscriber", []string{
					fmt.Sprintf("serviceAccount:%s-1@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
					fmt.Sprintf("serviceAccount:%s-2@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
				}),
			},
			{
				ResourceName:      "google_pubsub_subscription_iam_binding.foo",
				ImportStateId:     fmt.Sprintf("%s roles/pubsub.subscriber", getComputedSubscriptionName(getTestProjectFromEnv(), subscription)),
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccPubsubSubscriptionIamMember(t *testing.T) {
	t.Parallel()

	topic := "test-topic-iam-" + acctest.RandString(10)
	subscription := "test-subscription-iam-" + acctest.RandString(10)
	account := "test-iam-" + acctest.RandString(10)
	accountEmail := fmt.Sprintf("%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv())

	resource.Test(t, resource.TestCase{
		PreCheck:  func() { testAccPreCheck(t) },
		Providers: testAccProviders,
		Steps: []resource.TestStep{
			{
				// Test Iam Member creation (no update for member, no need to test)
				Config: testAccPubsubSubscriptionIamMember_basic(subscription, topic, account),
				Check: testAccCheckPubsubSubscriptionIam(subscription, "roles/pubsub.subscriber", []string{
					fmt.Sprintf("serviceAccount:%s", accountEmail),
				}),
			},
			{
				ResourceName:      "google_pubsub_subscription_iam_member.foo",
				ImportStateId:     fmt.Sprintf("%s roles/pubsub.subscriber serviceAccount:%s", getComputedSubscriptionName(getTestProjectFromEnv(), subscription), accountEmail),
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func TestAccPubsubSubscriptionIamPolicy(t *testing.T) {
	t.Parallel()

	topic := "test-topic-iam-" + acctest.RandString(10)
	subscription := "test-subscription-iam-" + acctest.RandString(10)
	account := "test-iam-" + acctest.RandString(10)

	resource.Test(t, resource.TestCase{
		PreCheck:  func() { testAccPreCheck(t) },
		Providers: testAccProviders,
		Steps: []resource.TestStep{
			{
				Config: testAccPubsubSubscriptionIamPolicy_basic(subscription, topic, account, "roles/pubsub.subscriber"),
				Check: testAccCheckPubsubSubscriptionIam(subscription, "roles/pubsub.subscriber", []string{
					fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
				}),
			},
			{
				Config: testAccPubsubSubscriptionIamPolicy_basic(subscription, topic, account, "roles/pubsub.viewer"),
				Check: testAccCheckPubsubSubscriptionIam(subscription, "roles/pubsub.viewer", []string{
					fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
				}),
			},
			{
				ResourceName:      "google_pubsub_subscription_iam_policy.foo",
				ImportStateId:     getComputedSubscriptionName(getTestProjectFromEnv(), subscription),
				ImportState:       true,
				ImportStateVerify: true,
			},
		},
	})
}

func testAccCheckPubsubSubscriptionIam(subscription, role string, members []string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		config := testAccProvider.Meta().(*Config)
		p, err := config.clientPubsub.Projects.Subscriptions.GetIamPolicy(getComputedSubscriptionName(getTestProjectFromEnv(), subscription)).Do()
		if err != nil {
			return err
		}

		for _, binding := range p.Bindings {
			if binding.Role == role {
				sort.Strings(members)
				sort.Strings(binding.Members)

				if reflect.DeepEqual(members, binding.Members) {
					return nil
				}

				return fmt.Errorf("Binding found but expected members is %v, got %v", members, binding.Members)
			}
		}

		return fmt.Errorf("No binding for role %q", role)
	}
}

func testAccPubsubSubscriptionIamBinding_basic(subscription, topic, account string) string {
	return fmt.Sprintf(`
resource "google_pubsub_topic" "topic" {
  name = "%s"
}

resource "google_pubsub_subscription" "subscription" {
  name  = "%s"
  topic = "${google_pubsub_topic.topic.id}"
}

resource "google_service_account" "test-account-1" {
  account_id   = "%s-1"
  display_name = "Pubsub Subscription Iam Testing Account"
}

resource "google_pubsub_subscription_iam_binding" "foo" {
  subscription = "${google_pubsub_subscription.subscription.id}"
  role         = "roles/pubsub.subscriber"
  members      = [
    "serviceAccount:${google_service_account.test-account-1.email}",
  ]
}
`, topic, subscription, account)
}

func testAccPubsubSubscriptionIamBinding_update(subscription, topic, account string) string {
	return fmt.Sprintf(`
resource "google_pubsub_topic" "topic" {
  name = "%s"
}

resource "google_pubsub_subscription" "subscription" {
  name  = "%s"
  topic = "${google_pubsub_topic.topic.id}"
}

resource "google_service_account" "test-account-1" {
  account_id   = "%s-1"
  display_name = "Pubsub Subscription Iam Testing Account"
}

resource "google_service_account" "test-account-2" {
  account_id   = "%s-2"
  display_name = "Pubsub Subscription Iam Testing Account"
}

resource "google_pubsub_subscription_iam_binding" "foo" {
  subscription = "${google_pubsub_subscription.subscription.id}"
  role         = "roles/pubsub.subscriber"
  members      = [
    "serviceAccount:${google_service_account.test-account-1.email}",
    "serviceAccount:${google_service_account.test-account-2.email}",
  ]
}
`, topic, subscription, account, account)
}

func testAccPubsubSubscriptionIamMember_basic(subscription, topic, account string) string {
	return fmt.Sprintf(`
resource "google_pubsub_topic" "topic" {
  name = "%s"
}

resource "google_pubsub_subscription" "subscription" {
  name  = "%s"
  topic = "${google_pubsub_topic.topic.id}"
}

resource "google_service_account" "test-account" {
  account_id   = "%s"
  display_name = "Pubsub Subscription Iam Testing Account"
}

resource "google_pubsub_subscription_iam_member" "foo" {
  subscription = "${google_pubsub_subscription.subscription.id}"
  role         = "roles/pubsub.subscriber"
  member       = "serviceAccount:${google_service_account.test-account.email}"
}
`, topic, subscription, account)
}

func testAccPubsubSubscriptionIamPolicy_basic(subscription, topic, account, role string) string {
	return fmt.Sprintf(`
resource "google_pubsub_topic" "topic" {
  name = "%s"
}

resource "google_pubsub_subscription" "subscription" {
  name  = "%s"
  topic = "${google_pubsub_topic.topic.id}"
}

resource "google_service_account" "test-account" {
  account_id   = "%s"
  display_name = "Pubsub Subscription Iam Testing Account"
}

data "google_iam_policy" "foo" {
	binding {
		role    = "%s"
		members = ["serviceAccount:${google_service_account.test-account.email}"]
	}
}

resource "google_pubsub_subscription_iam_policy" "foo" {
  subscription = "${google_pubsub_subscription.subscription.id}"
  policy_data  = "${data.google_iam_policy.foo.policy_data}"
}
`, topic, subscription, account, role)
}
<|endoftext|>


<commit_before><commit_msg>fix model.Nav deprecated field error<commit_after><|endoftext|>


<commit_before>// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package atomic

import (
	"encoding/json"
	"math"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestUintptr(t *testing.T) {
	atom := NewUintptr(42)

	require.Equal(t, uintptr(42), atom.Load(), "Load didn't work.")
	require.Equal(t, uintptr(46), atom.Add(4), "Add didn't work.")
	require.Equal(t, uintptr(44), atom.Sub(2), "Sub didn't work.")
	require.Equal(t, uintptr(45), atom.Inc(), "Inc didn't work.")
	require.Equal(t, uintptr(44), atom.Dec(), "Dec didn't work.")

	require.True(t, atom.CAS(44, 0), "CAS didn't report a swap.")
	require.Equal(t, uintptr(0), atom.Load(), "CAS didn't set the correct value.")

	require.Equal(t, uintptr(0), atom.Swap(1), "Swap didn't return the old value.")
	require.Equal(t, uintptr(1), atom.Load(), "Swap didn't set the correct value.")

	atom.Store(42)
	require.Equal(t, uintptr(42), atom.Load(), "Store didn't set the correct value.")

	t.Run("JSON/Marshal", func(t *testing.T) {
		bytes, err := json.Marshal(atom)
		require.NoError(t, err, "json.Marshal errored unexpectedly.")
		require.Equal(t, []byte("42"), bytes, "json.Marshal encoded the wrong bytes.")
	})

	t.Run("JSON/Unmarshal", func(t *testing.T) {
		err := json.Unmarshal([]byte("40"), &atom)
		require.NoError(t, err, "json.Unmarshal errored unexpectedly.")
		require.Equal(t, uintptr(40), atom.Load(), "json.Unmarshal didn't set the correct value.")
	})

	t.Run("JSON/Unmarshal/Error", func(t *testing.T) {
		err := json.Unmarshal([]byte(`"40"`), &atom)
		require.Error(t, err, "json.Unmarshal didn't error as expected.")
		assertErrorJSONUnmarshalType(t, err,
			"json.Unmarshal failed with unexpected error %v, want UnmarshalTypeError.", err)
	})

	t.Run("String", func(t *testing.T) {
		// Use an integer with the signed bit set. If we're converting
		// incorrectly, we'll get a negative value here.
		atom := NewUintptr(uintptr(math.MaxUint64))
		assert.Equal(t, "18446744073709551615", atom.String(),
			"String() returned an unexpected value.")
	})
}
<commit_msg>uintptr test: Fix overflow on architectures with 32-bit pointers (#100)<commit_after>// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package atomic

import (
	"encoding/json"
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestUintptr(t *testing.T) {
	atom := NewUintptr(42)

	require.Equal(t, uintptr(42), atom.Load(), "Load didn't work.")
	require.Equal(t, uintptr(46), atom.Add(4), "Add didn't work.")
	require.Equal(t, uintptr(44), atom.Sub(2), "Sub didn't work.")
	require.Equal(t, uintptr(45), atom.Inc(), "Inc didn't work.")
	require.Equal(t, uintptr(44), atom.Dec(), "Dec didn't work.")

	require.True(t, atom.CAS(44, 0), "CAS didn't report a swap.")
	require.Equal(t, uintptr(0), atom.Load(), "CAS didn't set the correct value.")

	require.Equal(t, uintptr(0), atom.Swap(1), "Swap didn't return the old value.")
	require.Equal(t, uintptr(1), atom.Load(), "Swap didn't set the correct value.")

	atom.Store(42)
	require.Equal(t, uintptr(42), atom.Load(), "Store didn't set the correct value.")

	t.Run("JSON/Marshal", func(t *testing.T) {
		bytes, err := json.Marshal(atom)
		require.NoError(t, err, "json.Marshal errored unexpectedly.")
		require.Equal(t, []byte("42"), bytes, "json.Marshal encoded the wrong bytes.")
	})

	t.Run("JSON/Unmarshal", func(t *testing.T) {
		err := json.Unmarshal([]byte("40"), &atom)
		require.NoError(t, err, "json.Unmarshal errored unexpectedly.")
		require.Equal(t, uintptr(40), atom.Load(), "json.Unmarshal didn't set the correct value.")
	})

	t.Run("JSON/Unmarshal/Error", func(t *testing.T) {
		err := json.Unmarshal([]byte(`"40"`), &atom)
		require.Error(t, err, "json.Unmarshal didn't error as expected.")
		assertErrorJSONUnmarshalType(t, err,
			"json.Unmarshal failed with unexpected error %v, want UnmarshalTypeError.", err)
	})

	t.Run("String", func(t *testing.T) {
		// Use an integer with the signed bit set. If we're converting
		// incorrectly, we'll get a negative value here.
		// Use an int variable, as constants cause compile-time overflows.
		negative := -1
		atom := NewUintptr(uintptr(negative))
		want := fmt.Sprint(uintptr(negative))
		assert.Equal(t, want, atom.String(),
			"String() returned an unexpected value.")
	})
}
<|endoftext|>


<commit_before>package unit

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"strings"
	"syscall"

	systemdDbus "github.com/coreos/go-systemd/dbus"
	log "github.com/golang/glog"

	"github.com/coreos/coreinit/job"
	"github.com/coreos/coreinit/machine"
)

const (
	defaultSystemdRuntimePath = "/run/systemd/system/"
)

type SystemdManager struct {
	Systemd    *systemdDbus.Conn
	Target     *SystemdTarget
	Machine    *machine.Machine
	UnitPrefix string
	unitPath   string
}

func NewSystemdManager(machine *machine.Machine, unitPrefix string) *SystemdManager {
	//TODO(bcwaldon): Handle error in call to New()
	systemd, _ := systemdDbus.New()

	name := "coreinit-" + machine.BootId + ".target"
	target := NewSystemdTarget(name)

	mgr := &SystemdManager{systemd, target, machine, unitPrefix, defaultSystemdRuntimePath}

	mgr.writeUnit(target.Name(), "")

	return mgr
}

func (m *SystemdManager) getUnitByName(name string) (*SystemdUnit, error) {
	var unit SystemdUnit
	if strings.HasSuffix(name, ".service") {
		unit = NewSystemdService(m, name)
	} else if strings.HasSuffix(name, ".socket") {
		unit = NewSystemdSocket(m, name)
	} else {
		panic("WAT")
	}

	return &unit, nil
}

func (m *SystemdManager) getUnitsByTarget(target *SystemdTarget) []SystemdUnit {
	info, err := m.Systemd.GetUnitProperties(target.Name())

	if err != nil {
		panic(err)
	}

	names := info["Wants"].([]string)

	var units []SystemdUnit
	for _, name := range names {
		unit, err := m.getUnitByName(name)
		if err == nil {
			units = append(units, *unit)
		} else {
			log.V(1).Infof("Unit %s seems to exist, yet unable to get corresponding SystemdUnit object", name)
		}
	}

	return units
}

func (m *SystemdManager) GetJobs() map[string]job.Job {
	units := m.getUnitsByTarget(m.Target)
	jobs := make(map[string]job.Job, len(units))
	for _, u := range units {
		state := m.getJobStateFromUnit(&u)
		name := m.stripUnitNamePrefix(u.Name())
		j := job.NewJob(name, state, nil)
		jobs[j.Name] = *j
	}

	return jobs
}

func (m *SystemdManager) getJobStateFromUnit(u *SystemdUnit) *job.JobState {
	loadState, activeState, subState, sockets, err := (*u).State()
	if err != nil {
		log.V(1).Infof("Failed to get state for unit %s", (*u).Name())
		return nil
	} else {
		return job.NewJobState(loadState, activeState, subState, sockets, m.Machine)
	}
}

func (m *SystemdManager) GetJobState(j *job.Job) *job.JobState {
	name := m.addUnitNamePrefix(j.Name)
	unit, err := m.getUnitByName(name)
	if err != nil {
		log.V(1).Infof("No local unit corresponding to job %s", j.Name)
		return nil
	}

	return m.getJobStateFromUnit(unit)
}

func (m *SystemdManager) StartJob(job *job.Job) {
	unitFile := NewSystemdUnitFile(job.Payload.Value)
	unitFile.SetField("Install", "WantedBy", m.Target.Name())

	name := m.addUnitNamePrefix(job.Name)
	m.writeUnit(name, unitFile.String())
	m.startUnit(name)
}

func (m *SystemdManager) StopJob(job *job.Job) {
	name := m.addUnitNamePrefix(job.Name)
	m.stopUnit(name)
	m.removeUnit(name)
}

func (m *SystemdManager) getUnitStates(name string) (string, string, string, error) {
	info, err := m.Systemd.GetUnitProperties(name)

	if err != nil {
		return "", "", "", err
	} else {
		loadState := info["LoadState"].(string)
		activeState := info["ActiveState"].(string)
		subState := info["SubState"].(string)
		return loadState, activeState, subState, nil
	}
}

func (m *SystemdManager) startUnit(name string) {
	log.Infof("Starting systemd unit %s", name)

	files := []string{name}
	m.Systemd.EnableUnitFiles(files, true, false)

	m.Systemd.StartUnit(name, "replace")
}

func (m *SystemdManager) stopUnit(name string) {
	log.Infof("Stopping systemd unit %s", name)

	m.Systemd.StopUnit(name, "replace")

	// go-systemd does not yet have this implemented
	//files := []string{name}
	//Systemd.DisableUnitFiles(files, true, false)
}

func (m *SystemdManager) removeUnit(name string) {
	log.Infof("Unlinking systemd unit %s from target %s", name, m.Target.Name())
	link := m.getLocalPath(path.Join(m.Target.Name()+".wants", name))
	syscall.Unlink(link)

	file := m.getLocalPath(name)
	log.Infof("Removing systemd unit file %s", file)
	syscall.Unlink(file)
}

func (m *SystemdManager) readUnit(name string) (string, error) {
	path := m.getLocalPath(name)
	contents, err := ioutil.ReadFile(path)
	if err == nil {
		return string(contents), nil
	} else {
		return "", errors.New(fmt.Sprintf("No unit file at local path %s", path))
	}
}

func (m *SystemdManager) writeUnit(name string, contents string) error {
	log.Infof("Writing systemd unit file %s", name)

	path := path.Join(m.unitPath, name)
	file, err := os.Create(path)
	defer file.Close()

	if err != nil {
		return err
	}

	file.Write([]byte(contents))
	return nil
}

func (m *SystemdManager) getLocalPath(name string) string {
	return path.Join(m.unitPath, name)
}

func (m *SystemdManager) addUnitNamePrefix(name string) string {
	if len(m.UnitPrefix) > 0 {
		return fmt.Sprintf("%s.%s", m.UnitPrefix, name)
	} else {
		return name
	}
}

func (m *SystemdManager) stripUnitNamePrefix(name string) string {
	if len(m.UnitPrefix) > 0 {
		return strings.TrimPrefix(name, fmt.Sprintf("%s.", m.UnitPrefix))
	} else {
		return name
	}
}
<commit_msg>refactor(unit): Call Subscribe manually<commit_after>package unit

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"strings"
	"syscall"

	systemdDbus "github.com/coreos/go-systemd/dbus"
	log "github.com/golang/glog"

	"github.com/coreos/coreinit/job"
	"github.com/coreos/coreinit/machine"
)

const (
	defaultSystemdRuntimePath = "/run/systemd/system/"
)

type SystemdManager struct {
	Systemd    *systemdDbus.Conn
	Target     *SystemdTarget
	Machine    *machine.Machine
	UnitPrefix string
	unitPath   string
}

func NewSystemdManager(machine *machine.Machine, unitPrefix string) *SystemdManager {
	//TODO(bcwaldon): Handle error in call to New()
	systemd, _ := systemdDbus.New()
	systemd.Subscribe()

	name := "coreinit-" + machine.BootId + ".target"
	target := NewSystemdTarget(name)

	mgr := &SystemdManager{systemd, target, machine, unitPrefix, defaultSystemdRuntimePath}

	mgr.writeUnit(target.Name(), "")

	return mgr
}

func (m *SystemdManager) getUnitByName(name string) (*SystemdUnit, error) {
	var unit SystemdUnit
	if strings.HasSuffix(name, ".service") {
		unit = NewSystemdService(m, name)
	} else if strings.HasSuffix(name, ".socket") {
		unit = NewSystemdSocket(m, name)
	} else {
		panic("WAT")
	}

	return &unit, nil
}

func (m *SystemdManager) getUnitsByTarget(target *SystemdTarget) []SystemdUnit {
	info, err := m.Systemd.GetUnitProperties(target.Name())

	if err != nil {
		panic(err)
	}

	names := info["Wants"].([]string)

	var units []SystemdUnit
	for _, name := range names {
		unit, err := m.getUnitByName(name)
		if err == nil {
			units = append(units, *unit)
		} else {
			log.V(1).Infof("Unit %s seems to exist, yet unable to get corresponding SystemdUnit object", name)
		}
	}

	return units
}

func (m *SystemdManager) GetJobs() map[string]job.Job {
	units := m.getUnitsByTarget(m.Target)
	jobs := make(map[string]job.Job, len(units))
	for _, u := range units {
		state := m.getJobStateFromUnit(&u)
		name := m.stripUnitNamePrefix(u.Name())
		j := job.NewJob(name, state, nil)
		jobs[j.Name] = *j
	}

	return jobs
}

func (m *SystemdManager) getJobStateFromUnit(u *SystemdUnit) *job.JobState {
	loadState, activeState, subState, sockets, err := (*u).State()
	if err != nil {
		log.V(1).Infof("Failed to get state for unit %s", (*u).Name())
		return nil
	} else {
		return job.NewJobState(loadState, activeState, subState, sockets, m.Machine)
	}
}

func (m *SystemdManager) GetJobState(j *job.Job) *job.JobState {
	name := m.addUnitNamePrefix(j.Name)
	unit, err := m.getUnitByName(name)
	if err != nil {
		log.V(1).Infof("No local unit corresponding to job %s", j.Name)
		return nil
	}

	return m.getJobStateFromUnit(unit)
}

func (m *SystemdManager) StartJob(job *job.Job) {
	unitFile := NewSystemdUnitFile(job.Payload.Value)
	unitFile.SetField("Install", "WantedBy", m.Target.Name())

	name := m.addUnitNamePrefix(job.Name)
	m.writeUnit(name, unitFile.String())
	m.startUnit(name)
}

func (m *SystemdManager) StopJob(job *job.Job) {
	name := m.addUnitNamePrefix(job.Name)
	m.stopUnit(name)
	m.removeUnit(name)
}

func (m *SystemdManager) getUnitStates(name string) (string, string, string, error) {
	info, err := m.Systemd.GetUnitProperties(name)

	if err != nil {
		return "", "", "", err
	} else {
		loadState := info["LoadState"].(string)
		activeState := info["ActiveState"].(string)
		subState := info["SubState"].(string)
		return loadState, activeState, subState, nil
	}
}

func (m *SystemdManager) startUnit(name string) {
	log.Infof("Starting systemd unit %s", name)

	files := []string{name}
	m.Systemd.EnableUnitFiles(files, true, false)

	m.Systemd.StartUnit(name, "replace")
}

func (m *SystemdManager) stopUnit(name string) {
	log.Infof("Stopping systemd unit %s", name)

	m.Systemd.StopUnit(name, "replace")

	// go-systemd does not yet have this implemented
	//files := []string{name}
	//Systemd.DisableUnitFiles(files, true, false)
}

func (m *SystemdManager) removeUnit(name string) {
	log.Infof("Unlinking systemd unit %s from target %s", name, m.Target.Name())
	link := m.getLocalPath(path.Join(m.Target.Name()+".wants", name))
	syscall.Unlink(link)

	file := m.getLocalPath(name)
	log.Infof("Removing systemd unit file %s", file)
	syscall.Unlink(file)
}

func (m *SystemdManager) readUnit(name string) (string, error) {
	path := m.getLocalPath(name)
	contents, err := ioutil.ReadFile(path)
	if err == nil {
		return string(contents), nil
	} else {
		return "", errors.New(fmt.Sprintf("No unit file at local path %s", path))
	}
}

func (m *SystemdManager) writeUnit(name string, contents string) error {
	log.Infof("Writing systemd unit file %s", name)

	path := path.Join(m.unitPath, name)
	file, err := os.Create(path)
	defer file.Close()

	if err != nil {
		return err
	}

	file.Write([]byte(contents))
	return nil
}

func (m *SystemdManager) getLocalPath(name string) string {
	return path.Join(m.unitPath, name)
}

func (m *SystemdManager) addUnitNamePrefix(name string) string {
	if len(m.UnitPrefix) > 0 {
		return fmt.Sprintf("%s.%s", m.UnitPrefix, name)
	} else {
		return name
	}
}

func (m *SystemdManager) stripUnitNamePrefix(name string) string {
	if len(m.UnitPrefix) > 0 {
		return strings.TrimPrefix(name, fmt.Sprintf("%s.", m.UnitPrefix))
	} else {
		return name
	}
}
<|endoftext|>


<commit_before>// Package main ...
package main

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"

	"github.com/nanobox-io/nanobox/util"
	"github.com/nanobox-io/nanobox/util/service"
	"github.com/nanobox-io/nanobox/util/update"
)

// main ...
func main() {

	path := ""
	var err error
	// this will write a new binary at location provided `nanobox-update newbinary`
	if len(os.Args) > 1 {
		path = os.Args[1]
	} else {
		// get the location of the current nanobox
		path, err = exec.LookPath(update.Name)
		if err != nil {
			fmt.Printf("Cannot find %s: %s\n", update.Name, err)
			os.Exit(1)
		}
	}

	if !util.IsPrivileged() {

		if runtime.GOOS == "windows" {
			// re-run this command as the administrative user
			fmt.Println()
			fmt.Println("The update process requires Administrator privileges.")
			fmt.Println("Another window will be opened as the Administrator to continue this process.")

			// block here until the user hits enter. It's not ideal, but we need to make
			// sure they see the new window open.
			fmt.Println("Enter to continue:")
			var input string
			fmt.Scanln(&input)

		}

		// todo: make sure removing this doesn't break things
		// // make sure the .nanobox folder is created by our user
		// models.LoadUpdate()

		cmd := fmt.Sprintf("%s \"%s\"", os.Args[0], path)
		if err := util.PrivilegeExec(cmd); err != nil {
			os.Exit(1)
		}

		// we're done
		return
	}

	// stop the nanobox service so we can replace the nanobox binary
	service.Stop("nanobox-server")

	// run the update
	err = update.Run(path)
	if err != nil {
		fmt.Println("error: %s", err)
	}

	if runtime.GOOS == "windows" {
		// The update process was spawned in a separate window, which will
		// close as soon as this command is finished. To ensure they see the
		// message, we need to hold open the process until they hit enter.
		fmt.Println()
		fmt.Println("Enter to continue:")
		var input string
		fmt.Scanln(&input)
	}
}
<commit_msg>use the full path instead of os.Args[0] when restarting with sudo<commit_after>// Package main ...
package main

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"

	"github.com/nanobox-io/nanobox/util"
	"github.com/nanobox-io/nanobox/util/service"
	"github.com/nanobox-io/nanobox/util/update"
)

// main ...
func main() {

	path := ""
	var err error
	// this will write a new binary at location provided `nanobox-update newbinary`
	if len(os.Args) > 1 {
		path = os.Args[1]
	} else {
		// get the location of the current nanobox
		path, err = exec.LookPath(update.Name)
		if err != nil {
			fmt.Printf("Cannot find %s: %s\n", update.Name, err)
			os.Exit(1)
		}
	}

	if !util.IsPrivileged() {

		if runtime.GOOS == "windows" {
			// re-run this command as the administrative user
			fmt.Println()
			fmt.Println("The update process requires Administrator privileges.")
			fmt.Println("Another window will be opened as the Administrator to continue this process.")

			// block here until the user hits enter. It's not ideal, but we need to make
			// sure they see the new window open.
			fmt.Println("Enter to continue:")
			var input string
			fmt.Scanln(&input)

		}

		// todo: make sure removing this doesn't break things
		// // make sure the .nanobox folder is created by our user
		// models.LoadUpdate()

		// determine the full path to the executable in case
		// it isn't in the path when run with sudo
		cmdPath, err := os.Executable()
		if err != nil {
			fmt.Printf("Cannot find the path for nanobox-update\n")
			os.Exit(1)
		}
		cmd := fmt.Sprintf("%s \"%s\"", cmdPath, path)
		if err := util.PrivilegeExec(cmd); err != nil {
			os.Exit(1)
		}

		// we're done
		return
	}

	// stop the nanobox service so we can replace the nanobox binary
	service.Stop("nanobox-server")

	// run the update
	err = update.Run(path)
	if err != nil {
		fmt.Println("error: %s", err)
	}

	if runtime.GOOS == "windows" {
		// The update process was spawned in a separate window, which will
		// close as soon as this command is finished. To ensure they see the
		// message, we need to hold open the process until they hit enter.
		fmt.Println()
		fmt.Println("Enter to continue:")
		var input string
		fmt.Scanln(&input)
	}
}
<|endoftext|>


<commit_before>// Copyright 2013 Google Inc.  All rights reserved.
// Copyright 2016 the gousb Authors.  All rights reserved.
//
// 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 usb

import "testing"

func TestListDevices(t *testing.T) {
	orig := libusb
	defer func() { libusb = orig }()
	libusb = newFakeLibusb()

	c := NewContext()
	defer c.Close()
	c.Debug(0)

	descs := []*Descriptor{}
	devs, err := c.ListDevices(func(desc *Descriptor) bool {
		descs = append(descs, desc)
		return true
	})
	defer func() {
		for _, d := range devs {
			d.Close()
		}
	}()
	if err != nil {
		t.Fatalf("ListDevices(): %s", err)
	}

	if got, want := len(devs), len(fakeDevices); got != want {
		t.Fatalf("len(devs) = %d, want %d (based on num fake devs)", got, want)
	}
	if got, want := len(devs), len(descs); got != want {
		t.Fatalf("len(devs) = %d, want %d (based on num opened devices)", got, want)
	}

	for i := range devs {
		if got, want := devs[i].Descriptor, descs[i]; got != want {
			t.Errorf("dev[%d].Descriptor = %p, want %p", i, got, want)
		}
	}
}

func TestOpenDeviceWithVidPid(t *testing.T) {
	orig := libusb
	defer func() { libusb = orig }()
	libusb = newFakeLibusb()

	for _, d := range []struct {
		vid, pid int
		exists   bool
	}{
		{0x7777, 0x0003, false},
		{0x8888, 0x0001, false},
		{0x8888, 0x0002, true},
		{0x9999, 0x0001, true},
		{0x9999, 0x0002, false},
	} {
		c := NewContext()
		dev, err := c.OpenDeviceWithVidPid(d.vid, d.pid)
		if (dev != nil) != d.exists {
			t.Errorf("OpenDeviceWithVidPid(%s/%s): device != nil is %v, want %v", ID(d.vid), ID(d.pid), dev != nil, d.exists)
		}
		if err != nil {
			t.Errorf("OpenDeviceWithVidPid(%s/%s): got error %v, want nil", ID(d.vid), ID(d.pid), err)
		}
		if dev != nil {
			dev.Close()
		}
	}
}
<commit_msg>ensure the vid/pid match<commit_after>// Copyright 2013 Google Inc.  All rights reserved.
// Copyright 2016 the gousb Authors.  All rights reserved.
//
// 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 usb

import "testing"

func TestListDevices(t *testing.T) {
	orig := libusb
	defer func() { libusb = orig }()
	libusb = newFakeLibusb()

	c := NewContext()
	defer c.Close()
	c.Debug(0)

	descs := []*Descriptor{}
	devs, err := c.ListDevices(func(desc *Descriptor) bool {
		descs = append(descs, desc)
		return true
	})
	defer func() {
		for _, d := range devs {
			d.Close()
		}
	}()
	if err != nil {
		t.Fatalf("ListDevices(): %s", err)
	}

	if got, want := len(devs), len(fakeDevices); got != want {
		t.Fatalf("len(devs) = %d, want %d (based on num fake devs)", got, want)
	}
	if got, want := len(devs), len(descs); got != want {
		t.Fatalf("len(devs) = %d, want %d (based on num opened devices)", got, want)
	}

	for i := range devs {
		if got, want := devs[i].Descriptor, descs[i]; got != want {
			t.Errorf("dev[%d].Descriptor = %p, want %p", i, got, want)
		}
	}
}

func TestOpenDeviceWithVidPid(t *testing.T) {
	orig := libusb
	defer func() { libusb = orig }()
	libusb = newFakeLibusb()

	for _, d := range []struct {
		vid, pid int
		exists   bool
	}{
		{0x7777, 0x0003, false},
		{0x8888, 0x0001, false},
		{0x8888, 0x0002, true},
		{0x9999, 0x0001, true},
		{0x9999, 0x0002, false},
	} {
		c := NewContext()
		dev, err := c.OpenDeviceWithVidPid(d.vid, d.pid)
		if (dev != nil) != d.exists {
			t.Errorf("OpenDeviceWithVidPid(%s/%s): device != nil is %v, want %v", ID(d.vid), ID(d.pid), dev != nil, d.exists)
		}
		if err != nil {
			t.Errorf("OpenDeviceWithVidPid(%s/%s): got error %v, want nil", ID(d.vid), ID(d.pid), err)
		}
		if dev != nil {
			if dev.Descriptor.Vendor != ID(d.vid) || dev.Descriptor.Product != ID(d.pid) {
				t.Errorf("OpenDeviceWithVidPid(%s/%s): the device returned has VID/PID %s/%s, different from specified in the arguments", ID(d.vid), ID(d.pid), dev.Descriptor.Vendor, dev.Descriptor.Product)
			}
			dev.Close()
		}
	}
}
<|endoftext|>


<commit_before>package resourcestore_test

import (
	"time"

	"github.com/cri-o/cri-o/internal/resourcestore"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var (
	testName = "name"
	testID   = "id"
)

type entry struct {
	id      string
	created bool
}

func (e *entry) ID() string {
	return e.id
}

func (e *entry) SetCreated() {
	e.created = true
}

// The actual test suite
var _ = t.Describe("ResourceStore", func() {
	// Setup the test
	var (
		sut          *resourcestore.ResourceStore
		cleanupFuncs []func()
		e            *entry
	)
	BeforeEach(func() {
		sut = resourcestore.New()
		cleanupFuncs = make([]func(), 0)
		e = &entry{
			id: testID,
		}
	})
	It("Put should be able to get resource after adding", func() {
		// Given

		// When
		Expect(sut.Put(testName, e, cleanupFuncs)).To(BeNil())

		// Then
		id := sut.Get(testName)
		Expect(id).To(Equal(e.id))

		id = sut.Get(testName)
		Expect(id).To(BeEmpty())
	})
	It("Put should fail to readd resource", func() {
		// Given

		// When
		Expect(sut.Put(testName, e, cleanupFuncs)).To(BeNil())

		// Then
		Expect(sut.Put(testName, e, cleanupFuncs)).NotTo(BeNil())
	})
	It("Get should call SetCreated", func() {
		// When
		Expect(sut.Put(testName, e, cleanupFuncs)).To(BeNil())

		// Then
		id := sut.Get(testName)
		Expect(id).To(Equal(e.id))
		Expect(e.created).To(BeTrue())
	})
})

var _ = t.Describe("ResourceStore and timeout", func() {
	// Setup the test
	var (
		sut          *resourcestore.ResourceStore
		cleanupFuncs []func()
		e            *entry
	)
	BeforeEach(func() {
		cleanupFuncs = make([]func(), 0)
		e = &entry{
			id: testID,
		}
	})
	It("Put should call cleanup funcs after timeout", func() {
		// Given
		timeout := 2 * time.Second
		sut = resourcestore.NewWithTimeout(timeout)

		timedOutChan := make(chan bool)
		cleanupFuncs = append(cleanupFuncs, func() {
			timedOutChan <- true
		})
		go func() {
			time.Sleep(timeout * 3)
			timedOutChan <- false
		}()

		// When
		Expect(sut.Put(testName, e, cleanupFuncs)).To(BeNil())

		// Then
		didStoreCallTimeoutFunc := <-timedOutChan
		Expect(didStoreCallTimeoutFunc).To(Equal(true))

		id := sut.Get(testName)
		Expect(id).To(BeEmpty())
	})
})
<commit_msg>ResourceStore: update tests to all run<commit_after>package resourcestore_test

import (
	"time"

	"github.com/cri-o/cri-o/internal/resourcestore"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var (
	testName = "name"
	testID   = "id"
)

type entry struct {
	id      string
	created bool
}

func (e *entry) ID() string {
	return e.id
}

func (e *entry) SetCreated() {
	e.created = true
}

// The actual test suite
var _ = t.Describe("ResourceStore", func() {
	// Setup the test
	var (
		sut          *resourcestore.ResourceStore
		cleanupFuncs []func()
		e            *entry
	)
	Context("no timeout", func() {
		BeforeEach(func() {
			sut = resourcestore.New()
			cleanupFuncs = make([]func(), 0)
			e = &entry{
				id: testID,
			}
		})
		It("Put should be able to get resource after adding", func() {
			// Given

			// When
			Expect(sut.Put(testName, e, cleanupFuncs)).To(BeNil())

			// Then
			id := sut.Get(testName)
			Expect(id).To(Equal(e.id))

			id = sut.Get(testName)
			Expect(id).To(BeEmpty())
		})
		It("Put should fail to readd resource", func() {
			// Given

			// When
			Expect(sut.Put(testName, e, cleanupFuncs)).To(BeNil())

			// Then
			Expect(sut.Put(testName, e, cleanupFuncs)).NotTo(BeNil())
		})
		It("Get should call SetCreated", func() {
			// When
			Expect(sut.Put(testName, e, cleanupFuncs)).To(BeNil())

			// Then
			id := sut.Get(testName)
			Expect(id).To(Equal(e.id))
			Expect(e.created).To(BeTrue())
		})
	})
	Context("with timeout", func() {
		BeforeEach(func() {
			cleanupFuncs = make([]func(), 0)
			e = &entry{
				id: testID,
			}
		})
		It("Put should call cleanup funcs after timeout", func() {
			// Given
			timeout := 2 * time.Second
			sut = resourcestore.NewWithTimeout(timeout)

			timedOutChan := make(chan bool)
			cleanupFuncs = append(cleanupFuncs, func() {
				timedOutChan <- true
			})
			go func() {
				time.Sleep(timeout * 3)
				timedOutChan <- false
			}()

			// When
			Expect(sut.Put(testName, e, cleanupFuncs)).To(BeNil())

			// Then
			didStoreCallTimeoutFunc := <-timedOutChan
			Expect(didStoreCallTimeoutFunc).To(Equal(true))

			id := sut.Get(testName)
			Expect(id).To(BeEmpty())
		})
	})
})
<|endoftext|>


<commit_before>package market

import (
	"bytes"
	"context"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/url"

	"github.com/greater-commons/paypal-marketplace/orders"
)

const (
	createOrderRoute           = "/v1/checkout/orders"
	getTransactionContextRoute = "/v1/risk/transaction-contexts"
	disbursePaymentsRoute      = "/v1/payments/referenced-payouts-items/pay"
)

func (c *Client) CreateOrder(ctx context.Context, trackingID string, params *orders.CreateOrderParams) (*orders.CreateOrderResponse, error) {
	d, err := json.Marshal(params)
	if err != nil {
		return nil, err
	}
	r := &request{
		client:   c,
		method:   http.MethodPost,
		endpoint: createOrderRoute,
		body:     bytes.NewReader(d),
		headers: map[string][]string{
			"Paypal-Client-Metadata-Id": []string{trackingID},
		},
	}
	res, err := r.do(ctx)
	if err != nil {
		return nil, err
	}
	if res.status == http.StatusCreated {
		r := &orders.CreateOrderResponse{}
		err = json.NewDecoder(res.body).Decode(r)
		if err != nil {
			return nil, err
		}
		return r, nil
	}
	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return nil, err
	}
	return nil, &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}

func (c *Client) CancelOrder(ctx context.Context, orderID string) error {
	r := &request{
		client:   c,
		method:   http.MethodDelete,
		endpoint: createOrderRoute + "/" + url.PathEscape(orderID),
	}
	res, err := r.do(ctx)
	if err != nil {
		return err
	}
	if res.status == http.StatusNoContent {
		return nil
	}
	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return err
	}
	return &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}

func (c *Client) GetOrderDetails(ctx context.Context, orderID string) (*orders.CreateOrderResponse, error) {
	r := &request{
		client:   c,
		method:   http.MethodGet,
		endpoint: createOrderRoute + "/" + url.PathEscape(orderID),
	}
	res, err := r.do(ctx)
	if err != nil {
		return nil, err
	}
	if res.status == http.StatusOK {
		r := &orders.CreateOrderResponse{}
		err := json.NewDecoder(res.body).Decode(r)
		if err != nil {
			return nil, err
		}
		return r, nil
	}
	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return nil, err
	}
	return nil, &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}

func (c *Client) SaveTransactionContext(ctx context.Context, merchantID, trackingID string, additionalData []orders.KeyValuePair) error {
	body := struct {
		AdditionalData []orders.KeyValuePair `json:"additional_data,omitempty"`
	}{
		AdditionalData: additionalData,
	}
	d, err := json.Marshal(&body)
	if err != nil {
		return err
	}
	r := &request{
		client:   c,
		method:   http.MethodPut,
		endpoint: getTransactionContextRoute + "/" + url.PathEscape(merchantID) + "/" + url.PathEscape(trackingID),
		body:     bytes.NewReader(d),
	}
	res, err := r.do(ctx)
	if err != nil {
		return err
	}
	if res.status == http.StatusOK {
		return nil
	}
	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return err
	}
	return &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}

func (c *Client) PayOrder(ctx context.Context, orderID string, disbursementMode orders.DisbursementModeData) (*orders.PayOrderResponse, error) {
	data := struct {
		DisbursementMode orders.DisbursementModeData `json:"disbursement_mode"`
	}{
		DisbursementMode: disbursementMode,
	}
	d, err := json.Marshal(&data)
	if err != nil {
		return nil, err
	}
	r := &request{
		client:   c,
		method:   http.MethodPost,
		endpoint: createOrderRoute + "/" + url.PathEscape(orderID) + "/pay",
		body:     bytes.NewReader(d),
	}
	res, err := r.do(ctx)
	if err != nil {
		return nil, err
	}
	if res.status >= 200 && res.status < 300 {
		r := &orders.PayOrderResponse{}
		err = json.NewDecoder(res.body).Decode(r)
		if err != nil {
			return nil, err
		}
		return r, nil
	}

	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return nil, err
	}
	return nil, &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}

func (c *Client) FinalizeDisbursement(ctx context.Context, responsePreference orders.ResponsePreferenceData, transactionID string) (*orders.FinalizeDisbursementResponse, error) {
	data := struct {
		ReferenceID   string `json:"reference_id"`
		ReferenceType string `json:"reference_type"`
	}{
		ReferenceID:   transactionID,
		ReferenceType: "TRANSACTION_ID",
	}
	d, err := json.Marshal(&data)
	if err != nil {
		return nil, err
	}
	r := &request{
		client:   c,
		method:   http.MethodPost,
		endpoint: disbursePaymentsRoute,
		body:     bytes.NewReader(d),
		headers: map[string][]string{
			"Prefer": []string{string(responsePreference)},
		},
	}
	res, err := r.do(ctx)
	if err != nil {
		return nil, err
	}
	if res.status == http.StatusOK {
		r := &orders.FinalizeDisbursementResponse{}
		err = json.NewDecoder(res.body).Decode(r)
		if err != nil {
			return nil, err
		}
		return r, nil
	}

	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return nil, err
	}
	return nil, &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}
<commit_msg>Revert "Fix endpoint for disburse payments"<commit_after>package market

import (
	"bytes"
	"context"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/url"

	"github.com/greater-commons/paypal-marketplace/orders"
)

const (
	createOrderRoute           = "/v1/checkout/orders"
	getTransactionContextRoute = "/v1/risk/transaction-contexts"
	disbursePaymentsRoute      = "/v1/payments/referenced-payouts-items"
)

func (c *Client) CreateOrder(ctx context.Context, trackingID string, params *orders.CreateOrderParams) (*orders.CreateOrderResponse, error) {
	d, err := json.Marshal(params)
	if err != nil {
		return nil, err
	}
	r := &request{
		client:   c,
		method:   http.MethodPost,
		endpoint: createOrderRoute,
		body:     bytes.NewReader(d),
		headers: map[string][]string{
			"Paypal-Client-Metadata-Id": []string{trackingID},
		},
	}
	res, err := r.do(ctx)
	if err != nil {
		return nil, err
	}
	if res.status == http.StatusCreated {
		r := &orders.CreateOrderResponse{}
		err = json.NewDecoder(res.body).Decode(r)
		if err != nil {
			return nil, err
		}
		return r, nil
	}
	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return nil, err
	}
	return nil, &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}

func (c *Client) CancelOrder(ctx context.Context, orderID string) error {
	r := &request{
		client:   c,
		method:   http.MethodDelete,
		endpoint: createOrderRoute + "/" + url.PathEscape(orderID),
	}
	res, err := r.do(ctx)
	if err != nil {
		return err
	}
	if res.status == http.StatusNoContent {
		return nil
	}
	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return err
	}
	return &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}

func (c *Client) GetOrderDetails(ctx context.Context, orderID string) (*orders.CreateOrderResponse, error) {
	r := &request{
		client:   c,
		method:   http.MethodGet,
		endpoint: createOrderRoute + "/" + url.PathEscape(orderID),
	}
	res, err := r.do(ctx)
	if err != nil {
		return nil, err
	}
	if res.status == http.StatusOK {
		r := &orders.CreateOrderResponse{}
		err := json.NewDecoder(res.body).Decode(r)
		if err != nil {
			return nil, err
		}
		return r, nil
	}
	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return nil, err
	}
	return nil, &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}

func (c *Client) SaveTransactionContext(ctx context.Context, merchantID, trackingID string, additionalData []orders.KeyValuePair) error {
	body := struct {
		AdditionalData []orders.KeyValuePair `json:"additional_data,omitempty"`
	}{
		AdditionalData: additionalData,
	}
	d, err := json.Marshal(&body)
	if err != nil {
		return err
	}
	r := &request{
		client:   c,
		method:   http.MethodPut,
		endpoint: getTransactionContextRoute + "/" + url.PathEscape(merchantID) + "/" + url.PathEscape(trackingID),
		body:     bytes.NewReader(d),
	}
	res, err := r.do(ctx)
	if err != nil {
		return err
	}
	if res.status == http.StatusOK {
		return nil
	}
	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return err
	}
	return &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}

func (c *Client) PayOrder(ctx context.Context, orderID string, disbursementMode orders.DisbursementModeData) (*orders.PayOrderResponse, error) {
	data := struct {
		DisbursementMode orders.DisbursementModeData `json:"disbursement_mode"`
	}{
		DisbursementMode: disbursementMode,
	}
	d, err := json.Marshal(&data)
	if err != nil {
		return nil, err
	}
	r := &request{
		client:   c,
		method:   http.MethodPost,
		endpoint: createOrderRoute + "/" + url.PathEscape(orderID) + "/pay",
		body:     bytes.NewReader(d),
	}
	res, err := r.do(ctx)
	if err != nil {
		return nil, err
	}
	if res.status >= 200 && res.status < 300 {
		r := &orders.PayOrderResponse{}
		err = json.NewDecoder(res.body).Decode(r)
		if err != nil {
			return nil, err
		}
		return r, nil
	}

	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return nil, err
	}
	return nil, &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}

func (c *Client) FinalizeDisbursement(ctx context.Context, responsePreference orders.ResponsePreferenceData, transactionID string) (*orders.FinalizeDisbursementResponse, error) {
	data := struct {
		ReferenceID   string `json:"reference_id"`
		ReferenceType string `json:"reference_type"`
	}{
		ReferenceID:   transactionID,
		ReferenceType: "TRANSACTION_ID",
	}
	d, err := json.Marshal(&data)
	if err != nil {
		return nil, err
	}
	r := &request{
		client:   c,
		method:   http.MethodPost,
		endpoint: disbursePaymentsRoute,
		body:     bytes.NewReader(d),
		headers: map[string][]string{
			"Prefer": []string{string(responsePreference)},
		},
	}
	res, err := r.do(ctx)
	if err != nil {
		return nil, err
	}
	if res.status == http.StatusOK {
		r := &orders.FinalizeDisbursementResponse{}
		err = json.NewDecoder(res.body).Decode(r)
		if err != nil {
			return nil, err
		}
		return r, nil
	}

	errorData, err := ioutil.ReadAll(res.body)
	if err != nil {
		return nil, err
	}
	return nil, &BadResponse{
		Status: res.status,
		Body:   string(errorData),
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"fmt"
	"os"
	"os/exec"
	"strings"

	"encoding/json"
)

type YumModule struct {
	Name    string
	Version string
	State   string
	Repo    string
}

func (ym *YumModule) IsInstalled() bool {
	cmd := exec.Command("rpm", "-qa", ym.Name)
	output, err := cmd.Output()
	if err != nil {
		panic(err.Error())
	}

	for _, pkg := range strings.Split(string(output), "\n") {
		if strings.HasPrefix(pkg, ym.Name) {
			if ym.Version != "" {
				if strings.HasSuffix(pkg, ym.Version) {
					return true
				}
			} else {
				return true
			}
		}
	}

	return false
}

var result map[string]interface{} = map[string]interface{}{}
var yumSubcommands map[string]string = map[string]string{
	"present": "install",
	"absent":  "remove",
}

func main() {
	// recover code
	// also does the printout of result
	defer func() {
		if r := recover(); r != nil {
			result["status"] = "error"
			result["msg"] = r
		}

		output, err := json.Marshal(result)
		if err != nil {
			panic(err)
		}
		fmt.Print(string(output))
	}()

	yumParams := YumModule{}

	// basically unmarshall but can take in a io.Reader
	dec := json.NewDecoder(os.Stdin)
	if err := dec.Decode(&yumParams); err != nil {
		panic(err.Error())
	}

	// checks if there's a specific repo needed
	if yumParams.Repo != "" {
		yumParams.Repo = "--enablerepo=" + yumParams.Repo
	}

	// if no state is present assume it's present
	if yumParams.State == "" {
		yumParams.State = "present"
	} else if yumParams.State != "present" || yumParams.State != "absent" {
		panic("Valid states are 'present' or 'absent'")
	}

	if yumParams.Version == "latest" {
		yumParams.Version = ""
	}

	fullPkgName := yumParams.Name
	if yumParams.Version != "" {
		fullPkgName += "-" + yumParams.Version
	}

	installed := yumParams.IsInstalled()

	if installed && yumParams.State == "present" {
		result["msg"] = fmt.Sprintf("Package %s already present", fullPkgName)
		result["status"] = "ok"
	} else if !installed && yumParams.State == "absent" {
		result["msg"] = fmt.Sprintf("Package %s already absent", fullPkgName)
		result["status"] = "ok"
	} else {
		subCommand := yumSubcommands[yumParams.State]
		cmd := exec.Command("yum", subCommand, "-y", fullPkgName, yumParams.Repo)

		var stdout, stderr bytes.Buffer
		cmd.Stdout = &stdout
		cmd.Stderr = &stderr

		if err := cmd.Run(); err != nil {
			panic(fmt.Sprintf("Error occurred while installing/removing package %s - %s", fullPkgName, stderr.String()))
		}

		result["status"] = "changed"
		result["msg"] = fmt.Sprintf("State of package %s changed - %s", fullPkgName, stdout.String())
		result["output"] = map[string]string{
			"stdout": stdout.String(),
			"stderr": stderr.String(),
		}
	}
}
<commit_msg>fixed godep vet<commit_after>package main

import (
	"bytes"
	"fmt"
	"os"
	"os/exec"
	"strings"

	"encoding/json"
)

type YumModule struct {
	Name    string
	Version string
	State   string
	Repo    string
}

func (ym *YumModule) IsInstalled() bool {
	cmd := exec.Command("rpm", "-qa", ym.Name)
	output, err := cmd.Output()
	if err != nil {
		panic(err.Error())
	}

	for _, pkg := range strings.Split(string(output), "\n") {
		if strings.HasPrefix(pkg, ym.Name) {
			if ym.Version != "" {
				if strings.HasSuffix(pkg, ym.Version) {
					return true
				}
			} else {
				return true
			}
		}
	}

	return false
}

var result map[string]interface{} = map[string]interface{}{}
var yumSubcommands map[string]string = map[string]string{
	"present": "install",
	"absent":  "remove",
}

func main() {
	// recover code
	// also does the printout of result
	defer func() {
		if r := recover(); r != nil {
			result["status"] = "error"
			result["msg"] = r
		}

		output, err := json.Marshal(result)
		if err != nil {
			panic(err)
		}
		fmt.Print(string(output))
	}()

	yumParams := YumModule{}

	// basically unmarshall but can take in a io.Reader
	dec := json.NewDecoder(os.Stdin)
	if err := dec.Decode(&yumParams); err != nil {
		panic(err.Error())
	}

	// checks if there's a specific repo needed
	if yumParams.Repo != "" {
		yumParams.Repo = "--enablerepo=" + yumParams.Repo
	}

	// if no state is present assume it's present
	if yumParams.State == "" {
		yumParams.State = "present"
	}

	if yumParams.State != "present" && yumParams.State != "absent" {
		panic("Valid states are 'present' or 'absent'")
	}

	if yumParams.Version == "latest" {
		yumParams.Version = ""
	}

	fullPkgName := yumParams.Name
	if yumParams.Version != "" {
		fullPkgName += "-" + yumParams.Version
	}

	installed := yumParams.IsInstalled()

	if installed && yumParams.State == "present" {
		result["msg"] = fmt.Sprintf("Package %s already present", fullPkgName)
		result["status"] = "ok"
	} else if !installed && yumParams.State == "absent" {
		result["msg"] = fmt.Sprintf("Package %s already absent", fullPkgName)
		result["status"] = "ok"
	} else {
		subCommand := yumSubcommands[yumParams.State]
		cmd := exec.Command("yum", subCommand, "-y", fullPkgName, yumParams.Repo)

		var stdout, stderr bytes.Buffer
		cmd.Stdout = &stdout
		cmd.Stderr = &stderr

		if err := cmd.Run(); err != nil {
			panic(fmt.Sprintf("Error occurred while installing/removing package %s - %s", fullPkgName, stderr.String()))
		}

		result["status"] = "changed"
		result["msg"] = fmt.Sprintf("State of package %s changed - %s", fullPkgName, stdout.String())
		result["output"] = map[string]string{
			"stdout": stdout.String(),
			"stderr": stderr.String(),
		}
	}
}
<|endoftext|>


<commit_before>/*
Copyright 2019 The Kubernetes Authors.

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

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

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

package events

import (
	"fmt"
	"time"

	v1 "k8s.io/api/core/v1"
	eventsv1 "k8s.io/api/events/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/clock"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/apimachinery/pkg/watch"
	"k8s.io/client-go/tools/record/util"
	"k8s.io/client-go/tools/reference"
	"k8s.io/klog/v2"
)

type recorderImpl struct {
	scheme              *runtime.Scheme
	reportingController string
	reportingInstance   string
	*watch.Broadcaster
	clock clock.Clock
}

func (recorder *recorderImpl) Eventf(regarding runtime.Object, related runtime.Object, eventtype, reason, action, note string, args ...interface{}) {
	timestamp := metav1.MicroTime{time.Now()}
	message := fmt.Sprintf(note, args...)
	refRegarding, err := reference.GetReference(recorder.scheme, regarding)
	if err != nil {
		klog.Errorf("Could not construct reference to: '%#v' due to: '%v'. Will not report event: '%v' '%v' '%v'", regarding, err, eventtype, reason, message)
		return
	}
	refRelated, err := reference.GetReference(recorder.scheme, related)
	if err != nil {
		klog.V(9).Infof("Could not construct reference to: '%#v' due to: '%v'.", related, err)
	}
	if !util.ValidateEventType(eventtype) {
		klog.Errorf("Unsupported event type: '%v'", eventtype)
		return
	}
	event := recorder.makeEvent(refRegarding, refRelated, timestamp, eventtype, reason, message, recorder.reportingController, recorder.reportingInstance, action)
	go func() {
		defer utilruntime.HandleCrash()
		recorder.Action(watch.Added, event)
	}()
}

func (recorder *recorderImpl) makeEvent(refRegarding *v1.ObjectReference, refRelated *v1.ObjectReference, timestamp metav1.MicroTime, eventtype, reason, message string, reportingController string, reportingInstance string, action string) *eventsv1.Event {
	t := metav1.Time{Time: recorder.clock.Now()}
	namespace := refRegarding.Namespace
	if namespace == "" {
		namespace = metav1.NamespaceDefault
	}
	return &eventsv1.Event{
		ObjectMeta: metav1.ObjectMeta{
			Name:      fmt.Sprintf("%v.%x", refRegarding.Name, t.UnixNano()),
			Namespace: namespace,
		},
		EventTime:           timestamp,
		Series:              nil,
		ReportingController: reportingController,
		ReportingInstance:   reportingInstance,
		Action:              action,
		Reason:              reason,
		Regarding:           *refRegarding,
		Related:             refRelated,
		Note:                message,
		Type:                eventtype,
	}
}
<commit_msg>client-go/events: avoid referencing a nil related object<commit_after>/*
Copyright 2019 The Kubernetes Authors.

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

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

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

package events

import (
	"fmt"
	"time"

	v1 "k8s.io/api/core/v1"
	eventsv1 "k8s.io/api/events/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/clock"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/apimachinery/pkg/watch"
	"k8s.io/client-go/tools/record/util"
	"k8s.io/client-go/tools/reference"
	"k8s.io/klog/v2"
)

type recorderImpl struct {
	scheme              *runtime.Scheme
	reportingController string
	reportingInstance   string
	*watch.Broadcaster
	clock clock.Clock
}

func (recorder *recorderImpl) Eventf(regarding runtime.Object, related runtime.Object, eventtype, reason, action, note string, args ...interface{}) {
	timestamp := metav1.MicroTime{time.Now()}
	message := fmt.Sprintf(note, args...)
	refRegarding, err := reference.GetReference(recorder.scheme, regarding)
	if err != nil {
		klog.Errorf("Could not construct reference to: '%#v' due to: '%v'. Will not report event: '%v' '%v' '%v'", regarding, err, eventtype, reason, message)
		return
	}

	var refRelated *v1.ObjectReference
	if related != nil {
		refRelated, err = reference.GetReference(recorder.scheme, related)
		if err != nil {
			klog.V(9).Infof("Could not construct reference to: '%#v' due to: '%v'.", related, err)
		}
	}
	if !util.ValidateEventType(eventtype) {
		klog.Errorf("Unsupported event type: '%v'", eventtype)
		return
	}
	event := recorder.makeEvent(refRegarding, refRelated, timestamp, eventtype, reason, message, recorder.reportingController, recorder.reportingInstance, action)
	go func() {
		defer utilruntime.HandleCrash()
		recorder.Action(watch.Added, event)
	}()
}

func (recorder *recorderImpl) makeEvent(refRegarding *v1.ObjectReference, refRelated *v1.ObjectReference, timestamp metav1.MicroTime, eventtype, reason, message string, reportingController string, reportingInstance string, action string) *eventsv1.Event {
	t := metav1.Time{Time: recorder.clock.Now()}
	namespace := refRegarding.Namespace
	if namespace == "" {
		namespace = metav1.NamespaceDefault
	}
	return &eventsv1.Event{
		ObjectMeta: metav1.ObjectMeta{
			Name:      fmt.Sprintf("%v.%x", refRegarding.Name, t.UnixNano()),
			Namespace: namespace,
		},
		EventTime:           timestamp,
		Series:              nil,
		ReportingController: reportingController,
		ReportingInstance:   reportingInstance,
		Action:              action,
		Reason:              reason,
		Regarding:           *refRegarding,
		Related:             refRelated,
		Note:                message,
		Type:                eventtype,
	}
}
<|endoftext|>


<commit_before>// Copyright 2015 Dorival de Moraes Pedroso. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goga

import (
	"encoding/json"

	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/io"
	"github.com/cpmech/gosl/rnd"
)

// Parameters hold all configuration parameters
type Parameters struct {

	// sizes
	Nova int // number of objective values
	Noor int // number of out-of-range values
	Nsol int // total number of solutions
	Ncpu int // number of cpus

	// time
	Tf    int // final time
	DtExc int // delta time for exchange
	DtOut int // delta time for output

	// options
	Pll        bool    // parallel
	Seed       int     // seed for random numbers generator
	LatinDup   int     // Latin Hypercube duplicates number
	EpsMinProb float64 // minimum value for 'h' constraints
	Verbose    bool    // show messages
	Problem    int     // problem index
	GenAll     bool    // generate all solutions together; i.e. not within each group/CPU

	// crossover and mutation
	DEpc    float64 // differential evolution pc
	DEmult  float64 // differential evolution multiplier
	DebEtac float64 // Deb's crossover parameter
	DebEtam float64 // Deb's mutation parameters
	PmFlt   float64 // probability of mutation for floats
	PmInt   float64 // probability of mutation for ints

	// range
	FltMin []float64 // minimum float allowed
	FltMax []float64 // maximum float allowed
	IntMin []int     // minimum int allowed
	IntMax []int     // maximum int allowed

	// derived
	Nflt   int       // number of floats
	Nint   int       // number of integers
	DelFlt []float64 // max float range
	DelInt []int     // max int range
}

// Default sets default parameters
func (o *Parameters) Default() {

	// sizes
	o.Nova = 1
	o.Noor = 0
	o.Nsol = 40
	o.Ncpu = 4

	// time
	o.Tf = 100
	o.DtExc = o.Tf / 10
	o.DtOut = o.Tf / 5

	// options
	o.Pll = true
	o.Seed = 0
	o.LatinDup = 2
	o.EpsMinProb = 0.1
	o.Verbose = true
	o.Problem = 1
	o.GenAll = false

	// crossover and mutation
	o.DEpc = 0.5
	o.DEmult = 0.1
	o.DebEtac = 1
	o.DebEtam = 1
	o.PmFlt = 0.0
	o.PmInt = 0.1
}

// Read reads configuration parameters from JSON file
func (o *Parameters) Read(filenamepath string) {
	o.Default()
	b, err := io.ReadFile(filenamepath)
	if err != nil {
		chk.Panic("cannot read parameters file %q", filenamepath)
	}
	err = json.Unmarshal(b, o)
	if err != nil {
		chk.Panic("cannot unmarshal parameters file %q", filenamepath)
	}
	return
}

// CalcDerived computes derived variables and checks consistency
func (o *Parameters) CalcDerived() {

	// check
	if o.Nova < 1 {
		chk.Panic("number of objective values (nova) must be greater than 0")
	}
	if o.Nsol < 6 {
		chk.Panic("number of solutions must greater than 6. Nsol = %d is invalid", o.Nsol)
	}
	if o.Ncpu < 2 {
		o.Ncpu = 1
		o.Pll = false
	}
	if o.Ncpu > o.Nsol/2 {
		chk.Panic("number of CPU must be smaller than or equal to half the number of solutions. Ncpu=%d > Nsol/2=%d", o.Ncpu, o.Nsol/2)
	}
	if o.Tf < 1 {
		o.Tf = 1
	}
	if o.DtExc < 1 {
		o.DtExc = 1
	}

	// derived
	o.Nflt = len(o.FltMin)
	o.Nint = len(o.IntMin)
	if o.Nflt == 0 && o.Nint == 0 {
		chk.Panic("either floats and ints must be set (via FltMin/Max or IntMin/Max)")
	}
	chk.IntAssert(len(o.FltMax), o.Nflt)
	chk.IntAssert(len(o.IntMax), o.Nint)
	o.DelFlt = make([]float64, o.Nflt)
	o.DelInt = make([]int, o.Nint)
	for i := 0; i < o.Nflt; i++ {
		o.DelFlt[i] = o.FltMax[i] - o.FltMin[i]
	}
	for i := 0; i < o.Nint; i++ {
		o.DelInt[i] = o.IntMax[i] - o.IntMin[i]
	}
	rnd.Init(o.Seed)
}

// EnforceRange makes sure x is within given range
func (o *Parameters) EnforceRange(i int, x float64) float64 {
	if x < o.FltMin[i] {
		return o.FltMin[i]
	}
	if x > o.FltMax[i] {
		return o.FltMax[i]
	}
	return x
}
<commit_msg>params fixed<commit_after>// Copyright 2015 Dorival de Moraes Pedroso. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goga

import (
	"encoding/json"

	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/io"
	"github.com/cpmech/gosl/rnd"
)

// Parameters hold all configuration parameters
type Parameters struct {

	// sizes
	Nova int // number of objective values
	Noor int // number of out-of-range values
	Nsol int // total number of solutions
	Ncpu int // number of cpus

	// time
	Tf    int // final time
	DtExc int // delta time for exchange
	DtOut int // delta time for output

	// options
	Pll        bool    // parallel
	Seed       int     // seed for random numbers generator
	LatinDup   int     // Latin Hypercube duplicates number
	EpsMinProb float64 // minimum value for 'h' constraints
	Verbose    bool    // show messages
	Problem    int     // problem index
	GenAll     bool    // generate all solutions together; i.e. not within each group/CPU

	// crossover and mutation
	DEpc    float64 // differential evolution pc
	DEmult  float64 // differential evolution multiplier
	DebEtac float64 // Deb's crossover parameter
	DebEtam float64 // Deb's mutation parameters
	PmFlt   float64 // probability of mutation for floats
	PmInt   float64 // probability of mutation for ints

	// range
	FltMin []float64 // minimum float allowed
	FltMax []float64 // maximum float allowed
	IntMin []int     // minimum int allowed
	IntMax []int     // maximum int allowed

	// derived
	Nflt   int       // number of floats
	Nint   int       // number of integers
	DelFlt []float64 // max float range
	DelInt []int     // max int range
}

// Default sets default parameters
func (o *Parameters) Default() {

	// sizes
	o.Nova = 1
	o.Noor = 0
	o.Nsol = 40
	o.Ncpu = 4

	// time
	o.Tf = 100
	o.DtExc = o.Tf / 10
	o.DtOut = o.Tf / 5

	// options
	o.Pll = true
	o.Seed = 0
	o.LatinDup = 2
	o.EpsMinProb = 0.1
	o.Verbose = true
	o.Problem = 1
	o.GenAll = false

	// crossover and mutation
	o.DEpc = 0.1
	o.DEmult = 0.5
	o.DebEtac = 1
	o.DebEtam = 1
	o.PmFlt = 0.0
	o.PmInt = 0.1
}

// Read reads configuration parameters from JSON file
func (o *Parameters) Read(filenamepath string) {
	o.Default()
	b, err := io.ReadFile(filenamepath)
	if err != nil {
		chk.Panic("cannot read parameters file %q", filenamepath)
	}
	err = json.Unmarshal(b, o)
	if err != nil {
		chk.Panic("cannot unmarshal parameters file %q", filenamepath)
	}
	return
}

// CalcDerived computes derived variables and checks consistency
func (o *Parameters) CalcDerived() {

	// check
	if o.Nova < 1 {
		chk.Panic("number of objective values (nova) must be greater than 0")
	}
	if o.Nsol < 6 {
		chk.Panic("number of solutions must greater than 6. Nsol = %d is invalid", o.Nsol)
	}
	if o.Ncpu < 2 {
		o.Ncpu = 1
		o.Pll = false
	}
	if o.Ncpu > o.Nsol/2 {
		chk.Panic("number of CPU must be smaller than or equal to half the number of solutions. Ncpu=%d > Nsol/2=%d", o.Ncpu, o.Nsol/2)
	}
	if o.Tf < 1 {
		o.Tf = 1
	}
	if o.DtExc < 1 {
		o.DtExc = 1
	}

	// derived
	o.Nflt = len(o.FltMin)
	o.Nint = len(o.IntMin)
	if o.Nflt == 0 && o.Nint == 0 {
		chk.Panic("either floats and ints must be set (via FltMin/Max or IntMin/Max)")
	}
	chk.IntAssert(len(o.FltMax), o.Nflt)
	chk.IntAssert(len(o.IntMax), o.Nint)
	o.DelFlt = make([]float64, o.Nflt)
	o.DelInt = make([]int, o.Nint)
	for i := 0; i < o.Nflt; i++ {
		o.DelFlt[i] = o.FltMax[i] - o.FltMin[i]
	}
	for i := 0; i < o.Nint; i++ {
		o.DelInt[i] = o.IntMax[i] - o.IntMin[i]
	}
	rnd.Init(o.Seed)
}

// EnforceRange makes sure x is within given range
func (o *Parameters) EnforceRange(i int, x float64) float64 {
	if x < o.FltMin[i] {
		return o.FltMin[i]
	}
	if x > o.FltMax[i] {
		return o.FltMax[i]
	}
	return x
}
<|endoftext|>


<commit_before>package cookoo

type Params struct {
	storage map[string]interface{}
}

// Create a new Params instance, initialized with the given map.
// Note that the given map is actually used (not copied).
func NewParamsWithValues (initialVals map[string]interface{}) *Params {
	p := new(Params)
	p.Init(initialVals)
	return p
}

func NewParams (size int) *Params {
	p := new(Params)
	p.storage = make(map[string]interface{}, size);
	return p;
}

func (p *Params) Init(initialValues map[string]interface{}) {
	p.storage = initialValues
}

func (p *Params) set(name string, value interface{}) bool {
	_, ret := p.storage[name]
	p.storage[name] = value
	return ret;
}

// Check if a parameter exists, and return it if found.
func (p *Params) Has(name string) (value interface{}, ok bool) {
	value, ok = p.storage[name]
	return
}

// Get a parameter value, or return the default value.
func (p *Params) Get(name string, defaultValue interface{}) interface{} {
	val, ok := p.Has(name)
	if ok {
		return val
	}
	return defaultValue
}

// Require that a given list of parameters are present.
// If they are all present, ok = true. Otherwise, ok = false and the
// `missing` array contains a list of missing params.
func (p *Params) Requires(paramNames ...string) (ok bool, missing []string) {
	missing = make([]string, 0, len(p.storage))
	for _, val := range paramNames {
		_, ok := p.storage[val]
		if !ok {
			missing = append(missing, val)
		}
	}
	ok = len(missing) == 0
	return
}

// Given a name and a validation function, return a valid value.
// If the value is not valid, ok = false.
func (p *Params) Validate(name string, validator func(interface{})bool) (value interface{}, ok bool) {
	value, ok = p.storage[name]
	if !ok {
		return
	}

	if !validator(value.(interface{})) {
		// XXX: For safety, we set a failed value to nil.
		value = nil
		ok = false
	}
	return
}
<commit_msg>Matching Context API in Params.<commit_after>package cookoo

type Params struct {
	storage map[string]interface{}
}

// Create a new Params instance, initialized with the given map.
// Note that the given map is actually used (not copied).
func NewParamsWithValues (initialVals map[string]interface{}) *Params {
	p := new(Params)
	p.Init(initialVals)
	return p
}

func NewParams (size int) *Params {
	p := new(Params)
	p.storage = make(map[string]interface{}, size);
	return p;
}

func (p *Params) Init(initialValues map[string]interface{}) {
	p.storage = initialValues
}

func (p *Params) set(name string, value interface{}) bool {
	_, ret := p.storage[name]
	p.storage[name] = value
	return ret;
}

// Check if a parameter exists, and return it if found.
func (p *Params) Has(name string) (value interface{}, ok bool) {
	value, ok = p.storage[name]
	return
}

// Get a parameter value, or return the default value.
func (p *Params) Get(name string, defaultValue interface{}) interface{} {
	val, ok := p.Has(name)
	if ok {
		return val
	}
	return defaultValue
}

// Require that a given list of parameters are present.
// If they are all present, ok = true. Otherwise, ok = false and the
// `missing` array contains a list of missing params.
func (p *Params) Requires(paramNames ...string) (ok bool, missing []string) {
	missing = make([]string, 0, len(p.storage))
	for _, val := range paramNames {
		_, ok := p.storage[val]
		if !ok {
			missing = append(missing, val)
		}
	}
	ok = len(missing) == 0
	return
}

// Get all of the parameters.
//
// This does no checking of the parameters.
func (p *Params) AsMap() map[string]interface{} {
	return p.storage
}

// Given a name and a validation function, return a valid value.
// If the value is not valid, ok = false.
func (p *Params) Validate(name string, validator func(interface{})bool) (value interface{}, ok bool) {
	value, ok = p.storage[name]
	if !ok {
		return
	}

	if !validator(value.(interface{})) {
		// XXX: For safety, we set a failed value to nil.
		value = nil
		ok = false
	}
	return
}
<|endoftext|>