From 5f3d4fc026a52ad89b4bdf25aa4810b7442c942c Mon Sep 17 00:00:00 2001 From: Ryan Walls Date: Sun, 17 Apr 2016 09:17:23 -0600 Subject: [PATCH 1/6] Use glide for dependency management and fix broken build --- .gitignore | 30 ++++++++++++++++++++++++++++++ ecs_state.go | 4 ++-- glide.lock | 31 +++++++++++++++++++++++++++++++ glide.yaml | 9 +++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 glide.lock create mode 100644 glide.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c592d8e --- /dev/null +++ b/.gitignore @@ -0,0 +1,30 @@ +# Vendoring support +vendor + +# binaries +ecs_state + +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof diff --git a/ecs_state.go b/ecs_state.go index f539b28..b5d1804 100644 --- a/ecs_state.go +++ b/ecs_state.go @@ -23,7 +23,7 @@ import ( // The State object provides methods to synchronize and query the state of the ECS cluster. type State struct { clusterName string - db gorm.DB + db *gorm.DB ecs_client *ecs.ECS log Logger } @@ -49,7 +49,7 @@ func Initialize(clusterName string, ecs_client *ecs.ECS, logger Logger) *State { // Provides direct access to the database through gorm to allow more advanced queries against state. func (state *State) DB() *gorm.DB { - return &state.db + return state.db } // Will parse and log any AWS errors received while contacting ECS. diff --git a/glide.lock b/glide.lock new file mode 100644 index 0000000..a75c9d4 --- /dev/null +++ b/glide.lock @@ -0,0 +1,31 @@ +hash: 32850099782d094173df895662da2af7ca2991a065f1e7799524a8819bae8d65 +updated: 2016-04-17T09:11:45.996685675-06:00 +imports: +- name: github.com/aws/aws-sdk-go + version: b743ca27e4b9237d7e2911d6dba2f039c4a3ad0f + subpackages: + - aws + - aws/awserr + - service/ecs + - aws/credentials + - aws/awsutil + - aws/client + - aws/client/metadata + - aws/request + - private/protocol/jsonrpc + - private/signer/v4 + - private/waiter + - private/protocol/json/jsonutil + - private/protocol/rest + - private/protocol +- name: github.com/go-ini/ini + version: 12f418cc7edc5a618a51407b7ac1f1f512139df3 +- name: github.com/jinzhu/gorm + version: 5174cc5c242a728b435ea2be8a2f7f998e15429b +- name: github.com/jinzhu/inflection + version: 3272df6c21d04180007eb3349844c89a3856bc25 +- name: github.com/jmespath/go-jmespath + version: 0b12d6b521d83fc7f755e7cfc1b1fbdd35a01a74 +- name: github.com/mattn/go-sqlite3 + version: aeb3180df56de252b6cfcd64985272010c920ab7 +devImports: [] diff --git a/glide.yaml b/glide.yaml new file mode 100644 index 0000000..f8ab790 --- /dev/null +++ b/glide.yaml @@ -0,0 +1,9 @@ +package: github.com/jhspaybar/ecs_state +import: +- package: github.com/aws/aws-sdk-go + subpackages: + - aws + - aws/awserr + - service/ecs +- package: github.com/jinzhu/gorm +- package: github.com/mattn/go-sqlite3 From 7a1b4348210e58d5460e47ca4c2896eff391d3e4 Mon Sep 17 00:00:00 2001 From: Ryan Walls Date: Sun, 17 Apr 2016 10:47:43 -0600 Subject: [PATCH 2/6] Remove underscore from package name --- cluster.go | 2 +- container_instance.go | 2 +- ecs_state.go | 2 +- logger.go | 2 +- task.go | 2 +- task_definition.go | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cluster.go b/cluster.go index 85b0e4d..4b7dd73 100644 --- a/cluster.go +++ b/cluster.go @@ -1,4 +1,4 @@ -package ecs_state +package ecsstate // Local representation of an ECS cluster and stored by gorm type Cluster struct { diff --git a/container_instance.go b/container_instance.go index 4b73824..5928473 100644 --- a/container_instance.go +++ b/container_instance.go @@ -1,4 +1,4 @@ -package ecs_state +package ecsstate // Local representation of an ECS ContainerInstance and stored by gorm. // Notably, resources and other sub-objects have been placed into their own diff --git a/ecs_state.go b/ecs_state.go index b5d1804..d8c0c9e 100644 --- a/ecs_state.go +++ b/ecs_state.go @@ -3,7 +3,7 @@ // placing tasks within the ECS cluster to replicate the cluster state into a local working copy and synchronize on occassion. // // Author: William Thurston -package ecs_state +package ecsstate import ( "bytes" diff --git a/logger.go b/logger.go index bf71f0c..da4b122 100644 --- a/logger.go +++ b/logger.go @@ -1,4 +1,4 @@ -package ecs_state +package ecsstate import ( "log" diff --git a/task.go b/task.go index 38ef2de..3c609f7 100644 --- a/task.go +++ b/task.go @@ -1,4 +1,4 @@ -package ecs_state +package ecsstate // Local representation of an ECS Task and stored by gorm. A number of fields are absent // for now as they are not needed to track and update the state of the state of the cluster typically. diff --git a/task_definition.go b/task_definition.go index 63495d2..465aa8f 100644 --- a/task_definition.go +++ b/task_definition.go @@ -1,4 +1,4 @@ -package ecs_state +package ecsstate // Local representation of an ECS TaskDefinition and stored by gorm. Resources are extracted, // but the complete definition is ignored. From 5266f563099166edf1842f8216f3754d1e2987c3 Mon Sep 17 00:00:00 2001 From: Ryan Walls Date: Sun, 17 Apr 2016 10:47:57 -0600 Subject: [PATCH 3/6] Add interface for state operations --- state.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 state.go diff --git a/state.go b/state.go new file mode 100644 index 0000000..06c35be --- /dev/null +++ b/state.go @@ -0,0 +1,12 @@ +package scheduler + +// StateOps is the interface for refreshing and interacting with the local +// ECS state. +type StateOps interface { + Initialize(clusterName string, ecs *ecs.ECS, logger Logger) *State + FindLocationsForTaskDefinition(td string) *[]ContainerInstance + FindTaskDefinition(td string) TaskDefinition + RefreshClusterState() + RefreshContainerInstanceState() + RefreshTaskState() +} From 42da1edb5d8ba8bb74d94c502f91892981e45f5b Mon Sep 17 00:00:00 2001 From: Ryan Walls Date: Sun, 17 Apr 2016 10:48:38 -0600 Subject: [PATCH 4/6] Create mocks package --- mocks/StateOps.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++ state.go | 6 +++- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 mocks/StateOps.go diff --git a/mocks/StateOps.go b/mocks/StateOps.go new file mode 100644 index 0000000..cc1abc1 --- /dev/null +++ b/mocks/StateOps.go @@ -0,0 +1,71 @@ +package mocks + +import "github.com/jhspaybar/ecsstate" +import "github.com/stretchr/testify/mock" + +import "github.com/aws/aws-sdk-go/service/ecs" + +type StateOps struct { + mock.Mock +} + +// Initialize provides a mock function with given fields: clusterName, ecs, logger +func (_m *StateOps) Initialize(clusterName string, e *ecs.ECS, logger ecsstate.Logger) *ecsstate.State { + ret := _m.Called(clusterName, e, logger) + + var r0 *ecsstate.State + if rf, ok := ret.Get(0).(func(string, *ecs.ECS, ecsstate.Logger) *ecsstate.State); ok { + r0 = rf(clusterName, e, logger) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*ecsstate.State) + } + } + + return r0 +} + +// FindLocationsForTaskDefinition provides a mock function with given fields: td +func (_m *StateOps) FindLocationsForTaskDefinition(td string) *[]ecsstate.ContainerInstance { + ret := _m.Called(td) + + var r0 *[]ecsstate.ContainerInstance + if rf, ok := ret.Get(0).(func(string) *[]ecsstate.ContainerInstance); ok { + r0 = rf(td) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*[]ecsstate.ContainerInstance) + } + } + + return r0 +} + +// FindTaskDefinition provides a mock function with given fields: td +func (_m *StateOps) FindTaskDefinition(td string) ecsstate.TaskDefinition { + ret := _m.Called(td) + + var r0 ecsstate.TaskDefinition + if rf, ok := ret.Get(0).(func(string) ecsstate.TaskDefinition); ok { + r0 = rf(td) + } else { + r0 = ret.Get(0).(ecsstate.TaskDefinition) + } + + return r0 +} + +// RefreshClusterState provides a mock function with given fields: +func (_m *StateOps) RefreshClusterState() { + _m.Called() +} + +// RefreshContainerInstanceState provides a mock function with given fields: +func (_m *StateOps) RefreshContainerInstanceState() { + _m.Called() +} + +// RefreshTaskState provides a mock function with given fields: +func (_m *StateOps) RefreshTaskState() { + _m.Called() +} diff --git a/state.go b/state.go index 06c35be..1af0d6e 100644 --- a/state.go +++ b/state.go @@ -1,4 +1,8 @@ -package scheduler +package ecsstate + +import ( + "github.com/aws/aws-sdk-go/service/ecs" +) // StateOps is the interface for refreshing and interacting with the local // ECS state. From 5d2fa4a7fdd89deb845cae196f2b5f8d3ba05d04 Mon Sep 17 00:00:00 2001 From: Ryan Walls Date: Sun, 17 Apr 2016 12:09:15 -0600 Subject: [PATCH 5/6] Return a StateOps object from Initialize --- ecs_state.go | 2 +- state.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ecs_state.go b/ecs_state.go index d8c0c9e..8e76f14 100644 --- a/ecs_state.go +++ b/ecs_state.go @@ -31,7 +31,7 @@ type State struct { // Create a new State object. The clusterName is the cluster to track, ecs_client should be provided by the caller // with proper credentials preferably scoped to read only access to ECS APIs, and the logger can use ecs_state.DefaultLogger // for output on stdout, or the user can provide a custom logger instead. -func Initialize(clusterName string, ecs_client *ecs.ECS, logger Logger) *State { +func Initialize(clusterName string, ecs_client *ecs.ECS, logger Logger) StateOps { logger.Info("Intializing ecs_state for cluster ", clusterName) db, err := gorm.Open("sqlite3", ":memory:") diff --git a/state.go b/state.go index 1af0d6e..b491817 100644 --- a/state.go +++ b/state.go @@ -1,16 +1,16 @@ package ecsstate import ( - "github.com/aws/aws-sdk-go/service/ecs" + "github.com/jinzhu/gorm" ) // StateOps is the interface for refreshing and interacting with the local // ECS state. type StateOps interface { - Initialize(clusterName string, ecs *ecs.ECS, logger Logger) *State FindLocationsForTaskDefinition(td string) *[]ContainerInstance FindTaskDefinition(td string) TaskDefinition RefreshClusterState() RefreshContainerInstanceState() RefreshTaskState() + DB() *gorm.DB } From 90d2418eac8fbb2fab5781e01221d6de7a34293b Mon Sep 17 00:00:00 2001 From: Ryan Walls Date: Sun, 17 Apr 2016 12:16:27 -0600 Subject: [PATCH 6/6] Update mock to match new interface --- mocks/StateOps.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/mocks/StateOps.go b/mocks/StateOps.go index cc1abc1..1f37ba0 100644 --- a/mocks/StateOps.go +++ b/mocks/StateOps.go @@ -3,28 +3,12 @@ package mocks import "github.com/jhspaybar/ecsstate" import "github.com/stretchr/testify/mock" -import "github.com/aws/aws-sdk-go/service/ecs" +import "github.com/jinzhu/gorm" type StateOps struct { mock.Mock } -// Initialize provides a mock function with given fields: clusterName, ecs, logger -func (_m *StateOps) Initialize(clusterName string, e *ecs.ECS, logger ecsstate.Logger) *ecsstate.State { - ret := _m.Called(clusterName, e, logger) - - var r0 *ecsstate.State - if rf, ok := ret.Get(0).(func(string, *ecs.ECS, ecsstate.Logger) *ecsstate.State); ok { - r0 = rf(clusterName, e, logger) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*ecsstate.State) - } - } - - return r0 -} - // FindLocationsForTaskDefinition provides a mock function with given fields: td func (_m *StateOps) FindLocationsForTaskDefinition(td string) *[]ecsstate.ContainerInstance { ret := _m.Called(td) @@ -69,3 +53,19 @@ func (_m *StateOps) RefreshContainerInstanceState() { func (_m *StateOps) RefreshTaskState() { _m.Called() } + +// DB provides a mock function with given fields: +func (_m *StateOps) DB() *gorm.DB { + ret := _m.Called() + + var r0 *gorm.DB + if rf, ok := ret.Get(0).(func() *gorm.DB); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(*gorm.DB) + } + } + + return r0 +}