func TestRecordCachedAgent(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() mockFS := NewMockfileSystem(mockCtrl) mockFS.EXPECT().WriteFile(config.CacheState(), []byte("1"), os.FileMode(orwPerm)) d := &Downloader{ fs: mockFS, } d.RecordCachedAgent() }
func TestIsAgentCachedFalseMissingState(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() mockFS := NewMockfileSystem(mockCtrl) mockFS.EXPECT().Stat(config.CacheState()).Return(nil, errors.New("test error")) d := &Downloader{ fs: mockFS, } if d.IsAgentCached() { t.Error("Expected d.IsAgentCached() to be false") } }
func TestIsAgentCachedFalseEmptyState(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() mockFS := NewMockfileSystem(mockCtrl) mockFSInfo := NewMockfileSizeInfo(mockCtrl) mockFS.EXPECT().Stat(config.CacheState()).Return(mockFSInfo, nil) mockFSInfo.EXPECT().Size().Return(int64(0)) d := &Downloader{ fs: mockFS, } if d.IsAgentCached() { t.Error("Expected d.IsAgentCached() to be false") } }
func TestIsAgentCachedTrue(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() mockFS := NewMockfileSystem(mockCtrl) mockFSInfo := NewMockfileSizeInfo(mockCtrl) mockFS.EXPECT().Stat(config.CacheState()).Return(mockFSInfo, nil) mockFS.EXPECT().Stat(config.AgentTarball()).Return(mockFSInfo, nil) mockFSInfo.EXPECT().Size().Return(int64(1)).Times(2) d := &Downloader{ fs: mockFS, } if !d.IsAgentCached() { t.Error("Expected d.IsAgentCached() to be true") } }
// IsAgentCached returns true if there is a cached copy of the Agent present // and a cache state file is not empty (no validation is performed on the // tarball or cache state file contents) func (d *Downloader) IsAgentCached() bool { return d.fileNotEmpty(config.CacheState()) && d.fileNotEmpty(config.AgentTarball()) }
func (d *Downloader) RecordCachedAgent() error { data := []byte("1") return d.fs.WriteFile(config.CacheState(), data, orwPerm) }