2015-08-27 00:07:31 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
// This file contains helper methods for testing fingerprinters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-01-24 14:09:53 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2015-08-27 00:07:31 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func testLogger() *log.Logger {
|
|
|
|
return log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
func assertFingerprintOK(t *testing.T, fp Fingerprint, node *structs.Node) *cstructs.FingerprintResponse {
|
|
|
|
request := &cstructs.FingerprintRequest{Config: new(config.Config), Node: node}
|
2018-01-26 16:21:07 +00:00
|
|
|
var response cstructs.FingerprintResponse
|
|
|
|
err := fp.Fingerprint(request, &response)
|
2015-08-27 00:07:31 +00:00
|
|
|
if err != nil {
|
2015-08-27 00:16:34 +00:00
|
|
|
t.Fatalf("Failed to fingerprint: %s", err)
|
2015-08-27 00:07:31 +00:00
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
2018-01-30 17:57:37 +00:00
|
|
|
if len(response.Attributes) == 0 {
|
2015-08-27 00:16:34 +00:00
|
|
|
t.Fatalf("Failed to apply node attributes")
|
2015-08-27 00:07:31 +00:00
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
return &response
|
2015-08-27 00:07:31 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
func assertNodeAttributeContains(t *testing.T, nodeAttributes map[string]string, attribute string) {
|
2018-01-30 17:57:37 +00:00
|
|
|
if nodeAttributes == nil {
|
|
|
|
t.Errorf("expected an initialized map for node attributes")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
actual, found := nodeAttributes[attribute]
|
2015-08-27 00:07:31 +00:00
|
|
|
if !found {
|
2018-01-24 14:09:53 +00:00
|
|
|
t.Errorf("Expected to find Attribute `%s`\n\n[DEBUG] %#v", attribute, nodeAttributes)
|
2015-08-27 00:07:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if actual == "" {
|
2018-01-24 14:09:53 +00:00
|
|
|
t.Errorf("Expected non-empty Attribute value for `%s`\n\n[DEBUG] %#v", attribute, nodeAttributes)
|
2015-08-27 00:07:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
func assertNodeAttributeEquals(t *testing.T, nodeAttributes map[string]string, attribute string, expected string) {
|
2018-01-30 17:57:37 +00:00
|
|
|
if nodeAttributes == nil {
|
|
|
|
t.Errorf("expected an initialized map for node attributes")
|
|
|
|
return
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
actual, found := nodeAttributes[attribute]
|
2015-08-27 00:07:31 +00:00
|
|
|
if !found {
|
2018-01-24 14:09:53 +00:00
|
|
|
t.Errorf("Expected to find Attribute `%s`; unable to check value\n\n[DEBUG] %#v", attribute, nodeAttributes)
|
2015-08-27 00:07:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if expected != actual {
|
2018-01-24 14:09:53 +00:00
|
|
|
t.Errorf("Expected `%s` Attribute to be `%s`, found `%s`\n\n[DEBUG] %#v", attribute, expected, actual, nodeAttributes)
|
2015-08-31 19:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
func assertNodeLinksContains(t *testing.T, nodeLinks map[string]string, link string) {
|
2018-01-30 17:57:37 +00:00
|
|
|
if nodeLinks == nil {
|
|
|
|
t.Errorf("expected an initialized map for node links")
|
|
|
|
return
|
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
actual, found := nodeLinks[link]
|
2015-08-31 19:18:40 +00:00
|
|
|
if !found {
|
2018-01-24 14:09:53 +00:00
|
|
|
t.Errorf("Expected to find Link `%s`\n\n[DEBUG]", link)
|
2015-08-31 19:18:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if actual == "" {
|
2018-01-24 14:09:53 +00:00
|
|
|
t.Errorf("Expected non-empty Link value for `%s`\n\n[DEBUG]", link)
|
2015-08-27 00:07:31 +00:00
|
|
|
}
|
|
|
|
}
|