github.com/go-chef/chef@v0.30.1/search_test.go (about)

     1  package chef
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  const partialSearchResponseFile_1 = "test/partial_search_test_1.json"
    14  const partialSearchResponseFile_2 = "test/partial_search_test_2.json"
    15  
    16  func TestSearch_Get(t *testing.T) {
    17  	setup()
    18  	defer teardown()
    19  
    20  	mux.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
    21  		fmt.Fprintf(w, `{
    22  			"node": "http://localhost:4000/search/node", 
    23  			"role": "http://localhost:4000/search/role", 
    24  			"client": "http://localhost:4000/search/client", 
    25  			"users": "http://localhost:4000/search/users" 
    26  		}`)
    27  	})
    28  
    29  	indexes, err := client.Search.Indexes()
    30  	assert.Nil(t, err, "Search.Get returned error")
    31  	wantedIdx := map[string]string{
    32  		"node":   "http://localhost:4000/search/node",
    33  		"role":   "http://localhost:4000/search/role",
    34  		"client": "http://localhost:4000/search/client",
    35  		"users":  "http://localhost:4000/search/users",
    36  	}
    37  	assert.Equal(t, wantedIdx, indexes, "Search for indexes")
    38  }
    39  
    40  func TestSearch_ExecDo(t *testing.T) {
    41  	setup()
    42  	defer teardown()
    43  
    44  	mux.HandleFunc("/search/nodes", func(w http.ResponseWriter, r *http.Request) {
    45  		fmt.Fprintf(w, `{
    46  	    "total": 1,
    47  	    "start": 0,
    48  	    "rows": [
    49  	       {
    50  			"url": "path1",
    51  			"data": {
    52  	        "overrides": {"hardware_type": "laptop"},
    53  	        "name": "latte",
    54  	        "chef_type": "node",
    55  	        "json_class": "Chef::Node",
    56  	        "attributes": {"hardware_type": "laptop"},
    57  	        "run_list": ["recipe[unicorn]"],
    58  	        "defaults": {}
    59  			}
    60  	       }
    61  				 ]
    62  		}`)
    63  	})
    64  
    65  	// test the fail case
    66  	_, err := client.Search.NewQuery("foo", "failsauce")
    67  	assert.NotNil(t, err, "Bad query wasn't caught, NewQuery")
    68  
    69  	// test the fail case
    70  	_, err = client.Search.Exec("foo", "failsauce")
    71  	assert.NotNil(t, err, "Bad query wasn't caught, Exec")
    72  
    73  	// test the positive case
    74  	query, err := client.Search.NewQuery("nodes", "name:latte")
    75  	assert.Nil(t, err, "failed to create query")
    76  
    77  	// for now we aren't testing the result..
    78  	_, err = query.Do(client)
    79  	assert.Nil(t, err, "Search Do failed")
    80  
    81  	_, err = client.Search.Exec("nodes", "name:latte")
    82  	assert.Nil(t, err, "Search Exec failed")
    83  
    84  }
    85  
    86  func TestSearch_PartialExec(t *testing.T) {
    87  	setup()
    88  	defer teardown()
    89  
    90  	mux.HandleFunc("/search/node", func(w http.ResponseWriter, r *http.Request) {
    91  		fmt.Fprintf(w, `{
    92  			"total": 1,
    93  			"start": 0,
    94  			"rows": [
    95  			   {
    96  				"url": "path2",
    97  				"data": {
    98  				"overrides": {"hardware_type": "laptop"},
    99  				"name": "latte",
   100  				"chef_type": "node",
   101  				"json_class": "Chef::Node",
   102  				"policy_group": "testing",
   103  				"policy_name": "grafana",
   104  				"policy_revision": "123xyz00009999",
   105  				"attributes": {"hardware_type": "laptop"},
   106  				"run_list": ["recipe[unicorn]"],
   107  				"defaults": {}
   108  				}
   109  			   }
   110  					 ]
   111  			}`)
   112  	})
   113  
   114  	query := map[string]interface{}{
   115  		"name":            []string{"name"},
   116  		"policy_group":    []string{"policy_group"},
   117  		"policy_name":     []string{"policy_name"},
   118  		"policy_revision": []string{"policy_revision"},
   119  	}
   120  
   121  	pres, err := client.Search.PartialExecJSON("node", "*.*", query)
   122  	assert.Nil(t, err, "Search.PartialExecJSON failed")
   123  
   124  	assert.Len(t, pres.Rows, 1)
   125  	actualNode := Node{}
   126  	assert.NoError(t, json.Unmarshal(pres.Rows[0].Data, &actualNode))
   127  	assert.Equal(t, "grafana", actualNode.PolicyName)
   128  
   129  }
   130  
   131  func TestSearch_PartialExecMultipleCalls(t *testing.T) {
   132  	setup()
   133  	defer teardown()
   134  
   135  	searchResponseOne, err := os.ReadFile(partialSearchResponseFile_1)
   136  	assert.Nil(t, err, "Read response file 1 failed")
   137  
   138  	searchResponseTwo, err := os.ReadFile(partialSearchResponseFile_2)
   139  	assert.Nil(t, err, "Read response file 2 failed")
   140  
   141  	mux.HandleFunc("/search/node", func(w http.ResponseWriter, r *http.Request) {
   142  
   143  		start, ok := r.URL.Query()["start"]
   144  
   145  		if !ok || len(start[0]) < 1 {
   146  			fmt.Println("Url Param 'start' is missing")
   147  			return
   148  		}
   149  
   150  		if start[0] == "0" {
   151  			fmt.Fprintf(w, string(searchResponseOne))
   152  		} else {
   153  			fmt.Fprintf(w, string(searchResponseTwo))
   154  		}
   155  	})
   156  
   157  	query := map[string]interface{}{
   158  		"name":            []string{"name"},
   159  		"policy_group":    []string{"policy_group"},
   160  		"policy_name":     []string{"policy_name"},
   161  		"policy_revision": []string{"policy_revision"},
   162  	}
   163  
   164  	pres, err := client.Search.PartialExecJSON("node", "*.*", query)
   165  	assert.Nil(t, err, "Search.PartialExec failed")
   166  	assert.Len(t, pres.Rows, 12)
   167  
   168  	firstNode := Node{}
   169  	assert.NoError(t, json.Unmarshal(pres.Rows[0].Data, &firstNode))
   170  	assert.Equal(t, "node1", firstNode.Name)
   171  
   172  	lastNode := Node{}
   173  	assert.NoError(t, json.Unmarshal(pres.Rows[len(pres.Rows)-1].Data, &lastNode))
   174  	assert.Equal(t, "node12", lastNode.Name)
   175  
   176  }