harbor/src/chartserver/cache_test.go

91 lines
2.1 KiB
Go
Raw Normal View History

package chartserver
import (
"os"
"testing"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/proto/hapi/chart"
helm_repo "k8s.io/helm/pkg/repo"
)
var (
mockChart = &ChartVersionDetails{
Metadata: &helm_repo.ChartVersion{
Metadata: &chart.Metadata{
Name: "mock_chart",
Version: "0.1.0",
},
Digest: "mock_digest",
},
Dependencies: make([]*chartutil.Dependency, 0),
}
)
//Test the no cache set scenario
func TestNoCache(t *testing.T) {
chartCache := NewChartCache()
if chartCache == nil {
t.Fatalf("cache instance should not be nil")
}
if chartCache.IsEnabled() {
t.Fatal("chart cache should not be enabled")
}
}
//Test the in memory cache
func TestInMemoryCache(t *testing.T) {
os.Setenv(cacheDriverENVKey, cacheDriverMem)
chartCache := NewChartCache()
if chartCache == nil {
t.Fatalf("cache instance should not be nil")
}
if !chartCache.IsEnabled() {
t.Fatal("chart cache should be enabled")
}
if chartCache.driverType != cacheDriverMem {
t.Fatalf("expect driver type %s but got %s", cacheDriverMem, chartCache.driverType)
}
chartCache.PutChart(mockChart)
theCachedChart := chartCache.GetChart(mockChart.Metadata.Digest)
if theCachedChart == nil || theCachedChart.Metadata.Name != mockChart.Metadata.Name {
t.Fatal("In memory cache does work")
}
os.Unsetenv(cacheDriverENVKey)
}
//Test redis cache
//Failed to config redis cache and then use in memory instead
func TestRedisCache(t *testing.T) {
os.Setenv(cacheDriverENVKey, cacheDriverRedis)
os.Setenv(redisENVKey, ":6379")
chartCache := NewChartCache()
if chartCache == nil {
t.Fatalf("cache instance should not be nil")
}
if !chartCache.IsEnabled() {
t.Fatal("chart cache should be enabled")
}
if chartCache.driverType != cacheDriverRedis {
t.Fatalf("expect driver type '%s' but got '%s'", cacheDriverRedis, chartCache.driverType)
}
chartCache.PutChart(mockChart)
theCachedChart := chartCache.GetChart(mockChart.Metadata.Digest)
if theCachedChart == nil || theCachedChart.Metadata.Name != mockChart.Metadata.Name {
t.Fatal("In memory cache does work")
}
os.Unsetenv(redisENVKey)
os.Unsetenv(cacheDriverENVKey)
}