github.com/jdolitsky/cnab-go@v0.7.1-beta1/claim/claim_test.go (about)

     1  package claim
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/qri-io/jsonschema"
    13  
    14  	"github.com/deislabs/cnab-go/bundle"
    15  )
    16  
    17  func TestNew(t *testing.T) {
    18  	// Make sure that the default Result has status and action set.
    19  	claim, err := New("my_claim")
    20  	assert.NoError(t, err)
    21  
    22  	assert.Equal(t, "my_claim", claim.Name, "Name is set")
    23  	assert.Equal(t, "unknown", claim.Result.Status)
    24  	assert.Equal(t, "unknown", claim.Result.Action)
    25  
    26  	assert.Equal(t, map[string]interface{}{}, claim.Outputs)
    27  	assert.Equal(t, map[string]interface{}{}, claim.Parameters)
    28  }
    29  
    30  func TestUpdate(t *testing.T) {
    31  	claim, err := New("claim")
    32  	assert.NoError(t, err)
    33  	oldMod := claim.Modified
    34  	oldUlid := claim.Revision
    35  
    36  	time.Sleep(1 * time.Millisecond) // Force the Update to happen at a new time. For those of us who remembered to press the Turbo button.
    37  
    38  	claim.Update(ActionInstall, StatusSuccess)
    39  
    40  	is := assert.New(t)
    41  	is.NotEqual(oldMod, claim.Modified)
    42  	is.NotEqual(oldUlid, claim.Revision)
    43  	is.Equal("install", claim.Result.Action)
    44  	is.Equal("success", claim.Result.Status)
    45  }
    46  
    47  func TestValidName(t *testing.T) {
    48  	for name, expect := range map[string]bool{
    49  		"M4cb3th":               true,
    50  		"Lady MacBeth":          false, // spaces illegal
    51  		"3_Witches":             true,
    52  		"Banquø":                false, // We could probably loosen this one up
    53  		"King-Duncan":           true,
    54  		"MacDuff@geocities.com": false,
    55  		"hecate":                true, // I wouldn't dare cross Hecate.
    56  		"foo bar baz":           false,
    57  		"foo.bar.baz":           true,
    58  		"foo-bar-baz":           true,
    59  		"foo_bar_baz":           true,
    60  		"":                      false,
    61  	} {
    62  		t.Run(name, func(t *testing.T) {
    63  			assert.Equal(t, expect, ValidName.MatchString(name), "expected '%s' to be %t", name, expect)
    64  		})
    65  	}
    66  }
    67  
    68  var (
    69  	staticRevision = "revision"
    70  	staticDate     = time.Date(1983, time.April, 18, 1, 2, 3, 4, time.UTC)
    71  	exampleBundle  = bundle.Bundle{
    72  		SchemaVersion: "schemaVersion",
    73  		Name:          "mybun",
    74  		Version:       "v0.1.0",
    75  		Description:   "this is my bundle",
    76  	}
    77  )
    78  
    79  func TestMarshal_New(t *testing.T) {
    80  	claim, err := New("my_claim")
    81  	assert.NoError(t, err)
    82  
    83  	// override dynamic fields for testing
    84  	claim.Revision = staticRevision
    85  	claim.Created = staticDate
    86  	claim.Modified = staticDate
    87  
    88  	bytes, err := json.Marshal(claim)
    89  	assert.NoError(t, err, "failed to json.Marshal claim")
    90  
    91  	wantClaim, err := ioutil.ReadFile("testdata/claim.default.json")
    92  	assert.NoError(t, err, "failed to read test claim")
    93  
    94  	assert.Equal(t, string(wantClaim), string(bytes), "marshaled claim does not match expected")
    95  }
    96  
    97  var exampleClaim = Claim{
    98  	Name:     "my_claim",
    99  	Revision: staticRevision,
   100  	Created:  staticDate,
   101  	Modified: staticDate,
   102  	Bundle:   &exampleBundle,
   103  	Result: Result{
   104  		Action:  ActionInstall,
   105  		Message: "result message",
   106  		Status:  StatusUnderway,
   107  	},
   108  	Parameters: map[string]interface{}{
   109  		"myparam": "myparamvalue",
   110  	},
   111  	Outputs: map[string]interface{}{
   112  		"myoutput": "myoutputvalue",
   113  	},
   114  	Custom: []string{
   115  		"anything goes",
   116  	},
   117  }
   118  
   119  func TestMarshal_AllFields(t *testing.T) {
   120  	bytes, err := json.Marshal(exampleClaim)
   121  	assert.NoError(t, err, "failed to json.Marshal claim")
   122  
   123  	wantClaim, err := ioutil.ReadFile("testdata/claim.allfields.json")
   124  	assert.NoError(t, err, "failed to read test claim")
   125  
   126  	assert.Equal(t, string(wantClaim), string(bytes), "marshaled claim does not match expected")
   127  }
   128  
   129  func TestClaimSchema(t *testing.T) {
   130  	t.Skip("this test is currently a work in progress; see issue comment below")
   131  
   132  	claimBytes, err := json.Marshal(exampleClaim)
   133  	assert.NoError(t, err, "failed to json.Marshal the claim")
   134  
   135  	url := "https://raw.githubusercontent.com/deislabs/cnab-spec/master/schema/claim.schema.json"
   136  	req, err := http.NewRequest("GET", url, nil)
   137  	assert.NoError(t, err, "failed to construct GET request for fetching claim schema")
   138  	res, err := http.DefaultClient.Do(req)
   139  	assert.NoError(t, err, "failed to get claim schema")
   140  
   141  	defer res.Body.Close()
   142  	schemaData, err := ioutil.ReadAll(res.Body)
   143  	assert.NoError(t, err, "failed to read claim schema")
   144  
   145  	rs := &jsonschema.RootSchema{}
   146  	// This currently fails; needs https://github.com/deislabs/cnab-spec/pull/243
   147  	err = json.Unmarshal(schemaData, rs)
   148  	assert.NoError(t, err, "failed to json.Unmarshal root claim schema")
   149  
   150  	// This currently fails due to https://github.com/deislabs/cnab-spec/issues/241
   151  	// Thus, since the referenced bundle schema can't be fetched, schema validation is impaired
   152  	// Alternatively, we could read the qri-o/jsonschema docs to see how we might 'seed' our Validator
   153  	// with a fetched version of the bundle schema (from GitHub, as above for the claim schema)
   154  	err = rs.FetchRemoteReferences()
   155  	assert.NoError(t, err, "failed to fetch remote references declared by claim schema")
   156  
   157  	errors, err := rs.ValidateBytes(claimBytes)
   158  	assert.NoError(t, err, "failed to validate claim")
   159  
   160  	if len(errors) > 0 {
   161  		t.Log("claim validation against the JSON schema failed:")
   162  		for _, error := range errors {
   163  			t.Log(error)
   164  		}
   165  		t.Fail()
   166  	}
   167  }