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

     1  package chef
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"net/http"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func TestUniverseGet(t *testing.T) {
    12  	setup()
    13  	defer teardown()
    14  
    15  	mux.HandleFunc("/universe", func(w http.ResponseWriter, r *http.Request) {
    16  		dec := json.NewDecoder(r.Body)
    17  		var request Universe
    18  		dec.Decode(&request)
    19  		switch {
    20  		case r.Method == "GET":
    21  			fmt.Fprintf(w, `{
    22  			"ffmpeg": {
    23  				"0.1.0": {
    24  					"location_path": "http://supermarket.chef.io/api/v1/cookbooks/ffmpeg/0.1.0/download",
    25  					"location_type": "supermarket",
    26  					"dependencies": {
    27  						"git": ">= 0.0.0",
    28  						"build-essential": ">= 0.0.0",
    29  						"libvpx": "~> 0.1.1",
    30  						"x264": "~> 0.1.1"
    31  					}
    32  				},
    33  				"0.1.1": {
    34  					"location_path": "http://supermarket.chef.io/api/v1/cookbooks/ffmpeg/0.1.1/download",
    35  					"location_type": "supermarket",
    36  					"dependencies": {
    37  						"git": ">= 0.0.0",
    38  						"build-essential": ">= 0.0.0",
    39  						"libvpx": "~> 0.1.1",
    40  						"x264": "~> 0.1.1"
    41  					}
    42  				}
    43  			},
    44  			"pssh": {
    45  				"0.1.0": {
    46  					"location_path": "http://supermarket.chef.io/api/v1/cookbooks/pssh.1.0/download",
    47  					"location_type": "supermarket",
    48  					"dependencies": {}
    49  				}
    50  			}
    51  		        }`)
    52  		}
    53  	})
    54  
    55  	wantU := Universe{}
    56  	wantU.Books = make(map[string]UniverseBook)
    57  	ffmpeg := UniverseBook{}
    58  	ffmpeg.Versions = make(map[string]UniverseVersion)
    59  	ffmpeg.Versions["0.1.0"] = UniverseVersion{
    60  		LocationPath: "http://supermarket.chef.io/api/v1/cookbooks/ffmpeg/0.1.0/download",
    61  		LocationType: "supermarket",
    62  		Dependencies: map[string]string{
    63  			"git":             ">= 0.0.0",
    64  			"build-essential": ">= 0.0.0",
    65  			"libvpx":          "~> 0.1.1",
    66  			"x264":            "~> 0.1.1",
    67  		},
    68  	}
    69  	ffmpeg.Versions["0.1.1"] = UniverseVersion{
    70  		LocationPath: "http://supermarket.chef.io/api/v1/cookbooks/ffmpeg/0.1.1/download",
    71  		LocationType: "supermarket",
    72  		Dependencies: map[string]string{
    73  			"git":             ">= 0.0.0",
    74  			"build-essential": ">= 0.0.0",
    75  			"libvpx":          "~> 0.1.1",
    76  			"x264":            "~> 0.1.1",
    77  		},
    78  	}
    79  	pssh := UniverseBook{}
    80  	pssh.Versions = make(map[string]UniverseVersion)
    81  	pssh.Versions["0.1.0"] = UniverseVersion{
    82  		LocationPath: "http://supermarket.chef.io/api/v1/cookbooks/pssh.1.0/download",
    83  		LocationType: "supermarket",
    84  		Dependencies: map[string]string{},
    85  	}
    86  	wantU.Books["ffmpeg"] = ffmpeg
    87  	wantU.Books["pssh"] = pssh
    88  
    89  	universe, err := client.Universe.Get()
    90  	if err != nil {
    91  		t.Errorf("Universe.Get returned error: %s", err.Error())
    92  	}
    93  
    94  	if !reflect.DeepEqual(universe, wantU) {
    95  		t.Errorf("Universe.Get returned %+v, want %+v", universe, wantU)
    96  	}
    97  }