This is a requirement of our file resource, so we should validate this and clearly express it in the documentation.
165 lines
3.9 KiB
Go
165 lines
3.9 KiB
Go
// Mgmt
|
|
// Copyright (C) 2013-2018+ James Shubin and the project contributors
|
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it 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.
|
|
//
|
|
// This program 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
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// +build !root
|
|
|
|
package resources
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/gob"
|
|
"testing"
|
|
|
|
"github.com/purpleidea/mgmt/engine"
|
|
"github.com/purpleidea/mgmt/engine/graph/autoedge"
|
|
engineUtil "github.com/purpleidea/mgmt/engine/util"
|
|
"github.com/purpleidea/mgmt/pgraph"
|
|
)
|
|
|
|
func TestFileAutoEdge1(t *testing.T) {
|
|
|
|
g, err := pgraph.NewGraph("TestGraph")
|
|
if err != nil {
|
|
t.Errorf("error creating graph: %v", err)
|
|
return
|
|
}
|
|
|
|
r1 := &FileRes{
|
|
Path: "/tmp/a/b/", // some dir
|
|
}
|
|
r2 := &FileRes{
|
|
Path: "/tmp/a/", // some parent dir
|
|
}
|
|
r3 := &FileRes{
|
|
Path: "/tmp/a/b/c", // some child file
|
|
}
|
|
g.AddVertex(r1, r2, r3)
|
|
|
|
if i := g.NumEdges(); i != 0 {
|
|
t.Errorf("should have 0 edges instead of: %d", i)
|
|
}
|
|
|
|
debug := testing.Verbose() // set via the -test.v flag to `go test`
|
|
logf := func(format string, v ...interface{}) {
|
|
t.Logf("test: "+format, v...)
|
|
}
|
|
// run artificially without the entire engine
|
|
if err := autoedge.AutoEdge(g, debug, logf); err != nil {
|
|
t.Errorf("error running autoedges: %v", err)
|
|
}
|
|
|
|
// two edges should have been added
|
|
if i := g.NumEdges(); i != 2 {
|
|
t.Errorf("should have 2 edges instead of: %d", i)
|
|
}
|
|
}
|
|
|
|
func TestMiscEncodeDecode1(t *testing.T) {
|
|
var err error
|
|
|
|
// encode
|
|
var input interface{} = &FileRes{}
|
|
b1 := bytes.Buffer{}
|
|
e := gob.NewEncoder(&b1)
|
|
err = e.Encode(&input) // pass with &
|
|
if err != nil {
|
|
t.Errorf("Gob failed to Encode: %v", err)
|
|
}
|
|
str := base64.StdEncoding.EncodeToString(b1.Bytes())
|
|
|
|
// decode
|
|
var output interface{}
|
|
bb, err := base64.StdEncoding.DecodeString(str)
|
|
if err != nil {
|
|
t.Errorf("Base64 failed to Decode: %v", err)
|
|
}
|
|
b2 := bytes.NewBuffer(bb)
|
|
d := gob.NewDecoder(b2)
|
|
err = d.Decode(&output) // pass with &
|
|
if err != nil {
|
|
t.Errorf("Gob failed to Decode: %v", err)
|
|
}
|
|
|
|
res1, ok := input.(engine.Res)
|
|
if !ok {
|
|
t.Errorf("Input %v is not a Res", res1)
|
|
return
|
|
}
|
|
res2, ok := output.(engine.Res)
|
|
if !ok {
|
|
t.Errorf("Output %v is not a Res", res2)
|
|
return
|
|
}
|
|
if err := res1.Cmp(res2); err != nil {
|
|
t.Errorf("The input and output Res values do not match: %+v", err)
|
|
}
|
|
}
|
|
|
|
func TestMiscEncodeDecode2(t *testing.T) {
|
|
var err error
|
|
|
|
// encode
|
|
input, err := engine.NewNamedResource("file", "file1")
|
|
if err != nil {
|
|
t.Errorf("Can't create: %v", err)
|
|
return
|
|
}
|
|
|
|
b64, err := engineUtil.ResToB64(input)
|
|
if err != nil {
|
|
t.Errorf("Can't encode: %v", err)
|
|
return
|
|
}
|
|
|
|
output, err := engineUtil.B64ToRes(b64)
|
|
if err != nil {
|
|
t.Errorf("Can't decode: %v", err)
|
|
return
|
|
}
|
|
|
|
res1, ok := input.(engine.Res)
|
|
if !ok {
|
|
t.Errorf("Input %v is not a Res", res1)
|
|
return
|
|
}
|
|
res2, ok := output.(engine.Res)
|
|
if !ok {
|
|
t.Errorf("Output %v is not a Res", res2)
|
|
return
|
|
}
|
|
if err := res1.Cmp(res2); err != nil {
|
|
t.Errorf("The input and output Res values do not match: %+v", err)
|
|
}
|
|
}
|
|
|
|
func TestFileAbsolute1(t *testing.T) {
|
|
// file resource paths should be absolute
|
|
f1 := &FileRes{
|
|
Path: "tmp/a/b", // some relative file
|
|
}
|
|
f2 := &FileRes{
|
|
Path: "tmp/a/b/", // some relative dir
|
|
}
|
|
f3 := &FileRes{
|
|
Path: "tmp", // some short relative file
|
|
}
|
|
if f1.Validate() == nil || f2.Validate() == nil || f3.Validate() == nil {
|
|
t.Errorf("file res should have failed validate")
|
|
}
|
|
}
|