Add tests for cache and locking

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2025-05-02 09:32:24 +00:00
parent 059734f064
commit 92d04c8e8d
10 changed files with 533 additions and 112 deletions

87
cache/cache_test.go vendored Normal file
View file

@ -0,0 +1,87 @@
package cache
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
commonParams "github.com/cloudbase/garm-provider-common/params"
garmTesting "github.com/cloudbase/garm/internal/testing"
"github.com/cloudbase/garm/params"
)
type CacheTestSuite struct {
suite.Suite
entity params.GithubEntity
}
func (c *CacheTestSuite) SetupTest() {
c.entity = params.GithubEntity{
ID: "1234",
EntityType: params.GithubEntityTypeOrganization,
Name: "test",
Owner: "test",
}
}
func (c *CacheTestSuite) TearDownTest() {
// Clean up the cache after each test
githubToolsCache.mux.Lock()
defer githubToolsCache.mux.Unlock()
githubToolsCache.entities = make(map[string]GithubEntityTools)
}
func (c *CacheTestSuite) TestCacheIsInitialized() {
c.Require().NotNil(githubToolsCache)
}
func (c *CacheTestSuite) TestSetCacheWorks() {
tools := []commonParams.RunnerApplicationDownload{
{
DownloadURL: garmTesting.Ptr("https://example.com"),
},
}
c.Require().NotNil(githubToolsCache)
c.Require().Len(githubToolsCache.entities, 0)
SetGithubToolsCache(c.entity, tools)
c.Require().Len(githubToolsCache.entities, 1)
cachedTools, ok := GetGithubToolsCache(c.entity)
c.Require().True(ok)
c.Require().Len(cachedTools, 1)
c.Require().Equal(tools[0].GetDownloadURL(), cachedTools[0].GetDownloadURL())
}
func (c *CacheTestSuite) TestTimedOutToolsCache() {
tools := []commonParams.RunnerApplicationDownload{
{
DownloadURL: garmTesting.Ptr("https://example.com"),
},
}
c.Require().NotNil(githubToolsCache)
c.Require().Len(githubToolsCache.entities, 0)
SetGithubToolsCache(c.entity, tools)
c.Require().Len(githubToolsCache.entities, 1)
entity := githubToolsCache.entities[c.entity.String()]
entity.updatedAt = entity.updatedAt.Add(-2 * time.Hour)
githubToolsCache.entities[c.entity.String()] = entity
cachedTools, ok := GetGithubToolsCache(c.entity)
c.Require().False(ok)
c.Require().Nil(cachedTools)
}
func (c *CacheTestSuite) TestGetInexistentCache() {
c.Require().NotNil(githubToolsCache)
c.Require().Len(githubToolsCache.entities, 0)
cachedTools, ok := GetGithubToolsCache(c.entity)
c.Require().False(ok)
c.Require().Nil(cachedTools)
}
func TestCacheTestSuite(t *testing.T) {
t.Parallel()
suite.Run(t, new(CacheTestSuite))
}