github.com/go-chef/chef@v0.30.1/cookbook_artifacts_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 CBAListResponseFile = "test/cookbook_artifacts_list_response.json"
    13  const CBAGetResponseFile = "test/cookbook_artifacts_get_response.json"
    14  const CBAGetVersionResponseFile = "test/cookbook_artifacts_getversion_response.json"
    15  
    16  func TestListCBA(t *testing.T) {
    17  	setup()
    18  	defer teardown()
    19  
    20  	file, err := os.ReadFile(CBAListResponseFile)
    21  	if err != nil {
    22  		t.Error(err)
    23  	}
    24  
    25  	mux.HandleFunc("/cookbook_artifacts", func(w http.ResponseWriter, r *http.Request) {
    26  		fmt.Fprintf(w, string(file))
    27  	})
    28  
    29  	data, err := client.CookbookArtifacts.List()
    30  	if err != nil {
    31  		t.Error(err)
    32  	}
    33  
    34  	if data == nil {
    35  		t.Fatal("We should have some data")
    36  	}
    37  
    38  	if len(data) != 2 {
    39  		t.Error("Mismatch in expected cookbook artifact count. Expected 2, Got: ", len(data))
    40  	}
    41  
    42  	if _, ok := data["oc-hec-postfix"]; !ok {
    43  		t.Error("oc-hec-postfix cookbook artifact should be listed")
    44  	}
    45  
    46  	if _, ok := data["grafana"]; !ok {
    47  		t.Error("grafana cookbook artifact should be listed")
    48  	}
    49  
    50  }
    51  
    52  func TestGetCBA(t *testing.T) {
    53  	setup()
    54  	defer teardown()
    55  
    56  	file, err := os.ReadFile(CBAGetResponseFile)
    57  	if err != nil {
    58  		t.Error(err)
    59  	}
    60  
    61  	mux.HandleFunc("/cookbook_artifacts/seven_zip", func(w http.ResponseWriter, r *http.Request) {
    62  		fmt.Fprintf(w, string(file))
    63  	})
    64  	mux.HandleFunc("/cookbook_artifacts/bad", func(w http.ResponseWriter, r *http.Request) {
    65  		http.Error(w, "Not Found", 404)
    66  	})
    67  
    68  	data, err := client.CookbookArtifacts.Get("seven_zip")
    69  	if err != nil {
    70  		t.Error(err)
    71  	}
    72  
    73  	if data["seven_zip"].Url != "https://localhost/organizations/utility/cookbook_artifacts/seven_zip" {
    74  		t.Errorf("URL mismatch, expected '%s', got '%s'\n", "https://api.chef.io/organizations/chef-utility/cookbook_artifacts/seven_zip", data["seven_zip"].Url)
    75  	}
    76  
    77  	if len(data["seven_zip"].CBAVersions) != 7 {
    78  		t.Errorf("Expected 7 versions of this cookbook artifact, received %d", len(data["seven_zip"].CBAVersions))
    79  	}
    80  	_, err = client.CookbookArtifacts.Get("bad")
    81  	if err == nil {
    82  		t.Error("We expected this bad request to error", err)
    83  	}
    84  }
    85  
    86  func TestGetVersionCBA(t *testing.T) {
    87  	setup()
    88  	defer teardown()
    89  
    90  	file, err := os.ReadFile(CBAGetVersionResponseFile)
    91  	if err != nil {
    92  		t.Error(err)
    93  	}
    94  
    95  	mux.HandleFunc("/cookbook_artifacts/seven_zip/0e1fed3b56aa5e84205e330d92aca22d8704a014", func(w http.ResponseWriter, r *http.Request) {
    96  		fmt.Fprintf(w, string(file))
    97  	})
    98  	mux.HandleFunc("/cookbook_artifacts/seven_zip/bad", func(w http.ResponseWriter, r *http.Request) {
    99  		http.Error(w, "Not Found", 404)
   100  	})
   101  
   102  	cookbookArtifact, err := client.CookbookArtifacts.GetVersion("seven_zip", "0e1fed3b56aa5e84205e330d92aca22d8704a014")
   103  	if err != nil {
   104  		t.Error(err)
   105  	}
   106  
   107  	if assert.NotNil(t, cookbookArtifact) {
   108  		assert.Equal(t, "seven_zip", cookbookArtifact.Name)
   109  		assert.Equal(t, "3.1.2", cookbookArtifact.Version)
   110  		assert.Equal(t, "0e1fed3b56aa5e84205e330d92aca22d8704a014", cookbookArtifact.Identifier)
   111  		assert.Equal(t, 9, len(cookbookArtifact.RootFiles))
   112  		assert.Equal(t, 1, len(cookbookArtifact.Providers))
   113  		assert.Equal(t, 2, len(cookbookArtifact.Resources))
   114  		assert.Equal(t, 1, len(cookbookArtifact.Libraries))
   115  		assert.Equal(t, 1, len(cookbookArtifact.Attributes))
   116  		assert.Equal(t, "https://s3-external-1/amazonaws.com:443/test-s3-url", cookbookArtifact.RootFiles[0].Url)
   117  		assert.Equal(t, "d07d9934f97445c5187cf86abf107b7b", cookbookArtifact.Providers[0].Checksum)
   118  		assert.Equal(t, "archive.rb", cookbookArtifact.Resources[0].Name)
   119  		assert.Equal(t, "matchers.rb", cookbookArtifact.Libraries[0].Name)
   120  		assert.Equal(t, "https://s3-external-1/amazonaws.com:443/test-s3-url-attribs", cookbookArtifact.Attributes[0].Url)
   121  		assert.Equal(t, "https://s3-external-1/amazonaws.com:443/test-s3-url-recipes", cookbookArtifact.Recipes[0].Url)
   122  		assert.Equal(t, "seven_zip", cookbookArtifact.Metadata.Name)
   123  		assert.Equal(t, "3.1.2", cookbookArtifact.Metadata.Version)
   124  		assert.Equal(t, "Apache-2.0", cookbookArtifact.Metadata.License)
   125  		assert.Equal(t, "Installs/Configures 7-Zip", cookbookArtifact.Metadata.Description)
   126  		assert.Equal(t, ">= 0.0.0", cookbookArtifact.Metadata.Depends["windows"])
   127  		assert.Equal(t, ">= 13.0", cookbookArtifact.Metadata.ChefVersions[0][0])
   128  	}
   129  
   130  	_, err = client.CookbookArtifacts.GetVersion("seven_zip", "bad")
   131  	if err == nil {
   132  		t.Error("We expected this bad request to error", err)
   133  	}
   134  }