This giant patch makes some much needed improvements to the code base. * The engine has been rewritten and lives within engine/graph/ * All of the common interfaces and code now live in engine/ * All of the resources are in one package called engine/resources/ * The Res API can use different "traits" from engine/traits/ * The Res API has been simplified to hide many of the old internals * The Watch & Process loops were previously inverted, but is now fixed * The likelihood of package cycles has been reduced drastically * And much, much more... Unfortunately, some code had to be temporarily removed. The remote code had to be taken out, as did the prometheus code. We hope to have these back in new forms as soon as possible.
147 lines
3.4 KiB
Go
147 lines
3.4 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/>.
|
|
|
|
package resources
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/gob"
|
|
"testing"
|
|
|
|
"github.com/purpleidea/mgmt/engine"
|
|
"github.com/purpleidea/mgmt/engine/graph/autoedge"
|
|
"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 := true
|
|
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 := util.ResToB64(input)
|
|
if err != nil {
|
|
t.Errorf("Can't encode: %v", err)
|
|
return
|
|
}
|
|
|
|
output, err := util.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)
|
|
}
|
|
}
|