This commit is contained in:
Nan Deng 2014-06-14 15:31:18 -07:00
parent 845d394755
commit 750b5b464e
2 changed files with 160 additions and 0 deletions

79
storage/storage.go Normal file
View File

@ -0,0 +1,79 @@
// Copyright 2014 Google Inc. 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 storage
import (
"fmt"
"sync"
"github.com/google/cadvisor/info"
)
type ContainerStatsWriter interface {
Write(ref info.ContainerReference, stats *info.ContainerStats) error
}
// Database config which should contain all information used to connect to
// all/most databases
type Config struct {
Engine string `json:"engine,omitempty"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Database string `json:"database,omitempty"`
Params map[string]string `json:"parameters,omitempty"`
}
type ContainerStatsWriterFactory interface {
String() string
New(config *Config) (ContainerStatsWriter, error)
}
type containerStatsWriterFactoryManager struct {
lock sync.RWMutex
factories map[string]ContainerStatsWriterFactory
}
func (self *containerStatsWriterFactoryManager) Register(factory ContainerStatsWriterFactory) {
self.lock.Lock()
defer self.lock.Unlock()
if self.factories == nil {
self.factories = make(map[string]ContainerStatsWriterFactory, 8)
}
self.factories[factory.String()] = factory
}
func (self *containerStatsWriterFactoryManager) New(config *Config) (ContainerStatsWriter, error) {
self.lock.RLock()
defer self.lock.RUnlock()
if factory, ok := self.factories[config.Engine]; ok {
return factory.New(config)
}
return nil, fmt.Errorf("unknown database %v", config.Engine)
}
var globalContainerStatsWriterFactoryManager containerStatsWriterFactoryManager
func RegisterContainerStatsWriterFactory(factory ContainerStatsWriterFactory) {
globalContainerStatsWriterFactoryManager.Register(factory)
}
func NewContainerStatsWriter(config *Config) (ContainerStatsWriter, error) {
return globalContainerStatsWriterFactoryManager.New(config)
}

81
storage/storage_test.go Normal file
View File

@ -0,0 +1,81 @@
package storage
import (
"sync"
"testing"
"github.com/google/cadvisor/info"
"github.com/stretchr/testify/mock"
)
type mockContainerStatsWriter struct {
storageName string
mock.Mock
}
func (self *mockContainerStatsWriter) Write(
ref info.ContainerReference,
stats *info.ContainerStats,
) error {
args := self.Called(ref, stats)
return args.Error(0)
}
type mockContainerStatsWriterFactory struct {
name string
}
func (self *mockContainerStatsWriterFactory) String() string {
return self.name
}
func (self *mockContainerStatsWriterFactory) New(
config *Config,
) (ContainerStatsWriter, error) {
mockWriter := &mockContainerStatsWriter{
storageName: self.name,
}
return mockWriter, nil
}
func TestContainerStatsWriterFactoryManager(t *testing.T) {
factoryNames := []string{
"abc",
"bcd",
}
wg := sync.WaitGroup{}
for _, name := range factoryNames {
wg.Add(1)
go func(n string) {
defer wg.Done()
factory := &mockContainerStatsWriterFactory{
name: n,
}
RegisterContainerStatsWriterFactory(factory)
}(name)
}
wg.Wait()
for _, name := range factoryNames {
wg.Add(1)
config := &Config{
Engine: name,
}
go func(n string) {
defer wg.Done()
writer, err := NewContainerStatsWriter(config)
if err != nil {
t.Error(err)
}
if mw, ok := writer.(*mockContainerStatsWriter); ok {
if mw.storageName != n {
t.Errorf("wrong writer. should be %v, got %v", n, mw.storageName)
}
} else {
t.Errorf("wrong writer: unknown type")
}
}(name)
}
wg.Wait()
}