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

     1  package chef
     2  
     3  import (
     4  	"crypto/md5"
     5  	"crypto/rand"
     6  	"fmt"
     7  	"github.com/stretchr/testify/assert"
     8  	"net/http"
     9  	_ "reflect"
    10  	"testing"
    11  )
    12  
    13  // generate random data for sandbox
    14  func random_data(size int) (b []byte) {
    15  	b = make([]byte, size)
    16  	rand.Read(b)
    17  	return
    18  }
    19  
    20  // mux.HandleFunc("/sandboxes/f1c560ccb472448e9cfb31ff98134247", func(w http.ResponseWriter, r *http.Request) { })
    21  func TestSandboxesPost(t *testing.T) {
    22  	setup()
    23  	defer teardown()
    24  
    25  	mux.HandleFunc("/sandboxes", func(w http.ResponseWriter, r *http.Request) {
    26  		fmt.Fprintf(w, `{
    27  			"sandbox_id": "f1c560ccb472448e9cfb31ff98134247",
    28  			"uri": "http://trendy.local:4545/sandboxes/f1c560ccb472448e9cfb31ff98134247",
    29  			"Checksums": {
    30  					"4bd9946774fff1fb53745c645e447c9d20a14cac410b9eea037299247e70aa1e": {
    31  							"url": "http://trendy.local:4545/file_store/4bd9946774fff1fb53745c645e447c9d20a14cac410b9eea037299247e70aa1e",
    32  							"needs_upload": true
    33  					},
    34  					"548c6928e3f5a800a0e9cc146647a31a2353c42950a611cfca646819cdaa54fa": {
    35  							"url": "http://trendy.local:4545/file_store/548c6928e3f5a800a0e9cc146647a31a2353c42950a611cfca646819cdaa54fa",
    36  							"needs_upload": true
    37  			}}}`)
    38  	})
    39  
    40  	// create junk files and sums
    41  	files := make(map[string][]byte)
    42  	// slice of strings for holding our hashes
    43  	sums := make([]string, 10)
    44  	for i := 0; i <= 10; i++ {
    45  		data := random_data(1024)
    46  		hashstr := fmt.Sprintf("%x", md5.Sum(data))
    47  		files[hashstr] = data
    48  		sums = append(sums, hashstr)
    49  	}
    50  
    51  	// post the new sums/files to the sandbox
    52  	_, err := client.Sandboxes.Post(sums)
    53  	if err != nil {
    54  		t.Errorf("Sandbox Post error making request: %+v", err)
    55  	}
    56  }
    57  
    58  func TestSandboxesPut(t *testing.T) {
    59  	setup()
    60  	defer teardown()
    61  
    62  	mux.HandleFunc("/sandboxes/f1c560ccb472448e9cfb31ff98134247", func(w http.ResponseWriter, r *http.Request) {
    63  		fmt.Fprintf(w, `{
    64  			"guid": "123",
    65  			"name": "123",
    66  			"CreateionTime": "2014-08-23 18:13:37",
    67  			"is_completed": true,
    68  			"uri": "https://127.0.0.1/sandboxes/f1c560ccb472448e9cfb31ff98134247",
    69  	  	"checksums": [
    70  				"3124216defe5849089a577ffefb0bb05",
    71  				"e06ddfa07ca97c80c368d08e189b928a",
    72  				"eb65444f8adeb11c56cf0df201a07cb4"
    73  			]
    74  		}`)
    75  	})
    76  
    77  	sandbox, err := client.Sandboxes.Put("f1c560ccb472448e9cfb31ff98134247")
    78  	if err != nil {
    79  		t.Errorf("Sandbox Put error making request: %+v", err)
    80  	}
    81  
    82  	expected := Sandbox{
    83  		ID:        "123",
    84  		Name:      "123",
    85  		Completed: true,
    86  		Checksums: []string{
    87  			"3124216defe5849089a577ffefb0bb05",
    88  			"e06ddfa07ca97c80c368d08e189b928a",
    89  			"eb65444f8adeb11c56cf0df201a07cb4",
    90  		},
    91  	}
    92  
    93  	assert.Equal(t, expected, sandbox, "Sandbox contents")
    94  	// Convey("Sandbox Equality", t, func() {
    95  	// So(sandbox, ShouldResemble, expected)
    96  	// })
    97  }