<commit_before>package server

import (
	"fmt"
	"testing"

	"github.com/bmorton/deployster/server/mocks"
	"github.com/coreos/fleet/schema"
	"github.com/rcrowley/go-tigertonic/mocking"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
)

type DeploysResourceTestSuite struct {
	suite.Suite
	Subject         DeploysResource
	FleetClientMock *mocks.FleetClient
	Service         *DeploysterService
}

func (suite *DeploysResourceTestSuite) SetupSuite() {
	suite.Service = NewDeploysterService("0.0.0.0:3000", "v1.0", "username", "password", "<PASSWORD>")
}

func (suite *DeploysResourceTestSuite) SetupTest() {
	suite.FleetClientMock = new(mocks.FleetClient)
	suite.Subject = DeploysResource{suite.FleetClientMock, "mmmhm"}
}

func (suite *DeploysResourceTestSuite) TestCreateWithoutPassedInstancesAndNoInstancesRunning() {
	expectedOptions := getUnitOptions(UnitTemplate{"carousel", "abc123", "mmmhm", "2006.01.02-15.04.05"})
	suite.FleetClientMock.On("Units").Return([]*schema.Unit{}, nil)

	// Should only start 1 unit
	suite.FleetClientMock.On("CreateUnit", &schema.Unit{Name: "carousel:abc123:2006.01.02-15.04.05@1.service", Options: expectedOptions}).Return(nil)
	suite.FleetClientMock.On("SetUnitTargetState", "carousel:abc123:2006.01.02-15.04.05@1.service", "launched").Return(nil)

	suite.Subject.Create(
		mocking.URL(suite.Service.RootMux, "POST", "http://example.com/v1/services/carousel/deploys"),
		mocking.Header(nil),
		&DeployRequest{Deploy{Version: "abc123", DestroyPrevious: false, Timestamp: "2006.01.02-15.04.05"}},
	)

	suite.FleetClientMock.Mock.AssertExpectations(suite.T())
}

func (suite *DeploysResourceTestSuite) TestCreateWithoutPassedInstancesAndMultipleInstancesRunning() {
	expectedOptions := getUnitOptions(UnitTemplate{"carousel", "abc123", "mmmhm", "2006.01.02-15.04.05"})
	suite.FleetClientMock.On("Units").Return([]*schema.Unit{
		&schema.Unit{"running", "running", "efefeff", "carousel:efefeff:2006.01.02-15.04.05@1.service", []*schema.UnitOption{}},
		&schema.Unit{"running", "running", "efefeff", "carousel:efefeff:2006.01.02-15.04.05@2.service", []*schema.UnitOption{}},
	}, nil)

	// Should start 2 units
	suite.FleetClientMock.On("CreateUnit", &schema.Unit{Name: "carousel:abc123:2006.01.02-15.04.05@1.service", Options: expectedOptions}).Return(nil)
	suite.FleetClientMock.On("SetUnitTargetState", "carousel:abc123:2006.01.02-15.04.05@1.service", "launched").Return(nil)
	suite.FleetClientMock.On("CreateUnit", &schema.Unit{Name: "carousel:abc123:2006.01.02-15.04.05@2.service", Options: expectedOptions}).Return(nil)
	suite.FleetClientMock.On("SetUnitTargetState", "carousel:abc123:2006.01.02-15.04.05@2.service", "launched").Return(nil)

	suite.Subject.Create(
		mocking.URL(suite.Service.RootMux, "POST", "http://example.com/v1/services/carousel/deploys"),
		mocking.Header(nil),
		&DeployRequest{Deploy{Version: "abc123", DestroyPrevious: false, Timestamp: "2006.01.02-15.04.05"}},
	)

	suite.FleetClientMock.Mock.AssertExpectations(suite.T())
}

