func TestCreateTimestampFile(t *testing.T) { realDir := filepath.Join(os.TempDir(), "util_test") util.Mkdir(realDir, 0755) defer util.RemoveAll(realDir) timestampFilePath := filepath.Join(realDir, TIMESTAMP_FILE_NAME) if err := CreateTimestampFile(realDir); err != nil { t.Errorf("Unexpected error: %s", err) } // Assert timestamp file exists. if _, err := os.Stat(timestampFilePath); err != nil { t.Errorf("Timestamp file %s was not created!", timestampFilePath) } // Assert timestamp file contains int64. fileContent, err := ioutil.ReadFile(timestampFilePath) if err != nil { t.Errorf("Unexpected error: %s", err) } if _, err := strconv.ParseInt(string(fileContent), 10, 64); err != nil { t.Errorf("Unexpected value in %s: %s", timestampFilePath, err) } // Assert error returned when specified dir does not exist. nonexistantDir := filepath.Join(os.TempDir(), "util_test_nonexistant") util.RemoveAll(nonexistantDir) if err := CreateTimestampFile(nonexistantDir); err != nil { // Expected error } else { t.Error("Unexpected lack of error") } }
func TestAreTimestampsEqual(t *testing.T) { gs, err := NewGsUtil(util.NewTimeoutClient()) if err != nil { t.Errorf("Unexpected error: %s", err) } tmpDir := filepath.Join(os.TempDir(), "util_test") util.Mkdir(tmpDir, 0777) defer util.RemoveAll(tmpDir) f, err := os.Create(filepath.Join(tmpDir, TIMESTAMP_FILE_NAME)) if err != nil { t.Errorf("Unexpected error: %s", err) } defer util.Close(f) // Test with matching timestamps. if _, err := f.WriteString(GS_TEST_TIMESTAMP_VALUE); err != nil { t.Errorf("Unexpected error: %s", err) } result1, err := gs.AreTimeStampsEqual(tmpDir, "unit-tests/util/") if err != nil { t.Errorf("Unexpected error: %s", err) } assert.True(t, result1) // Test with differing timestamps. if _, err := f.WriteString(GS_TEST_TIMESTAMP_VALUE); err != nil { t.Errorf("Unexpected error: %s", err) } result2, err := gs.AreTimeStampsEqual(tmpDir, "unit-tests/util/") if err != nil { t.Errorf("Unexpected error: %s", err) } assert.False(t, result2) // Test with Google Storage timestamp missing. result3, err := gs.AreTimeStampsEqual(tmpDir, "unit-tests/util/dummy_name/") if err == nil { t.Error("Expected an error") } assert.False(t, result3) // Test with local timestamp missing. result4, err := gs.AreTimeStampsEqual(tmpDir+"dummy_name", "unit-tests/util/") if err == nil { t.Error("Expected an error") } assert.False(t, result4) }