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

     1  package chef
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestCBADownloadThatDoesNotExist(t *testing.T) {
    14  	setup()
    15  	defer teardown()
    16  
    17  	mux.HandleFunc("/cookbook_artifacts/seven_zip/xyz", func(w http.ResponseWriter, r *http.Request) {
    18  		http.Error(w, "Not Found", 404)
    19  	})
    20  
    21  	err := client.CookbookArtifacts.DownloadTo("seven_zip", "xyz", "")
    22  	if assert.NotNil(t, err) {
    23  		assert.Contains(t, err.Error(), "404")
    24  	}
    25  }
    26  
    27  func TestCBADownloadTo(t *testing.T) {
    28  	setup()
    29  	defer teardown()
    30  
    31  	mockedCBAResponseFile := cbaData()
    32  	tempDir, err := os.MkdirTemp("", "seven_zip-cookbook")
    33  	if err != nil {
    34  		t.Error(err)
    35  	}
    36  	defer os.RemoveAll(tempDir) // clean up
    37  
    38  	mux.HandleFunc("/cookbook_artifacts/seven_zip/0e1fed3b56aa5e84205e330d92aca22d8704a014", func(w http.ResponseWriter, r *http.Request) {
    39  		fmt.Fprintf(w, string(mockedCBAResponseFile))
    40  	})
    41  	mux.HandleFunc("/bookshelf/seven_zip/metadata_rb", func(w http.ResponseWriter, r *http.Request) {
    42  		fmt.Fprintf(w, "name 'foo'")
    43  	})
    44  	mux.HandleFunc("/bookshelf/seven_zip/default_rb", func(w http.ResponseWriter, r *http.Request) {
    45  		fmt.Fprintf(w, "log 'this is a resource'")
    46  	})
    47  
    48  	err = client.CookbookArtifacts.DownloadTo("seven_zip", "0e1fed3b56aa5e84205e330d92aca22d8704a014", tempDir)
    49  	assert.Nil(t, err)
    50  
    51  	var (
    52  		cookbookPath = path.Join(tempDir, "seven_zip-0e1fed3b56aa5e84205e")
    53  		metadataPath = path.Join(cookbookPath, "metadata.rb")
    54  		recipesPath  = path.Join(cookbookPath, "recipes")
    55  		defaultPath  = path.Join(recipesPath, "default.rb")
    56  	)
    57  	assert.DirExistsf(t, cookbookPath, "the cookbook directory should exist")
    58  	assert.DirExistsf(t, recipesPath, "the recipes directory should exist")
    59  	if assert.FileExistsf(t, metadataPath, "a metadata.rb file should exist") {
    60  		metadataBytes, err := os.ReadFile(metadataPath)
    61  		assert.Nil(t, err)
    62  		assert.Equal(t, "name 'foo'", string(metadataBytes))
    63  	}
    64  	if assert.FileExistsf(t, defaultPath, "the default.rb recipes should exist") {
    65  		recipeBytes, err := os.ReadFile(defaultPath)
    66  		assert.Nil(t, err)
    67  		assert.Equal(t, "log 'this is a resource'", string(recipeBytes))
    68  	}
    69  
    70  }
    71  
    72  func cbaData() string {
    73  	return `
    74  
    75  {
    76  	"version": "3.1.2",
    77  	"name": "seven_zip",
    78  	"identifier": "0e1fed3b56aa5e84205e330d92aca22d8704a014",
    79  	"frozen?": false,
    80    	"chef_type": "cookbook_version",
    81  	"root_files": [
    82        {
    83  		"name": "metadata.rb",
    84  		"path": "metadata.rb",
    85  		"checksum": "6607f3131919e82dc4ba4c026fcfee9f",
    86  		"specificity": "default",
    87  		"url": "` + server.URL + `/bookshelf/seven_zip/metadata_rb"
    88  	  }
    89  	],
    90    	"attributes": [],
    91    	"definitions": [],
    92    	"files": [],
    93    	"libraries": [],
    94  	"providers": [],
    95  	"recipes": [
    96        {
    97        	"name": "default.rb",
    98        	"path": "recipes/default.rb",
    99        	"checksum": "8e751ed8663cb9b97499956b6a20b0de",
   100        	"specificity": "default",
   101        	"url": "` + server.URL + `/bookshelf/seven_zip/default_rb"
   102        }
   103    	],
   104     "resources": [],
   105     "templates": [],
   106     "metadata": {},
   107     "access": {}
   108  } `
   109  }