func (suite *DeploysResourceTestSuite) TestCreateWithoutPassedInstancesAndMultipleVersionsRunning() {<commit_msg>Add failing test for covering the case discovered in #45 dealing with failed units.<commit_after>package server

import (
	"fmt"
	"testing"

	"github.com/bmorton/deployster/server/mocks"
	"github.com/coreos/fleet/schema"
	"github.com/rcrowley/go-tigertonic/mocking"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
)

type DeploysResourceTestSuite struct {
	suite.Suite
	Subject         DeploysResource
	FleetClientMock *mocks.FleetClient
	Service         *DeploysterService
}

func (suite *DeploysResourceTestSuite) SetupSuite() {
	suite.Service = NewDeploysterService("0.0.0.0:3000", "v1.0", "username", "password", "<PASSWORD>")
}

func (suite *DeploysResourceTestSuite) SetupTest() {
	suite.FleetClientMock = new(mocks.FleetClient)
	suite.Subject = DeploysResource{suite.FleetClientMock, "mmmhm"}
}

func (suite *DeploysResourceTestSuite) TestCreateWithoutPassedInstancesAndNoInstancesRunning() {
	expectedOptions := getUnitOptions(UnitTemplate{"carousel", "abc123", "mmmhm", "2006.01.02-15.04.05"})
	suite.FleetClientMock.On("Units").Return([]*schema.Unit{}, nil)

	// Should only start 1 unit
	suite.FleetClientMock.On("CreateUnit", &schema.Unit{Name: "carousel:abc123:2006.01.02-15.04.05@1.service", Options: expectedOptions}).Return(nil)
	suite.FleetClientMock.On("SetUnitTargetState", "carousel:abc123:2006.01.02-15.04.05@1.service", "launched").Return(nil)

	suite.Subject.Create(
		mocking.URL(suite.Service.RootMux, "POST", "http://example.com/v1/services/carousel/deploys"),
		mocking.Header(nil),
		&DeployRequest{Deploy{Version: "abc123", DestroyPrevious: false, Timestamp: "2006.01.02-15.04.05"}},
	)

	suite.FleetClientMock.Mock.AssertExpectations(suite.T())
}

func (suite *DeploysResourceTestSuite) TestCreateWithoutPassedInstancesAndMultipleInstancesRunning() {
	expectedOptions := getUnitOptions(UnitTemplate{"carousel", "abc123", "mmmhm", "2006.01.02-15.04.05"})
	suite.FleetClientMock.On("Units").Return([]*schema.Unit{
		&schema.Unit{"running", "running", "efefeff", "carousel:efefeff:2006.01.02-15.04.05@1.service", []*schema.UnitOption{}},
		&schema.Unit{"running", "running", "efefeff", "carousel:efefeff:2006.01.02-15.04.05@2.service", []*schema.UnitOption{}},
	}, nil)

	// Should start 2 units
	suite.FleetClientMock.On("CreateUnit", &schema.Unit{Name: "carousel:abc123:2006.01.02-15.04.05@1.service", Options: expectedOptions}).Return(nil)
	suite.FleetClientMock.On("SetUnitTargetState", "carousel:abc123:2006.01.02-15.04.05@1.service", "launched").Return(nil)
	suite.FleetClientMock.On("CreateUnit", &schema.Unit{Name: "carousel:abc123:2006.01.02-15.04.05@2.service", Options: expectedOptions}).Return(nil)
	suite.FleetClientMock.On("SetUnitTargetState", "carousel:abc123:2006.01.02-15.04.05@2.service", "launched").Return(nil)

	suite.Subject.Create(
		mocking.URL(suite.Service.RootMux, "POST", "http://example.com/v1/services/carousel/deploys"),
		mocking.Header(nil),
		&DeployRequest{Deploy{Version: "abc123", DestroyPrevious: false, Timestamp: "2006.01.02-15.04.05"}},
	)

	suite.FleetClientMock.Mock.AssertExpectations(suite.T())
}

func (suite *DeploysResourceTestSuite) TestCreateWithoutPassedInstancesAndFailedInstances() {
	expectedOptions := getUnitOptions(UnitTemplate{"carousel", "abc123", "mmmhm", "2008.01.02-15.04.05"})
	suite.FleetClientMock.On("Units").Return([]*schema.Unit{
		&schema.Unit{"running", "running", "efefeff", "carousel:efefeff:2006.01.02-15.04.05@1.service", []*schema.UnitOption{}},
		&schema.Unit{"failed", "failed", "efefeff", "carousel:efefeff:2007.01.02-15.04.05@1.service", []*schema.UnitOption{}},
	}, nil)

	// Should start 1 unit
	suite.FleetClientMock.On("CreateUnit", &schema.Unit{Name: "carousel:abc123:2008.01.02-15.04.05@1.service", Options: expectedOptions}).Return(nil)
	suite.FleetClientMock.On("SetUnitTargetState", "carousel:abc123:2008.01.02-15.04.05@1.service", "launched").Return(nil)

	suite.Subject.Create(
		mocking.URL(suite.Service.RootMux, "POST", "http://example.com/v1/services/carousel/deploys"),
		mocking.Header(nil),
		&DeployRequest{Deploy{Version: "abc123", DestroyPrevious: false, Timestamp: "2008.01.02-15.04.05"}},
	)

	suite.FleetClientMock.Mock.AssertExpectations(suite.T())
}

func (suite *DeploysResourceTestSuite) TestCreateWithoutPassedInstancesAndMultipleVersionsRunning() {
{'fraction_non_alphanumeric': 0.1724230614164763, 'fraction_numerical': 0.0731084531648972, 'mean_word_length': 6.695966907962771, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 22, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '340247', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2715, 'n_words': 322}