Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarcic committed Nov 9, 2016
1 parent 97ed0ba commit c911c96
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 39 deletions.
3 changes: 1 addition & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

## Certs

* Store cert names from API
* Remove cert API (file and name)
* Read cert names from env
* Distribute certs request
* Distribute cert files
Expand All @@ -12,6 +10,7 @@
* Explain certs inside Docker (files + env)
* Explain certs API
* Fix TODOs
* Remove cert API (file and name)

## Content

Expand Down
7 changes: 7 additions & 0 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ func (m *ProxyMock) Reload() error {
return params.Error(0)
}

func (m *ProxyMock) AddCert(certName string) {
m.Called(certName)
}

func getProxyMock(skipMethod string) *ProxyMock {
mockObj := new(ProxyMock)
if skipMethod != "RunCmd" {
Expand All @@ -411,5 +415,8 @@ func getProxyMock(skipMethod string) *ProxyMock {
if skipMethod != "Reload" {
mockObj.On("Reload").Return(nil)
}
if skipMethod != "AddCert" {
mockObj.On("AddCert", mock.Anything).Return(nil)
}
return mockObj
}
22 changes: 14 additions & 8 deletions proxy/ha_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@ import (
)

type HaProxy struct {
Certs []string
TemplatesPath string
ConfigsPath string
}

// TODO: Change to pointer
var Instance Proxy

func NewHaProxy(templatesPath, configsPath string, certs []string) Proxy {
func NewHaProxy(templatesPath, configsPath string, certs map[string]bool) Proxy {
data.Certs = certs
return HaProxy{
TemplatesPath: templatesPath,
ConfigsPath: configsPath,
Certs: certs,
}
}

func (m HaProxy) AddCert(certName string) {
if data.Certs == nil {
data.Certs = map[string]bool{}
}
data.Certs[certName] = true
}

func (m HaProxy) RunCmd(extraArgs []string) error {
args := []string{
"-f",
Expand Down Expand Up @@ -109,19 +115,19 @@ backend dummy-be
strings.Join(contentArr, "\n\n"),
)
var content bytes.Buffer
type Data struct {
type ConfigData struct {
CertsString string
}
certs := []string{}
if len(m.Certs) > 0 {
if len(data.Certs) > 0 {
certs = append(certs, " ssl")
for _, cert := range m.Certs {
for cert, _ := range data.Certs {
certs = append(certs, fmt.Sprintf("crt /certs/%s", cert))
}
}
data := Data{
configData := ConfigData{
CertsString: strings.Join(certs, " "),
}
tmpl.Execute(&content, data)
tmpl.Execute(&content, configData)
return content.String(), nil
}
46 changes: 39 additions & 7 deletions proxy/ha_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@ func (s *HaProxyTestSuite) SetupTest() {
}
}

// AddCertName

func (s HaProxyTestSuite) Test_AddCert_StoresCertificateName() {
dataOrig := data
defer func() { data = dataOrig }()
orig := Instance
defer func() { Instance = orig }()
p := HaProxy{}

p.AddCert("my-cert-3")

s.Equal(1, len(data.Certs))
s.Equal(map[string]bool{"my-cert-3": true}, data.Certs)
}

func (s HaProxyTestSuite) Test_AddCert_DoesNotStoreDuplicates() {
dataOrig := data
defer func() { data = dataOrig }()
expected := map[string]bool{"cert-1": true, "cert-2": true, "cert-3": true}
data.Certs = expected
orig := Instance
defer func() { Instance = orig }()
p := HaProxy{}

p.AddCert("cert-3")

s.Equal(3, len(data.Certs))
s.Equal(expected, data.Certs)
}

// CreateConfigFromTemplates

func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_ReturnsError_WhenReadDirFails() {
Expand All @@ -42,7 +72,7 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_ReturnsError_WhenReadDi
return nil, fmt.Errorf("Could not read the directory")
}

err := NewHaProxy(s.TemplatesPath, s.ConfigsPath, []string{}).CreateConfigFromTemplates()
err := NewHaProxy(s.TemplatesPath, s.ConfigsPath, map[string]bool{}).CreateConfigFromTemplates()

s.Error(err)
}
Expand All @@ -62,7 +92,7 @@ config2 content`
return nil
}

NewHaProxy(s.TemplatesPath, s.ConfigsPath, []string{}).CreateConfigFromTemplates()
NewHaProxy(s.TemplatesPath, s.ConfigsPath, map[string]bool{}).CreateConfigFromTemplates()

s.Equal(expectedFilename, actualFilename)
s.Equal(expectedData, actualData)
Expand All @@ -83,7 +113,7 @@ config2 content`
return nil
}

NewHaProxy(s.TemplatesPath, s.ConfigsPath, []string{"my-cert.pem"}).CreateConfigFromTemplates()
NewHaProxy(s.TemplatesPath, s.ConfigsPath, map[string]bool{"my-cert.pem": true}).CreateConfigFromTemplates()

s.Equal(expectedFilename, actualFilename)
s.Equal(expectedData, actualData)
Expand All @@ -104,7 +134,8 @@ config2 content`
return nil
}

NewHaProxy(s.TemplatesPath, s.ConfigsPath, []string{"my-cert.pem", "my-other-cert.pem"}).CreateConfigFromTemplates()
p := NewHaProxy(s.TemplatesPath, s.ConfigsPath, map[string]bool{"my-cert.pem": true, "my-other-cert.pem": true})
p.CreateConfigFromTemplates()

s.Equal(expectedFilename, actualFilename)
s.Equal(expectedData, actualData)
Expand Down Expand Up @@ -132,7 +163,7 @@ backend dummy-be
return nil
}

NewHaProxy(s.TemplatesPath, s.ConfigsPath, []string{}).CreateConfigFromTemplates()
NewHaProxy(s.TemplatesPath, s.ConfigsPath, map[string]bool{}).CreateConfigFromTemplates()

s.Equal(expectedData, actualData)
}
Expand All @@ -146,7 +177,7 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_ReturnsError_WhenReadCo
return nil, fmt.Errorf("Could not read the directory")
}

err := NewHaProxy(s.TemplatesPath, s.ConfigsPath, []string{}).CreateConfigFromTemplates()
err := NewHaProxy(s.TemplatesPath, s.ConfigsPath, map[string]bool{}).CreateConfigFromTemplates()

s.Error(err)
}
Expand All @@ -168,7 +199,7 @@ config2 content`
return []byte(expectedData), nil
}

actualData, _ := NewHaProxy(s.TemplatesPath, s.ConfigsPath, []string{}).ReadConfig()
actualData, _ := NewHaProxy(s.TemplatesPath, s.ConfigsPath, map[string]bool{}).ReadConfig()

s.Equal(expectedFilename, actualFilename)
s.Equal(expectedData, actualData)
Expand Down Expand Up @@ -253,3 +284,4 @@ func (s HaProxyTestSuite) mockHaExecCmd() *[]string {
}
return &actualCommand
}

58 changes: 58 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
package proxy
import "github.com/stretchr/testify/mock"

var ProxyInstance Proxy = HaProxy{}

type Data struct {
Certs map[string]bool
}

var data = Data{}

type Proxy interface {
RunCmd(extraArgs []string) error
CreateConfigFromTemplates() error
ReadConfig() (string, error)
Reload() error
AddCert(certName string)
}

// Mock

type ProxyMock struct {
mock.Mock
}

func (m *ProxyMock) RunCmd(extraArgs []string) error {
params := m.Called(extraArgs)
return params.Error(0)
}

func (m *ProxyMock) CreateConfigFromTemplates() error {
params := m.Called()
return params.Error(0)
}

func (m *ProxyMock) ReadConfig() (string, error) {
params := m.Called()
return params.String(0), params.Error(1)
}

func (m *ProxyMock) Reload() error {
params := m.Called()
return params.Error(0)
}

func (m *ProxyMock) AddCert(certName string) {
m.Called(certName)
}

func GetProxyMock(skipMethod string) *ProxyMock {
mockObj := new(ProxyMock)
if skipMethod != "RunCmd" {
mockObj.On("RunCmd", mock.Anything).Return(nil)
}
if skipMethod != "CreateConfigFromTemplates" {
mockObj.On("CreateConfigFromTemplates").Return(nil)
}
if skipMethod != "ReadConfig" {
mockObj.On("ReadConfig").Return("", nil)
}
if skipMethod != "Reload" {
mockObj.On("Reload").Return(nil)
}
if skipMethod != "AddCert" {
mockObj.On("AddCert", mock.Anything).Return(nil)
}
return mockObj
}
8 changes: 4 additions & 4 deletions reconfigure.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func (m *Reconfigure) Execute(args []string) error {
return err
}
// TODO: Move the logic somewhere else. Test whether it will work from NewReconfigure.
// TODO: Change []string{} env vars
// TODO: Change map[string]bool{} env vars
if haproxy.Instance == nil {
haproxy.Instance = haproxy.NewHaProxy(m.TemplatesPath, m.ConfigsPath, []string{})
haproxy.Instance = haproxy.NewHaProxy(m.TemplatesPath, m.ConfigsPath, map[string]bool{})
}
if err := haproxy.Instance.CreateConfigFromTemplates(); err != nil {
return err
Expand Down Expand Up @@ -180,9 +180,9 @@ func (m *Reconfigure) reloadFromRegistry(addresses []string, instanceName, mode
}

// TODO: Move the logic somewhere else. Test whether it will work from NewReconfigure.
// TODO: Change []string{} to env. vars
// TODO: Change map[string]bool{} to env. vars
if haproxy.Instance == nil {
haproxy.Instance = haproxy.NewHaProxy(m.TemplatesPath, m.ConfigsPath, []string{})
haproxy.Instance = haproxy.NewHaProxy(m.TemplatesPath, m.ConfigsPath, map[string]bool{})
}
if err := haproxy.Instance.CreateConfigFromTemplates(); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func (m *Remove) Execute(args []string) error {
}

// TODO: Move the logic somewhere else. Test whether it will work from NewRemove.
// TODO: Change []string{} to env vars
// TODO: Change map[string]bool{} to env vars
if haproxy.Instance == nil {
haproxy.Instance = haproxy.NewHaProxy(m.TemplatesPath, m.ConfigsPath, []string{})
haproxy.Instance = haproxy.NewHaProxy(m.TemplatesPath, m.ConfigsPath, map[string]bool{})
}
if err := haproxy.Instance.CreateConfigFromTemplates(); err != nil {
logPrintf(err.Error())
Expand Down
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ func (m *Serve) remove(w http.ResponseWriter, req *http.Request) {
func (m *Serve) config(w http.ResponseWriter, req *http.Request) {
httpWriterSetContentType(w, "text/html")
// TODO: Move the logic somewhere else. Test whether it will work from NewReconfigure.
// TODO: Change []string{} env vars
// TODO: Change map[string]bool{} env vars
if haproxy.Instance == nil {
haproxy.Instance = haproxy.NewHaProxy(m.TemplatesPath, m.ConfigsPath, []string{})
haproxy.Instance = haproxy.NewHaProxy(m.TemplatesPath, m.ConfigsPath, map[string]bool{})
}
out, err := haproxy.Instance.ReadConfig()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions server/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
"path/filepath"
"sync"
// "../proxy"
"../proxy"
)

var mu = &sync.Mutex{}
Expand Down Expand Up @@ -63,7 +63,7 @@ func (m Cert) Put(w http.ResponseWriter, req *http.Request) (string, error) {
Message: "",
})
w.Write(js)
// proxy.Instance.AddCert(name)
proxy.Instance.AddCert(name)
return path, nil
}

Expand Down
34 changes: 22 additions & 12 deletions server/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"path/filepath"
"strings"
"testing"
// "../proxy"
"../proxy"
)

type CertTestSuite struct {
Expand All @@ -22,6 +22,10 @@ func (s *CertTestSuite) SetupTest() {
}

func TestCertUnitTestSuite(t *testing.T) {
proxyOrig := proxy.Instance
defer func() { proxy.Instance = proxyOrig }()
proxyMock := proxy.GetProxyMock("")
proxy.Instance = proxyMock
s := new(CertTestSuite)
suite.Run(t, s)
}
Expand All @@ -48,17 +52,23 @@ func (s *CertTestSuite) Test_Put_SavesBodyAsFile() {
s.Equal(expected, string(actual))
}

func (s *CertTestSuite) Test_Put_StoresCertInMemory() {
// p := proxy.NewHaProxy("", "", []string{})
// c := NewCert("../certs")
// certName := "test.pem"
// http.NewRequest(
// "PUT",
// fmt.Sprintf("http://acme.com/v1/docker-flow-proxy/cert?certName=%s", certName),
// strings.NewReader("THIS IS A CERTIFICATE"),
// )
//
// p.
func (s *CertTestSuite) Test_Put_InvokesProxyAddCert() {
proxyOrig := proxy.Instance
defer func() { proxy.Instance = proxyOrig }()
proxyMock := proxy.GetProxyMock("")
proxy.Instance = proxyMock
c := NewCert("../certs")
certName := "test.pem"
w := getResponseWriterMock()
req, _ := http.NewRequest(
"PUT",
fmt.Sprintf("http://acme.com/v1/docker-flow-proxy/cert?certName=%s", certName),
strings.NewReader("THIS IS A CERTIFICATE"),
)

c.Put(w, req)

proxyMock.AssertCalled(s.T(), "AddCert", certName)
}

func (s *CertTestSuite) Test_Put_SetsContentTypeToJson() {
Expand Down

0 comments on commit c911c96

Please sign in to comment.