File: search_test.go

package info (click to toggle)
golang-github-go-chef-chef 0.0.1%2Bgit20161023.60.deb8c38-1.2~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 372 kB
  • sloc: sh: 14; makefile: 7
file content (89 lines) | stat: -rw-r--r-- 2,073 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package chef

import (
	"fmt"
	"net/http"
	"reflect"
	"testing"
)

func TestSearch_Get(t *testing.T) {
	setup()
	defer teardown()

	mux.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, `{
			"node": "http://localhost:4000/search/node", 
			"role": "http://localhost:4000/search/role", 
			"client": "http://localhost:4000/search/client", 
			"users": "http://localhost:4000/search/users" 
		}`)
	})

	indexes, err := client.Search.Indexes()
	if err != nil {
		t.Errorf("Search.Get returned error: %+v", err)
	}
	wantedIdx := map[string]string{
		"node":   "http://localhost:4000/search/node",
		"role":   "http://localhost:4000/search/role",
		"client": "http://localhost:4000/search/client",
		"users":  "http://localhost:4000/search/users",
	}
	if !reflect.DeepEqual(indexes, wantedIdx) {
		t.Errorf("Search.Get returned %+v, want %+v", indexes, wantedIdx)
	}
}

func TestSearch_ExecDo(t *testing.T) {
	setup()
	defer teardown()

	mux.HandleFunc("/search/nodes", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, `{
	    "total": 1,
	    "start": 0,
	    "rows": [
	       {
	        "overrides": {"hardware_type": "laptop"},
	        "name": "latte",
	        "chef_type": "node",
	        "json_class": "Chef::Node",
	        "attributes": {"hardware_type": "laptop"},
	        "run_list": ["recipe[unicorn]"],
	        "defaults": {}
	       }
				 ]
		}`)
	})

	// test the fail case
	_, err := client.Search.NewQuery("foo", "failsauce")
	if err == nil {
		t.Errorf("Bad query wasn't caught")
	}

	// test the fail case
	_, err = client.Search.Exec("foo", "failsauce")
	if err == nil {
		t.Errorf("Bad query wasn't caught")
	}

	// test the positive case
	query, err := client.Search.NewQuery("nodes", "name:latte")
	if err != nil {
		t.Errorf("failed to create query")
	}

	// for now we aren't testing the result..
	_, err = query.Do(client)
	if err != nil {
		t.Errorf("Search.Exec failed", err)
	}

	_, err = client.Search.Exec("nodes", "name:latte")
	if err != nil {
		t.Errorf("Search.Exec failed", err)
	}

}