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

     1  package chef
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  const cookbookListResponseFile = "test/cookbooks_response.json"
    13  const cookbookResponseFile = "test/cookbook.json"
    14  const _IssueUrl = "https://github.com/<insert_org_here>/apache/issues"
    15  const _Name = "apache"
    16  const _Maintainer = "The Authors"
    17  const _MaintainerEmail = "you@example.com"
    18  const _SourceUrl = "https://github.com/<insert_org_here>/apache"
    19  const _License = "All Rights Reserved"
    20  const _Version = "0.1.0"
    21  const _ChefVersion = ">= 15.0"
    22  const _Description = "Installs/Configures apache"
    23  
    24  var _Gems = [][]string{[]string{"foobar"}, []string{"aws-sdk-ec2", "~> 1.214.0"}}
    25  
    26  func TestGetVersion(t *testing.T) {
    27  	setup()
    28  	defer teardown()
    29  
    30  	cbookResp, err := os.ReadFile(cookbookResponseFile)
    31  	if err != nil {
    32  		t.Error(err)
    33  	}
    34  
    35  	mux.HandleFunc("/cookbooks/foo/_latest", func(w http.ResponseWriter, r *http.Request) {
    36  		fmt.Fprintf(w, string(cbookResp))
    37  	})
    38  
    39  	cookbook, err := client.Cookbooks.GetVersion("foo", "_latest")
    40  	assert.Nil(t, err)
    41  	if assert.NotNil(t, cookbook) {
    42  		assert.Equal(t, "foo", cookbook.CookbookName)
    43  		assert.Equal(t, "0.3.0", cookbook.Version)
    44  		assert.Equal(t, "foo-0.3.0", cookbook.Name)
    45  		assert.Equal(t, "cookbook_version", cookbook.ChefType)
    46  		assert.Equal(t, false, cookbook.Frozen)
    47  		assert.Equal(t, "Chef::CookbookVersion", cookbook.JsonClass)
    48  		assert.Equal(t, 0, len(cookbook.Files))
    49  		assert.Equal(t, 0, len(cookbook.Templates))
    50  		assert.Equal(t, 0, len(cookbook.Attributes))
    51  		assert.Equal(t, 0, len(cookbook.Definitions))
    52  		assert.Equal(t, 0, len(cookbook.Libraries))
    53  		assert.Equal(t, 0, len(cookbook.Providers))
    54  		assert.Equal(t, 0, len(cookbook.Resources))
    55  		// Assert Recipes (verify only one field)
    56  		assert.Equal(t, 1, len(cookbook.Recipes))
    57  		assert.Equal(t, "default.rb", cookbook.Recipes[0].Name)
    58  		assert.Equal(t, "recipes/default.rb", cookbook.Recipes[0].Path)
    59  		assert.Equal(t, "4e855dcab35b481ee56518db164b501d", cookbook.Recipes[0].Checksum)
    60  		assert.Equal(t, "default", cookbook.Recipes[0].Specificity)
    61  		// Check partial string just for convenience
    62  		assert.Contains(t, cookbook.Recipes[0].Url, "https://localhost:443/bookshelf/organization-")
    63  		// Assert RootFiles
    64  		assert.Equal(t, 8, len(cookbook.RootFiles))
    65  		// Assert CookbookMeta struct
    66  		assert.Equal(t, "foo", cookbook.Metadata.Name)
    67  		assert.Equal(t, "0.3.0", cookbook.Metadata.Version)
    68  		assert.Equal(t, "The Authors", cookbook.Metadata.Maintainer)
    69  		assert.Equal(t, "you@example.com", cookbook.Metadata.MaintainerEmail)
    70  		assert.Equal(t, "Installs/Configures foo", cookbook.Metadata.Description)
    71  		assert.Equal(t, "All Rights Reserved", cookbook.Metadata.License)
    72  		// Assert CookbookAccess struct
    73  		assert.Equal(t, true, cookbook.Access.Read)
    74  		assert.Equal(t, true, cookbook.Access.Create)
    75  		assert.Equal(t, true, cookbook.Access.Grant)
    76  		assert.Equal(t, true, cookbook.Access.Update)
    77  		assert.Equal(t, true, cookbook.Access.Delete)
    78  	}
    79  }
    80  
    81  func TestCookbookList(t *testing.T) {
    82  	setup()
    83  	defer teardown()
    84  
    85  	file, err := os.ReadFile(cookbookListResponseFile)
    86  	if err != nil {
    87  		t.Error(err)
    88  	}
    89  
    90  	mux.HandleFunc("/cookbooks", func(w http.ResponseWriter, r *http.Request) {
    91  		fmt.Fprintf(w, string(file))
    92  	})
    93  
    94  	data, err := client.Cookbooks.List()
    95  	if err != nil {
    96  		t.Error(err)
    97  	}
    98  
    99  	if data == nil {
   100  		t.Fatal("WTF we should have some data")
   101  	}
   102  	fmt.Println(data)
   103  
   104  	_, err = client.Cookbooks.ListAvailableVersions("3")
   105  	if err != nil {
   106  		t.Error(err)
   107  	}
   108  
   109  	_, err = client.Cookbooks.ListAvailableVersions("0")
   110  	if err != nil {
   111  		t.Error(err)
   112  	}
   113  }
   114  
   115  func TestCookbookListAvailableVersions_0(t *testing.T) {
   116  	setup()
   117  	defer teardown()
   118  
   119  	mux.HandleFunc("/cookbooks", func(w http.ResponseWriter, r *http.Request) {
   120  		http.Error(w, "BAD FUCKING REQUEST", 503)
   121  	})
   122  
   123  	_, err := client.Cookbooks.ListAvailableVersions("2")
   124  	if err == nil {
   125  		t.Error("We expected this bad request to error", err)
   126  	}
   127  }
   128  
   129  func TestCookBookDelete(t *testing.T) {
   130  	setup()
   131  	defer teardown()
   132  
   133  	mux.HandleFunc("/cookbooks/good/1.1.1", func(w http.ResponseWriter, r *http.Request) {
   134  		fmt.Fprintf(w, "")
   135  	})
   136  	mux.HandleFunc("/cookbooks/bad/1.1.1", func(w http.ResponseWriter, r *http.Request) {
   137  		http.Error(w, "Not Found", 404)
   138  	})
   139  
   140  	err := client.Cookbooks.Delete("bad", "1.1.1")
   141  	if err == nil {
   142  		t.Error("We expected this bad request to error", err)
   143  	}
   144  
   145  	err = client.Cookbooks.Delete("good", "1.1.1")
   146  	if err != nil {
   147  		t.Error(err)
   148  	}
   149  }
   150  
   151  func TestCookBookGet(t *testing.T) {
   152  	setup()
   153  	defer teardown()
   154  
   155  	cookbookVerionJSON := `{"url": "http://localhost:4000/cookbooks/apache2/5.1.0", "version": "5.1.0"}`
   156  	mux.HandleFunc("/cookbooks/good", func(w http.ResponseWriter, r *http.Request) {
   157  		fmt.Fprintf(w, cookbookVerionJSON)
   158  	})
   159  	mux.HandleFunc("/cookbooks/bad", func(w http.ResponseWriter, r *http.Request) {
   160  		http.Error(w, "Not Found", 404)
   161  	})
   162  
   163  	data, err := client.Cookbooks.Get("good")
   164  	if err != nil {
   165  		t.Error(err)
   166  	}
   167  
   168  	if data.Version != "5.1.0" {
   169  		t.Errorf("We expected '5.1.0' and got '%s'\n", data.Version)
   170  	}
   171  
   172  	_, err = client.Cookbooks.Get("bad")
   173  	if err == nil {
   174  		t.Error("We expected this bad request to error", err)
   175  	}
   176  }
   177  
   178  func TestCookBookGetAvailableVersions(t *testing.T) {
   179  	setup()
   180  	defer teardown()
   181  
   182  	cookbookVerionsJSON := `
   183  	{	"apache2": {
   184      "url": "http://localhost:4000/cookbooks/apache2",
   185      "versions": [
   186        {"url": "http://localhost:4000/cookbooks/apache2/5.1.0",
   187         "version": "5.1.0"},
   188        {"url": "http://localhost:4000/cookbooks/apache2/4.2.0",
   189         "version": "4.2.0"}
   190      ]
   191  	}}`
   192  
   193  	mux.HandleFunc("/cookbooks/good", func(w http.ResponseWriter, r *http.Request) {
   194  		fmt.Fprintf(w, cookbookVerionsJSON)
   195  	})
   196  	mux.HandleFunc("/cookbooks/bad", func(w http.ResponseWriter, r *http.Request) {
   197  		http.Error(w, "Not Found", 404)
   198  	})
   199  
   200  	data, err := client.Cookbooks.GetAvailableVersions("good", "3")
   201  	if err != nil {
   202  		t.Error(err)
   203  	}
   204  	fmt.Println(data)
   205  }
   206  
   207  func TestCookBookListAllRecipes(t *testing.T) {
   208  	setup()
   209  	defer teardown()
   210  
   211  	cookbookRecipesJSON := `
   212  	[
   213  	  "apache2",
   214  	  "apache2::mod_access_compat",
   215  	  "apache2::mod_actions",
   216  	  "apache2::mod_alias"
   217  	]`
   218  
   219  	mux.HandleFunc("/cookbooks/_recipes", func(w http.ResponseWriter, r *http.Request) {
   220  		fmt.Fprintf(w, cookbookRecipesJSON)
   221  	})
   222  
   223  	data, err := client.Cookbooks.ListAllRecipes()
   224  	if err != nil {
   225  		t.Error(err)
   226  	}
   227  	fmt.Println(data)
   228  }
   229  
   230  func TestNewCookbookMeta(t *testing.T) {
   231  	data := `   name 'apache'
   232  				maintainer 'The Authors'
   233  				maintainer_email 'you@example.com'
   234  				license 'All Rights Reserved'
   235  				description 'Installs/Configures apache'
   236  				version '0.1.0'
   237  				chef_version '>= 15.0'
   238  				
   239  				#issues_url points to the location where issues for this cookbook are
   240  				# tracked.  A View Issues link will be displayed on this cookbook's page when
   241  				# uploaded to a Supermarket.
   242  				#
   243  				issues_url 'https://github.com/<insert_org_here>/apache/issues'
   244  				
   245  				source_url 'https://github.com/<insert_org_here>/apache'`
   246  	md, err := NewMetaData(data)
   247  	if err != nil {
   248  		t.Error("invalid metadata.rb contain please validate it", err)
   249  	}
   250  	validateCookbookMetaData(md, t, "TestNewMetaData")
   251  
   252  }
   253  
   254  func TestNewCookbookMetaFromJson(t *testing.T) {
   255  	data := `{"name":"apache","description":"Installs/Configures apache","long_description":"","maintainer":"The Authors","maintainer_email":"you@example.com","license":"All Rights Reserved","platforms":{},"dependencies":{},"providing":null,"recipes":null,"version":"0.1.0","source_url":"https://github.com/\u003cinsert_org_here\u003e/apache","issues_url":"https://github.com/\u003cinsert_org_here\u003e/apache/issues","ChefVersion":"\u003e= 15.0","OhaiVersion":"","gems":null,"eager_load_libraries":false,"privacy":false}`
   256  	md, err := NewMetaDataFromJson([]byte(data))
   257  	if err != nil {
   258  		t.Error("invalid metadata.rb contain please validate it", err)
   259  	}
   260  	validateCookbookMetaData(md, t, "TestNewMetaDataFromJson")
   261  }
   262  func TestReadCookbookMeta(t *testing.T) {
   263  	file, err := os.Create("/tmp/metadata.rb")
   264  	if err != nil {
   265  		t.Error("unable to create to metadata.rb", err)
   266  	}
   267  	defer file.Close()
   268  	data := `   name 'apache'
   269  				maintainer 'The Authors'
   270  				maintainer_email 'you@example.com'
   271  				license 'All Rights Reserved'
   272  				description 'Installs/Configures apache'
   273  				version '0.1.0'
   274  				chef_version '>= 15.0'
   275  				
   276  				#issues_url points to the location where issues for this cookbook are
   277  				# tracked.  A View Issues link will be displayed on this cookbook's page when
   278  				# uploaded to a Supermarket.
   279  				#
   280  				issues_url 'https://github.com/<insert_org_here>/apache/issues'
   281  				
   282  				source_url 'https://github.com/<insert_org_here>/apache'`
   283  	_, err = file.WriteString(data)
   284  	if err != nil {
   285  		t.Error("error in creating tmp file for metadata.rb", err)
   286  	}
   287  	md, err := ReadMetaData("/tmp")
   288  	if err != nil {
   289  		t.Error("error in reading tmp file for metadata.rb", err)
   290  	}
   291  	validateCookbookMetaData(md, t, "TestReadMetaData")
   292  	os.Remove("/tmp/metadata.rb")
   293  
   294  }
   295  func TestReadCookbookMeta2(t *testing.T) {
   296  	file, err := os.Create("/tmp/metadata.json")
   297  	if err != nil {
   298  		t.Error("unable to create to metadata.rb", err)
   299  	}
   300  	defer file.Close()
   301  	data := `{"name":"apache","description":"Installs/Configures apache","long_description":"","maintainer":"The Authors","maintainer_email":"you@example.com","license":"All Rights Reserved","platforms":{},"dependencies":{},"providing":null,"recipes":null,"version":"0.1.0","source_url":"https://github.com/\u003cinsert_org_here\u003e/apache","issues_url":"https://github.com/\u003cinsert_org_here\u003e/apache/issues","ChefVersion":"\u003e= 15.0","OhaiVersion":"","gems":[["foobar"], ["aws-sdk-ec2", "~> 1.214.0"]],"eager_load_libraries":false,"privacy":false}`
   302  	_, err = file.WriteString(data)
   303  	if err != nil {
   304  		t.Error("error in creating tmp file for metadata.json", err)
   305  	}
   306  	md, err := ReadMetaData("/tmp")
   307  	if err != nil {
   308  		t.Error("error in reading tmp file for metadata.json", err)
   309  	}
   310  	validateCookbookMetaData(md, t, "TestReadMetaData")
   311  	validateCookbookGem(md, t, "TestReadMetaData")
   312  	os.Remove("/tmp/metadata.json")
   313  }
   314  func validateCookbookMetaData(md CookbookMeta, t *testing.T, funcName string) {
   315  	assert.Equal(t, _Description, md.Description)
   316  	assert.Equal(t, _IssueUrl, md.IssueUrl)
   317  	assert.Equal(t, _Name, md.Name)
   318  	assert.Equal(t, _Maintainer, md.Maintainer)
   319  	assert.Equal(t, _MaintainerEmail, md.MaintainerEmail)
   320  	assert.Equal(t, _SourceUrl, md.SourceUrl)
   321  	assert.Equal(t, _License, md.License)
   322  	assert.Equal(t, _Version, md.Version)
   323  	assert.Equal(t, _ChefVersion, md.ChefVersion)
   324  }
   325  
   326  func validateCookbookGem(md CookbookMeta, t *testing.T, funcName string) {
   327  	assert.Equal(t, _Gems, md.Gems)
   328  }