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

     1  package chef
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"net/http"
     9  	"os"
    10  	"reflect"
    11  	"testing"
    12  )
    13  
    14  var (
    15  	testContainerJSON = "test/container.json"
    16  )
    17  
    18  func TestContainerFromJSONDecoder(t *testing.T) {
    19  	if file, err := os.Open(testContainerJSON); err != nil {
    20  		t.Error("unexpected error", err, "during os.Open on", testContainerJSON)
    21  	} else {
    22  		dec := json.NewDecoder(file)
    23  		var n Container
    24  		if err := dec.Decode(&n); err == io.EOF {
    25  			log.Println(n)
    26  		} else if err != nil {
    27  			log.Fatal(err)
    28  		}
    29  	}
    30  }
    31  
    32  func TestContainersService_Methods(t *testing.T) {
    33  	setup()
    34  	defer teardown()
    35  
    36  	mux.HandleFunc("/containers", func(w http.ResponseWriter, r *http.Request) {
    37  		switch {
    38  		case r.Method == "GET":
    39  			fmt.Fprintf(w, `{"container1":"https://chef/containers/container1", "container2":"https://chef/containers/container2"}`)
    40  		case r.Method == "POST":
    41  			fmt.Fprintf(w, `{ "uri": "http://localhost:4545/containers/container1" }`)
    42  		}
    43  	})
    44  
    45  	mux.HandleFunc("/containers/container1", func(w http.ResponseWriter, r *http.Request) {
    46  		switch {
    47  		case r.Method == "GET" || r.Method == "PUT":
    48  			fmt.Fprintf(w, `{
    49  	    "containername": "container1",
    50  	    "containerpath": "container1"
    51  		}`)
    52  		case r.Method == "DELETE":
    53  		}
    54  	})
    55  
    56  	// Test list
    57  	containers, err := client.Containers.List()
    58  	if err != nil {
    59  		t.Errorf("Containers.List returned error: %v", err)
    60  	}
    61  
    62  	listWant := ContainerListResult{"container1": "https://chef/containers/container1", "container2": "https://chef/containers/container2"}
    63  
    64  	if !reflect.DeepEqual(containers, listWant) {
    65  		t.Errorf("Containers.List returned %+v, want %+v", containers, listWant)
    66  	}
    67  
    68  	// test Get
    69  	container, err := client.Containers.Get("container1")
    70  	if err != nil {
    71  		t.Errorf("Containers.Get returned error: %v", err)
    72  	}
    73  
    74  	wantContainer := Container{
    75  		ContainerName: "container1",
    76  		ContainerPath: "container1",
    77  	}
    78  	if !reflect.DeepEqual(container, wantContainer) {
    79  		t.Errorf("Containers.Get returned %+v, want %+v", container, wantContainer)
    80  	}
    81  
    82  	// test Post
    83  	res, err := client.Containers.Create(wantContainer)
    84  	if err != nil {
    85  		t.Errorf("Containers.Post returned error: %s", err.Error())
    86  	}
    87  
    88  	postResult := &ContainerCreateResult{Uri: "http://localhost:4545/containers/container1"}
    89  	if !reflect.DeepEqual(postResult, res) {
    90  		t.Errorf("Containers.Post returned %+v, want %+v", res, postResult)
    91  	}
    92  
    93  	// test Delete
    94  	err = client.Containers.Delete(container.ContainerName)
    95  	if err != nil {
    96  		t.Errorf("Containers.Delete returned error %+v", err)
    97  	}
    98  }