mirror of https://bitbucket.org/ausocean/av.git
160 lines
4.0 KiB
Go
160 lines
4.0 KiB
Go
// +build test
|
|
|
|
/*
|
|
DESCRIPTION
|
|
revid_test.go provides integration testing of the revid API.
|
|
|
|
AUTHORS
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
LICENSE
|
|
Copyright (C) 2017-2021 the Australian Ocean Lab (AusOcean)
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
under the terms of the GNU General Public License as published by the
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
option) any later version.
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
*/
|
|
|
|
package revid
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"bitbucket.org/ausocean/av/container/mts"
|
|
"bitbucket.org/ausocean/av/container/mts/meta"
|
|
"bitbucket.org/ausocean/av/revid/config"
|
|
)
|
|
|
|
func TestRaspistill(t *testing.T) {
|
|
// Copyright information prefixed to all metadata.
|
|
const (
|
|
metaPreambleKey = "copyright"
|
|
metaPreambleData = "ausocean.org/license/content2021"
|
|
)
|
|
|
|
// Configuration parameters.
|
|
const (
|
|
timelapseInterval = "4"
|
|
timelapseDuration = "25"
|
|
poolStartElementSize = "1000000"
|
|
input = "Raspistill"
|
|
codec = "JPEG"
|
|
output = "Files"
|
|
outDir = "out"
|
|
outputPath = outDir + "/"
|
|
logging = "Debug"
|
|
testImgDir = "../../../test/test-data/av/input/jpeg/"
|
|
)
|
|
|
|
const runTime = 40 * time.Second
|
|
|
|
// Add the copyright metadata.
|
|
mts.Meta = meta.NewWith([][2]string{{metaPreambleKey, metaPreambleData}})
|
|
|
|
// First remove out dir (if exists) to clear contents, then create dir.
|
|
err := os.RemoveAll(outDir)
|
|
if err != nil {
|
|
t.Fatalf("could not remove any prior out directory: %v", err)
|
|
}
|
|
|
|
os.Mkdir(outDir, os.ModePerm)
|
|
if err != nil {
|
|
t.Fatalf("could not create new out directory: %v", err)
|
|
}
|
|
|
|
rv, err := New(config.Config{Logger: (*testLogger)(t)}, nil)
|
|
if err != nil {
|
|
t.Fatalf("did not expect error from revid.New(): %v", err)
|
|
}
|
|
|
|
err = rv.Update(
|
|
map[string]string{
|
|
config.KeyInput: input,
|
|
config.KeyInputCodec: codec,
|
|
config.KeyOutput: output,
|
|
config.KeyOutputPath: outputPath,
|
|
config.KeyTimelapseInterval: timelapseInterval,
|
|
config.KeyTimelapseDuration: timelapseDuration,
|
|
config.KeyLogging: logging,
|
|
config.KeyPoolStartElementSize: poolStartElementSize,
|
|
},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("did not expect error from rv.Update(): %v", err)
|
|
}
|
|
|
|
err = rv.Start()
|
|
if err != nil {
|
|
t.Fatalf("did not expect error from rv.Start(): %v", err)
|
|
}
|
|
time.Sleep(runTime)
|
|
rv.Stop()
|
|
|
|
// Get output file information.
|
|
os.Chdir(outDir)
|
|
var files []string
|
|
err = filepath.Walk(
|
|
".",
|
|
func(path string, info os.FileInfo, err error) error {
|
|
files = append(files, path)
|
|
return nil
|
|
},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("did not expect error from filepath.Walk(): %v", err)
|
|
}
|
|
|
|
if len(files) == 0 {
|
|
t.Fatalf("did not expect 0 output files")
|
|
}
|
|
|
|
// Load files outputted files and compare each one with corresponding input.
|
|
for i, n := range files {
|
|
// Ignore first file (which is prev dir ".").
|
|
if i == 0 {
|
|
continue
|
|
}
|
|
|
|
mtsBytes, err := ioutil.ReadFile(n)
|
|
if err != nil {
|
|
t.Fatalf("could not read output file: %v", err)
|
|
}
|
|
|
|
clip, err := mts.Extract(mtsBytes)
|
|
if err != nil {
|
|
t.Fatalf("could not extract clips from MPEG-TS stream: %v", err)
|
|
}
|
|
img := clip.Bytes()
|
|
|
|
inImg, err := ioutil.ReadFile(testImgDir + strconv.Itoa(i) + ".jpg")
|
|
if err != nil {
|
|
t.Fatalf("could not load input test image: %v", err)
|
|
}
|
|
|
|
if !bytes.Equal(img, inImg) {
|
|
t.Errorf("unexpected image extracted for img: %d", i)
|
|
}
|
|
}
|
|
|
|
// Clean up out directory.
|
|
err = os.RemoveAll(outDir)
|
|
if err != nil {
|
|
t.Fatalf("could not clean up out directory: %v", err)
|
|
}
|
|
}
|