github.com/Datadog/cnab-go@v0.3.3-beta1.0.20191007143216-bba4b7e723d0/driver/driver_test.go (about)

     1  package driver
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/deislabs/cnab-go/bundle"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  var _ Driver = &DebugDriver{}
    14  
    15  func TestDebugDriver_Handles(t *testing.T) {
    16  	d := &DebugDriver{}
    17  	is := assert.New(t)
    18  	is.NotNil(d)
    19  	is.True(d.Handles(ImageTypeDocker))
    20  	is.True(d.Handles("anything"))
    21  }
    22  
    23  func TestDebugDriver_Run(t *testing.T) {
    24  	d := &DebugDriver{}
    25  	is := assert.New(t)
    26  	is.NotNil(d)
    27  
    28  	op := &Operation{
    29  		Installation: "test",
    30  		Image: bundle.InvocationImage{
    31  			BaseImage: bundle.BaseImage{
    32  				Image:     "test:1.2.3",
    33  				ImageType: "oci",
    34  			},
    35  		},
    36  		Out: ioutil.Discard,
    37  	}
    38  
    39  	_, err := d.Run(op)
    40  	is.NoError(err)
    41  }
    42  
    43  func TestOperation_Unmarshall(t *testing.T) {
    44  	expectedOp := Operation{
    45  		Action:       "install",
    46  		Installation: "test",
    47  		Parameters: map[string]interface{}{
    48  			"param1": "value1",
    49  			"param2": "value2",
    50  		},
    51  		Image: bundle.InvocationImage{
    52  			BaseImage: bundle.BaseImage{
    53  				Image:     "cnab/helloworld:latest",
    54  				ImageType: "docker",
    55  				Digest:    "sha256:55f83710272990efab4e076f9281453e136980becfd879640b06552ead751284",
    56  			},
    57  		},
    58  		Revision: "01DDY0MT808KX0GGZ6SMXN4TW",
    59  		Environment: map[string]string{
    60  			"ENV1": "value1",
    61  			"ENV2": "value2",
    62  		},
    63  		Files: map[string]string{
    64  			"/cnab/app/image-map.json": "{}",
    65  		},
    66  	}
    67  	var op Operation
    68  	is := assert.New(t)
    69  	bytes, err := ioutil.ReadFile("../testdata/operations/valid-operation.json")
    70  	is.NoError(err, "Error reading from testdata/operations/valid-operation.json")
    71  	is.NoError(json.Unmarshal(bytes, &op), "Error unmarshalling operation")
    72  	is.NotNil(op, "Expected Operation not to be nil")
    73  	is.Equal(expectedOp, op, "Validating value of unmarshalled operation failed")
    74  }
    75  
    76  func TestOperation_Marshall(t *testing.T) {
    77  	actualOp := Operation{
    78  		Action:       "install",
    79  		Installation: "test",
    80  		Parameters: map[string]interface{}{
    81  			"param1": "value1",
    82  			"param2": "value2",
    83  		},
    84  		Image: bundle.InvocationImage{
    85  			BaseImage: bundle.BaseImage{
    86  				Image:     "cnab/helloworld:latest",
    87  				ImageType: "docker",
    88  				Digest:    "sha256:55f83710272990efab4e076f9281453e136980becfd879640b06552ead751284",
    89  			},
    90  		},
    91  		Revision: "01DDY0MT808KX0GGZ6SMXN4TW",
    92  		Environment: map[string]string{
    93  			"ENV1": "value1",
    94  			"ENV2": "value2",
    95  		},
    96  		Files: map[string]string{
    97  			"/cnab/app/image-map.json": "{}",
    98  		},
    99  		Out: os.Stdout,
   100  	}
   101  	is := assert.New(t)
   102  	bytes, err := json.Marshal(actualOp)
   103  	is.NoError(err, "Error Marshalling actual operation to json")
   104  	is.NotNil(bytes, "Expected marshalled json not to be nil")
   105  	actualJSON := string(bytes)
   106  	var expectedOp Operation
   107  	bytes, err = ioutil.ReadFile("../testdata/operations/valid-operation.json")
   108  	is.NoError(err, "Error reading from testdata/operations/valid-operation.json")
   109  	is.NoError(json.Unmarshal(bytes, &expectedOp), "Error unmarshalling expected operation")
   110  	bytes, err = json.Marshal(expectedOp)
   111  	is.NoError(err, "Error Marshalling expected operation to json")
   112  	is.NotNil(bytes, "Expected marshalled json not to be nil")
   113  	expectedJSON := string(bytes)
   114  	is.Equal(expectedJSON, actualJSON)
   115  }