コード例 #1
0
ファイル: writer_test.go プロジェクト: hopkings2008/collector
func testWriteToFile(t *testing.T, outMapMap map[string]map[string]interface{}, script, image, destDir, format string, suffix string) (b []byte) {
	fw := NewFileWriter(format, destDir)
	fw.WriteImageAllData(outMapMap)
	// Test if correct output file exists
	finalDir := destDir + "/" + trimExtension(script) + "/"
	blog.Debug("final dir: " + finalDir)
	var filenamePath string
	if ok, e := fsutil.DirExists(finalDir); ok {
		if len(image) > 12 {
			image = image[0:12]
		}
		file := image + suffix + "." + format
		filenamePath = finalDir + "/" + file
		_, err := os.Stat(filenamePath)
		if err != nil {
			if os.IsNotExist(err) {
				t.Fatal(err, ": File ", filenamePath, " doesn't exist")
			}
			t.Fatal(err, ": Unknown error while locating file: ", filenamePath)
		}
	} else {
		t.Fatal(e, ": Directory: ", finalDir, " doesn't exist")
	}

	b, err := ioutil.ReadFile(filenamePath)
	if err != nil {
		t.Fatal(err, ": Error in reading file: ", filenamePath)
	}
	return b
}
コード例 #2
0
ファイル: writer_test.go プロジェクト: hopkings2008/collector
func testWriteImageMDToFile(t *testing.T, imageMD []ImageMetadataInfo, destDir, format, action string) (b []byte) {
	// Append/Remove
	fw := NewFileWriter(format, destDir)
	switch action {
	case "ADD":
		fw.AppendImageMetadata(imageMD)
	case "REMOVE":
		fw.RemoveImageMetadata(imageMD)
	}

	// Check vailidity of output files
	var filenamePath string
	if ok, e := fsutil.DirExists(destDir); ok {
		file := "metadata." + format
		filenamePath = destDir + "/" + file
		_, err := os.Stat(filenamePath)
		if err != nil {
			if os.IsNotExist(err) {
				t.Fatal(err, ": File ", filenamePath, " doesn't exist")
			}
			t.Fatal(err, ": Unknown error while locating file: ", filenamePath)
		}
	} else {
		t.Fatal(e, ": Directory: ", destDir, " doesn't exist")
	}

	// Read output and return
	b, err := ioutil.ReadFile(filenamePath)
	if err != nil {
		t.Fatal(err, ": Error in reading file: ", filenamePath)
	}

	return b
}